blob: d62ac73fa17db08bc871d3995e8f7041248b8251 [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>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaua9679ac2010-01-03 17:32:57 +010082/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020083const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
103const char *HTTP_401_fmt =
104 "HTTP/1.0 401 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200107 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
114 [HTTP_ERR_400] = 400,
115 [HTTP_ERR_403] = 403,
116 [HTTP_ERR_408] = 408,
117 [HTTP_ERR_500] = 500,
118 [HTTP_ERR_502] = 502,
119 [HTTP_ERR_503] = 503,
120 [HTTP_ERR_504] = 504,
121};
122
Willy Tarreau80587432006-12-24 17:47:20 +0100123static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100125 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 "Cache-Control: no-cache\r\n"
127 "Connection: close\r\n"
128 "Content-Type: text/html\r\n"
129 "\r\n"
130 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
131
132 [HTTP_ERR_403] =
133 "HTTP/1.0 403 Forbidden\r\n"
134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
139
140 [HTTP_ERR_408] =
141 "HTTP/1.0 408 Request Time-out\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
147
148 [HTTP_ERR_500] =
149 "HTTP/1.0 500 Server Error\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
155
156 [HTTP_ERR_502] =
157 "HTTP/1.0 502 Bad Gateway\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
163
164 [HTTP_ERR_503] =
165 "HTTP/1.0 503 Service Unavailable\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
171
172 [HTTP_ERR_504] =
173 "HTTP/1.0 504 Gateway Time-out\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
179
180};
181
Willy Tarreau80587432006-12-24 17:47:20 +0100182/* We must put the messages here since GCC cannot initialize consts depending
183 * on strlen().
184 */
185struct chunk http_err_chunks[HTTP_ERR_SIZE];
186
Willy Tarreau42250582007-04-01 01:30:43 +0200187#define FD_SETS_ARE_BITFIELDS
188#ifdef FD_SETS_ARE_BITFIELDS
189/*
190 * This map is used with all the FD_* macros to check whether a particular bit
191 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
192 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
193 * byte should be encoded. Be careful to always pass bytes from 0 to 255
194 * exclusively to the macros.
195 */
196fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
198
199#else
200#error "Check if your OS uses bitfields for fd_sets"
201#endif
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203void init_proto_http()
204{
Willy Tarreau42250582007-04-01 01:30:43 +0200205 int i;
206 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100207 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200208
Willy Tarreau80587432006-12-24 17:47:20 +0100209 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
210 if (!http_err_msgs[msg]) {
211 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
212 abort();
213 }
214
215 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
216 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
217 }
Willy Tarreau42250582007-04-01 01:30:43 +0200218
219 /* initialize the log header encoding map : '{|}"#' should be encoded with
220 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
221 * URL encoding only requires '"', '#' to be encoded as well as non-
222 * printable characters above.
223 */
224 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
225 memset(url_encode_map, 0, sizeof(url_encode_map));
226 for (i = 0; i < 32; i++) {
227 FD_SET(i, hdr_encode_map);
228 FD_SET(i, url_encode_map);
229 }
230 for (i = 127; i < 256; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234
235 tmp = "\"#{|}";
236 while (*tmp) {
237 FD_SET(*tmp, hdr_encode_map);
238 tmp++;
239 }
240
241 tmp = "\"#";
242 while (*tmp) {
243 FD_SET(*tmp, url_encode_map);
244 tmp++;
245 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200246
247 /* memory allocations */
248 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200249 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100250}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
Willy Tarreau53b6c742006-12-17 13:37:46 +0100252/*
253 * We have 26 list of methods (1 per first letter), each of which can have
254 * up to 3 entries (2 valid, 1 null).
255 */
256struct http_method_desc {
257 http_meth_t meth;
258 int len;
259 const char text[8];
260};
261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100262const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263 ['C' - 'A'] = {
264 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
265 },
266 ['D' - 'A'] = {
267 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
268 },
269 ['G' - 'A'] = {
270 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
271 },
272 ['H' - 'A'] = {
273 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
274 },
275 ['P' - 'A'] = {
276 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
277 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
278 },
279 ['T' - 'A'] = {
280 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
281 },
282 /* rest is empty like this :
283 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
284 */
285};
286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200288 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289 * RFC2616 for those chars.
290 */
291
292const char http_is_spht[256] = {
293 [' '] = 1, ['\t'] = 1,
294};
295
296const char http_is_crlf[256] = {
297 ['\r'] = 1, ['\n'] = 1,
298};
299
300const char http_is_lws[256] = {
301 [' '] = 1, ['\t'] = 1,
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_sep[256] = {
306 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
307 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
308 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
309 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
310 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
311};
312
313const char http_is_ctl[256] = {
314 [0 ... 31] = 1,
315 [127] = 1,
316};
317
318/*
319 * A token is any ASCII char that is neither a separator nor a CTL char.
320 * Do not overwrite values in assignment since gcc-2.95 will not handle
321 * them correctly. Instead, define every non-CTL char's status.
322 */
323const char http_is_token[256] = {
324 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
325 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
326 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
327 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
328 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
329 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
330 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
331 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
332 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
333 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
334 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
335 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
336 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
337 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
338 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
339 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
340 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
341 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
342 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
343 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
344 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
345 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
346 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
347 ['|'] = 1, ['}'] = 0, ['~'] = 1,
348};
349
350
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100351/*
352 * An http ver_token is any ASCII which can be found in an HTTP version,
353 * which includes 'H', 'T', 'P', '/', '.' and any digit.
354 */
355const char http_is_ver_token[256] = {
356 ['.'] = 1, ['/'] = 1,
357 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
358 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
359 ['H'] = 1, ['P'] = 1, ['T'] = 1,
360};
361
362
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100363/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100364 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
365 */
366#if defined(DEBUG_FSM)
367static void http_silent_debug(int line, struct session *s)
368{
369 int size = 0;
370 size += snprintf(trash + size, sizeof(trash) - size,
371 "[%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",
372 line,
373 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
374 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);
375 write(-1, trash, size);
376 size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 " %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",
379 line,
380 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
381 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);
382
383 write(-1, trash, size);
384}
385#else
386#define http_silent_debug(l,s) do { } while (0)
387#endif
388
389/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100390 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
391 * CRLF. Text length is measured first, so it cannot be NULL.
392 * The header is also automatically added to the index <hdr_idx>, and the end
393 * of headers is automatically adjusted. The number of bytes added is returned
394 * on success, otherwise <0 is returned indicating an error.
395 */
396int http_header_add_tail(struct buffer *b, struct http_msg *msg,
397 struct hdr_idx *hdr_idx, const char *text)
398{
399 int bytes, len;
400
401 len = strlen(text);
402 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
403 if (!bytes)
404 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100405 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100406 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
407}
408
409/*
410 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
411 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
412 * the buffer is only opened and the space reserved, but nothing is copied.
413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
417int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
418 struct hdr_idx *hdr_idx, const char *text, int len)
419{
420 int bytes;
421
422 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428
429/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100430 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
431 * If so, returns the position of the first non-space character relative to
432 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
433 * to return a pointer to the place after the first space. Returns 0 if the
434 * header name does not match. Checks are case-insensitive.
435 */
436int http_header_match2(const char *hdr, const char *end,
437 const char *name, int len)
438{
439 const char *val;
440
441 if (hdr + len >= end)
442 return 0;
443 if (hdr[len] != ':')
444 return 0;
445 if (strncasecmp(hdr, name, len) != 0)
446 return 0;
447 val = hdr + len + 1;
448 while (val < end && HTTP_IS_SPHT(*val))
449 val++;
450 if ((val >= end) && (len + 2 <= end - hdr))
451 return len + 2; /* we may replace starting from second space */
452 return val - hdr;
453}
454
Willy Tarreau33a7e692007-06-10 19:45:56 +0200455/* Find the end of the header value contained between <s> and <e>.
456 * See RFC2616, par 2.2 for more information. Note that it requires
457 * a valid header to return a valid result.
458 */
459const char *find_hdr_value_end(const char *s, const char *e)
460{
461 int quoted, qdpair;
462
463 quoted = qdpair = 0;
464 for (; s < e; s++) {
465 if (qdpair) qdpair = 0;
466 else if (quoted && *s == '\\') qdpair = 1;
467 else if (quoted && *s == '"') quoted = 0;
468 else if (*s == '"') quoted = 1;
469 else if (*s == ',') return s;
470 }
471 return s;
472}
473
474/* Find the first or next occurrence of header <name> in message buffer <sol>
475 * using headers index <idx>, and return it in the <ctx> structure. This
476 * structure holds everything necessary to use the header and find next
477 * occurrence. If its <idx> member is 0, the header is searched from the
478 * beginning. Otherwise, the next occurrence is returned. The function returns
479 * 1 when it finds a value, and 0 when there is no more.
480 */
481int http_find_header2(const char *name, int len,
482 const char *sol, struct hdr_idx *idx,
483 struct hdr_ctx *ctx)
484{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200485 const char *eol, *sov;
486 int cur_idx;
487
488 if (ctx->idx) {
489 /* We have previously returned a value, let's search
490 * another one on the same line.
491 */
492 cur_idx = ctx->idx;
493 sol = ctx->line;
494 sov = sol + ctx->val + ctx->vlen;
495 eol = sol + idx->v[cur_idx].len;
496
497 if (sov >= eol)
498 /* no more values in this header */
499 goto next_hdr;
500
501 /* values remaining for this header, skip the comma */
502 sov++;
503 while (sov < eol && http_is_lws[(unsigned char)*sov])
504 sov++;
505
506 goto return_hdr;
507 }
508
509 /* first request for this header */
510 sol += hdr_idx_first_pos(idx);
511 cur_idx = hdr_idx_first_idx(idx);
512
513 while (cur_idx) {
514 eol = sol + idx->v[cur_idx].len;
515
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200516 if (len == 0) {
517 /* No argument was passed, we want any header.
518 * To achieve this, we simply build a fake request. */
519 while (sol + len < eol && sol[len] != ':')
520 len++;
521 name = sol;
522 }
523
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 if ((len < eol - sol) &&
525 (sol[len] == ':') &&
526 (strncasecmp(sol, name, len) == 0)) {
527
528 sov = sol + len + 1;
529 while (sov < eol && http_is_lws[(unsigned char)*sov])
530 sov++;
531 return_hdr:
532 ctx->line = sol;
533 ctx->idx = cur_idx;
534 ctx->val = sov - sol;
535
536 eol = find_hdr_value_end(sov, eol);
537 ctx->vlen = eol - sov;
538 return 1;
539 }
540 next_hdr:
541 sol = eol + idx->v[cur_idx].cr + 1;
542 cur_idx = idx->v[cur_idx].next;
543 }
544 return 0;
545}
546
547int http_find_header(const char *name,
548 const char *sol, struct hdr_idx *idx,
549 struct hdr_ctx *ctx)
550{
551 return http_find_header2(name, strlen(name), sol, idx, ctx);
552}
553
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100554/* This function handles a server error at the stream interface level. The
555 * stream interface is assumed to be already in a closed state. An optional
556 * message is copied into the input buffer, and an HTTP status code stored.
557 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100558 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100560static void http_server_error(struct session *t, struct stream_interface *si,
561 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100563 buffer_erase(si->ob);
564 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200565 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100566 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100567 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100568 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100569 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 }
571 if (!(t->flags & SN_ERR_MASK))
572 t->flags |= err;
573 if (!(t->flags & SN_FINST_MASK))
574 t->flags |= finst;
575}
576
Willy Tarreau80587432006-12-24 17:47:20 +0100577/* This function returns the appropriate error location for the given session
578 * and message.
579 */
580
581struct chunk *error_message(struct session *s, int msgnum)
582{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200583 if (s->be->errmsg[msgnum].str)
584 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100585 else if (s->fe->errmsg[msgnum].str)
586 return &s->fe->errmsg[msgnum];
587 else
588 return &http_err_chunks[msgnum];
589}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591/*
592 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
593 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
594 */
595static http_meth_t find_http_meth(const char *str, const int len)
596{
597 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100598 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100599
600 m = ((unsigned)*str - 'A');
601
602 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100603 for (h = http_methods[m]; h->len > 0; h++) {
604 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100605 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100606 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100607 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100608 };
609 return HTTP_METH_OTHER;
610 }
611 return HTTP_METH_NONE;
612
613}
614
Willy Tarreau21d2af32008-02-14 20:25:24 +0100615/* Parse the URI from the given transaction (which is assumed to be in request
616 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
617 * It is returned otherwise.
618 */
619static char *
620http_get_path(struct http_txn *txn)
621{
622 char *ptr, *end;
623
Willy Tarreau962c3f42010-01-10 00:15:35 +0100624 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100625 end = ptr + txn->req.sl.rq.u_l;
626
627 if (ptr >= end)
628 return NULL;
629
630 /* RFC2616, par. 5.1.2 :
631 * Request-URI = "*" | absuri | abspath | authority
632 */
633
634 if (*ptr == '*')
635 return NULL;
636
637 if (isalpha((unsigned char)*ptr)) {
638 /* this is a scheme as described by RFC3986, par. 3.1 */
639 ptr++;
640 while (ptr < end &&
641 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
642 ptr++;
643 /* skip '://' */
644 if (ptr == end || *ptr++ != ':')
645 return NULL;
646 if (ptr == end || *ptr++ != '/')
647 return NULL;
648 if (ptr == end || *ptr++ != '/')
649 return NULL;
650 }
651 /* skip [user[:passwd]@]host[:[port]] */
652
653 while (ptr < end && *ptr != '/')
654 ptr++;
655
656 if (ptr == end)
657 return NULL;
658
659 /* OK, we got the '/' ! */
660 return ptr;
661}
662
Willy Tarreauefb453c2008-10-26 20:49:47 +0100663/* Returns a 302 for a redirectable request. This may only be called just after
664 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
665 * left unchanged and will follow normal proxy processing.
666 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100668{
669 struct http_txn *txn;
670 struct chunk rdr;
671 char *path;
672 int len;
673
674 /* 1: create the response header */
675 rdr.len = strlen(HTTP_302);
676 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100677 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100678 memcpy(rdr.str, HTTP_302, rdr.len);
679
680 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200681 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100682 return;
683
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100684 /* special prefix "/" means don't change URL */
685 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
686 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
687 rdr.len += s->srv->rdr_len;
688 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689
690 /* 3: add the request URI */
691 txn = &s->txn;
692 path = http_get_path(txn);
693 if (!path)
694 return;
695
Willy Tarreau962c3f42010-01-10 00:15:35 +0100696 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200697 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100698 return;
699
700 memcpy(rdr.str + rdr.len, path, len);
701 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100702 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
703 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100704
705 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100706 si->shutr(si);
707 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100708 si->err_type = SI_ET_NONE;
709 si->err_loc = NULL;
710 si->state = SI_ST_CLO;
711
712 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100713 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100714
715 /* FIXME: we should increase a counter of redirects per server and per backend. */
716 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100717 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100718}
719
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100720/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100721 * that the server side is closed. Note that err_type is actually a
722 * bitmask, where almost only aborts may be cumulated with other
723 * values. We consider that aborted operations are more important
724 * than timeouts or errors due to the fact that nobody else in the
725 * logs might explain incomplete retries. All others should avoid
726 * being cumulated. It should normally not be possible to have multiple
727 * aborts at once, but just in case, the first one in sequence is reported.
728 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100729void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100730{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100731 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100732
733 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100734 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
735 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100736 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100737 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
738 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100739 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100740 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
741 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100742 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100743 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
744 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100746 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
747 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100748 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100749 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
750 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100752 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
753 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754}
755
Willy Tarreau42250582007-04-01 01:30:43 +0200756extern const char sess_term_cond[8];
757extern const char sess_fin_state[8];
758extern const char *monthname[12];
759const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
760const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
761 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
762 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200763struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200764struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100765
Emeric Brun3a058f32009-06-30 18:26:00 +0200766void http_sess_clflog(struct session *s)
767{
768 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
769 struct proxy *fe = s->fe;
770 struct proxy *be = s->be;
771 struct proxy *prx_log;
772 struct http_txn *txn = &s->txn;
773 int tolog, level, err;
774 char *uri, *h;
775 char *svid;
776 struct tm tm;
777 static char tmpline[MAX_SYSLOG_LEN];
778 int hdr;
779 size_t w;
780 int t_request;
781
782 prx_log = fe;
783 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
784 (s->conn_retries != be->conn_retries) ||
785 txn->status >= 500;
786
787 if (s->cli_addr.ss_family == AF_INET)
788 inet_ntop(AF_INET,
789 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
790 pn, sizeof(pn));
791 else
792 inet_ntop(AF_INET6,
793 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
794 pn, sizeof(pn));
795
796 get_gmtime(s->logs.accept_date.tv_sec, &tm);
797
798 /* FIXME: let's limit ourselves to frontend logging for now. */
799 tolog = fe->to_log;
800
801 h = tmpline;
802
803 w = snprintf(h, sizeof(tmpline),
804 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
805 pn,
806 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
807 tm.tm_hour, tm.tm_min, tm.tm_sec);
808 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
809 goto trunc;
810 h += w;
811
812 if (h >= tmpline + sizeof(tmpline) - 4)
813 goto trunc;
814
815 *(h++) = ' ';
816 *(h++) = '\"';
817 uri = txn->uri ? txn->uri : "<BADREQ>";
818 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
819 '#', url_encode_map, uri);
820 *(h++) = '\"';
821
822 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
823 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
824 goto trunc;
825 h += w;
826
827 if (h >= tmpline + sizeof(tmpline) - 9)
828 goto trunc;
829 memcpy(h, " \"-\" \"-\"", 8);
830 h += 8;
831
832 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
833 " %d %03d",
834 (s->cli_addr.ss_family == AF_INET) ?
835 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
836 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
837 (int)s->logs.accept_date.tv_usec/1000);
838 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
839 goto trunc;
840 h += w;
841
842 w = strlen(fe->id);
843 if (h >= tmpline + sizeof(tmpline) - 4 - w)
844 goto trunc;
845 *(h++) = ' ';
846 *(h++) = '\"';
847 memcpy(h, fe->id, w);
848 h += w;
849 *(h++) = '\"';
850
851 w = strlen(be->id);
852 if (h >= tmpline + sizeof(tmpline) - 4 - w)
853 goto trunc;
854 *(h++) = ' ';
855 *(h++) = '\"';
856 memcpy(h, be->id, w);
857 h += w;
858 *(h++) = '\"';
859
860 svid = (tolog & LW_SVID) ?
861 (s->data_source != DATA_SRC_STATS) ?
862 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
863
864 w = strlen(svid);
865 if (h >= tmpline + sizeof(tmpline) - 4 - w)
866 goto trunc;
867 *(h++) = ' ';
868 *(h++) = '\"';
869 memcpy(h, svid, w);
870 h += w;
871 *(h++) = '\"';
872
873 t_request = -1;
874 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
875 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
876 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
877 " %d %ld %ld %ld %ld",
878 t_request,
879 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
880 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
881 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
882 s->logs.t_close);
883 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
884 goto trunc;
885 h += w;
886
887 if (h >= tmpline + sizeof(tmpline) - 8)
888 goto trunc;
889 *(h++) = ' ';
890 *(h++) = '\"';
891 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
892 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
893 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
894 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
895 *(h++) = '\"';
896
897 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
898 " %d %d %d %d %d %ld %ld",
899 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
900 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
901 s->logs.srv_queue_size, s->logs.prx_queue_size);
902
903 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
904 goto trunc;
905 h += w;
906
907 if (txn->cli_cookie) {
908 w = strlen(txn->cli_cookie);
909 if (h >= tmpline + sizeof(tmpline) - 4 - w)
910 goto trunc;
911 *(h++) = ' ';
912 *(h++) = '\"';
913 memcpy(h, txn->cli_cookie, w);
914 h += w;
915 *(h++) = '\"';
916 } else {
917 if (h >= tmpline + sizeof(tmpline) - 5)
918 goto trunc;
919 memcpy(h, " \"-\"", 4);
920 h += 4;
921 }
922
923 if (txn->srv_cookie) {
924 w = strlen(txn->srv_cookie);
925 if (h >= tmpline + sizeof(tmpline) - 4 - w)
926 goto trunc;
927 *(h++) = ' ';
928 *(h++) = '\"';
929 memcpy(h, txn->srv_cookie, w);
930 h += w;
931 *(h++) = '\"';
932 } else {
933 if (h >= tmpline + sizeof(tmpline) - 5)
934 goto trunc;
935 memcpy(h, " \"-\"", 4);
936 h += 4;
937 }
938
939 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
940 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
941 if (h >= sizeof (tmpline) + tmpline - 4)
942 goto trunc;
943 *(h++) = ' ';
944 *(h++) = '\"';
945 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
946 '#', hdr_encode_map, txn->req.cap[hdr]);
947 *(h++) = '\"';
948 }
949 }
950
951 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
952 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
953 if (h >= sizeof (tmpline) + tmpline - 4)
954 goto trunc;
955 *(h++) = ' ';
956 *(h++) = '\"';
957 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
958 '#', hdr_encode_map, txn->rsp.cap[hdr]);
959 *(h++) = '\"';
960 }
961 }
962
963trunc:
964 *h = '\0';
965
966 level = LOG_INFO;
967 if (err && (fe->options2 & PR_O2_LOGERRORS))
968 level = LOG_ERR;
969
970 send_log(prx_log, level, "%s\n", tmpline);
971
972 s->logs.logwait = 0;
973}
974
Willy Tarreau42250582007-04-01 01:30:43 +0200975/*
976 * send a log for the session when we have enough info about it.
977 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100979void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200980{
981 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
982 struct proxy *fe = s->fe;
983 struct proxy *be = s->be;
984 struct proxy *prx_log;
985 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200986 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200987 char *uri, *h;
988 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200989 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200990 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200991 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200992 int hdr;
993
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200994 /* if we don't want to log normal traffic, return now */
995 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
996 (s->conn_retries != be->conn_retries) ||
997 txn->status >= 500;
998 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
999 return;
1000
Willy Tarreau42250582007-04-01 01:30:43 +02001001 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1002 return;
1003 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001004
Emeric Brun3a058f32009-06-30 18:26:00 +02001005 if (prx_log->options2 & PR_O2_CLFLOG)
1006 return http_sess_clflog(s);
1007
Willy Tarreau42250582007-04-01 01:30:43 +02001008 if (s->cli_addr.ss_family == AF_INET)
1009 inet_ntop(AF_INET,
1010 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1011 pn, sizeof(pn));
1012 else
1013 inet_ntop(AF_INET6,
1014 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1015 pn, sizeof(pn));
1016
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001017 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001018
1019 /* FIXME: let's limit ourselves to frontend logging for now. */
1020 tolog = fe->to_log;
1021
1022 h = tmpline;
1023 if (fe->to_log & LW_REQHDR &&
1024 txn->req.cap &&
1025 (h < tmpline + sizeof(tmpline) - 10)) {
1026 *(h++) = ' ';
1027 *(h++) = '{';
1028 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1029 if (hdr)
1030 *(h++) = '|';
1031 if (txn->req.cap[hdr] != NULL)
1032 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1033 '#', hdr_encode_map, txn->req.cap[hdr]);
1034 }
1035 *(h++) = '}';
1036 }
1037
1038 if (fe->to_log & LW_RSPHDR &&
1039 txn->rsp.cap &&
1040 (h < tmpline + sizeof(tmpline) - 7)) {
1041 *(h++) = ' ';
1042 *(h++) = '{';
1043 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1044 if (hdr)
1045 *(h++) = '|';
1046 if (txn->rsp.cap[hdr] != NULL)
1047 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1048 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1049 }
1050 *(h++) = '}';
1051 }
1052
1053 if (h < tmpline + sizeof(tmpline) - 4) {
1054 *(h++) = ' ';
1055 *(h++) = '"';
1056 uri = txn->uri ? txn->uri : "<BADREQ>";
1057 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1058 '#', url_encode_map, uri);
1059 *(h++) = '"';
1060 }
1061 *h = '\0';
1062
1063 svid = (tolog & LW_SVID) ?
1064 (s->data_source != DATA_SRC_STATS) ?
1065 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1066
Willy Tarreau70089872008-06-13 21:12:51 +02001067 t_request = -1;
1068 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1069 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1070
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001071 level = LOG_INFO;
1072 if (err && (fe->options2 & PR_O2_LOGERRORS))
1073 level = LOG_ERR;
1074
1075 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001076 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001077 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1078 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001079 pn,
1080 (s->cli_addr.ss_family == AF_INET) ?
1081 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1082 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001083 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001084 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001085 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001086 t_request,
1087 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001088 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1089 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1090 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1091 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001092 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001093 txn->cli_cookie ? txn->cli_cookie : "-",
1094 txn->srv_cookie ? txn->srv_cookie : "-",
1095 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1096 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1097 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1098 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1099 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001100 (s->flags & SN_REDISP)?"+":"",
1101 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001102 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1103
1104 s->logs.logwait = 0;
1105}
1106
Willy Tarreau117f59e2007-03-04 18:17:17 +01001107
1108/*
1109 * Capture headers from message starting at <som> according to header list
1110 * <cap_hdr>, and fill the <idx> structure appropriately.
1111 */
1112void capture_headers(char *som, struct hdr_idx *idx,
1113 char **cap, struct cap_hdr *cap_hdr)
1114{
1115 char *eol, *sol, *col, *sov;
1116 int cur_idx;
1117 struct cap_hdr *h;
1118 int len;
1119
1120 sol = som + hdr_idx_first_pos(idx);
1121 cur_idx = hdr_idx_first_idx(idx);
1122
1123 while (cur_idx) {
1124 eol = sol + idx->v[cur_idx].len;
1125
1126 col = sol;
1127 while (col < eol && *col != ':')
1128 col++;
1129
1130 sov = col + 1;
1131 while (sov < eol && http_is_lws[(unsigned char)*sov])
1132 sov++;
1133
1134 for (h = cap_hdr; h; h = h->next) {
1135 if ((h->namelen == col - sol) &&
1136 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1137 if (cap[h->index] == NULL)
1138 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001139 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001140
1141 if (cap[h->index] == NULL) {
1142 Alert("HTTP capture : out of memory.\n");
1143 continue;
1144 }
1145
1146 len = eol - sov;
1147 if (len > h->len)
1148 len = h->len;
1149
1150 memcpy(cap[h->index], sov, len);
1151 cap[h->index][len]=0;
1152 }
1153 }
1154 sol = eol + idx->v[cur_idx].cr + 1;
1155 cur_idx = idx->v[cur_idx].next;
1156 }
1157}
1158
1159
Willy Tarreau42250582007-04-01 01:30:43 +02001160/* either we find an LF at <ptr> or we jump to <bad>.
1161 */
1162#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1163
1164/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1165 * otherwise to <http_msg_ood> with <state> set to <st>.
1166 */
1167#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1168 ptr++; \
1169 if (likely(ptr < end)) \
1170 goto good; \
1171 else { \
1172 state = (st); \
1173 goto http_msg_ood; \
1174 } \
1175 } while (0)
1176
1177
Willy Tarreaubaaee002006-06-26 02:48:02 +02001178/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001179 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1181 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1182 * will give undefined results.
1183 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1184 * and that msg->sol points to the beginning of the response.
1185 * If a complete line is found (which implies that at least one CR or LF is
1186 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1187 * returned indicating an incomplete line (which does not mean that parts have
1188 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1189 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1190 * upon next call.
1191 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001192 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1194 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001195 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001197const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1198 unsigned int state, const char *ptr, const char *end,
1199 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001200{
Willy Tarreau8973c702007-01-21 23:58:29 +01001201 switch (state) {
1202 http_msg_rpver:
1203 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001204 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1206
1207 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001208 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1210 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001211 state = HTTP_MSG_ERROR;
1212 break;
1213
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 http_msg_rpver_sp:
1215 case HTTP_MSG_RPVER_SP:
1216 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001217 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001218 goto http_msg_rpcode;
1219 }
1220 if (likely(HTTP_IS_SPHT(*ptr)))
1221 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1222 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001223 state = HTTP_MSG_ERROR;
1224 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001225
1226 http_msg_rpcode:
1227 case HTTP_MSG_RPCODE:
1228 if (likely(!HTTP_IS_LWS(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1230
1231 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001232 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001233 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1234 }
1235
1236 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001237 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 http_msg_rsp_reason:
1239 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001240 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 msg->sl.st.r_l = 0;
1242 goto http_msg_rpline_eol;
1243
1244 http_msg_rpcode_sp:
1245 case HTTP_MSG_RPCODE_SP:
1246 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001247 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 goto http_msg_rpreason;
1249 }
1250 if (likely(HTTP_IS_SPHT(*ptr)))
1251 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1252 /* so it's a CR/LF, so there is no reason phrase */
1253 goto http_msg_rsp_reason;
1254
1255 http_msg_rpreason:
1256 case HTTP_MSG_RPREASON:
1257 if (likely(!HTTP_IS_CRLF(*ptr)))
1258 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001259 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 http_msg_rpline_eol:
1261 /* We have seen the end of line. Note that we do not
1262 * necessarily have the \n yet, but at least we know that we
1263 * have EITHER \r OR \n, otherwise the response would not be
1264 * complete. We can then record the response length and return
1265 * to the caller which will be able to register it.
1266 */
1267 msg->sl.st.l = ptr - msg->sol;
1268 return ptr;
1269
1270#ifdef DEBUG_FULL
1271 default:
1272 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1273 exit(1);
1274#endif
1275 }
1276
1277 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001278 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 if (ret_state)
1280 *ret_state = state;
1281 if (ret_ptr)
1282 *ret_ptr = (char *)ptr;
1283 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001284}
1285
1286
1287/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 * This function parses a request line between <ptr> and <end>, starting with
1289 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1290 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1291 * will give undefined results.
1292 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1293 * and that msg->sol points to the beginning of the request.
1294 * If a complete line is found (which implies that at least one CR or LF is
1295 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1296 * returned indicating an incomplete line (which does not mean that parts have
1297 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1298 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1299 * upon next call.
1300 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001301 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1303 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001304 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001305 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001306const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1307 unsigned int state, const char *ptr, const char *end,
1308 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 switch (state) {
1311 http_msg_rqmeth:
1312 case HTTP_MSG_RQMETH:
1313 if (likely(HTTP_IS_TOKEN(*ptr)))
1314 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001317 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 if (likely(HTTP_IS_CRLF(*ptr))) {
1322 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001323 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001325 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001327 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001329 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 msg->sl.rq.v_l = 0;
1331 goto http_msg_rqline_eol;
1332 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001333 state = HTTP_MSG_ERROR;
1334 break;
1335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 http_msg_rqmeth_sp:
1337 case HTTP_MSG_RQMETH_SP:
1338 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001339 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 goto http_msg_rquri;
1341 }
1342 if (likely(HTTP_IS_SPHT(*ptr)))
1343 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1344 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1345 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_rquri:
1348 case HTTP_MSG_RQURI:
1349 if (likely(!HTTP_IS_LWS(*ptr)))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001353 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1355 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1358 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 http_msg_rquri_sp:
1361 case HTTP_MSG_RQURI_SP:
1362 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001363 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 goto http_msg_rqver;
1365 }
1366 if (likely(HTTP_IS_SPHT(*ptr)))
1367 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1368 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1369 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 http_msg_rqver:
1372 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001373 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001375
1376 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001377 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001378 http_msg_rqline_eol:
1379 /* We have seen the end of line. Note that we do not
1380 * necessarily have the \n yet, but at least we know that we
1381 * have EITHER \r OR \n, otherwise the request would not be
1382 * complete. We can then record the request length and return
1383 * to the caller which will be able to register it.
1384 */
1385 msg->sl.rq.l = ptr - msg->sol;
1386 return ptr;
1387 }
1388
1389 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001390 state = HTTP_MSG_ERROR;
1391 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393#ifdef DEBUG_FULL
1394 default:
1395 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1396 exit(1);
1397#endif
1398 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001401 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 if (ret_state)
1403 *ret_state = state;
1404 if (ret_ptr)
1405 *ret_ptr = (char *)ptr;
1406 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001408
1409
Willy Tarreau8973c702007-01-21 23:58:29 +01001410/*
1411 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001412 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 * when data are missing and recalled at the exact same location with no
1414 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001415 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001416 * fields. Note that msg->som and msg->sol will be initialized after completing
1417 * the first state, so that none of the msg pointers has to be initialized
1418 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1421{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001422 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001425 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 ptr = buf->lr;
1427 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 if (unlikely(ptr >= end))
1430 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /*
1434 * First, states that are specific to the response only.
1435 * We check them first so that request and headers are
1436 * closer to each other (accessed more often).
1437 */
1438 http_msg_rpbefore:
1439 case HTTP_MSG_RPBEFORE:
1440 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001441 /* we have a start of message, but we have to check
1442 * first if we need to remove some CRLF. We can only
1443 * do this when send_max=0.
1444 */
1445 char *beg = buf->w + buf->send_max;
1446 if (beg >= buf->data + buf->size)
1447 beg -= buf->size;
1448 if (unlikely(ptr != beg)) {
1449 if (buf->send_max)
1450 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001451 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001452 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001454 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001455 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001456 hdr_idx_init(idx);
1457 state = HTTP_MSG_RPVER;
1458 goto http_msg_rpver;
1459 }
1460
1461 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1462 goto http_msg_invalid;
1463
1464 if (unlikely(*ptr == '\n'))
1465 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1466 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1467 /* stop here */
1468
1469 http_msg_rpbefore_cr:
1470 case HTTP_MSG_RPBEFORE_CR:
1471 EXPECT_LF_HERE(ptr, http_msg_invalid);
1472 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1473 /* stop here */
1474
1475 http_msg_rpver:
1476 case HTTP_MSG_RPVER:
1477 case HTTP_MSG_RPVER_SP:
1478 case HTTP_MSG_RPCODE:
1479 case HTTP_MSG_RPCODE_SP:
1480 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001481 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001482 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 if (unlikely(!ptr))
1484 return;
1485
1486 /* we have a full response and we know that we have either a CR
1487 * or an LF at <ptr>.
1488 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001489 //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 +01001490 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1491
1492 msg->sol = ptr;
1493 if (likely(*ptr == '\r'))
1494 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1495 goto http_msg_rpline_end;
1496
1497 http_msg_rpline_end:
1498 case HTTP_MSG_RPLINE_END:
1499 /* msg->sol must point to the first of CR or LF. */
1500 EXPECT_LF_HERE(ptr, http_msg_invalid);
1501 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1502 /* stop here */
1503
1504 /*
1505 * Second, states that are specific to the request only
1506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 http_msg_rqbefore:
1508 case HTTP_MSG_RQBEFORE:
1509 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001510 /* we have a start of message, but we have to check
1511 * first if we need to remove some CRLF. We can only
1512 * do this when send_max=0.
1513 */
1514 char *beg = buf->w + buf->send_max;
1515 if (beg >= buf->data + buf->size)
1516 beg -= buf->size;
1517 if (likely(ptr != beg)) {
1518 if (buf->send_max)
1519 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001520 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001521 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001523 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001524 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001525 /* we will need this when keep-alive will be supported
1526 hdr_idx_init(idx);
1527 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001528 state = HTTP_MSG_RQMETH;
1529 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1533 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 if (unlikely(*ptr == '\n'))
1536 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1537 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001538 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 http_msg_rqbefore_cr:
1541 case HTTP_MSG_RQBEFORE_CR:
1542 EXPECT_LF_HERE(ptr, http_msg_invalid);
1543 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001544 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 http_msg_rqmeth:
1547 case HTTP_MSG_RQMETH:
1548 case HTTP_MSG_RQMETH_SP:
1549 case HTTP_MSG_RQURI:
1550 case HTTP_MSG_RQURI_SP:
1551 case HTTP_MSG_RQVER:
1552 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001553 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 if (unlikely(!ptr))
1555 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 /* we have a full request and we know that we have either a CR
1558 * or an LF at <ptr>.
1559 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001560 //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 +01001561 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 msg->sol = ptr;
1564 if (likely(*ptr == '\r'))
1565 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 http_msg_rqline_end:
1569 case HTTP_MSG_RQLINE_END:
1570 /* check for HTTP/0.9 request : no version information available.
1571 * msg->sol must point to the first of CR or LF.
1572 */
1573 if (unlikely(msg->sl.rq.v_l == 0))
1574 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 EXPECT_LF_HERE(ptr, http_msg_invalid);
1577 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001578 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8973c702007-01-21 23:58:29 +01001580 /*
1581 * Common states below
1582 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 http_msg_hdr_first:
1584 case HTTP_MSG_HDR_FIRST:
1585 msg->sol = ptr;
1586 if (likely(!HTTP_IS_CRLF(*ptr))) {
1587 goto http_msg_hdr_name;
1588 }
1589
1590 if (likely(*ptr == '\r'))
1591 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1592 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 http_msg_hdr_name:
1595 case HTTP_MSG_HDR_NAME:
1596 /* assumes msg->sol points to the first char */
1597 if (likely(HTTP_IS_TOKEN(*ptr)))
1598 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 if (likely(*ptr == ':')) {
1601 msg->col = ptr - buf->data;
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1603 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001604
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001605 if (likely(msg->err_pos < -1) || *ptr == '\n')
1606 goto http_msg_invalid;
1607
1608 if (msg->err_pos == -1) /* capture error pointer */
1609 msg->err_pos = ptr - buf->data; /* >= 0 now */
1610
1611 /* and we still accept this non-token character */
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 http_msg_hdr_l1_sp:
1615 case HTTP_MSG_HDR_L1_SP:
1616 /* assumes msg->sol points to the first char and msg->col to the colon */
1617 if (likely(HTTP_IS_SPHT(*ptr)))
1618 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* header value can be basically anything except CR/LF */
1621 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (likely(!HTTP_IS_CRLF(*ptr))) {
1624 goto http_msg_hdr_val;
1625 }
1626
1627 if (likely(*ptr == '\r'))
1628 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1629 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 http_msg_hdr_l1_lf:
1632 case HTTP_MSG_HDR_L1_LF:
1633 EXPECT_LF_HERE(ptr, http_msg_invalid);
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 http_msg_hdr_l1_lws:
1637 case HTTP_MSG_HDR_L1_LWS:
1638 if (likely(HTTP_IS_SPHT(*ptr))) {
1639 /* replace HT,CR,LF with spaces */
1640 for (; buf->data+msg->sov < ptr; msg->sov++)
1641 buf->data[msg->sov] = ' ';
1642 goto http_msg_hdr_l1_sp;
1643 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001644 /* we had a header consisting only in spaces ! */
1645 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 goto http_msg_complete_header;
1647
1648 http_msg_hdr_val:
1649 case HTTP_MSG_HDR_VAL:
1650 /* assumes msg->sol points to the first char, msg->col to the
1651 * colon, and msg->sov points to the first character of the
1652 * value.
1653 */
1654 if (likely(!HTTP_IS_CRLF(*ptr)))
1655 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 msg->eol = ptr;
1658 /* Note: we could also copy eol into ->eoh so that we have the
1659 * real header end in case it ends with lots of LWS, but is this
1660 * really needed ?
1661 */
1662 if (likely(*ptr == '\r'))
1663 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1664 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 http_msg_hdr_l2_lf:
1667 case HTTP_MSG_HDR_L2_LF:
1668 EXPECT_LF_HERE(ptr, http_msg_invalid);
1669 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 http_msg_hdr_l2_lws:
1672 case HTTP_MSG_HDR_L2_LWS:
1673 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1674 /* LWS: replace HT,CR,LF with spaces */
1675 for (; msg->eol < ptr; msg->eol++)
1676 *msg->eol = ' ';
1677 goto http_msg_hdr_val;
1678 }
1679 http_msg_complete_header:
1680 /*
1681 * It was a new header, so the last one is finished.
1682 * Assumes msg->sol points to the first char, msg->col to the
1683 * colon, msg->sov points to the first character of the value
1684 * and msg->eol to the first CR or LF so we know how the line
1685 * ends. We insert last header into the index.
1686 */
1687 /*
1688 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1689 write(2, msg->sol, msg->eol-msg->sol);
1690 fprintf(stderr,"\n");
1691 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1694 idx, idx->tail) < 0))
1695 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 msg->sol = ptr;
1698 if (likely(!HTTP_IS_CRLF(*ptr))) {
1699 goto http_msg_hdr_name;
1700 }
1701
1702 if (likely(*ptr == '\r'))
1703 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1704 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 http_msg_last_lf:
1707 case HTTP_MSG_LAST_LF:
1708 /* Assumes msg->sol points to the first of either CR or LF */
1709 EXPECT_LF_HERE(ptr, http_msg_invalid);
1710 ptr++;
1711 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001712 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001714 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001715 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 return;
1717#ifdef DEBUG_FULL
1718 default:
1719 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1720 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001721#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 }
1723 http_msg_ood:
1724 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001725 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 buf->lr = ptr;
1727 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 http_msg_invalid:
1730 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001731 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001732 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 return;
1734}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001735
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001736/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1737 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1738 * nothing is done and 1 is returned.
1739 */
1740static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1741{
1742 int delta;
1743 char *cur_end;
1744
1745 if (msg->sl.rq.v_l != 0)
1746 return 1;
1747
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001748 cur_end = msg->sol + msg->sl.rq.l;
1749 delta = 0;
1750
1751 if (msg->sl.rq.u_l == 0) {
1752 /* if no URI was set, add "/" */
1753 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1754 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001755 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001756 }
1757 /* add HTTP version */
1758 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001759 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760 cur_end += delta;
1761 cur_end = (char *)http_parse_reqline(msg, req->data,
1762 HTTP_MSG_RQMETH,
1763 msg->sol, cur_end + 1,
1764 NULL, NULL);
1765 if (unlikely(!cur_end))
1766 return 0;
1767
1768 /* we have a full HTTP/1.0 request now and we know that
1769 * we have either a CR or an LF at <ptr>.
1770 */
1771 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1772 return 1;
1773}
1774
Willy Tarreau5b154472009-12-21 20:11:07 +01001775/* Parse the Connection: headaer of an HTTP request, and set the transaction
1776 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1777 * is also set so that we don't parse a second time. If some dangerous values
1778 * are encountered, we leave the status to indicate that the request might be
1779 * interpreted as keep-alive, but we also set the connection flags to indicate
1780 * that we WANT it to be a close, so that the header will be fixed. This
1781 * function should only be called when we know we're interested in checking
1782 * the request (not a CONNECT, and FE or BE mangles the header).
1783 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001784void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001785{
1786 struct http_msg *msg = &txn->req;
1787 struct hdr_ctx ctx;
1788 int conn_cl, conn_ka;
1789
1790 if (txn->flags & TX_CON_HDR_PARS)
1791 return;
1792
1793 conn_cl = 0;
1794 conn_ka = 0;
1795 ctx.idx = 0;
1796
1797 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1798 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1799 conn_cl = 1;
1800 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1801 conn_ka = 1;
1802 }
1803
1804 /* Determine if the client wishes keep-alive or close.
1805 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1806 * are persistent unless "Connection: close" is explicitly specified.
1807 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1808 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1809 * they explicitly specify "Connection: Keep-Alive", regardless of any
1810 * optional "Keep-Alive" header.
1811 * Note that if we find a request with both "Connection: close" and
1812 * "Connection: Keep-Alive", we indicate we want a close but don't have
1813 * it, so that it can be enforced later.
1814 */
1815
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001816 if (conn_cl && conn_ka) {
1817 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1818 }
1819 else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 if (conn_cl) {
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001821 txn->flags |= TX_REQ_CONN_CLO;
1822 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1823 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01001824 }
1825 } else { /* HTTP/1.0 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001826 if (!conn_ka) {
1827 txn->flags |= TX_REQ_CONN_CLO;
1828 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1829 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1830 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001831 }
1832 txn->flags |= TX_CON_HDR_PARS;
1833}
1834
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1836 * first byte of body, and increments msg->sov by the number of bytes parsed,
1837 * so that we know we can forward between ->som and ->sov. Note that due to
1838 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1839 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001840 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001841 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001842 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001843int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001844{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001845 char *ptr = buf->lr;
1846 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 unsigned int chunk = 0;
1848
1849 /* The chunk size is in the following form, though we are only
1850 * interested in the size and CRLF :
1851 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1852 */
1853 while (1) {
1854 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001856 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001858 if (c < 0) /* not a hex digit anymore */
1859 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860 if (++ptr >= end)
1861 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001862 if (chunk & 0xF000000) /* overflow will occur */
1863 return -1;
1864 chunk = (chunk << 4) + c;
1865 }
1866
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867 /* empty size not allowed */
1868 if (ptr == buf->lr)
1869 return -1;
1870
1871 while (http_is_spht[(unsigned char)*ptr]) {
1872 if (++ptr >= end)
1873 ptr = buf->data;
1874 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001875 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001876 }
1877
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878 /* Up to there, we know that at least one byte is present at *ptr. Check
1879 * for the end of chunk size.
1880 */
1881 while (1) {
1882 if (likely(HTTP_IS_CRLF(*ptr))) {
1883 /* we now have a CR or an LF at ptr */
1884 if (likely(*ptr == '\r')) {
1885 if (++ptr >= end)
1886 ptr = buf->data;
1887 if (ptr == buf->r)
1888 return 0;
1889 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001890
Willy Tarreaud98cf932009-12-27 22:54:55 +01001891 if (*ptr != '\n')
1892 return -1;
1893 if (++ptr >= end)
1894 ptr = buf->data;
1895 /* done */
1896 break;
1897 }
1898 else if (*ptr == ';') {
1899 /* chunk extension, ends at next CRLF */
1900 if (++ptr >= end)
1901 ptr = buf->data;
1902 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001903 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001904
1905 while (!HTTP_IS_CRLF(*ptr)) {
1906 if (++ptr >= end)
1907 ptr = buf->data;
1908 if (ptr == buf->r)
1909 return 0;
1910 }
1911 /* we have a CRLF now, loop above */
1912 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001915 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001916 }
1917
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 /* OK we found our CRLF and now <ptr> points to the next byte,
1919 * which may or may not be present. We save that into ->lr and
1920 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 msg->sov += ptr - buf->lr;
1923 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001924 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 return 1;
1927}
1928
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929/* This function skips trailers in the buffer <buf> associated with HTTP
1930 * message <msg>. The first visited position is buf->lr. If the end of
1931 * the trailers is found, it is automatically scheduled to be forwarded,
1932 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1933 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001934 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1935 * zero. If a parse error is encountered, the function returns < 0 and does not
1936 * change anything except maybe buf->lr and msg->sov. Note that the message
1937 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1938 * which implies that all non-trailers data have already been scheduled for
1939 * forwarding, and that the difference between msg->som and msg->sov exactly
1940 * matches the length of trailers already parsed and not forwarded. It is also
1941 * important to note that this function is designed to be able to parse wrapped
1942 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 */
1944int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1945{
1946 /* we have buf->lr which points to next line. Look for CRLF. */
1947 while (1) {
1948 char *p1 = NULL, *p2 = NULL;
1949 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001950 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951
1952 /* scan current line and stop at LF or CRLF */
1953 while (1) {
1954 if (ptr == buf->r)
1955 return 0;
1956
1957 if (*ptr == '\n') {
1958 if (!p1)
1959 p1 = ptr;
1960 p2 = ptr;
1961 break;
1962 }
1963
1964 if (*ptr == '\r') {
1965 if (p1)
1966 return -1;
1967 p1 = ptr;
1968 }
1969
1970 ptr++;
1971 if (ptr >= buf->data + buf->size)
1972 ptr = buf->data;
1973 }
1974
1975 /* after LF; point to beginning of next line */
1976 p2++;
1977 if (p2 >= buf->data + buf->size)
1978 p2 = buf->data;
1979
Willy Tarreau638cd022010-01-03 07:42:04 +01001980 bytes = p2 - buf->lr;
1981 if (bytes < 0)
1982 bytes += buf->size;
1983
1984 /* schedule this line for forwarding */
1985 msg->sov += bytes;
1986 if (msg->sov >= buf->size)
1987 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001988
Willy Tarreau638cd022010-01-03 07:42:04 +01001989 if (p1 == buf->lr) {
1990 /* LF/CRLF at beginning of line => end of trailers at p2.
1991 * Everything was scheduled for forwarding, there's nothing
1992 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001993 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001994 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001995 msg->msg_state = HTTP_MSG_DONE;
1996 return 1;
1997 }
1998 /* OK, next line then */
1999 buf->lr = p2;
2000 }
2001}
2002
2003/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2004 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2005 * ->som, buf->lr in order to include this part into the next forwarding phase.
2006 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2007 * not enough data are available, the function does not change anything and
2008 * returns zero. If a parse error is encountered, the function returns < 0 and
2009 * does not change anything. Note: this function is designed to parse wrapped
2010 * CRLF at the end of the buffer.
2011 */
2012int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2013{
2014 char *ptr;
2015 int bytes;
2016
2017 /* NB: we'll check data availabilty at the end. It's not a
2018 * problem because whatever we match first will be checked
2019 * against the correct length.
2020 */
2021 bytes = 1;
2022 ptr = buf->lr;
2023 if (*ptr == '\r') {
2024 bytes++;
2025 ptr++;
2026 if (ptr >= buf->data + buf->size)
2027 ptr = buf->data;
2028 }
2029
2030 if (buf->l < bytes)
2031 return 0;
2032
2033 if (*ptr != '\n')
2034 return -1;
2035
2036 ptr++;
2037 if (ptr >= buf->data + buf->size)
2038 ptr = buf->data;
2039 buf->lr = ptr;
2040 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2041 msg->sov = ptr - buf->data;
2042 msg->som = msg->sov - bytes;
2043 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2044 return 1;
2045}
Willy Tarreau5b154472009-12-21 20:11:07 +01002046
Willy Tarreau83e3af02009-12-28 17:39:57 +01002047void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2048{
2049 char *end = buf->data + buf->size;
2050 int off = buf->data + buf->size - buf->w;
2051
2052 /* two possible cases :
2053 * - the buffer is in one contiguous block, we move it in-place
2054 * - the buffer is in two blocks, we move it via the trash
2055 */
2056 if (buf->l) {
2057 int block1 = buf->l;
2058 int block2 = 0;
2059 if (buf->r <= buf->w) {
2060 /* non-contiguous block */
2061 block1 = buf->data + buf->size - buf->w;
2062 block2 = buf->r - buf->data;
2063 }
2064 if (block2)
2065 memcpy(trash, buf->data, block2);
2066 memmove(buf->data, buf->w, block1);
2067 if (block2)
2068 memcpy(buf->data + block1, trash, block2);
2069 }
2070
2071 /* adjust all known pointers */
2072 buf->w = buf->data;
2073 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2074 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2075 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2076 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2077
2078 /* adjust relative pointers */
2079 msg->som = 0;
2080 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2081 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2082 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2083
Willy Tarreau83e3af02009-12-28 17:39:57 +01002084 if (msg->err_pos >= 0) {
2085 msg->err_pos += off;
2086 if (msg->err_pos >= buf->size)
2087 msg->err_pos -= buf->size;
2088 }
2089
2090 buf->flags &= ~BF_FULL;
2091 if (buf->l >= buffer_max_len(buf))
2092 buf->flags |= BF_FULL;
2093}
2094
Willy Tarreaud787e662009-07-07 10:14:51 +02002095/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2096 * processing can continue on next analysers, or zero if it either needs more
2097 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2098 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2099 * when it has nothing left to do, and may remove any analyser when it wants to
2100 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002101 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002102int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002103{
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 /*
2105 * We will parse the partial (or complete) lines.
2106 * We will check the request syntax, and also join multi-line
2107 * headers. An index of all the lines will be elaborated while
2108 * parsing.
2109 *
2110 * For the parsing, we use a 28 states FSM.
2111 *
2112 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002113 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002114 * req->data + msg->eoh = end of processed headers / start of current one
2115 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 * req->lr = first non-visited byte
2117 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002118 *
2119 * At end of parsing, we may perform a capture of the error (if any), and
2120 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2121 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2122 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002124
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002126 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 struct http_txn *txn = &s->txn;
2128 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002129 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002130
Willy Tarreau6bf17362009-02-24 10:48:35 +01002131 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2132 now_ms, __FUNCTION__,
2133 s,
2134 req,
2135 req->rex, req->wex,
2136 req->flags,
2137 req->l,
2138 req->analysers);
2139
Willy Tarreau52a0c602009-08-16 22:45:38 +02002140 /* we're speaking HTTP here, so let's speak HTTP to the client */
2141 s->srv_error = http_return_srv_error;
2142
Willy Tarreau83e3af02009-12-28 17:39:57 +01002143 /* There's a protected area at the end of the buffer for rewriting
2144 * purposes. We don't want to start to parse the request if the
2145 * protected area is affected, because we may have to move processed
2146 * data later, which is much more complicated.
2147 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002148 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002149 if ((txn->flags & TX_NOT_FIRST) &&
2150 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002151 req->r < req->lr ||
2152 req->r > req->data + req->size - global.tune.maxrewrite)) {
2153 if (req->send_max) {
2154 /* some data has still not left the buffer, wake us once that's done */
2155 buffer_dont_connect(req);
2156 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2157 return 0;
2158 }
2159 if (req->l <= req->size - global.tune.maxrewrite)
2160 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002161 }
2162
Willy Tarreau065e8332010-01-08 00:30:20 +01002163 /* Note that we have the same problem with the response ; we
2164 * may want to send a redirect, error or anything which requires
2165 * some spare space. So we'll ensure that we have at least
2166 * maxrewrite bytes available in the response buffer before
2167 * processing that one. This will only affect pipelined
2168 * keep-alive requests.
2169 */
2170 if ((txn->flags & TX_NOT_FIRST) &&
2171 unlikely((s->rep->flags & BF_FULL) ||
2172 s->rep->r < s->rep->lr ||
2173 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2174 if (s->rep->send_max) {
2175 /* don't let a connection request be initiated */
2176 buffer_dont_connect(req);
2177 return 0;
2178 }
2179 }
2180
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002181 if (likely(req->lr < req->r))
2182 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002183 }
2184
Willy Tarreau59234e92008-11-30 23:51:27 +01002185 /* 1: we might have to print this header in debug mode */
2186 if (unlikely((global.mode & MODE_DEBUG) &&
2187 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002188 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002190
Willy Tarreau962c3f42010-01-10 00:15:35 +01002191 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 eol = sol + msg->sl.rq.l;
2193 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002194
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 sol += hdr_idx_first_pos(&txn->hdr_idx);
2196 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 while (cur_idx) {
2199 eol = sol + txn->hdr_idx.v[cur_idx].len;
2200 debug_hdr("clihdr", s, sol, eol);
2201 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2202 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002203 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 }
2205
Willy Tarreau58f10d72006-12-04 02:26:12 +01002206
Willy Tarreau59234e92008-11-30 23:51:27 +01002207 /*
2208 * Now we quickly check if we have found a full valid request.
2209 * If not so, we check the FD and buffer states before leaving.
2210 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002211 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002212 * requests are checked first.
2213 *
2214 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002215
Willy Tarreau655dce92009-11-08 13:10:58 +01002216 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002217 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002218 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002219 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2221 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002222
Willy Tarreau59234e92008-11-30 23:51:27 +01002223 /* 1: Since we are in header mode, if there's no space
2224 * left for headers, we won't be able to free more
2225 * later, so the session will never terminate. We
2226 * must terminate it now.
2227 */
2228 if (unlikely(req->flags & BF_FULL)) {
2229 /* FIXME: check if URI is set and return Status
2230 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002231 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 goto return_bad_req;
2233 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002234
Willy Tarreau59234e92008-11-30 23:51:27 +01002235 /* 2: have we encountered a read error ? */
2236 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaufcffa692010-01-10 14:21:19 +01002237 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002238 goto failed_keep_alive;
2239
Willy Tarreau59234e92008-11-30 23:51:27 +01002240 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002241 if (msg->err_pos >= 0)
2242 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002243 msg->msg_state = HTTP_MSG_ERROR;
2244 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002245
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002246 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002247 if (s->listener->counters)
2248 s->listener->counters->failed_req++;
2249
Willy Tarreau59234e92008-11-30 23:51:27 +01002250 if (!(s->flags & SN_ERR_MASK))
2251 s->flags |= SN_ERR_CLICL;
2252 if (!(s->flags & SN_FINST_MASK))
2253 s->flags |= SN_FINST_R;
2254 return 0;
2255 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002256
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 /* 3: has the read timeout expired ? */
2258 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaufcffa692010-01-10 14:21:19 +01002259 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002260 goto failed_keep_alive;
2261
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002263 if (msg->err_pos >= 0)
2264 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 txn->status = 408;
2266 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2267 msg->msg_state = HTTP_MSG_ERROR;
2268 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002269
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002270 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002271 if (s->listener->counters)
2272 s->listener->counters->failed_req++;
2273
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 if (!(s->flags & SN_ERR_MASK))
2275 s->flags |= SN_ERR_CLITO;
2276 if (!(s->flags & SN_FINST_MASK))
2277 s->flags |= SN_FINST_R;
2278 return 0;
2279 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002280
Willy Tarreau59234e92008-11-30 23:51:27 +01002281 /* 4: have we encountered a close ? */
2282 else if (req->flags & BF_SHUTR) {
Willy Tarreaufcffa692010-01-10 14:21:19 +01002283 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002284 goto failed_keep_alive;
2285
Willy Tarreau4076a152009-04-02 15:18:36 +02002286 if (msg->err_pos >= 0)
2287 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 txn->status = 400;
2289 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2290 msg->msg_state = HTTP_MSG_ERROR;
2291 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002292
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002293 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002294 if (s->listener->counters)
2295 s->listener->counters->failed_req++;
2296
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 if (!(s->flags & SN_ERR_MASK))
2298 s->flags |= SN_ERR_CLICL;
2299 if (!(s->flags & SN_FINST_MASK))
2300 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002301 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002302 }
2303
Willy Tarreau520d95e2009-09-19 21:04:57 +02002304 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002305 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2306
Willy Tarreaufcffa692010-01-10 14:21:19 +01002307 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2308 /* If the client starts to talk, let's fall back to
2309 * request timeout processing.
2310 */
2311 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002312 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002313 }
2314
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002316 if (!tick_isset(req->analyse_exp)) {
2317 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2318 (txn->flags & TX_WAIT_NEXT_RQ) &&
2319 tick_isset(s->be->timeout.httpka))
2320 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2321 else
2322 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2323 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002324
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 /* we're not ready yet */
2326 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002327
2328 failed_keep_alive:
2329 /* Here we process low-level errors for keep-alive requests. In
2330 * short, if the request is not the first one and it experiences
2331 * a timeout, read error or shutdown, we just silently close so
2332 * that the client can try again.
2333 */
2334 txn->status = 0;
2335 msg->msg_state = HTTP_MSG_RQBEFORE;
2336 req->analysers = 0;
2337 s->logs.logwait = 0;
Willy Tarreau148d0992010-01-10 10:21:21 +01002338 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002339 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002340 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002341
Willy Tarreaud787e662009-07-07 10:14:51 +02002342 /* OK now we have a complete HTTP request with indexed headers. Let's
2343 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002344 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2345 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2346 * points to the CRLF of the request line. req->lr points to the first
2347 * byte after the last LF. msg->col and msg->sov point to the first
2348 * byte of data. msg->eol cannot be trusted because it may have been
2349 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002350 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002351
Willy Tarreaub16a5742010-01-10 14:46:16 +01002352 if (txn->flags & TX_WAIT_NEXT_RQ) {
2353 /* kill the pending keep-alive timeout */
2354 txn->flags &= ~TX_WAIT_NEXT_RQ;
2355 req->analyse_exp = TICK_ETERNITY;
2356 }
2357
2358
Willy Tarreaud787e662009-07-07 10:14:51 +02002359 /* Maybe we found in invalid header name while we were configured not
2360 * to block on that, so we have to capture it now.
2361 */
2362 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002363 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 /*
2366 * 1: identify the method
2367 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002368 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002369
2370 /* we can make use of server redirect on GET and HEAD */
2371 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2372 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002373
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 /*
2375 * 2: check if the URI matches the monitor_uri.
2376 * We have to do this for every request which gets in, because
2377 * the monitor-uri is defined by the frontend.
2378 */
2379 if (unlikely((s->fe->monitor_uri_len != 0) &&
2380 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002381 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 s->fe->monitor_uri,
2383 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002384 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002386 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002388
Willy Tarreau59234e92008-11-30 23:51:27 +01002389 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002392 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2393 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002394
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 ret = acl_pass(ret);
2396 if (cond->pol == ACL_COND_UNLESS)
2397 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002398
Willy Tarreau59234e92008-11-30 23:51:27 +01002399 if (ret) {
2400 /* we fail this request, let's return 503 service unavail */
2401 txn->status = 503;
2402 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2403 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002404 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002405 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 /* nothing to fail, let's reply normaly */
2408 txn->status = 200;
2409 stream_int_retnclose(req->prod, &http_200_chunk);
2410 goto return_prx_cond;
2411 }
2412
2413 /*
2414 * 3: Maybe we have to copy the original REQURI for the logs ?
2415 * Note: we cannot log anymore if the request has been
2416 * classified as invalid.
2417 */
2418 if (unlikely(s->logs.logwait & LW_REQ)) {
2419 /* we have a complete HTTP request that we must log */
2420 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2421 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002422
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 if (urilen >= REQURI_LEN)
2424 urilen = REQURI_LEN - 1;
2425 memcpy(txn->uri, &req->data[msg->som], urilen);
2426 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002427
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 if (!(s->logs.logwait &= ~LW_REQ))
2429 s->do_log(s);
2430 } else {
2431 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002432 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 }
Willy Tarreau06619262006-12-17 08:37:22 +01002434
Willy Tarreau59234e92008-11-30 23:51:27 +01002435 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002436 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2437 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002438
Willy Tarreau5b154472009-12-21 20:11:07 +01002439 /* ... and check if the request is HTTP/1.1 or above */
2440 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002441 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2442 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2443 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002444 txn->flags |= TX_REQ_VER_11;
2445
2446 /* "connection" has not been parsed yet */
2447 txn->flags &= ~TX_CON_HDR_PARS;
2448
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002449 /* transfer length unknown*/
2450 txn->flags &= ~TX_REQ_XFER_LEN;
2451
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 /* 5: we may need to capture headers */
2453 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002454 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002455 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002456
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002457 /* 6: determine the transfer-length.
2458 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2459 * the presence of a message-body in a REQUEST and its transfer length
2460 * must be determined that way (in order of precedence) :
2461 * 1. The presence of a message-body in a request is signaled by the
2462 * inclusion of a Content-Length or Transfer-Encoding header field
2463 * in the request's header fields. When a request message contains
2464 * both a message-body of non-zero length and a method that does
2465 * not define any semantics for that request message-body, then an
2466 * origin server SHOULD either ignore the message-body or respond
2467 * with an appropriate error message (e.g., 413). A proxy or
2468 * gateway, when presented the same request, SHOULD either forward
2469 * the request inbound with the message- body or ignore the
2470 * message-body when determining a response.
2471 *
2472 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2473 * and the "chunked" transfer-coding (Section 6.2) is used, the
2474 * transfer-length is defined by the use of this transfer-coding.
2475 * If a Transfer-Encoding header field is present and the "chunked"
2476 * transfer-coding is not present, the transfer-length is defined
2477 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002479 * 3. If a Content-Length header field is present, its decimal value in
2480 * OCTETs represents both the entity-length and the transfer-length.
2481 * If a message is received with both a Transfer-Encoding header
2482 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002483 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002484 * 4. By the server closing the connection. (Closing the connection
2485 * cannot be used to indicate the end of a request body, since that
2486 * would leave no possibility for the server to send back a response.)
2487 *
2488 * Whenever a transfer-coding is applied to a message-body, the set of
2489 * transfer-codings MUST include "chunked", unless the message indicates
2490 * it is terminated by closing the connection. When the "chunked"
2491 * transfer-coding is used, it MUST be the last transfer-coding applied
2492 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 */
2494
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002495 /* CONNECT sets a tunnel and ignores everything else */
2496 if (txn->meth == HTTP_METH_CONNECT)
2497 goto skip_xfer_len;
2498
2499 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002500 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002501 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002502 while ((txn->flags & TX_REQ_VER_11) &&
2503 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002504 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2505 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2506 else if (txn->flags & TX_REQ_TE_CHNK) {
2507 /* bad transfer-encoding (chunked followed by something else) */
2508 use_close_only = 1;
2509 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2510 break;
2511 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002512 }
2513
Willy Tarreau32b47f42009-10-18 20:55:02 +02002514 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002515 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002516 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2517 signed long long cl;
2518
2519 if (!ctx.vlen)
2520 goto return_bad_req;
2521
2522 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2523 goto return_bad_req; /* parse failure */
2524
2525 if (cl < 0)
2526 goto return_bad_req;
2527
2528 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2529 goto return_bad_req; /* already specified, was different */
2530
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002531 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002532 msg->hdr_content_len = cl;
2533 }
2534
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002535 /* bodyless requests have a known length */
2536 if (!use_close_only)
2537 txn->flags |= TX_REQ_XFER_LEN;
2538
2539 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002541 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002542 req->analyse_exp = TICK_ETERNITY;
2543 return 1;
2544
2545 return_bad_req:
2546 /* We centralize bad requests processing here */
2547 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2548 /* we detected a parsing error. We want to archive this request
2549 * in the dedicated proxy area for later troubleshooting.
2550 */
2551 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2552 }
2553
2554 txn->req.msg_state = HTTP_MSG_ERROR;
2555 txn->status = 400;
2556 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002557
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002558 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002559 if (s->listener->counters)
2560 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002561
2562 return_prx_cond:
2563 if (!(s->flags & SN_ERR_MASK))
2564 s->flags |= SN_ERR_PRXCOND;
2565 if (!(s->flags & SN_FINST_MASK))
2566 s->flags |= SN_FINST_R;
2567
2568 req->analysers = 0;
2569 req->analyse_exp = TICK_ETERNITY;
2570 return 0;
2571}
2572
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002573/* This stream analyser runs all HTTP request processing which is common to
2574 * frontends and backends, which means blocking ACLs, filters, connection-close,
2575 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002576 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002577 * either needs more data or wants to immediately abort the request (eg: deny,
2578 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002579 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002580int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002581{
Willy Tarreaud787e662009-07-07 10:14:51 +02002582 struct http_txn *txn = &s->txn;
2583 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002584 struct acl_cond *cond;
2585 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002586 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002587 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002588
Willy Tarreau655dce92009-11-08 13:10:58 +01002589 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002590 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002591 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002592 return 0;
2593 }
2594
Willy Tarreau3a816292009-07-07 10:55:49 +02002595 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002596 req->analyse_exp = TICK_ETERNITY;
2597
2598 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2599 now_ms, __FUNCTION__,
2600 s,
2601 req,
2602 req->rex, req->wex,
2603 req->flags,
2604 req->l,
2605 req->analysers);
2606
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002607 /* first check whether we have some ACLs set to block this request */
2608 list_for_each_entry(cond, &px->block_cond, list) {
2609 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002610
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002611 ret = acl_pass(ret);
2612 if (cond->pol == ACL_COND_UNLESS)
2613 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002614
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002615 if (ret) {
2616 txn->status = 403;
2617 /* let's log the request time */
2618 s->logs.tv_request = now;
2619 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2620 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002622 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002623
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002624 /* try headers filters */
2625 if (px->req_exp != NULL) {
2626 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2627 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002628
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 /* has the request been denied ? */
2630 if (txn->flags & TX_CLDENY) {
2631 /* no need to go further */
2632 txn->status = 403;
2633 /* let's log the request time */
2634 s->logs.tv_request = now;
2635 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2636 goto return_prx_cond;
2637 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002638
2639 /* When a connection is tarpitted, we use the tarpit timeout,
2640 * which may be the same as the connect timeout if unspecified.
2641 * If unset, then set it to zero because we really want it to
2642 * eventually expire. We build the tarpit as an analyser.
2643 */
2644 if (txn->flags & TX_CLTARPIT) {
2645 buffer_erase(s->req);
2646 /* wipe the request out so that we can drop the connection early
2647 * if the client closes first.
2648 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002649 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002650 req->analysers = 0; /* remove switching rules etc... */
2651 req->analysers |= AN_REQ_HTTP_TARPIT;
2652 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2653 if (!req->analyse_exp)
2654 req->analyse_exp = tick_add(now_ms, 0);
2655 return 1;
2656 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002657 }
Willy Tarreau06619262006-12-17 08:37:22 +01002658
Willy Tarreau5b154472009-12-21 20:11:07 +01002659 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2660 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002661 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2662 * in which headers are mangled. However, if another mode is set, it will
2663 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002664 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002665
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002666 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002667 ((s->fe->options|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 +01002668 int tmp = TX_CON_WANT_TUN;
2669 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2670 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002671 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2672 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002673 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002674 tmp = TX_CON_WANT_CLO;
2675
Willy Tarreau5b154472009-12-21 20:11:07 +01002676 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2677 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002678
Willy Tarreau8db1c172010-01-05 23:12:12 +01002679 if (!(txn->flags & TX_CON_HDR_PARS))
2680 http_req_parse_connection_header(txn);
2681
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002682 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2683 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2684 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2685 if (!(txn->flags & TX_REQ_XFER_LEN))
2686 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2687 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002688 }
2689
2690 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2691 * once we know the backend, because the tunnel mode can be implied by the
2692 * lack of any close/keepalive options in both the FE and the BE. Since
2693 * this information can evolve with time, we proceed by trying to make the
2694 * header status match the desired status. For this, we'll have to adjust
2695 * the "Connection" header. The test for persistent connections has already
2696 * been performed, so we only enter here if there is a risk the connection
2697 * is considered as persistent and we want it to be closed on the server
2698 * side. It would be nice if we could enter this place only when a
2699 * Connection header exists. Note that a CONNECT method will not enter
2700 * here.
2701 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002702 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2703 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2704 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002705 char *cur_ptr, *cur_end, *cur_next;
2706 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002707 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002708 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002709
Willy Tarreau5b154472009-12-21 20:11:07 +01002710 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002711 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002712
Willy Tarreau5b154472009-12-21 20:11:07 +01002713 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714 cur_hdr = &txn->hdr_idx.v[cur_idx];
2715 cur_ptr = cur_next;
2716 cur_end = cur_ptr + cur_hdr->len;
2717 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002718
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002720 if (!val)
2721 continue;
2722
2723 /* 3 possibilities :
2724 * - we have already set "Connection: close" or we're in
2725 * HTTP/1.0, so we remove this line.
2726 * - we have not yet set "Connection: close", but this line
2727 * indicates close. We leave it untouched and set the flag.
2728 * - we have not yet set "Connection: close", and this line
2729 * indicates non-close. We replace it and set the flag.
2730 */
2731 if (must_delete) {
2732 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2733 http_msg_move_end(&txn->req, delta);
2734 cur_next += delta;
2735 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2736 txn->hdr_idx.used--;
2737 cur_hdr->len = 0;
2738 txn->flags |= TX_REQ_CONN_CLO;
2739 } else {
2740 if (cur_end - cur_ptr - val != 5 ||
2741 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2742 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2743 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002744 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002745 cur_hdr->len += delta;
2746 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002747 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002748 txn->flags |= TX_REQ_CONN_CLO;
2749 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002750 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002751 } /* for loop */
2752 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002753
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002755 list_for_each_entry(wl, &px->req_add, list) {
2756 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002757 goto return_bad_req;
2758 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002759
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 /* check if stats URI was requested, and if an auth is needed */
2761 if (px->uri_auth != NULL &&
2762 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2763 /* we have to check the URI and auth for this request.
2764 * FIXME!!! that one is rather dangerous, we want to
2765 * make it follow standard rules (eg: clear req->analysers).
2766 */
2767 if (stats_check_uri_auth(s, px)) {
2768 req->analysers = 0;
2769 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002771 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002772
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 /* check whether we have some ACLs set to redirect this request */
2774 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002775 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002776
Willy Tarreauf285f542010-01-03 20:03:03 +01002777 if (rule->cond) {
2778 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2779 ret = acl_pass(ret);
2780 if (rule->cond->pol == ACL_COND_UNLESS)
2781 ret = !ret;
2782 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002783
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002784 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002785 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002787
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002788 /* build redirect message */
2789 switch(rule->code) {
2790 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791 msg_fmt = HTTP_303;
2792 break;
2793 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002794 msg_fmt = HTTP_301;
2795 break;
2796 case 302:
2797 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798 msg_fmt = HTTP_302;
2799 break;
2800 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002801
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002802 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002804
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002805 switch(rule->type) {
2806 case REDIRECT_TYPE_PREFIX: {
2807 const char *path;
2808 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002809
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002810 path = http_get_path(txn);
2811 /* build message using path */
2812 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002813 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002814 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2815 int qs = 0;
2816 while (qs < pathlen) {
2817 if (path[qs] == '?') {
2818 pathlen = qs;
2819 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002820 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002821 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002822 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002823 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002824 } else {
2825 path = "/";
2826 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002827 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002828
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002829 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002830 goto return_bad_req;
2831
2832 /* add prefix. Note that if prefix == "/", we don't want to
2833 * add anything, otherwise it makes it hard for the user to
2834 * configure a self-redirection.
2835 */
2836 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002837 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2838 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002839 }
2840
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002841 /* add path */
2842 memcpy(rdr.str + rdr.len, path, pathlen);
2843 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01002844
2845 /* append a slash at the end of the location is needed and missing */
2846 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
2847 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2848 if (rdr.len > rdr.size - 5)
2849 goto return_bad_req;
2850 rdr.str[rdr.len] = '/';
2851 rdr.len++;
2852 }
2853
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002854 break;
2855 }
2856 case REDIRECT_TYPE_LOCATION:
2857 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002858 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002859 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002860
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 /* add location */
2862 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2863 rdr.len += rule->rdr_len;
2864 break;
2865 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002866
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002867 if (rule->cookie_len) {
2868 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2869 rdr.len += 14;
2870 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2871 rdr.len += rule->cookie_len;
2872 memcpy(rdr.str + rdr.len, "\r\n", 2);
2873 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002874 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002875
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002876 /* add end of headers and the keep-alive/close status.
2877 * We may choose to set keep-alive if the Location begins
2878 * with a slash, because the client will come back to the
2879 * same server.
2880 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002881 txn->status = rule->code;
2882 /* let's log the request time */
2883 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002884
2885 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2886 (txn->flags & TX_REQ_XFER_LEN) &&
2887 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2888 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2889 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2890 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01002891 if (!(txn->flags & TX_REQ_VER_11)) {
2892 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
2893 rdr.len += 24;
2894 }
2895 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2896 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002897 buffer_write(req->prod->ob, rdr.str, rdr.len);
2898 /* "eat" the request */
2899 buffer_ignore(req, msg->sov - msg->som);
2900 msg->som = msg->sov;
2901 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002902 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2903 txn->req.msg_state = HTTP_MSG_CLOSED;
2904 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002905 break;
2906 } else {
2907 /* keep-alive not possible */
2908 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2909 rdr.len += 23;
Willy Tarreau148d0992010-01-10 10:21:21 +01002910 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002911 goto return_prx_cond;
2912 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002913 }
2914 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002915
Willy Tarreau2be39392010-01-03 17:24:51 +01002916 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2917 * If this happens, then the data will not come immediately, so we must
2918 * send all what we have without waiting. Note that due to the small gain
2919 * in waiting for the body of the request, it's easier to simply put the
2920 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2921 * itself once used.
2922 */
2923 req->flags |= BF_SEND_DONTWAIT;
2924
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002925 /* that's OK for us now, let's move on to next analysers */
2926 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002927
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002928 return_bad_req:
2929 /* We centralize bad requests processing here */
2930 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2931 /* we detected a parsing error. We want to archive this request
2932 * in the dedicated proxy area for later troubleshooting.
2933 */
2934 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2935 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002936
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002937 txn->req.msg_state = HTTP_MSG_ERROR;
2938 txn->status = 400;
2939 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002940
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002941 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002942 if (s->listener->counters)
2943 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002944
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002945 return_prx_cond:
2946 if (!(s->flags & SN_ERR_MASK))
2947 s->flags |= SN_ERR_PRXCOND;
2948 if (!(s->flags & SN_FINST_MASK))
2949 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002950
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002951 req->analysers = 0;
2952 req->analyse_exp = TICK_ETERNITY;
2953 return 0;
2954}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002955
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002956/* This function performs all the processing enabled for the current request.
2957 * It returns 1 if the processing can continue on next analysers, or zero if it
2958 * needs more data, encounters an error, or wants to immediately abort the
2959 * request. It relies on buffers flags, and updates s->req->analysers.
2960 */
2961int http_process_request(struct session *s, struct buffer *req, int an_bit)
2962{
2963 struct http_txn *txn = &s->txn;
2964 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002965
Willy Tarreau655dce92009-11-08 13:10:58 +01002966 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002967 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002968 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002969 return 0;
2970 }
2971
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002972 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2973 now_ms, __FUNCTION__,
2974 s,
2975 req,
2976 req->rex, req->wex,
2977 req->flags,
2978 req->l,
2979 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002980
Willy Tarreau59234e92008-11-30 23:51:27 +01002981 /*
2982 * Right now, we know that we have processed the entire headers
2983 * and that unwanted requests have been filtered out. We can do
2984 * whatever we want with the remaining request. Also, now we
2985 * may have separate values for ->fe, ->be.
2986 */
Willy Tarreau06619262006-12-17 08:37:22 +01002987
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 /*
2989 * If HTTP PROXY is set we simply get remote server address
2990 * parsing incoming request.
2991 */
2992 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002993 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01002994 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002995
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002997 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002998 * Note that doing so might move headers in the request, but
2999 * the fields will stay coherent and the URI will not move.
3000 * This should only be performed in the backend.
3001 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003002 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3004 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003005
Willy Tarreau59234e92008-11-30 23:51:27 +01003006 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003007 * 8: the appsession cookie was looked up very early in 1.2,
3008 * so let's do the same now.
3009 */
3010
3011 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003012 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003013 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003014 }
3015
3016 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003017 * 9: add X-Forwarded-For if either the frontend or the backend
3018 * asks for it.
3019 */
3020 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3021 if (s->cli_addr.ss_family == AF_INET) {
3022 /* Add an X-Forwarded-For header unless the source IP is
3023 * in the 'except' network range.
3024 */
3025 if ((!s->fe->except_mask.s_addr ||
3026 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3027 != s->fe->except_net.s_addr) &&
3028 (!s->be->except_mask.s_addr ||
3029 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3030 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003031 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 unsigned char *pn;
3033 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003034
3035 /* Note: we rely on the backend to get the header name to be used for
3036 * x-forwarded-for, because the header is really meant for the backends.
3037 * However, if the backend did not specify any option, we have to rely
3038 * on the frontend's header name.
3039 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003040 if (s->be->fwdfor_hdr_len) {
3041 len = s->be->fwdfor_hdr_len;
3042 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003043 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003044 len = s->fe->fwdfor_hdr_len;
3045 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003046 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003047 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003048
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003049 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003050 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003051 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003052 }
3053 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003054 else if (s->cli_addr.ss_family == AF_INET6) {
3055 /* FIXME: for the sake of completeness, we should also support
3056 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003057 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003058 int len;
3059 char pn[INET6_ADDRSTRLEN];
3060 inet_ntop(AF_INET6,
3061 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3062 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003063
Willy Tarreau59234e92008-11-30 23:51:27 +01003064 /* Note: we rely on the backend to get the header name to be used for
3065 * x-forwarded-for, because the header is really meant for the backends.
3066 * However, if the backend did not specify any option, we have to rely
3067 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003068 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003069 if (s->be->fwdfor_hdr_len) {
3070 len = s->be->fwdfor_hdr_len;
3071 memcpy(trash, s->be->fwdfor_hdr_name, len);
3072 } else {
3073 len = s->fe->fwdfor_hdr_len;
3074 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003075 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003076 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003077
Willy Tarreau59234e92008-11-30 23:51:27 +01003078 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003079 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 goto return_bad_req;
3081 }
3082 }
3083
3084 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003085 * 10: add X-Original-To if either the frontend or the backend
3086 * asks for it.
3087 */
3088 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3089
3090 /* FIXME: don't know if IPv6 can handle that case too. */
3091 if (s->cli_addr.ss_family == AF_INET) {
3092 /* Add an X-Original-To header unless the destination IP is
3093 * in the 'except' network range.
3094 */
3095 if (!(s->flags & SN_FRT_ADDR_SET))
3096 get_frt_addr(s);
3097
3098 if ((!s->fe->except_mask_to.s_addr ||
3099 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3100 != s->fe->except_to.s_addr) &&
3101 (!s->be->except_mask_to.s_addr ||
3102 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3103 != s->be->except_to.s_addr)) {
3104 int len;
3105 unsigned char *pn;
3106 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3107
3108 /* Note: we rely on the backend to get the header name to be used for
3109 * x-original-to, because the header is really meant for the backends.
3110 * However, if the backend did not specify any option, we have to rely
3111 * on the frontend's header name.
3112 */
3113 if (s->be->orgto_hdr_len) {
3114 len = s->be->orgto_hdr_len;
3115 memcpy(trash, s->be->orgto_hdr_name, len);
3116 } else {
3117 len = s->fe->orgto_hdr_len;
3118 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003119 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003120 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3121
3122 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003123 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003124 goto return_bad_req;
3125 }
3126 }
3127 }
3128
Willy Tarreau78599912009-10-17 20:12:21 +02003129 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003130 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3131 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3132 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003133 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003134 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003135 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003136 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003137 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003138
3139 /* If we have no server assigned yet and we're balancing on url_param
3140 * with a POST request, we may be interested in checking the body for
3141 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003142 */
3143 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3144 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003145 s->be->url_param_post_limit != 0 &&
3146 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003147 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003148 buffer_dont_connect(req);
3149 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003150 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003151
Willy Tarreaud98cf932009-12-27 22:54:55 +01003152 if (txn->flags & TX_REQ_XFER_LEN)
3153 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003154
Willy Tarreau59234e92008-11-30 23:51:27 +01003155 /*************************************************************
3156 * OK, that's finished for the headers. We have done what we *
3157 * could. Let's switch to the DATA state. *
3158 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003159 req->analyse_exp = TICK_ETERNITY;
3160 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003161
Willy Tarreau59234e92008-11-30 23:51:27 +01003162 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003163 /* OK let's go on with the BODY now */
3164 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003165
Willy Tarreau59234e92008-11-30 23:51:27 +01003166 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003167 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003168 /* we detected a parsing error. We want to archive this request
3169 * in the dedicated proxy area for later troubleshooting.
3170 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003171 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003172 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003173
Willy Tarreau59234e92008-11-30 23:51:27 +01003174 txn->req.msg_state = HTTP_MSG_ERROR;
3175 txn->status = 400;
3176 req->analysers = 0;
3177 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003178
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003179 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003180 if (s->listener->counters)
3181 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003182
Willy Tarreau59234e92008-11-30 23:51:27 +01003183 if (!(s->flags & SN_ERR_MASK))
3184 s->flags |= SN_ERR_PRXCOND;
3185 if (!(s->flags & SN_FINST_MASK))
3186 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003187 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003188}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003189
Willy Tarreau60b85b02008-11-30 23:28:40 +01003190/* This function is an analyser which processes the HTTP tarpit. It always
3191 * returns zero, at the beginning because it prevents any other processing
3192 * from occurring, and at the end because it terminates the request.
3193 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003194int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003195{
3196 struct http_txn *txn = &s->txn;
3197
3198 /* This connection is being tarpitted. The CLIENT side has
3199 * already set the connect expiration date to the right
3200 * timeout. We just have to check that the client is still
3201 * there and that the timeout has not expired.
3202 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003203 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003204 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3205 !tick_is_expired(req->analyse_exp, now_ms))
3206 return 0;
3207
3208 /* We will set the queue timer to the time spent, just for
3209 * logging purposes. We fake a 500 server error, so that the
3210 * attacker will not suspect his connection has been tarpitted.
3211 * It will not cause trouble to the logs because we can exclude
3212 * the tarpitted connections by filtering on the 'PT' status flags.
3213 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003214 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3215
3216 txn->status = 500;
3217 if (req->flags != BF_READ_ERROR)
3218 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3219
3220 req->analysers = 0;
3221 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003222
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003223 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003224 if (s->listener->counters)
3225 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003226
Willy Tarreau60b85b02008-11-30 23:28:40 +01003227 if (!(s->flags & SN_ERR_MASK))
3228 s->flags |= SN_ERR_PRXCOND;
3229 if (!(s->flags & SN_FINST_MASK))
3230 s->flags |= SN_FINST_T;
3231 return 0;
3232}
3233
Willy Tarreaud34af782008-11-30 23:36:37 +01003234/* This function is an analyser which processes the HTTP request body. It looks
3235 * for parameters to be used for the load balancing algorithm (url_param). It
3236 * must only be called after the standard HTTP request processing has occurred,
3237 * because it expects the request to be parsed. It returns zero if it needs to
3238 * read more data, or 1 once it has completed its analysis.
3239 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003240int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003241{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003242 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003243 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003244 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003245
3246 /* We have to parse the HTTP request body to find any required data.
3247 * "balance url_param check_post" should have been the only way to get
3248 * into this. We were brought here after HTTP header analysis, so all
3249 * related structures are ready.
3250 */
3251
Willy Tarreau522d6c02009-12-06 18:49:18 +01003252 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3253 goto missing_data;
3254
3255 if (msg->msg_state < HTTP_MSG_100_SENT) {
3256 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3257 * send an HTTP/1.1 100 Continue intermediate response.
3258 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003259 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003260 struct hdr_ctx ctx;
3261 ctx.idx = 0;
3262 /* Expect is allowed in 1.1, look for it */
3263 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3264 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3265 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3266 }
3267 }
3268 msg->msg_state = HTTP_MSG_100_SENT;
3269 }
3270
3271 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003272 /* we have msg->col and msg->sov which both point to the first
3273 * byte of message body. msg->som still points to the beginning
3274 * of the message. We must save the body in req->lr because it
3275 * survives buffer re-alignments.
3276 */
3277 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003278 if (txn->flags & TX_REQ_TE_CHNK)
3279 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3280 else
3281 msg->msg_state = HTTP_MSG_DATA;
3282 }
3283
3284 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003285 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003286 * set ->sov and ->lr to point to the body and switch to DATA or
3287 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003288 */
3289 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003290
Willy Tarreau115acb92009-12-26 13:56:06 +01003291 if (!ret)
3292 goto missing_data;
3293 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003294 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003295 }
3296
Willy Tarreaud98cf932009-12-27 22:54:55 +01003297 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003298 * We have the first non-header byte in msg->col, which is either the
3299 * beginning of the chunk size or of the data. The first data byte is in
3300 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3301 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003302 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003303
3304 if (msg->hdr_content_len < limit)
3305 limit = msg->hdr_content_len;
3306
Willy Tarreau7c96f672009-12-27 22:47:25 +01003307 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003308 goto http_end;
3309
3310 missing_data:
3311 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003312 if (req->flags & BF_FULL)
3313 goto return_bad_req;
3314
Willy Tarreau522d6c02009-12-06 18:49:18 +01003315 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3316 txn->status = 408;
3317 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3318 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003319 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003320
3321 /* we get here if we need to wait for more data */
3322 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003323 /* Not enough data. We'll re-use the http-request
3324 * timeout here. Ideally, we should set the timeout
3325 * relative to the accept() date. We just set the
3326 * request timeout once at the beginning of the
3327 * request.
3328 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003329 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003330 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003331 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003332 return 0;
3333 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003334
3335 http_end:
3336 /* The situation will not evolve, so let's give up on the analysis. */
3337 s->logs.tv_request = now; /* update the request timer to reflect full request */
3338 req->analysers &= ~an_bit;
3339 req->analyse_exp = TICK_ETERNITY;
3340 return 1;
3341
3342 return_bad_req: /* let's centralize all bad requests */
3343 txn->req.msg_state = HTTP_MSG_ERROR;
3344 txn->status = 400;
3345 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3346
3347 return_err_msg:
3348 req->analysers = 0;
3349 s->fe->counters.failed_req++;
3350 if (s->listener->counters)
3351 s->listener->counters->failed_req++;
3352
3353 if (!(s->flags & SN_ERR_MASK))
3354 s->flags |= SN_ERR_PRXCOND;
3355 if (!(s->flags & SN_FINST_MASK))
3356 s->flags |= SN_FINST_R;
3357 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003358}
3359
Willy Tarreau610ecce2010-01-04 21:15:02 +01003360/* Terminate current transaction and prepare a new one. This is very tricky
3361 * right now but it works.
3362 */
3363void http_end_txn_clean_session(struct session *s)
3364{
3365 /* FIXME: We need a more portable way of releasing a backend's and a
3366 * server's connections. We need a safer way to reinitialize buffer
3367 * flags. We also need a more accurate method for computing per-request
3368 * data.
3369 */
3370 http_silent_debug(__LINE__, s);
3371
3372 s->req->cons->flags |= SI_FL_NOLINGER;
3373 s->req->cons->shutr(s->req->cons);
3374 s->req->cons->shutw(s->req->cons);
3375
3376 http_silent_debug(__LINE__, s);
3377
3378 if (s->flags & SN_BE_ASSIGNED)
3379 s->be->beconn--;
3380
3381 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3382 session_process_counters(s);
3383
3384 if (s->txn.status) {
3385 int n;
3386
3387 n = s->txn.status / 100;
3388 if (n < 1 || n > 5)
3389 n = 0;
3390
3391 if (s->fe->mode == PR_MODE_HTTP)
3392 s->fe->counters.p.http.rsp[n]++;
3393
3394 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3395 (s->be->mode == PR_MODE_HTTP))
3396 s->be->counters.p.http.rsp[n]++;
3397 }
3398
3399 /* don't count other requests' data */
3400 s->logs.bytes_in -= s->req->l - s->req->send_max;
3401 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3402
3403 /* let's do a final log if we need it */
3404 if (s->logs.logwait &&
3405 !(s->flags & SN_MONITOR) &&
3406 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3407 s->do_log(s);
3408 }
3409
3410 s->logs.accept_date = date; /* user-visible date for logging */
3411 s->logs.tv_accept = now; /* corrected date for internal use */
3412 tv_zero(&s->logs.tv_request);
3413 s->logs.t_queue = -1;
3414 s->logs.t_connect = -1;
3415 s->logs.t_data = -1;
3416 s->logs.t_close = 0;
3417 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3418 s->logs.srv_queue_size = 0; /* we will get this number soon */
3419
3420 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3421 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3422
3423 if (s->pend_pos)
3424 pendconn_free(s->pend_pos);
3425
3426 if (s->srv) {
3427 if (s->flags & SN_CURR_SESS) {
3428 s->flags &= ~SN_CURR_SESS;
3429 s->srv->cur_sess--;
3430 }
3431 if (may_dequeue_tasks(s->srv, s->be))
3432 process_srv_queue(s->srv);
3433 }
3434
3435 if (unlikely(s->srv_conn))
3436 sess_change_server(s, NULL);
3437 s->srv = NULL;
3438
3439 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3440 s->req->cons->fd = -1; /* just to help with debugging */
3441 s->req->cons->err_type = SI_ET_NONE;
3442 s->req->cons->err_loc = NULL;
3443 s->req->cons->exp = TICK_ETERNITY;
3444 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003445 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3446 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);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003447 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3448 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3449 s->txn.meth = 0;
3450 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003451 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003452 if (s->be->options2 & PR_O2_INDEPSTR)
3453 s->req->cons->flags |= SI_FL_INDEP_STR;
3454
3455 /* if the request buffer is not empty, it means we're
3456 * about to process another request, so send pending
3457 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003458 * Just don't do this if the buffer is close to be full,
3459 * because the request will wait for it to flush a little
3460 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003461 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003462 if (s->req->l > s->req->send_max) {
3463 if (s->rep->send_max &&
3464 !(s->rep->flags & BF_FULL) &&
3465 s->rep->lr <= s->rep->r &&
3466 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3467 s->rep->flags |= BF_EXPECT_MORE;
3468 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003469
3470 /* we're removing the analysers, we MUST re-enable events detection */
3471 buffer_auto_read(s->req);
3472 buffer_auto_close(s->req);
3473 buffer_auto_read(s->rep);
3474 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003475
3476 /* make ->lr point to the first non-forwarded byte */
3477 s->req->lr = s->req->w + s->req->send_max;
3478 if (s->req->lr >= s->req->data + s->req->size)
3479 s->req->lr -= s->req->size;
3480 s->rep->lr = s->rep->w + s->rep->send_max;
3481 if (s->rep->lr >= s->rep->data + s->rep->size)
3482 s->rep->lr -= s->req->size;
3483
3484 s->req->analysers |= s->fe->fe_req_ana;
3485 s->rep->analysers = 0;
3486
3487 http_silent_debug(__LINE__, s);
3488}
3489
3490
3491/* This function updates the request state machine according to the response
3492 * state machine and buffer flags. It returns 1 if it changes anything (flag
3493 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3494 * it is only used to find when a request/response couple is complete. Both
3495 * this function and its equivalent should loop until both return zero. It
3496 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3497 */
3498int http_sync_req_state(struct session *s)
3499{
3500 struct buffer *buf = s->req;
3501 struct http_txn *txn = &s->txn;
3502 unsigned int old_flags = buf->flags;
3503 unsigned int old_state = txn->req.msg_state;
3504
3505 http_silent_debug(__LINE__, s);
3506 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3507 return 0;
3508
3509 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003510 /* No need to read anymore, the request was completely parsed.
3511 * We can shut the read side unless we want to abort_on_close.
3512 */
3513 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3514 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003515
3516 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3517 goto wait_other_side;
3518
3519 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3520 /* The server has not finished to respond, so we
3521 * don't want to move in order not to upset it.
3522 */
3523 goto wait_other_side;
3524 }
3525
3526 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3527 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003528 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003529 txn->req.msg_state = HTTP_MSG_TUNNEL;
3530 goto wait_other_side;
3531 }
3532
3533 /* When we get here, it means that both the request and the
3534 * response have finished receiving. Depending on the connection
3535 * mode, we'll have to wait for the last bytes to leave in either
3536 * direction, and sometimes for a close to be effective.
3537 */
3538
3539 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3540 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3541 /* Server-close mode : queue a connection close to the server */
3542 buffer_shutw_now(buf);
3543 buf->cons->flags |= SI_FL_NOLINGER;
3544 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003545 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003546 /* Option forceclose is set, let's enforce it now
3547 * that we're not expecting any new data to come.
3548 */
3549 buffer_shutr_now(buf);
3550 buffer_shutw_now(buf);
3551 buf->cons->flags |= SI_FL_NOLINGER;
3552 }
3553 /* other modes include httpclose (no action) and keepalive (not implemented) */
3554 }
3555
3556 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3557 /* if we've just closed an output, let's switch */
3558 if (!(buf->flags & BF_OUT_EMPTY)) {
3559 txn->req.msg_state = HTTP_MSG_CLOSING;
3560 goto http_msg_closing;
3561 }
3562 else {
3563 txn->req.msg_state = HTTP_MSG_CLOSED;
3564 goto http_msg_closed;
3565 }
3566 }
3567 else {
3568 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003569 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003570 txn->req.msg_state = HTTP_MSG_TUNNEL;
3571 goto wait_other_side;
3572 }
3573 }
3574
3575 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3576 http_msg_closing:
3577 /* nothing else to forward, just waiting for the output buffer
3578 * to be empty and for the shutw_now to take effect.
3579 */
3580 if (buf->flags & BF_OUT_EMPTY) {
3581 txn->req.msg_state = HTTP_MSG_CLOSED;
3582 goto http_msg_closed;
3583 }
3584 else if (buf->flags & BF_SHUTW) {
3585 txn->req.msg_state = HTTP_MSG_ERROR;
3586 goto wait_other_side;
3587 }
3588 }
3589
3590 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3591 http_msg_closed:
3592 goto wait_other_side;
3593 }
3594
3595 wait_other_side:
3596 http_silent_debug(__LINE__, s);
3597 return txn->req.msg_state != old_state || buf->flags != old_flags;
3598}
3599
3600
3601/* This function updates the response state machine according to the request
3602 * state machine and buffer flags. It returns 1 if it changes anything (flag
3603 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3604 * it is only used to find when a request/response couple is complete. Both
3605 * this function and its equivalent should loop until both return zero. It
3606 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3607 */
3608int http_sync_res_state(struct session *s)
3609{
3610 struct buffer *buf = s->rep;
3611 struct http_txn *txn = &s->txn;
3612 unsigned int old_flags = buf->flags;
3613 unsigned int old_state = txn->rsp.msg_state;
3614
3615 http_silent_debug(__LINE__, s);
3616 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3617 return 0;
3618
3619 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3620 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003621 * still monitor the server connection for a possible close
3622 * while the request is being uploaded, so we don't disable
3623 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003624 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003625 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003626
3627 if (txn->req.msg_state == HTTP_MSG_ERROR)
3628 goto wait_other_side;
3629
3630 if (txn->req.msg_state < HTTP_MSG_DONE) {
3631 /* The client seems to still be sending data, probably
3632 * because we got an error response during an upload.
3633 * We have the choice of either breaking the connection
3634 * or letting it pass through. Let's do the later.
3635 */
3636 goto wait_other_side;
3637 }
3638
3639 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3640 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003641 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003642 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3643 goto wait_other_side;
3644 }
3645
3646 /* When we get here, it means that both the request and the
3647 * response have finished receiving. Depending on the connection
3648 * mode, we'll have to wait for the last bytes to leave in either
3649 * direction, and sometimes for a close to be effective.
3650 */
3651
3652 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3653 /* Server-close mode : shut read and wait for the request
3654 * side to close its output buffer. The caller will detect
3655 * when we're in DONE and the other is in CLOSED and will
3656 * catch that for the final cleanup.
3657 */
3658 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3659 buffer_shutr_now(buf);
3660 goto wait_other_side;
3661 }
3662 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3663 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3664 /* Option forceclose is set, let's enforce it now
3665 * that we're not expecting any new data to come.
3666 * The caller knows the session is complete once
3667 * both states are CLOSED.
3668 */
3669 buffer_shutr_now(buf);
3670 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003671 }
3672 else {
3673 /* other modes include httpclose (no action) and keepalive
3674 * (not implemented). These modes are used as a tunnel right
3675 * now.
3676 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003677 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003678 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3679 goto wait_other_side;
3680 }
3681
3682 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3683 /* if we've just closed an output, let's switch */
3684 if (!(buf->flags & BF_OUT_EMPTY)) {
3685 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3686 goto http_msg_closing;
3687 }
3688 else {
3689 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3690 goto http_msg_closed;
3691 }
3692 }
3693 goto wait_other_side;
3694 }
3695
3696 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3697 http_msg_closing:
3698 /* nothing else to forward, just waiting for the output buffer
3699 * to be empty and for the shutw_now to take effect.
3700 */
3701 if (buf->flags & BF_OUT_EMPTY) {
3702 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3703 goto http_msg_closed;
3704 }
3705 else if (buf->flags & BF_SHUTW) {
3706 txn->rsp.msg_state = HTTP_MSG_ERROR;
3707 goto wait_other_side;
3708 }
3709 }
3710
3711 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3712 http_msg_closed:
3713 /* drop any pending data */
3714 buffer_ignore(buf, buf->l - buf->send_max);
3715 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003716 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717 goto wait_other_side;
3718 }
3719
3720 wait_other_side:
3721 http_silent_debug(__LINE__, s);
3722 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3723}
3724
3725
3726/* Resync the request and response state machines. Return 1 if either state
3727 * changes.
3728 */
3729int http_resync_states(struct session *s)
3730{
3731 struct http_txn *txn = &s->txn;
3732 int old_req_state = txn->req.msg_state;
3733 int old_res_state = txn->rsp.msg_state;
3734
3735 http_silent_debug(__LINE__, s);
3736 http_sync_req_state(s);
3737 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003738 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003739 if (!http_sync_res_state(s))
3740 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003741 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 if (!http_sync_req_state(s))
3743 break;
3744 }
3745 http_silent_debug(__LINE__, s);
3746 /* OK, both state machines agree on a compatible state.
3747 * There are a few cases we're interested in :
3748 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3749 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3750 * directions, so let's simply disable both analysers.
3751 * - HTTP_MSG_CLOSED on the response only means we must abort the
3752 * request.
3753 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3754 * with server-close mode means we've completed one request and we
3755 * must re-initialize the server connection.
3756 */
3757
3758 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3759 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3760 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3761 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3762 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003763 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003764 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003766 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003767 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003768 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003769 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3770 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3771 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003772 s->rep->analysers = 0;
3773 buffer_auto_close(s->rep);
3774 buffer_auto_read(s->rep);
3775 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776 buffer_abort(s->req);
3777 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003778 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003779 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780 }
3781 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3782 txn->rsp.msg_state == HTTP_MSG_DONE &&
3783 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3784 /* server-close: terminate this server connection and
3785 * reinitialize a fresh-new transaction.
3786 */
3787 http_end_txn_clean_session(s);
3788 }
3789
3790 http_silent_debug(__LINE__, s);
3791 return txn->req.msg_state != old_req_state ||
3792 txn->rsp.msg_state != old_res_state;
3793}
3794
Willy Tarreaud98cf932009-12-27 22:54:55 +01003795/* This function is an analyser which forwards request body (including chunk
3796 * sizes if any). It is called as soon as we must forward, even if we forward
3797 * zero byte. The only situation where it must not be called is when we're in
3798 * tunnel mode and we want to forward till the close. It's used both to forward
3799 * remaining data and to resync after end of body. It expects the msg_state to
3800 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3801 * read more data, or 1 once we can go on with next request or end the session.
3802 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3803 * bytes of pending data + the headers if not already done (between som and sov).
3804 * It eventually adjusts som to match sov after the data in between have been sent.
3805 */
3806int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3807{
3808 struct http_txn *txn = &s->txn;
3809 struct http_msg *msg = &s->txn.req;
3810
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003811 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3812 return 0;
3813
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003814 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3815 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3816 /* Output closed while we were sending data. We must abort. */
3817 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003818 buffer_auto_read(req);
3819 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003820 req->analysers &= ~an_bit;
3821 return 1;
3822 }
3823
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003824 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003825
3826 /* Note that we don't have to send 100-continue back because we don't
3827 * need the data to complete our job, and it's up to the server to
3828 * decide whether to return 100, 417 or anything else in return of
3829 * an "Expect: 100-continue" header.
3830 */
3831
3832 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3833 /* we have msg->col and msg->sov which both point to the first
3834 * byte of message body. msg->som still points to the beginning
3835 * of the message. We must save the body in req->lr because it
3836 * survives buffer re-alignments.
3837 */
3838 req->lr = req->data + msg->sov;
3839 if (txn->flags & TX_REQ_TE_CHNK)
3840 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3841 else {
3842 msg->msg_state = HTTP_MSG_DATA;
3843 }
3844 }
3845
Willy Tarreaud98cf932009-12-27 22:54:55 +01003846 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003847 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003848 /* we may have some data pending */
3849 if (msg->hdr_content_len || msg->som != msg->sov) {
3850 int bytes = msg->sov - msg->som;
3851 if (bytes < 0) /* sov may have wrapped at the end */
3852 bytes += req->size;
3853 buffer_forward(req, bytes + msg->hdr_content_len);
3854 msg->hdr_content_len = 0; /* don't forward that again */
3855 msg->som = msg->sov;
3856 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003857
Willy Tarreaucaabe412010-01-03 23:08:28 +01003858 if (msg->msg_state == HTTP_MSG_DATA) {
3859 /* must still forward */
3860 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003861 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003862
3863 /* nothing left to forward */
3864 if (txn->flags & TX_REQ_TE_CHNK)
3865 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003867 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003868 }
3869 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003870 /* read the chunk size and assign it to ->hdr_content_len, then
3871 * set ->sov and ->lr to point to the body and switch to DATA or
3872 * TRAILERS state.
3873 */
3874 int ret = http_parse_chunk_size(req, msg);
3875
3876 if (!ret)
3877 goto missing_data;
3878 else if (ret < 0)
3879 goto return_bad_req;
3880 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003881 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003882 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3883 /* we want the CRLF after the data */
3884 int ret;
3885
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003886 req->lr = req->w + req->send_max;
3887 if (req->lr >= req->data + req->size)
3888 req->lr -= req->size;
3889
Willy Tarreaud98cf932009-12-27 22:54:55 +01003890 ret = http_skip_chunk_crlf(req, msg);
3891
3892 if (ret == 0)
3893 goto missing_data;
3894 else if (ret < 0)
3895 goto return_bad_req;
3896 /* we're in MSG_CHUNK_SIZE now */
3897 }
3898 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3899 int ret = http_forward_trailers(req, msg);
3900
3901 if (ret == 0)
3902 goto missing_data;
3903 else if (ret < 0)
3904 goto return_bad_req;
3905 /* we're in HTTP_MSG_DONE now */
3906 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003907 else {
3908 /* other states, DONE...TUNNEL */
3909 if (http_resync_states(s)) {
3910 /* some state changes occurred, maybe the analyser
3911 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003912 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003913 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3914 goto return_bad_req;
3915 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003916 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003918 }
3919 }
3920
Willy Tarreaud98cf932009-12-27 22:54:55 +01003921 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003922 /* stop waiting for data if the input is closed before the end */
3923 if (req->flags & BF_SHUTR)
3924 goto return_bad_req;
3925
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003926 /* waiting for the last bits to leave the buffer */
3927 if (req->flags & BF_SHUTW)
3928 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929
3930 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003931 return 0;
3932
Willy Tarreaud98cf932009-12-27 22:54:55 +01003933 return_bad_req: /* let's centralize all bad requests */
3934 txn->req.msg_state = HTTP_MSG_ERROR;
3935 txn->status = 400;
3936 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01003937 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003938 req->analysers = 0;
3939 s->fe->counters.failed_req++;
3940 if (s->listener->counters)
3941 s->listener->counters->failed_req++;
3942
3943 if (!(s->flags & SN_ERR_MASK))
3944 s->flags |= SN_ERR_PRXCOND;
3945 if (!(s->flags & SN_FINST_MASK))
3946 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003948 return 0;
3949}
3950
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003951/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3952 * processing can continue on next analysers, or zero if it either needs more
3953 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3954 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3955 * when it has nothing left to do, and may remove any analyser when it wants to
3956 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003957 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003958int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003959{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003960 struct http_txn *txn = &s->txn;
3961 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003962 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003963 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003964 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003965 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003966
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003967 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 +02003968 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003969 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003970 rep,
3971 rep->rex, rep->wex,
3972 rep->flags,
3973 rep->l,
3974 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003975
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003976 /*
3977 * Now parse the partial (or complete) lines.
3978 * We will check the response syntax, and also join multi-line
3979 * headers. An index of all the lines will be elaborated while
3980 * parsing.
3981 *
3982 * For the parsing, we use a 28 states FSM.
3983 *
3984 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003985 * rep->data + msg->som = beginning of response
3986 * rep->data + msg->eoh = end of processed headers / start of current one
3987 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003988 * rep->lr = first non-visited byte
3989 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01003990 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003991 */
3992
Willy Tarreau83e3af02009-12-28 17:39:57 +01003993 /* There's a protected area at the end of the buffer for rewriting
3994 * purposes. We don't want to start to parse the request if the
3995 * protected area is affected, because we may have to move processed
3996 * data later, which is much more complicated.
3997 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003998 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3999 if (unlikely((rep->flags & BF_FULL) ||
4000 rep->r < rep->lr ||
4001 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4002 if (rep->send_max) {
4003 /* some data has still not left the buffer, wake us once that's done */
4004 buffer_dont_close(rep);
4005 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4006 return 0;
4007 }
4008 if (rep->l <= rep->size - global.tune.maxrewrite)
4009 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004010 }
4011
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004012 if (likely(rep->lr < rep->r))
4013 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004014 }
4015
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004016 /* 1: we might have to print this header in debug mode */
4017 if (unlikely((global.mode & MODE_DEBUG) &&
4018 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004019 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004020 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004021
Willy Tarreau962c3f42010-01-10 00:15:35 +01004022 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004023 eol = sol + msg->sl.rq.l;
4024 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004025
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004026 sol += hdr_idx_first_pos(&txn->hdr_idx);
4027 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004028
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004029 while (cur_idx) {
4030 eol = sol + txn->hdr_idx.v[cur_idx].len;
4031 debug_hdr("srvhdr", s, sol, eol);
4032 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4033 cur_idx = txn->hdr_idx.v[cur_idx].next;
4034 }
4035 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004036
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004037 /*
4038 * Now we quickly check if we have found a full valid response.
4039 * If not so, we check the FD and buffer states before leaving.
4040 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004041 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004042 * responses are checked first.
4043 *
4044 * Depending on whether the client is still there or not, we
4045 * may send an error response back or not. Note that normally
4046 * we should only check for HTTP status there, and check I/O
4047 * errors somewhere else.
4048 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004049
Willy Tarreau655dce92009-11-08 13:10:58 +01004050 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004051 /* Invalid response */
4052 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4053 /* we detected a parsing error. We want to archive this response
4054 * in the dedicated proxy area for later troubleshooting.
4055 */
4056 hdr_response_bad:
4057 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4058 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4059
4060 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004061 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004062 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004063 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4064 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004065
Willy Tarreau90deb182010-01-07 00:20:41 +01004066 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004067 rep->analysers = 0;
4068 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004069 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004070 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4071
4072 if (!(s->flags & SN_ERR_MASK))
4073 s->flags |= SN_ERR_PRXCOND;
4074 if (!(s->flags & SN_FINST_MASK))
4075 s->flags |= SN_FINST_H;
4076
4077 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004078 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004080 /* too large response does not fit in buffer. */
4081 else if (rep->flags & BF_FULL) {
4082 goto hdr_response_bad;
4083 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004084
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004085 /* read error */
4086 else if (rep->flags & BF_READ_ERROR) {
4087 if (msg->err_pos >= 0)
4088 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004089
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004090 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004091 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004092 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004093 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4094 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004095
Willy Tarreau90deb182010-01-07 00:20:41 +01004096 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004097 rep->analysers = 0;
4098 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004099 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004100 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004101
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004102 if (!(s->flags & SN_ERR_MASK))
4103 s->flags |= SN_ERR_SRVCL;
4104 if (!(s->flags & SN_FINST_MASK))
4105 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004106 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004107 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004108
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004109 /* read timeout : return a 504 to the client. */
4110 else if (rep->flags & BF_READ_TIMEOUT) {
4111 if (msg->err_pos >= 0)
4112 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004113
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004114 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004115 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004116 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004117 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4118 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004119
Willy Tarreau90deb182010-01-07 00:20:41 +01004120 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004121 rep->analysers = 0;
4122 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004123 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004124 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004125
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004126 if (!(s->flags & SN_ERR_MASK))
4127 s->flags |= SN_ERR_SRVTO;
4128 if (!(s->flags & SN_FINST_MASK))
4129 s->flags |= SN_FINST_H;
4130 return 0;
4131 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004132
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004133 /* close from server */
4134 else if (rep->flags & BF_SHUTR) {
4135 if (msg->err_pos >= 0)
4136 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004137
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004138 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004139 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004140 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004141 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4142 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004143
Willy Tarreau90deb182010-01-07 00:20:41 +01004144 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004145 rep->analysers = 0;
4146 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004147 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004148 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004149
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004150 if (!(s->flags & SN_ERR_MASK))
4151 s->flags |= SN_ERR_SRVCL;
4152 if (!(s->flags & SN_FINST_MASK))
4153 s->flags |= SN_FINST_H;
4154 return 0;
4155 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004156
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004157 /* write error to client (we don't send any message then) */
4158 else if (rep->flags & BF_WRITE_ERROR) {
4159 if (msg->err_pos >= 0)
4160 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004161
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004162 s->be->counters.failed_resp++;
4163 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004164 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004165
4166 if (!(s->flags & SN_ERR_MASK))
4167 s->flags |= SN_ERR_CLICL;
4168 if (!(s->flags & SN_FINST_MASK))
4169 s->flags |= SN_FINST_H;
4170
4171 /* process_session() will take care of the error */
4172 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004173 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004174
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004175 buffer_dont_close(rep);
4176 return 0;
4177 }
4178
4179 /* More interesting part now : we know that we have a complete
4180 * response which at least looks like HTTP. We have an indicator
4181 * of each header's length, so we can parse them quickly.
4182 */
4183
4184 if (unlikely(msg->err_pos >= 0))
4185 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4186
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004187 /*
4188 * 1: get the status code
4189 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004190 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004191 if (n < 1 || n > 5)
4192 n = 0;
4193 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004194
Willy Tarreau5b154472009-12-21 20:11:07 +01004195 /* check if the response is HTTP/1.1 or above */
4196 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004197 ((msg->sol[5] > '1') ||
4198 ((msg->sol[5] == '1') &&
4199 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004200 txn->flags |= TX_RES_VER_11;
4201
4202 /* "connection" has not been parsed yet */
4203 txn->flags &= ~TX_CON_HDR_PARS;
4204
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004205 /* transfer length unknown*/
4206 txn->flags &= ~TX_RES_XFER_LEN;
4207
Willy Tarreau962c3f42010-01-10 00:15:35 +01004208 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004209
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004210 if (txn->status >= 100 && txn->status < 500)
4211 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4212 else
4213 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4214
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004215 /*
4216 * 2: check for cacheability.
4217 */
4218
4219 switch (txn->status) {
4220 case 200:
4221 case 203:
4222 case 206:
4223 case 300:
4224 case 301:
4225 case 410:
4226 /* RFC2616 @13.4:
4227 * "A response received with a status code of
4228 * 200, 203, 206, 300, 301 or 410 MAY be stored
4229 * by a cache (...) unless a cache-control
4230 * directive prohibits caching."
4231 *
4232 * RFC2616 @9.5: POST method :
4233 * "Responses to this method are not cacheable,
4234 * unless the response includes appropriate
4235 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004236 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004237 if (likely(txn->meth != HTTP_METH_POST) &&
4238 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4239 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4240 break;
4241 default:
4242 break;
4243 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004244
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004245 /*
4246 * 3: we may need to capture headers
4247 */
4248 s->logs.logwait &= ~LW_RESP;
4249 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004250 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004251 txn->rsp.cap, s->fe->rsp_cap);
4252
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004253 /* 4: determine the transfer-length.
4254 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4255 * the presence of a message-body in a RESPONSE and its transfer length
4256 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004257 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004258 * All responses to the HEAD request method MUST NOT include a
4259 * message-body, even though the presence of entity-header fields
4260 * might lead one to believe they do. All 1xx (informational), 204
4261 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4262 * message-body. All other responses do include a message-body,
4263 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004264 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004265 * 1. Any response which "MUST NOT" include a message-body (such as the
4266 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4267 * always terminated by the first empty line after the header fields,
4268 * regardless of the entity-header fields present in the message.
4269 *
4270 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4271 * the "chunked" transfer-coding (Section 6.2) is used, the
4272 * transfer-length is defined by the use of this transfer-coding.
4273 * If a Transfer-Encoding header field is present and the "chunked"
4274 * transfer-coding is not present, the transfer-length is defined by
4275 * the sender closing the connection.
4276 *
4277 * 3. If a Content-Length header field is present, its decimal value in
4278 * OCTETs represents both the entity-length and the transfer-length.
4279 * If a message is received with both a Transfer-Encoding header
4280 * field and a Content-Length header field, the latter MUST be ignored.
4281 *
4282 * 4. If the message uses the media type "multipart/byteranges", and
4283 * the transfer-length is not otherwise specified, then this self-
4284 * delimiting media type defines the transfer-length. This media
4285 * type MUST NOT be used unless the sender knows that the recipient
4286 * can parse it; the presence in a request of a Range header with
4287 * multiple byte-range specifiers from a 1.1 client implies that the
4288 * client can parse multipart/byteranges responses.
4289 *
4290 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004291 */
4292
4293 /* Skip parsing if no content length is possible. The response flags
4294 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004295 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004296 * FIXME: should we parse anyway and return an error on chunked encoding ?
4297 */
4298 if (txn->meth == HTTP_METH_HEAD ||
4299 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004300 txn->status == 204 || txn->status == 304) {
4301 txn->flags |= TX_RES_XFER_LEN;
4302 goto skip_content_length;
4303 }
4304
4305 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004306 goto skip_content_length;
4307
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004308 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004309 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004310 while ((txn->flags & TX_RES_VER_11) &&
4311 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004312 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4313 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4314 else if (txn->flags & TX_RES_TE_CHNK) {
4315 /* bad transfer-encoding (chunked followed by something else) */
4316 use_close_only = 1;
4317 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4318 break;
4319 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004320 }
4321
4322 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4323 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004324 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004325 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4326 signed long long cl;
4327
4328 if (!ctx.vlen)
4329 goto hdr_response_bad;
4330
4331 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4332 goto hdr_response_bad; /* parse failure */
4333
4334 if (cl < 0)
4335 goto hdr_response_bad;
4336
4337 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4338 goto hdr_response_bad; /* already specified, was different */
4339
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004340 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004341 msg->hdr_content_len = cl;
4342 }
4343
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004344 /* FIXME: we should also implement the multipart/byterange method.
4345 * For now on, we resort to close mode in this case (unknown length).
4346 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004347skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004348
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004349 /* end of job, return OK */
4350 rep->analysers &= ~an_bit;
4351 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004352 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004353 return 1;
4354}
4355
4356/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004357 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4358 * and updates t->rep->analysers. It might make sense to explode it into several
4359 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004360 */
4361int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4362{
4363 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004364 struct http_msg *msg = &txn->rsp;
4365 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004366 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004367 int conn_ka = 0, conn_cl = 0;
4368 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004369 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004370
4371 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4372 now_ms, __FUNCTION__,
4373 t,
4374 rep,
4375 rep->rex, rep->wex,
4376 rep->flags,
4377 rep->l,
4378 rep->analysers);
4379
Willy Tarreau655dce92009-11-08 13:10:58 +01004380 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004381 return 0;
4382
4383 rep->analysers &= ~an_bit;
4384 rep->analyse_exp = TICK_ETERNITY;
4385
Willy Tarreau5b154472009-12-21 20:11:07 +01004386 /* Now we have to check if we need to modify the Connection header.
4387 * This is more difficult on the response than it is on the request,
4388 * because we can have two different HTTP versions and we don't know
4389 * how the client will interprete a response. For instance, let's say
4390 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4391 * HTTP/1.1 response without any header. Maybe it will bound itself to
4392 * HTTP/1.0 because it only knows about it, and will consider the lack
4393 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4394 * the lack of header as a keep-alive. Thus we will use two flags
4395 * indicating how a request MAY be understood by the client. In case
4396 * of multiple possibilities, we'll fix the header to be explicit. If
4397 * ambiguous cases such as both close and keepalive are seen, then we
4398 * will fall back to explicit close. Note that we won't take risks with
4399 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4400 */
4401
4402 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004403 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4404 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4405 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004406 int may_keep = 0, may_close = 0; /* how it may be understood */
4407 struct hdr_ctx ctx;
4408
4409 ctx.idx = 0;
4410 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4411 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4412 conn_cl = 1;
4413 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4414 conn_ka = 1;
4415 }
4416
4417 if (conn_cl) {
4418 /* close header present */
4419 may_close = 1;
4420 if (conn_ka) /* we have both close and keep-alive */
4421 may_keep = 1;
4422 }
4423 else if (conn_ka) {
4424 /* keep-alive alone */
4425 may_keep = 1;
4426 }
4427 else {
4428 /* no close nor keep-alive header */
4429 if (txn->flags & TX_RES_VER_11)
4430 may_keep = 1;
4431 else
4432 may_close = 1;
4433
4434 if (txn->flags & TX_REQ_VER_11)
4435 may_keep = 1;
4436 else
4437 may_close = 1;
4438 }
4439
4440 /* let's update the transaction status to reflect any close.
4441 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004442 * handled. We also explicitly state that we will close in
4443 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004444 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004445 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004446 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4447 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004448 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4449
4450 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004451 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004452 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4453 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004454 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004455 if (may_keep &&
4456 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4457 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004458 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004459 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4460 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4461 must_del_close = 1;
4462 if (!(txn->flags & TX_REQ_VER_11))
4463 must_keep = 1;
4464 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004465
4466 txn->flags |= TX_CON_HDR_PARS;
4467 }
4468
4469 /* We might have to check for "Connection:" if the server
4470 * returns a connection status that is not compatible with
4471 * the client's or with the config.
4472 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004473 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004474 char *cur_ptr, *cur_end, *cur_next;
4475 int cur_idx, old_idx, delta, val;
4476 int must_delete;
4477 struct hdr_idx_elem *cur_hdr;
4478
4479 /* we just have to remove the headers if both sides are 1.0 */
4480 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004481
4482 /* same if we want to re-enable keep-alive on 1.1 */
4483 must_delete |= must_del_close;
4484
Willy Tarreau962c3f42010-01-10 00:15:35 +01004485 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau5b154472009-12-21 20:11:07 +01004486
4487 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4488 cur_hdr = &txn->hdr_idx.v[cur_idx];
4489 cur_ptr = cur_next;
4490 cur_end = cur_ptr + cur_hdr->len;
4491 cur_next = cur_end + cur_hdr->cr + 1;
4492
4493 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4494 if (!val)
4495 continue;
4496
4497 /* 3 possibilities :
4498 * - we have already set "Connection: close" or we're in
4499 * HTTP/1.0, so we remove this line.
4500 * - we have not yet set "Connection: close", but this line
4501 * indicates close. We leave it untouched and set the flag.
4502 * - we have not yet set "Connection: close", and this line
4503 * indicates non-close. We replace it and set the flag.
4504 */
4505 if (must_delete) {
4506 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4507 http_msg_move_end(&txn->rsp, delta);
4508 cur_next += delta;
4509 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4510 txn->hdr_idx.used--;
4511 cur_hdr->len = 0;
4512 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004513 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004514 } else {
4515 if (cur_end - cur_ptr - val != 5 ||
4516 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4517 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4518 "close", 5);
4519 cur_next += delta;
4520 cur_hdr->len += delta;
4521 http_msg_move_end(&txn->rsp, delta);
4522 }
4523 must_delete = 1;
4524 must_close = 0;
4525 }
4526 } /* for loop */
4527 } /* if must close keep-alive */
4528
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004529 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004530 /*
4531 * 3: we will have to evaluate the filters.
4532 * As opposed to version 1.2, now they will be evaluated in the
4533 * filters order and not in the header order. This means that
4534 * each filter has to be validated among all headers.
4535 *
4536 * Filters are tried with ->be first, then with ->fe if it is
4537 * different from ->be.
4538 */
4539
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004540 cur_proxy = t->be;
4541 while (1) {
4542 struct proxy *rule_set = cur_proxy;
4543
4544 /* try headers filters */
4545 if (rule_set->rsp_exp != NULL) {
4546 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4547 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004548 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004549 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004550 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4551 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004552 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004553 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004554 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004555 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004556 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004557 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004558 if (!(t->flags & SN_ERR_MASK))
4559 t->flags |= SN_ERR_PRXCOND;
4560 if (!(t->flags & SN_FINST_MASK))
4561 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004562 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004563 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004564 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004565
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004566 /* has the response been denied ? */
4567 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004568 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004569 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004570
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004571 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004572 if (t->listener->counters)
4573 t->listener->counters->denied_resp++;
4574
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004575 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004576 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004577
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004578 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004579 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004580 if (txn->status < 200)
4581 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004582 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004583 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004584 }
4585
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004586 /* check whether we're already working on the frontend */
4587 if (cur_proxy == t->fe)
4588 break;
4589 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004590 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004591
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004592 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004593 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4594 * in which case this is not the right response, and we're waiting for the
4595 * next one. Let's allow this response to go to the client and wait for the
4596 * next one.
4597 */
4598 if (txn->status < 200) {
4599 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004600 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004601 msg->msg_state = HTTP_MSG_RPBEFORE;
4602 txn->status = 0;
4603 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4604 return 1;
4605 }
4606
4607 /* we don't have any 1xx status code now */
4608
4609 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004610 * 4: check for server cookie.
4611 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004612 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4613 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004614 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004615
Willy Tarreaubaaee002006-06-26 02:48:02 +02004616
Willy Tarreaua15645d2007-03-18 16:22:39 +01004617 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004618 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004619 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004620 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004621 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004622
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004623 /*
4624 * 6: add server cookie in the response if needed
4625 */
4626 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004627 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004628 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004629
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004630 /* the server is known, it's not the one the client requested, we have to
4631 * insert a set-cookie here, except if we want to insert only on POST
4632 * requests and this one isn't. Note that servers which don't have cookies
4633 * (eg: some backup servers) will return a full cookie removal request.
4634 */
4635 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4636 t->be->cookie_name,
4637 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004638
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004639 if (t->be->cookie_domain)
4640 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004641
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004642 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004643 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004644 goto return_bad_resp;
4645 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004646
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004647 /* Here, we will tell an eventual cache on the client side that we don't
4648 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4649 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4650 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4651 */
4652 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004653
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004654 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4655
4656 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004657 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004658 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004659 }
4660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004661
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004662 /*
4663 * 7: check if result will be cacheable with a cookie.
4664 * We'll block the response if security checks have caught
4665 * nasty things such as a cacheable cookie.
4666 */
4667 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4668 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004669 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004670
4671 /* we're in presence of a cacheable response containing
4672 * a set-cookie header. We'll block it as requested by
4673 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004674 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004675 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004676 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004677
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004678 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004679 if (t->listener->counters)
4680 t->listener->counters->denied_resp++;
4681
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004682 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4683 t->be->id, t->srv?t->srv->id:"<dispatch>");
4684 send_log(t->be, LOG_ALERT,
4685 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4686 t->be->id, t->srv?t->srv->id:"<dispatch>");
4687 goto return_srv_prx_502;
4688 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004689
4690 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004691 * 8: add "Connection: close" if needed and not yet set. This is
4692 * only needed for 1.1 responses since we know there is no other
4693 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004694 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004695 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004696 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004697 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004698 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004699 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004700 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004701 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4702 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4703 "Connection: keep-alive", 22) < 0))
4704 goto return_bad_resp;
4705 must_keep = 0;
4706 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004707
Willy Tarreaud98cf932009-12-27 22:54:55 +01004708 if (txn->flags & TX_RES_XFER_LEN)
4709 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004710
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004711 /*************************************************************
4712 * OK, that's finished for the headers. We have done what we *
4713 * could. Let's switch to the DATA state. *
4714 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004715
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004716 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004717
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004718 /* if the user wants to log as soon as possible, without counting
4719 * bytes from the server, then this is the right moment. We have
4720 * to temporarily assign bytes_out to log what we currently have.
4721 */
4722 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4723 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4724 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004725 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004726 t->logs.bytes_out = 0;
4727 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004728
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004729 /* Note: we must not try to cheat by jumping directly to DATA,
4730 * otherwise we would not let the client side wake up.
4731 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004733 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004734 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004735 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004736}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004737
Willy Tarreaud98cf932009-12-27 22:54:55 +01004738/* This function is an analyser which forwards response body (including chunk
4739 * sizes if any). It is called as soon as we must forward, even if we forward
4740 * zero byte. The only situation where it must not be called is when we're in
4741 * tunnel mode and we want to forward till the close. It's used both to forward
4742 * remaining data and to resync after end of body. It expects the msg_state to
4743 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4744 * read more data, or 1 once we can go on with next request or end the session.
4745 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4746 * bytes of pending data + the headers if not already done (between som and sov).
4747 * It eventually adjusts som to match sov after the data in between have been sent.
4748 */
4749int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4750{
4751 struct http_txn *txn = &s->txn;
4752 struct http_msg *msg = &s->txn.rsp;
4753
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004754 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4755 return 0;
4756
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004757 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004758 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004759 !s->req->analysers) {
4760 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4761 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004762 buffer_auto_read(res);
4763 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004764 res->analysers &= ~an_bit;
4765 return 1;
4766 }
4767
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004768 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004769
Willy Tarreaud98cf932009-12-27 22:54:55 +01004770 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4771 /* we have msg->col and msg->sov which both point to the first
4772 * byte of message body. msg->som still points to the beginning
4773 * of the message. We must save the body in req->lr because it
4774 * survives buffer re-alignments.
4775 */
4776 res->lr = res->data + msg->sov;
4777 if (txn->flags & TX_RES_TE_CHNK)
4778 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4779 else {
4780 msg->msg_state = HTTP_MSG_DATA;
4781 }
4782 }
4783
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004785 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004786 /* we may have some data pending */
4787 if (msg->hdr_content_len || msg->som != msg->sov) {
4788 int bytes = msg->sov - msg->som;
4789 if (bytes < 0) /* sov may have wrapped at the end */
4790 bytes += res->size;
4791 buffer_forward(res, bytes + msg->hdr_content_len);
4792 msg->hdr_content_len = 0; /* don't forward that again */
4793 msg->som = msg->sov;
4794 }
4795
Willy Tarreaucaabe412010-01-03 23:08:28 +01004796 if (msg->msg_state == HTTP_MSG_DATA) {
4797 /* must still forward */
4798 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004799 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004800
4801 /* nothing left to forward */
4802 if (txn->flags & TX_RES_TE_CHNK)
4803 msg->msg_state = HTTP_MSG_DATA_CRLF;
4804 else
4805 msg->msg_state = HTTP_MSG_DONE;
4806 }
4807 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004808 /* read the chunk size and assign it to ->hdr_content_len, then
4809 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4810 */
4811 int ret = http_parse_chunk_size(res, msg);
4812
4813 if (!ret)
4814 goto missing_data;
4815 else if (ret < 0)
4816 goto return_bad_res;
4817 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004818 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004819 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4820 /* we want the CRLF after the data */
4821 int ret;
4822
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004823 res->lr = res->w + res->send_max;
4824 if (res->lr >= res->data + res->size)
4825 res->lr -= res->size;
4826
Willy Tarreaud98cf932009-12-27 22:54:55 +01004827 ret = http_skip_chunk_crlf(res, msg);
4828
4829 if (!ret)
4830 goto missing_data;
4831 else if (ret < 0)
4832 goto return_bad_res;
4833 /* we're in MSG_CHUNK_SIZE now */
4834 }
4835 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4836 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004837
Willy Tarreaud98cf932009-12-27 22:54:55 +01004838 if (ret == 0)
4839 goto missing_data;
4840 else if (ret < 0)
4841 goto return_bad_res;
4842 /* we're in HTTP_MSG_DONE now */
4843 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004844 else {
4845 /* other states, DONE...TUNNEL */
4846 if (http_resync_states(s)) {
4847 http_silent_debug(__LINE__, s);
4848 /* some state changes occurred, maybe the analyser
4849 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004850 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004851 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4852 goto return_bad_res;
4853 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004854 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856 }
4857 }
4858
Willy Tarreaud98cf932009-12-27 22:54:55 +01004859 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004860 /* stop waiting for data if the input is closed before the end */
4861 if (res->flags & BF_SHUTR)
4862 goto return_bad_res;
4863
Willy Tarreau610ecce2010-01-04 21:15:02 +01004864 if (!s->req->analysers)
4865 goto return_bad_res;
4866
Willy Tarreaud98cf932009-12-27 22:54:55 +01004867 /* forward the chunk size as well as any pending data */
4868 if (msg->hdr_content_len || msg->som != msg->sov) {
4869 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4870 msg->hdr_content_len = 0; /* don't forward that again */
4871 msg->som = msg->sov;
4872 }
4873
Willy Tarreaud98cf932009-12-27 22:54:55 +01004874 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004876 return 0;
4877
4878 return_bad_res: /* let's centralize all bad resuests */
4879 txn->rsp.msg_state = HTTP_MSG_ERROR;
4880 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01004881 /* don't send any error message as we're in the body */
4882 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004883 res->analysers = 0;
4884 s->be->counters.failed_resp++;
4885 if (s->srv) {
4886 s->srv->counters.failed_resp++;
4887 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4888 }
4889
4890 if (!(s->flags & SN_ERR_MASK))
4891 s->flags |= SN_ERR_PRXCOND;
4892 if (!(s->flags & SN_FINST_MASK))
4893 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004895 return 0;
4896}
4897
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004898/* Iterate the same filter through all request headers.
4899 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004900 * Since it can manage the switch to another backend, it updates the per-proxy
4901 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004902 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004903int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004904{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004905 char term;
4906 char *cur_ptr, *cur_end, *cur_next;
4907 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004908 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004909 struct hdr_idx_elem *cur_hdr;
4910 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004912 last_hdr = 0;
4913
Willy Tarreau962c3f42010-01-10 00:15:35 +01004914 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004915 old_idx = 0;
4916
4917 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004918 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004919 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004920 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004921 (exp->action == ACT_ALLOW ||
4922 exp->action == ACT_DENY ||
4923 exp->action == ACT_TARPIT))
4924 return 0;
4925
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004926 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004927 if (!cur_idx)
4928 break;
4929
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004930 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004931 cur_ptr = cur_next;
4932 cur_end = cur_ptr + cur_hdr->len;
4933 cur_next = cur_end + cur_hdr->cr + 1;
4934
4935 /* Now we have one header between cur_ptr and cur_end,
4936 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004937 */
4938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004939 /* The annoying part is that pattern matching needs
4940 * that we modify the contents to null-terminate all
4941 * strings before testing them.
4942 */
4943
4944 term = *cur_end;
4945 *cur_end = '\0';
4946
4947 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4948 switch (exp->action) {
4949 case ACT_SETBE:
4950 /* It is not possible to jump a second time.
4951 * FIXME: should we return an HTTP/500 here so that
4952 * the admin knows there's a problem ?
4953 */
4954 if (t->be != t->fe)
4955 break;
4956
4957 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004958 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004959 last_hdr = 1;
4960 break;
4961
4962 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004963 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004964 last_hdr = 1;
4965 break;
4966
4967 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004968 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004969 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004970
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004971 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004972 if (t->listener->counters)
4973 t->listener->counters->denied_resp++;
4974
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004975 break;
4976
4977 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004978 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004979 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004980
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004981 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004982 if (t->listener->counters)
4983 t->listener->counters->denied_resp++;
4984
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004985 break;
4986
4987 case ACT_REPLACE:
4988 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4989 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4990 /* FIXME: if the user adds a newline in the replacement, the
4991 * index will not be recalculated for now, and the new line
4992 * will not be counted as a new header.
4993 */
4994
4995 cur_end += delta;
4996 cur_next += delta;
4997 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004998 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004999 break;
5000
5001 case ACT_REMOVE:
5002 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5003 cur_next += delta;
5004
Willy Tarreaufa355d42009-11-29 18:12:29 +01005005 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005006 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5007 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005008 cur_hdr->len = 0;
5009 cur_end = NULL; /* null-term has been rewritten */
5010 break;
5011
5012 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005013 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 if (cur_end)
5015 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005017 /* keep the link from this header to next one in case of later
5018 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005019 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005020 old_idx = cur_idx;
5021 }
5022 return 0;
5023}
5024
5025
5026/* Apply the filter to the request line.
5027 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5028 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005029 * Since it can manage the switch to another backend, it updates the per-proxy
5030 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005031 */
5032int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5033{
5034 char term;
5035 char *cur_ptr, *cur_end;
5036 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005037 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005038 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005039
Willy Tarreau58f10d72006-12-04 02:26:12 +01005040
Willy Tarreau3d300592007-03-18 18:34:41 +01005041 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005042 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005043 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005044 (exp->action == ACT_ALLOW ||
5045 exp->action == ACT_DENY ||
5046 exp->action == ACT_TARPIT))
5047 return 0;
5048 else if (exp->action == ACT_REMOVE)
5049 return 0;
5050
5051 done = 0;
5052
Willy Tarreau962c3f42010-01-10 00:15:35 +01005053 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005054 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005055
5056 /* Now we have the request line between cur_ptr and cur_end */
5057
5058 /* The annoying part is that pattern matching needs
5059 * that we modify the contents to null-terminate all
5060 * strings before testing them.
5061 */
5062
5063 term = *cur_end;
5064 *cur_end = '\0';
5065
5066 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5067 switch (exp->action) {
5068 case ACT_SETBE:
5069 /* It is not possible to jump a second time.
5070 * FIXME: should we return an HTTP/500 here so that
5071 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005072 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005073 if (t->be != t->fe)
5074 break;
5075
5076 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005077 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005078 done = 1;
5079 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005081 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005082 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005083 done = 1;
5084 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005086 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005087 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005088
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005089 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005090 if (t->listener->counters)
5091 t->listener->counters->denied_resp++;
5092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005093 done = 1;
5094 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005096 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005097 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005098
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005099 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005100 if (t->listener->counters)
5101 t->listener->counters->denied_resp++;
5102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005103 done = 1;
5104 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005105
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005106 case ACT_REPLACE:
5107 *cur_end = term; /* restore the string terminator */
5108 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5109 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5110 /* FIXME: if the user adds a newline in the replacement, the
5111 * index will not be recalculated for now, and the new line
5112 * will not be counted as a new header.
5113 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005114
Willy Tarreaufa355d42009-11-29 18:12:29 +01005115 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005116 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005117 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005118 HTTP_MSG_RQMETH,
5119 cur_ptr, cur_end + 1,
5120 NULL, NULL);
5121 if (unlikely(!cur_end))
5122 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005124 /* we have a full request and we know that we have either a CR
5125 * or an LF at <ptr>.
5126 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005127 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5128 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005129 /* there is no point trying this regex on headers */
5130 return 1;
5131 }
5132 }
5133 *cur_end = term; /* restore the string terminator */
5134 return done;
5135}
Willy Tarreau97de6242006-12-27 17:18:38 +01005136
Willy Tarreau58f10d72006-12-04 02:26:12 +01005137
Willy Tarreau58f10d72006-12-04 02:26:12 +01005138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005139/*
5140 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5141 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005142 * unparsable request. Since it can manage the switch to another backend, it
5143 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005144 */
5145int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5146{
Willy Tarreau3d300592007-03-18 18:34:41 +01005147 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005148 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005149 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005150 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005152 /*
5153 * The interleaving of transformations and verdicts
5154 * makes it difficult to decide to continue or stop
5155 * the evaluation.
5156 */
5157
Willy Tarreau3d300592007-03-18 18:34:41 +01005158 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005159 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5160 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5161 exp = exp->next;
5162 continue;
5163 }
5164
5165 /* Apply the filter to the request line. */
5166 ret = apply_filter_to_req_line(t, req, exp);
5167 if (unlikely(ret < 0))
5168 return -1;
5169
5170 if (likely(ret == 0)) {
5171 /* The filter did not match the request, it can be
5172 * iterated through all headers.
5173 */
5174 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005175 }
5176 exp = exp->next;
5177 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005178 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005179}
5180
5181
Willy Tarreaua15645d2007-03-18 16:22:39 +01005182
Willy Tarreau58f10d72006-12-04 02:26:12 +01005183/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005184 * Try to retrieve the server associated to the appsession.
5185 * If the server is found, it's assigned to the session.
5186 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005187void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005188 struct http_txn *txn = &t->txn;
5189 appsess *asession = NULL;
5190 char *sessid_temp = NULL;
5191
Cyril Bontéb21570a2009-11-29 20:04:48 +01005192 if (len > t->be->appsession_len) {
5193 len = t->be->appsession_len;
5194 }
5195
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005196 if (t->be->options2 & PR_O2_AS_REQL) {
5197 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005198 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005199 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005200 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005201 }
5202
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005203 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005204 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5205 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5206 return;
5207 }
5208
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005209 memcpy(txn->sessid, buf, len);
5210 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005211 }
5212
5213 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5214 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5215 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5216 return;
5217 }
5218
Cyril Bontéb21570a2009-11-29 20:04:48 +01005219 memcpy(sessid_temp, buf, len);
5220 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005221
5222 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5223 /* free previously allocated memory */
5224 pool_free2(apools.sessid, sessid_temp);
5225
5226 if (asession != NULL) {
5227 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5228 if (!(t->be->options2 & PR_O2_AS_REQL))
5229 asession->request_count++;
5230
5231 if (asession->serverid != NULL) {
5232 struct server *srv = t->be->srv;
5233 while (srv) {
5234 if (strcmp(srv->id, asession->serverid) == 0) {
5235 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5236 /* we found the server and it's usable */
5237 txn->flags &= ~TX_CK_MASK;
5238 txn->flags |= TX_CK_VALID;
5239 t->flags |= SN_DIRECT | SN_ASSIGNED;
5240 t->srv = srv;
5241 break;
5242 } else {
5243 txn->flags &= ~TX_CK_MASK;
5244 txn->flags |= TX_CK_DOWN;
5245 }
5246 }
5247 srv = srv->next;
5248 }
5249 }
5250 }
5251}
5252
5253/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005254 * Manage client-side cookie. It can impact performance by about 2% so it is
5255 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005256 */
5257void manage_client_side_cookies(struct session *t, struct buffer *req)
5258{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005259 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005260 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005261 char *del_colon, *del_cookie, *colon;
5262 int app_cookies;
5263
Willy Tarreau58f10d72006-12-04 02:26:12 +01005264 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005265 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005266
Willy Tarreau2a324282006-12-05 00:05:46 +01005267 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005268 * we start with the start line.
5269 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005270 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005271 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005272
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005273 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005274 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005275 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005276
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005277 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005278 cur_ptr = cur_next;
5279 cur_end = cur_ptr + cur_hdr->len;
5280 cur_next = cur_end + cur_hdr->cr + 1;
5281
5282 /* We have one full header between cur_ptr and cur_end, and the
5283 * next header starts at cur_next. We're only interested in
5284 * "Cookie:" headers.
5285 */
5286
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005287 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5288 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005289 old_idx = cur_idx;
5290 continue;
5291 }
5292
5293 /* Now look for cookies. Conforming to RFC2109, we have to support
5294 * attributes whose name begin with a '$', and associate them with
5295 * the right cookie, if we want to delete this cookie.
5296 * So there are 3 cases for each cookie read :
5297 * 1) it's a special attribute, beginning with a '$' : ignore it.
5298 * 2) it's a server id cookie that we *MAY* want to delete : save
5299 * some pointers on it (last semi-colon, beginning of cookie...)
5300 * 3) it's an application cookie : we *MAY* have to delete a previous
5301 * "special" cookie.
5302 * At the end of loop, if a "special" cookie remains, we may have to
5303 * remove it. If no application cookie persists in the header, we
5304 * *MUST* delete it
5305 */
5306
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005307 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005308
Willy Tarreau58f10d72006-12-04 02:26:12 +01005309 /* del_cookie == NULL => nothing to be deleted */
5310 del_colon = del_cookie = NULL;
5311 app_cookies = 0;
5312
5313 while (p1 < cur_end) {
5314 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005315 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005316 while (p1 < cur_end) {
5317 if (*p1 == ';' || *p1 == ',')
5318 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005319 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005320 break;
5321 p1++;
5322 }
5323
5324 if (p1 == cur_end)
5325 break;
5326
5327 /* p1 is at the beginning of the cookie name */
5328 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005329 while (p2 < cur_end && *p2 != '=') {
5330 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5331 /* oops, the cookie name was truncated, resync */
5332 p1 = p2;
5333 goto resync_name;
5334 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005335 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005336 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005337
5338 if (p2 == cur_end)
5339 break;
5340
5341 p3 = p2 + 1; /* skips the '=' sign */
5342 if (p3 == cur_end)
5343 break;
5344
Willy Tarreau305ae852010-01-03 19:45:54 +01005345 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5346 p5 = p4 = p3;
5347 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5348 if (!isspace((unsigned char)*p5))
5349 p4 = p5 + 1;
5350 p5++;
5351 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005352
5353 /* here, we have the cookie name between p1 and p2,
5354 * and its value between p3 and p4.
5355 * we can process it :
5356 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005357 * Cookie: NAME=VALUE ;
5358 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005359 * | || || +--> p4
5360 * | || |+-------> p3
5361 * | || +--------> p2
5362 * | |+------------> p1
5363 * | +-------------> colon
5364 * +--------------------> cur_ptr
5365 */
5366
5367 if (*p1 == '$') {
5368 /* skip this one */
5369 }
5370 else {
5371 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005372 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005373 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005374 (p4 - p1 >= t->fe->capture_namelen) &&
5375 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005376 int log_len = p4 - p1;
5377
Willy Tarreau086b3b42007-05-13 21:45:51 +02005378 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005379 Alert("HTTP logging : out of memory.\n");
5380 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005381 if (log_len > t->fe->capture_len)
5382 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005383 memcpy(txn->cli_cookie, p1, log_len);
5384 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005385 }
5386 }
5387
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005388 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5389 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005390 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005391 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005392 char *delim;
5393
5394 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5395 * have the server ID betweek p3 and delim, and the original cookie between
5396 * delim+1 and p4. Otherwise, delim==p4 :
5397 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005398 * Cookie: NAME=SRV~VALUE ;
5399 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005400 * | || || | +--> p4
5401 * | || || +--------> delim
5402 * | || |+-----------> p3
5403 * | || +------------> p2
5404 * | |+----------------> p1
5405 * | +-----------------> colon
5406 * +------------------------> cur_ptr
5407 */
5408
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005409 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005410 for (delim = p3; delim < p4; delim++)
5411 if (*delim == COOKIE_DELIM)
5412 break;
5413 }
5414 else
5415 delim = p4;
5416
5417
5418 /* Here, we'll look for the first running server which supports the cookie.
5419 * This allows to share a same cookie between several servers, for example
5420 * to dedicate backup servers to specific servers only.
5421 * However, to prevent clients from sticking to cookie-less backup server
5422 * when they have incidentely learned an empty cookie, we simply ignore
5423 * empty cookies and mark them as invalid.
5424 */
5425 if (delim == p3)
5426 srv = NULL;
5427
5428 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005429 if (srv->cookie && (srv->cklen == delim - p3) &&
5430 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005431 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005432 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005433 txn->flags &= ~TX_CK_MASK;
5434 txn->flags |= TX_CK_VALID;
5435 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005436 t->srv = srv;
5437 break;
5438 } else {
5439 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005440 txn->flags &= ~TX_CK_MASK;
5441 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442 }
5443 }
5444 srv = srv->next;
5445 }
5446
Willy Tarreau3d300592007-03-18 18:34:41 +01005447 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005448 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005449 txn->flags &= ~TX_CK_MASK;
5450 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005451 }
5452
5453 /* depending on the cookie mode, we may have to either :
5454 * - delete the complete cookie if we're in insert+indirect mode, so that
5455 * the server never sees it ;
5456 * - remove the server id from the cookie value, and tag the cookie as an
5457 * application cookie so that it does not get accidentely removed later,
5458 * if we're in cookie prefix mode
5459 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005460 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005461 int delta; /* negative */
5462
5463 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5464 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005465 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005466 cur_end += delta;
5467 cur_next += delta;
5468 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005469 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005470
5471 del_cookie = del_colon = NULL;
5472 app_cookies++; /* protect the header from deletion */
5473 }
5474 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005475 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005476 del_cookie = p1;
5477 del_colon = colon;
5478 }
5479 } else {
5480 /* now we know that we must keep this cookie since it's
5481 * not ours. But if we wanted to delete our cookie
5482 * earlier, we cannot remove the complete header, but we
5483 * can remove the previous block itself.
5484 */
5485 app_cookies++;
5486
5487 if (del_cookie != NULL) {
5488 int delta; /* negative */
5489
5490 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5491 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005492 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005493 cur_end += delta;
5494 cur_next += delta;
5495 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005496 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005497 del_cookie = del_colon = NULL;
5498 }
5499 }
5500
Cyril Bontéb21570a2009-11-29 20:04:48 +01005501 if (t->be->appsession_name != NULL) {
5502 int cmp_len, value_len;
5503 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005504
Cyril Bontéb21570a2009-11-29 20:04:48 +01005505 if (t->be->options2 & PR_O2_AS_PFX) {
5506 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5507 value_begin = p1 + t->be->appsession_name_len;
5508 value_len = p4 - p1 - t->be->appsession_name_len;
5509 } else {
5510 cmp_len = p2 - p1;
5511 value_begin = p3;
5512 value_len = p4 - p3;
5513 }
5514
5515 /* let's see if the cookie is our appcookie */
5516 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5517 /* Cool... it's the right one */
5518 manage_client_side_appsession(t, value_begin, value_len);
5519 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005520#if defined(DEBUG_HASH)
5521 Alert("manage_client_side_cookies\n");
5522 appsession_hash_dump(&(t->be->htbl_proxy));
5523#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005524 }/* end if ((t->proxy->appsession_name != NULL) ... */
5525 }
5526
5527 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005528 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005529 } /* while (p1 < cur_end) */
5530
5531 /* There's no more cookie on this line.
5532 * We may have marked the last one(s) for deletion.
5533 * We must do this now in two ways :
5534 * - if there is no app cookie, we simply delete the header ;
5535 * - if there are app cookies, we must delete the end of the
5536 * string properly, including the colon/semi-colon before
5537 * the cookie name.
5538 */
5539 if (del_cookie != NULL) {
5540 int delta;
5541 if (app_cookies) {
5542 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5543 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005544 cur_hdr->len += delta;
5545 } else {
5546 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005547
5548 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005549 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5550 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005551 cur_hdr->len = 0;
5552 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005553 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005554 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005555 }
5556
5557 /* keep the link from this header to next one */
5558 old_idx = cur_idx;
5559 } /* end of cookie processing on this header */
5560}
5561
5562
Willy Tarreaua15645d2007-03-18 16:22:39 +01005563/* Iterate the same filter through all response headers contained in <rtr>.
5564 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5565 */
5566int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5567{
5568 char term;
5569 char *cur_ptr, *cur_end, *cur_next;
5570 int cur_idx, old_idx, last_hdr;
5571 struct http_txn *txn = &t->txn;
5572 struct hdr_idx_elem *cur_hdr;
5573 int len, delta;
5574
5575 last_hdr = 0;
5576
Willy Tarreau962c3f42010-01-10 00:15:35 +01005577 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005578 old_idx = 0;
5579
5580 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005581 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005582 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005583 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005584 (exp->action == ACT_ALLOW ||
5585 exp->action == ACT_DENY))
5586 return 0;
5587
5588 cur_idx = txn->hdr_idx.v[old_idx].next;
5589 if (!cur_idx)
5590 break;
5591
5592 cur_hdr = &txn->hdr_idx.v[cur_idx];
5593 cur_ptr = cur_next;
5594 cur_end = cur_ptr + cur_hdr->len;
5595 cur_next = cur_end + cur_hdr->cr + 1;
5596
5597 /* Now we have one header between cur_ptr and cur_end,
5598 * and the next header starts at cur_next.
5599 */
5600
5601 /* The annoying part is that pattern matching needs
5602 * that we modify the contents to null-terminate all
5603 * strings before testing them.
5604 */
5605
5606 term = *cur_end;
5607 *cur_end = '\0';
5608
5609 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5610 switch (exp->action) {
5611 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005612 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005613 last_hdr = 1;
5614 break;
5615
5616 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005617 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005618 last_hdr = 1;
5619 break;
5620
5621 case ACT_REPLACE:
5622 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5623 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5624 /* FIXME: if the user adds a newline in the replacement, the
5625 * index will not be recalculated for now, and the new line
5626 * will not be counted as a new header.
5627 */
5628
5629 cur_end += delta;
5630 cur_next += delta;
5631 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005632 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005633 break;
5634
5635 case ACT_REMOVE:
5636 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5637 cur_next += delta;
5638
Willy Tarreaufa355d42009-11-29 18:12:29 +01005639 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005640 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5641 txn->hdr_idx.used--;
5642 cur_hdr->len = 0;
5643 cur_end = NULL; /* null-term has been rewritten */
5644 break;
5645
5646 }
5647 }
5648 if (cur_end)
5649 *cur_end = term; /* restore the string terminator */
5650
5651 /* keep the link from this header to next one in case of later
5652 * removal of next header.
5653 */
5654 old_idx = cur_idx;
5655 }
5656 return 0;
5657}
5658
5659
5660/* Apply the filter to the status line in the response buffer <rtr>.
5661 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5662 * or -1 if a replacement resulted in an invalid status line.
5663 */
5664int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5665{
5666 char term;
5667 char *cur_ptr, *cur_end;
5668 int done;
5669 struct http_txn *txn = &t->txn;
5670 int len, delta;
5671
5672
Willy Tarreau3d300592007-03-18 18:34:41 +01005673 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005674 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005675 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005676 (exp->action == ACT_ALLOW ||
5677 exp->action == ACT_DENY))
5678 return 0;
5679 else if (exp->action == ACT_REMOVE)
5680 return 0;
5681
5682 done = 0;
5683
Willy Tarreau962c3f42010-01-10 00:15:35 +01005684 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005685 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5686
5687 /* Now we have the status line between cur_ptr and cur_end */
5688
5689 /* The annoying part is that pattern matching needs
5690 * that we modify the contents to null-terminate all
5691 * strings before testing them.
5692 */
5693
5694 term = *cur_end;
5695 *cur_end = '\0';
5696
5697 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5698 switch (exp->action) {
5699 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005700 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005701 done = 1;
5702 break;
5703
5704 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005705 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005706 done = 1;
5707 break;
5708
5709 case ACT_REPLACE:
5710 *cur_end = term; /* restore the string terminator */
5711 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5712 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5713 /* FIXME: if the user adds a newline in the replacement, the
5714 * index will not be recalculated for now, and the new line
5715 * will not be counted as a new header.
5716 */
5717
Willy Tarreaufa355d42009-11-29 18:12:29 +01005718 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005719 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005720 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005721 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005722 cur_ptr, cur_end + 1,
5723 NULL, NULL);
5724 if (unlikely(!cur_end))
5725 return -1;
5726
5727 /* we have a full respnse and we know that we have either a CR
5728 * or an LF at <ptr>.
5729 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005730 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005731 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5732 /* there is no point trying this regex on headers */
5733 return 1;
5734 }
5735 }
5736 *cur_end = term; /* restore the string terminator */
5737 return done;
5738}
5739
5740
5741
5742/*
5743 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5744 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5745 * unparsable response.
5746 */
5747int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5748{
Willy Tarreau3d300592007-03-18 18:34:41 +01005749 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005750 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005751 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005752 int ret;
5753
5754 /*
5755 * The interleaving of transformations and verdicts
5756 * makes it difficult to decide to continue or stop
5757 * the evaluation.
5758 */
5759
Willy Tarreau3d300592007-03-18 18:34:41 +01005760 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005761 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5762 exp->action == ACT_PASS)) {
5763 exp = exp->next;
5764 continue;
5765 }
5766
5767 /* Apply the filter to the status line. */
5768 ret = apply_filter_to_sts_line(t, rtr, exp);
5769 if (unlikely(ret < 0))
5770 return -1;
5771
5772 if (likely(ret == 0)) {
5773 /* The filter did not match the response, it can be
5774 * iterated through all headers.
5775 */
5776 apply_filter_to_resp_headers(t, rtr, exp);
5777 }
5778 exp = exp->next;
5779 }
5780 return 0;
5781}
5782
5783
5784
5785/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005786 * Manage server-side cookies. It can impact performance by about 2% so it is
5787 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005788 */
5789void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5790{
5791 struct http_txn *txn = &t->txn;
5792 char *p1, *p2, *p3, *p4;
5793
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 char *cur_ptr, *cur_end, *cur_next;
5795 int cur_idx, old_idx, delta;
5796
Willy Tarreaua15645d2007-03-18 16:22:39 +01005797 /* Iterate through the headers.
5798 * we start with the start line.
5799 */
5800 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005801 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005802
5803 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5804 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005805 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005806
5807 cur_hdr = &txn->hdr_idx.v[cur_idx];
5808 cur_ptr = cur_next;
5809 cur_end = cur_ptr + cur_hdr->len;
5810 cur_next = cur_end + cur_hdr->cr + 1;
5811
5812 /* We have one full header between cur_ptr and cur_end, and the
5813 * next header starts at cur_next. We're only interested in
5814 * "Cookie:" headers.
5815 */
5816
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005817 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5818 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005819 old_idx = cur_idx;
5820 continue;
5821 }
5822
5823 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005824 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005825
5826
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005827 /* maybe we only wanted to see if there was a set-cookie. Note that
5828 * the cookie capture is declared in the fronend.
5829 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005830 if (t->be->cookie_name == NULL &&
5831 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005832 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005833 return;
5834
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005835 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005836
5837 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005838 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5839 break;
5840
5841 /* p1 is at the beginning of the cookie name */
5842 p2 = p1;
5843
5844 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5845 p2++;
5846
5847 if (p2 == cur_end || *p2 == ';') /* next cookie */
5848 break;
5849
5850 p3 = p2 + 1; /* skip the '=' sign */
5851 if (p3 == cur_end)
5852 break;
5853
5854 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005855 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005856 p4++;
5857
5858 /* here, we have the cookie name between p1 and p2,
5859 * and its value between p3 and p4.
5860 * we can process it.
5861 */
5862
5863 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005864 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005865 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005866 (p4 - p1 >= t->fe->capture_namelen) &&
5867 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868 int log_len = p4 - p1;
5869
Willy Tarreau086b3b42007-05-13 21:45:51 +02005870 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005871 Alert("HTTP logging : out of memory.\n");
5872 }
5873
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005874 if (log_len > t->fe->capture_len)
5875 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005876 memcpy(txn->srv_cookie, p1, log_len);
5877 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005878 }
5879
5880 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005881 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5882 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005883 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005884 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005885
5886 /* If the cookie is in insert mode on a known server, we'll delete
5887 * this occurrence because we'll insert another one later.
5888 * We'll delete it too if the "indirect" option is set and we're in
5889 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005890 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5891 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005892 /* this header must be deleted */
5893 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5894 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5895 txn->hdr_idx.used--;
5896 cur_hdr->len = 0;
5897 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005898 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005899
Willy Tarreau3d300592007-03-18 18:34:41 +01005900 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005901 }
5902 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005903 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005904 /* replace bytes p3->p4 with the cookie name associated
5905 * with this server since we know it.
5906 */
5907 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5908 cur_hdr->len += delta;
5909 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005910 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005911
Willy Tarreau3d300592007-03-18 18:34:41 +01005912 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005913 }
5914 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005915 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005916 /* insert the cookie name associated with this server
5917 * before existing cookie, and insert a delimitor between them..
5918 */
5919 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5920 cur_hdr->len += delta;
5921 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005922 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005923
5924 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005925 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005926 }
5927 }
5928 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005929 else if (t->be->appsession_name != NULL) {
5930 int cmp_len, value_len;
5931 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005932
Cyril Bontéb21570a2009-11-29 20:04:48 +01005933 if (t->be->options2 & PR_O2_AS_PFX) {
5934 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5935 value_begin = p1 + t->be->appsession_name_len;
5936 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5937 } else {
5938 cmp_len = p2 - p1;
5939 value_begin = p3;
5940 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005941 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005942
5943 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5944 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005945 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005946 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005947 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01005948 }
5949 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005950 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005951 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5952 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5953 return;
5954 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005955 memcpy(txn->sessid, value_begin, value_len);
5956 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005957 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005958 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5960 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005961 /* keep the link from this header to next one */
5962 old_idx = cur_idx;
5963 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005964
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005965 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005966 appsess *asession = NULL;
5967 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005968 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005969 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01005970 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005971 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5972 Alert("Not enough Memory process_srv():asession:calloc().\n");
5973 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5974 return;
5975 }
5976 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5977 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5978 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005979 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005980 return;
5981 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005982 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005983 asession->sessid[t->be->appsession_len] = 0;
5984
Willy Tarreau1fac7532010-01-09 19:23:06 +01005985 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005986 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5987 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5988 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005989 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005990 return;
5991 }
5992 asession->serverid[0] = '\0';
5993 memcpy(asession->serverid, t->srv->id, server_id_len);
5994
5995 asession->request_count = 0;
5996 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5997 }
5998
5999 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6000 asession->request_count++;
6001 }
6002
6003#if defined(DEBUG_HASH)
6004 Alert("manage_server_side_cookies\n");
6005 appsession_hash_dump(&(t->be->htbl_proxy));
6006#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006007}
6008
6009
6010
6011/*
6012 * Check if response is cacheable or not. Updates t->flags.
6013 */
6014void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6015{
6016 struct http_txn *txn = &t->txn;
6017 char *p1, *p2;
6018
6019 char *cur_ptr, *cur_end, *cur_next;
6020 int cur_idx;
6021
Willy Tarreau5df51872007-11-25 16:20:08 +01006022 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006023 return;
6024
6025 /* Iterate through the headers.
6026 * we start with the start line.
6027 */
6028 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006029 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006030
6031 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6032 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006033 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006034
6035 cur_hdr = &txn->hdr_idx.v[cur_idx];
6036 cur_ptr = cur_next;
6037 cur_end = cur_ptr + cur_hdr->len;
6038 cur_next = cur_end + cur_hdr->cr + 1;
6039
6040 /* We have one full header between cur_ptr and cur_end, and the
6041 * next header starts at cur_next. We're only interested in
6042 * "Cookie:" headers.
6043 */
6044
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006045 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6046 if (val) {
6047 if ((cur_end - (cur_ptr + val) >= 8) &&
6048 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6049 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6050 return;
6051 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006052 }
6053
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006054 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6055 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056 continue;
6057
6058 /* OK, right now we know we have a cache-control header at cur_ptr */
6059
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006060 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006061
6062 if (p1 >= cur_end) /* no more info */
6063 continue;
6064
6065 /* p1 is at the beginning of the value */
6066 p2 = p1;
6067
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006068 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006069 p2++;
6070
6071 /* we have a complete value between p1 and p2 */
6072 if (p2 < cur_end && *p2 == '=') {
6073 /* we have something of the form no-cache="set-cookie" */
6074 if ((cur_end - p1 >= 21) &&
6075 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6076 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006077 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006078 continue;
6079 }
6080
6081 /* OK, so we know that either p2 points to the end of string or to a comma */
6082 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6083 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6084 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6085 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006086 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006087 return;
6088 }
6089
6090 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006091 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006092 continue;
6093 }
6094 }
6095}
6096
6097
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098/*
6099 * Try to retrieve a known appsession in the URI, then the associated server.
6100 * If the server is found, it's assigned to the session.
6101 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006102void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006104 char *end_params, *first_param, *cur_param, *next_param;
6105 char separator;
6106 int value_len;
6107
6108 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006109
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006110 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006111 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006112 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006114
Cyril Bontéb21570a2009-11-29 20:04:48 +01006115 first_param = NULL;
6116 switch (mode) {
6117 case PR_O2_AS_M_PP:
6118 first_param = memchr(begin, ';', len);
6119 break;
6120 case PR_O2_AS_M_QS:
6121 first_param = memchr(begin, '?', len);
6122 break;
6123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006124
Cyril Bontéb21570a2009-11-29 20:04:48 +01006125 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006127 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006128
Cyril Bontéb21570a2009-11-29 20:04:48 +01006129 switch (mode) {
6130 case PR_O2_AS_M_PP:
6131 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6132 end_params = (char *) begin + len;
6133 }
6134 separator = ';';
6135 break;
6136 case PR_O2_AS_M_QS:
6137 end_params = (char *) begin + len;
6138 separator = '&';
6139 break;
6140 default:
6141 /* unknown mode, shouldn't happen */
6142 return;
6143 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144
Cyril Bontéb21570a2009-11-29 20:04:48 +01006145 cur_param = next_param = end_params;
6146 while (cur_param > first_param) {
6147 cur_param--;
6148 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6149 /* let's see if this is the appsession parameter */
6150 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6151 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6152 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6153 /* Cool... it's the right one */
6154 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6155 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6156 if (value_len > 0) {
6157 manage_client_side_appsession(t, cur_param, value_len);
6158 }
6159 break;
6160 }
6161 next_param = cur_param;
6162 }
6163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006164#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006165 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006166 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006167#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006168}
6169
6170
Willy Tarreaub2513902006-12-17 14:52:38 +01006171/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006172 * In a GET or HEAD request, check if the requested URI matches the stats uri
6173 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006174 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006175 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006176 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006177 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006178 *
6179 * Returns 1 if the session's state changes, otherwise 0.
6180 */
6181int stats_check_uri_auth(struct session *t, struct proxy *backend)
6182{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006183 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006184 struct uri_auth *uri_auth = backend->uri_auth;
6185 struct user_auth *user;
6186 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006187 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006188
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006189 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006191 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006192 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006193 return 0;
6194
Willy Tarreau962c3f42010-01-10 00:15:35 +01006195 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006196
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006197 /* the URI is in h */
6198 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006199 return 0;
6200
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006201 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006202 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006203 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006204 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006205 break;
6206 }
6207 h++;
6208 }
6209
6210 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006211 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6212 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006213 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006214 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006215 break;
6216 }
6217 h++;
6218 }
6219 }
6220
Willy Tarreau962c3f42010-01-10 00:15:35 +01006221 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6222 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006223 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006224 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006225 break;
6226 }
6227 h++;
6228 }
6229
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006230 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6231
Willy Tarreaub2513902006-12-17 14:52:38 +01006232 /* we are in front of a interceptable URI. Let's check
6233 * if there's an authentication and if it's valid.
6234 */
6235 user = uri_auth->users;
6236 if (!user) {
6237 /* no user auth required, it's OK */
6238 authenticated = 1;
6239 } else {
6240 authenticated = 0;
6241
6242 /* a user list is defined, we have to check.
6243 * skip 21 chars for "Authorization: Basic ".
6244 */
6245
6246 /* FIXME: this should move to an earlier place */
6247 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006248 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006249 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6250 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006251 if (len > 14 &&
6252 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006253 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006254 break;
6255 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006256 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006257 }
6258
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006259 if (txn->auth_hdr.len < 21 ||
6260 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006261 user = NULL;
6262
6263 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006264 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6265 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006266 user->user_pwd, user->user_len)) {
6267 authenticated = 1;
6268 break;
6269 }
6270 user = user->next;
6271 }
6272 }
6273
6274 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006275 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006276
6277 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006278 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6279 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006280 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006281 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006282 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006283 if (!(t->flags & SN_ERR_MASK))
6284 t->flags |= SN_ERR_PRXCOND;
6285 if (!(t->flags & SN_FINST_MASK))
6286 t->flags |= SN_FINST_R;
6287 return 1;
6288 }
6289
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006290 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006291 * data.
6292 */
Willy Tarreau70089872008-06-13 21:12:51 +02006293 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006294 t->data_source = DATA_SRC_STATS;
6295 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006296 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006297 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6298 t->rep->prod->private = t;
6299 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006300 return 1;
6301}
6302
Willy Tarreau4076a152009-04-02 15:18:36 +02006303/*
6304 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006305 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6306 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006307 */
6308void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6309 struct buffer *buf, struct http_msg *msg,
6310 struct proxy *other_end)
6311{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006312 es->len = buf->r - (buf->data + msg->som);
6313 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006314 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006315 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006316 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006317 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006318 es->when = date; // user-visible date
6319 es->sid = s->uniq_id;
6320 es->srv = s->srv;
6321 es->oe = other_end;
6322 es->src = s->cli_addr;
6323}
Willy Tarreaub2513902006-12-17 14:52:38 +01006324
Willy Tarreaubaaee002006-06-26 02:48:02 +02006325/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326 * Print a debug line with a header
6327 */
6328void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6329{
6330 int len, max;
6331 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006332 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333 max = end - start;
6334 UBOUND(max, sizeof(trash) - len - 1);
6335 len += strlcpy2(trash + len, start, max + 1);
6336 trash[len++] = '\n';
6337 write(1, trash, len);
6338}
6339
Willy Tarreau0937bc42009-12-22 15:03:09 +01006340/*
6341 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6342 * the required fields are properly allocated and that we only need to (re)init
6343 * them. This should be used before processing any new request.
6344 */
6345void http_init_txn(struct session *s)
6346{
6347 struct http_txn *txn = &s->txn;
6348 struct proxy *fe = s->fe;
6349
6350 txn->flags = 0;
6351 txn->status = -1;
6352
6353 txn->req.sol = txn->req.eol = NULL;
6354 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6355 txn->rsp.sol = txn->rsp.eol = NULL;
6356 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6357 txn->req.hdr_content_len = 0LL;
6358 txn->rsp.hdr_content_len = 0LL;
6359 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6360 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6361 chunk_reset(&txn->auth_hdr);
6362
6363 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6364 if (fe->options2 & PR_O2_REQBUG_OK)
6365 txn->req.err_pos = -1; /* let buggy requests pass */
6366
Willy Tarreau46023632010-01-07 22:51:47 +01006367 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006368 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6369
Willy Tarreau46023632010-01-07 22:51:47 +01006370 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006371 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6372
6373 if (txn->hdr_idx.v)
6374 hdr_idx_init(&txn->hdr_idx);
6375}
6376
6377/* to be used at the end of a transaction */
6378void http_end_txn(struct session *s)
6379{
6380 struct http_txn *txn = &s->txn;
6381
6382 /* these ones will have been dynamically allocated */
6383 pool_free2(pool2_requri, txn->uri);
6384 pool_free2(pool2_capture, txn->cli_cookie);
6385 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006386 pool_free2(apools.sessid, txn->sessid);
6387 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006388 txn->uri = NULL;
6389 txn->srv_cookie = NULL;
6390 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006391
6392 if (txn->req.cap) {
6393 struct cap_hdr *h;
6394 for (h = s->fe->req_cap; h; h = h->next)
6395 pool_free2(h->pool, txn->req.cap[h->index]);
6396 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6397 }
6398
6399 if (txn->rsp.cap) {
6400 struct cap_hdr *h;
6401 for (h = s->fe->rsp_cap; h; h = h->next)
6402 pool_free2(h->pool, txn->rsp.cap[h->index]);
6403 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6404 }
6405
Willy Tarreau0937bc42009-12-22 15:03:09 +01006406}
6407
6408/* to be used at the end of a transaction to prepare a new one */
6409void http_reset_txn(struct session *s)
6410{
6411 http_end_txn(s);
6412 http_init_txn(s);
6413
6414 s->be = s->fe;
6415 s->req->analysers = s->listener->analysers;
6416 s->logs.logwait = s->fe->to_log;
6417 s->srv = s->prev_srv = s->srv_conn = NULL;
6418 s->pend_pos = NULL;
6419 s->conn_retries = s->be->conn_retries;
6420
6421 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6422
6423 s->req->rto = s->fe->timeout.client;
6424 s->req->wto = s->be->timeout.server;
6425 s->req->cto = s->be->timeout.connect;
6426
6427 s->rep->rto = s->be->timeout.server;
6428 s->rep->wto = s->fe->timeout.client;
6429 s->rep->cto = TICK_ETERNITY;
6430
6431 s->req->rex = TICK_ETERNITY;
6432 s->req->wex = TICK_ETERNITY;
6433 s->req->analyse_exp = TICK_ETERNITY;
6434 s->rep->rex = TICK_ETERNITY;
6435 s->rep->wex = TICK_ETERNITY;
6436 s->rep->analyse_exp = TICK_ETERNITY;
6437}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006438
Willy Tarreau8797c062007-05-07 00:55:35 +02006439/************************************************************************/
6440/* The code below is dedicated to ACL parsing and matching */
6441/************************************************************************/
6442
6443
6444
6445
6446/* 1. Check on METHOD
6447 * We use the pre-parsed method if it is known, and store its number as an
6448 * integer. If it is unknown, we use the pointer and the length.
6449 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006450static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006451{
6452 int len, meth;
6453
Willy Tarreauae8b7962007-06-09 23:10:04 +02006454 len = strlen(*text);
6455 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006456
6457 pattern->val.i = meth;
6458 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006459 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006460 if (!pattern->ptr.str)
6461 return 0;
6462 pattern->len = len;
6463 }
6464 return 1;
6465}
6466
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006467static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006468acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6469 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006470{
6471 int meth;
6472 struct http_txn *txn = l7;
6473
Willy Tarreaub6866442008-07-14 23:54:42 +02006474 if (!txn)
6475 return 0;
6476
Willy Tarreau655dce92009-11-08 13:10:58 +01006477 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006478 return 0;
6479
Willy Tarreau8797c062007-05-07 00:55:35 +02006480 meth = txn->meth;
6481 test->i = meth;
6482 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006483 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6484 /* ensure the indexes are not affected */
6485 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006486 test->len = txn->req.sl.rq.m_l;
6487 test->ptr = txn->req.sol;
6488 }
6489 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6490 return 1;
6491}
6492
6493static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6494{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006495 int icase;
6496
Willy Tarreau8797c062007-05-07 00:55:35 +02006497 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006498 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006499
6500 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006501 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006502
6503 /* Other method, we must compare the strings */
6504 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006505 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006506
6507 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6508 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6509 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006510 return ACL_PAT_FAIL;
6511 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006512}
6513
6514/* 2. Check on Request/Status Version
6515 * We simply compare strings here.
6516 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006517static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006518{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006519 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006520 if (!pattern->ptr.str)
6521 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006522 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006523 return 1;
6524}
6525
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006526static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006527acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6528 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006529{
6530 struct http_txn *txn = l7;
6531 char *ptr;
6532 int len;
6533
Willy Tarreaub6866442008-07-14 23:54:42 +02006534 if (!txn)
6535 return 0;
6536
Willy Tarreau655dce92009-11-08 13:10:58 +01006537 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006538 return 0;
6539
Willy Tarreau8797c062007-05-07 00:55:35 +02006540 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006541 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006542
6543 while ((len-- > 0) && (*ptr++ != '/'));
6544 if (len <= 0)
6545 return 0;
6546
6547 test->ptr = ptr;
6548 test->len = len;
6549
6550 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6551 return 1;
6552}
6553
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006554static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006555acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6556 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006557{
6558 struct http_txn *txn = l7;
6559 char *ptr;
6560 int len;
6561
Willy Tarreaub6866442008-07-14 23:54:42 +02006562 if (!txn)
6563 return 0;
6564
Willy Tarreau655dce92009-11-08 13:10:58 +01006565 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006566 return 0;
6567
Willy Tarreau8797c062007-05-07 00:55:35 +02006568 len = txn->rsp.sl.st.v_l;
6569 ptr = txn->rsp.sol;
6570
6571 while ((len-- > 0) && (*ptr++ != '/'));
6572 if (len <= 0)
6573 return 0;
6574
6575 test->ptr = ptr;
6576 test->len = len;
6577
6578 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6579 return 1;
6580}
6581
6582/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006583static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006584acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6585 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006586{
6587 struct http_txn *txn = l7;
6588 char *ptr;
6589 int len;
6590
Willy Tarreaub6866442008-07-14 23:54:42 +02006591 if (!txn)
6592 return 0;
6593
Willy Tarreau655dce92009-11-08 13:10:58 +01006594 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006595 return 0;
6596
Willy Tarreau8797c062007-05-07 00:55:35 +02006597 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006598 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006599
6600 test->i = __strl2ui(ptr, len);
6601 test->flags = ACL_TEST_F_VOL_1ST;
6602 return 1;
6603}
6604
6605/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006606static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006607acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6608 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006609{
6610 struct http_txn *txn = l7;
6611
Willy Tarreaub6866442008-07-14 23:54:42 +02006612 if (!txn)
6613 return 0;
6614
Willy Tarreau655dce92009-11-08 13:10:58 +01006615 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006616 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006617
Willy Tarreauc11416f2007-06-17 16:58:38 +02006618 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6619 /* ensure the indexes are not affected */
6620 return 0;
6621
Willy Tarreau8797c062007-05-07 00:55:35 +02006622 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006623 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006624
Willy Tarreauf3d25982007-05-08 22:45:09 +02006625 /* we do not need to set READ_ONLY because the data is in a buffer */
6626 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006627 return 1;
6628}
6629
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006630static int
6631acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6632 struct acl_expr *expr, struct acl_test *test)
6633{
6634 struct http_txn *txn = l7;
6635
Willy Tarreaub6866442008-07-14 23:54:42 +02006636 if (!txn)
6637 return 0;
6638
Willy Tarreau655dce92009-11-08 13:10:58 +01006639 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006640 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006641
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006642 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6643 /* ensure the indexes are not affected */
6644 return 0;
6645
6646 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006647 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 +01006648 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6649 test->i = AF_INET;
6650
6651 /*
6652 * If we are parsing url in frontend space, we prepare backend stage
6653 * to not parse again the same url ! optimization lazyness...
6654 */
6655 if (px->options & PR_O_HTTP_PROXY)
6656 l4->flags |= SN_ADDR_SET;
6657
6658 test->flags = ACL_TEST_F_READ_ONLY;
6659 return 1;
6660}
6661
6662static int
6663acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6664 struct acl_expr *expr, struct acl_test *test)
6665{
6666 struct http_txn *txn = l7;
6667
Willy Tarreaub6866442008-07-14 23:54:42 +02006668 if (!txn)
6669 return 0;
6670
Willy Tarreau655dce92009-11-08 13:10:58 +01006671 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006672 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006673
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006674 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6675 /* ensure the indexes are not affected */
6676 return 0;
6677
6678 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006679 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 +01006680 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6681
6682 if (px->options & PR_O_HTTP_PROXY)
6683 l4->flags |= SN_ADDR_SET;
6684
6685 test->flags = ACL_TEST_F_READ_ONLY;
6686 return 1;
6687}
6688
Willy Tarreauc11416f2007-06-17 16:58:38 +02006689/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6690 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6691 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006692static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006693acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006694 struct acl_expr *expr, struct acl_test *test)
6695{
6696 struct http_txn *txn = l7;
6697 struct hdr_idx *idx = &txn->hdr_idx;
6698 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006699
Willy Tarreaub6866442008-07-14 23:54:42 +02006700 if (!txn)
6701 return 0;
6702
Willy Tarreau33a7e692007-06-10 19:45:56 +02006703 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6704 /* search for header from the beginning */
6705 ctx->idx = 0;
6706
Willy Tarreau33a7e692007-06-10 19:45:56 +02006707 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6708 test->flags |= ACL_TEST_F_FETCH_MORE;
6709 test->flags |= ACL_TEST_F_VOL_HDR;
6710 test->len = ctx->vlen;
6711 test->ptr = (char *)ctx->line + ctx->val;
6712 return 1;
6713 }
6714
6715 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6716 test->flags |= ACL_TEST_F_VOL_HDR;
6717 return 0;
6718}
6719
Willy Tarreau33a7e692007-06-10 19:45:56 +02006720static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006721acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6722 struct acl_expr *expr, struct acl_test *test)
6723{
6724 struct http_txn *txn = l7;
6725
Willy Tarreaub6866442008-07-14 23:54:42 +02006726 if (!txn)
6727 return 0;
6728
Willy Tarreau655dce92009-11-08 13:10:58 +01006729 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006730 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006731
Willy Tarreauc11416f2007-06-17 16:58:38 +02006732 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6733 /* ensure the indexes are not affected */
6734 return 0;
6735
6736 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6737}
6738
6739static int
6740acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6741 struct acl_expr *expr, struct acl_test *test)
6742{
6743 struct http_txn *txn = l7;
6744
Willy Tarreaub6866442008-07-14 23:54:42 +02006745 if (!txn)
6746 return 0;
6747
Willy Tarreau655dce92009-11-08 13:10:58 +01006748 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006749 return 0;
6750
6751 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6752}
6753
6754/* 6. Check on HTTP header count. The number of occurrences is returned.
6755 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6756 */
6757static int
6758acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006759 struct acl_expr *expr, struct acl_test *test)
6760{
6761 struct http_txn *txn = l7;
6762 struct hdr_idx *idx = &txn->hdr_idx;
6763 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006764 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006765
Willy Tarreaub6866442008-07-14 23:54:42 +02006766 if (!txn)
6767 return 0;
6768
Willy Tarreau33a7e692007-06-10 19:45:56 +02006769 ctx.idx = 0;
6770 cnt = 0;
6771 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6772 cnt++;
6773
6774 test->i = cnt;
6775 test->flags = ACL_TEST_F_VOL_HDR;
6776 return 1;
6777}
6778
Willy Tarreauc11416f2007-06-17 16:58:38 +02006779static int
6780acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6781 struct acl_expr *expr, struct acl_test *test)
6782{
6783 struct http_txn *txn = l7;
6784
Willy Tarreaub6866442008-07-14 23:54:42 +02006785 if (!txn)
6786 return 0;
6787
Willy Tarreau655dce92009-11-08 13:10:58 +01006788 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006789 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006790
Willy Tarreauc11416f2007-06-17 16:58:38 +02006791 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6792 /* ensure the indexes are not affected */
6793 return 0;
6794
6795 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6796}
6797
6798static int
6799acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6800 struct acl_expr *expr, struct acl_test *test)
6801{
6802 struct http_txn *txn = l7;
6803
Willy Tarreaub6866442008-07-14 23:54:42 +02006804 if (!txn)
6805 return 0;
6806
Willy Tarreau655dce92009-11-08 13:10:58 +01006807 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006808 return 0;
6809
6810 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6811}
6812
Willy Tarreau33a7e692007-06-10 19:45:56 +02006813/* 7. Check on HTTP header's integer value. The integer value is returned.
6814 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006815 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006816 */
6817static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006818acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006819 struct acl_expr *expr, struct acl_test *test)
6820{
6821 struct http_txn *txn = l7;
6822 struct hdr_idx *idx = &txn->hdr_idx;
6823 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006824
Willy Tarreaub6866442008-07-14 23:54:42 +02006825 if (!txn)
6826 return 0;
6827
Willy Tarreau33a7e692007-06-10 19:45:56 +02006828 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6829 /* search for header from the beginning */
6830 ctx->idx = 0;
6831
Willy Tarreau33a7e692007-06-10 19:45:56 +02006832 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6833 test->flags |= ACL_TEST_F_FETCH_MORE;
6834 test->flags |= ACL_TEST_F_VOL_HDR;
6835 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6836 return 1;
6837 }
6838
6839 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6840 test->flags |= ACL_TEST_F_VOL_HDR;
6841 return 0;
6842}
6843
Willy Tarreauc11416f2007-06-17 16:58:38 +02006844static int
6845acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6846 struct acl_expr *expr, struct acl_test *test)
6847{
6848 struct http_txn *txn = l7;
6849
Willy Tarreaub6866442008-07-14 23:54:42 +02006850 if (!txn)
6851 return 0;
6852
Willy Tarreau655dce92009-11-08 13:10:58 +01006853 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006854 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006855
Willy Tarreauc11416f2007-06-17 16:58:38 +02006856 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6857 /* ensure the indexes are not affected */
6858 return 0;
6859
6860 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6861}
6862
6863static int
6864acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6865 struct acl_expr *expr, struct acl_test *test)
6866{
6867 struct http_txn *txn = l7;
6868
Willy Tarreaub6866442008-07-14 23:54:42 +02006869 if (!txn)
6870 return 0;
6871
Willy Tarreau655dce92009-11-08 13:10:58 +01006872 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006873 return 0;
6874
6875 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6876}
6877
Willy Tarreau106f9792009-09-19 07:54:16 +02006878/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6879 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6880 */
6881static int
6882acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6883 struct acl_expr *expr, struct acl_test *test)
6884{
6885 struct http_txn *txn = l7;
6886 struct hdr_idx *idx = &txn->hdr_idx;
6887 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6888
6889 if (!txn)
6890 return 0;
6891
6892 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6893 /* search for header from the beginning */
6894 ctx->idx = 0;
6895
6896 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6897 test->flags |= ACL_TEST_F_FETCH_MORE;
6898 test->flags |= ACL_TEST_F_VOL_HDR;
6899 /* Same optimization as url_ip */
6900 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6901 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6902 test->ptr = (void *)&l4->srv_addr.sin_addr;
6903 test->i = AF_INET;
6904 return 1;
6905 }
6906
6907 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6908 test->flags |= ACL_TEST_F_VOL_HDR;
6909 return 0;
6910}
6911
6912static int
6913acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6914 struct acl_expr *expr, struct acl_test *test)
6915{
6916 struct http_txn *txn = l7;
6917
6918 if (!txn)
6919 return 0;
6920
Willy Tarreau655dce92009-11-08 13:10:58 +01006921 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006922 return 0;
6923
6924 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6925 /* ensure the indexes are not affected */
6926 return 0;
6927
6928 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6929}
6930
6931static int
6932acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6933 struct acl_expr *expr, struct acl_test *test)
6934{
6935 struct http_txn *txn = l7;
6936
6937 if (!txn)
6938 return 0;
6939
Willy Tarreau655dce92009-11-08 13:10:58 +01006940 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006941 return 0;
6942
6943 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6944}
6945
Willy Tarreau737b0c12007-06-10 21:28:46 +02006946/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6947 * the first '/' after the possible hostname, and ends before the possible '?'.
6948 */
6949static int
6950acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6951 struct acl_expr *expr, struct acl_test *test)
6952{
6953 struct http_txn *txn = l7;
6954 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006955
Willy Tarreaub6866442008-07-14 23:54:42 +02006956 if (!txn)
6957 return 0;
6958
Willy Tarreau655dce92009-11-08 13:10:58 +01006959 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006960 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006961
Willy Tarreauc11416f2007-06-17 16:58:38 +02006962 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6963 /* ensure the indexes are not affected */
6964 return 0;
6965
Willy Tarreau962c3f42010-01-10 00:15:35 +01006966 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006967 ptr = http_get_path(txn);
6968 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006969 return 0;
6970
6971 /* OK, we got the '/' ! */
6972 test->ptr = ptr;
6973
6974 while (ptr < end && *ptr != '?')
6975 ptr++;
6976
6977 test->len = ptr - test->ptr;
6978
6979 /* we do not need to set READ_ONLY because the data is in a buffer */
6980 test->flags = ACL_TEST_F_VOL_1ST;
6981 return 1;
6982}
6983
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006984static int
6985acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6986 struct acl_expr *expr, struct acl_test *test)
6987{
6988 struct buffer *req = s->req;
6989 struct http_txn *txn = &s->txn;
6990 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006991
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006992 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6993 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6994 */
6995
6996 if (!s || !req)
6997 return 0;
6998
Willy Tarreau655dce92009-11-08 13:10:58 +01006999 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007000 /* Already decoded as OK */
7001 test->flags |= ACL_TEST_F_SET_RES_PASS;
7002 return 1;
7003 }
7004
7005 /* Try to decode HTTP request */
7006 if (likely(req->lr < req->r))
7007 http_msg_analyzer(req, msg, &txn->hdr_idx);
7008
Willy Tarreau655dce92009-11-08 13:10:58 +01007009 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007010 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7011 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7012 return 1;
7013 }
7014 /* wait for final state */
7015 test->flags |= ACL_TEST_F_MAY_CHANGE;
7016 return 0;
7017 }
7018
7019 /* OK we got a valid HTTP request. We have some minor preparation to
7020 * perform so that further checks can rely on HTTP tests.
7021 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007022 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007023 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7024 s->flags |= SN_REDIRECTABLE;
7025
7026 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7027 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7028 return 1;
7029 }
7030
7031 test->flags |= ACL_TEST_F_SET_RES_PASS;
7032 return 1;
7033}
7034
Willy Tarreau8797c062007-05-07 00:55:35 +02007035
7036/************************************************************************/
7037/* All supported keywords must be declared here. */
7038/************************************************************************/
7039
7040/* Note: must not be declared <const> as its list will be overwritten */
7041static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007042 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7043
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007044 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7045 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7046 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7047 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007048
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007049 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7050 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7051 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7052 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7053 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7054 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7055 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7056 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7057 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007058
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007059 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7060 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7061 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7062 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7063 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7064 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7065 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7066 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7067 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7068 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007069 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007070
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007071 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7072 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7073 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7074 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7075 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7076 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7077 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7078 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7079 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007080 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007081
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007082 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7083 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7084 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7085 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7086 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7087 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7088 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007089
Willy Tarreauf3d25982007-05-08 22:45:09 +02007090 { NULL, NULL, NULL, NULL },
7091
7092#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007093 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7094 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7095 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7096 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7097 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7098 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7099 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7100
Willy Tarreau8797c062007-05-07 00:55:35 +02007101 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7102 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7103 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7104 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7105 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7106 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7107 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7108 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7109
7110 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7111 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7112 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7113 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7114 { NULL, NULL, NULL, NULL },
7115#endif
7116}};
7117
7118
7119__attribute__((constructor))
7120static void __http_protocol_init(void)
7121{
7122 acl_register_keywords(&acl_kws);
7123}
7124
7125
Willy Tarreau58f10d72006-12-04 02:26:12 +01007126/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007127 * Local variables:
7128 * c-indent-level: 8
7129 * c-basic-offset: 8
7130 * End:
7131 */