blob: f2f312e3b756bfa82d21e82d62740536cced51e7 [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 Tarreaub608feb2010-01-02 22:47:18 +01002237 if (txn->flags & TX_NOT_FIRST)
2238 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 Tarreaub608feb2010-01-02 22:47:18 +01002259 if (txn->flags & TX_NOT_FIRST)
2260 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 Tarreaub608feb2010-01-02 22:47:18 +01002283 if (txn->flags & TX_NOT_FIRST)
2284 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 Tarreau59234e92008-11-30 23:51:27 +01002307 /* just set the request timeout once at the beginning of the request */
2308 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002309 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 /* we're not ready yet */
2312 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002313
2314 failed_keep_alive:
2315 /* Here we process low-level errors for keep-alive requests. In
2316 * short, if the request is not the first one and it experiences
2317 * a timeout, read error or shutdown, we just silently close so
2318 * that the client can try again.
2319 */
2320 txn->status = 0;
2321 msg->msg_state = HTTP_MSG_RQBEFORE;
2322 req->analysers = 0;
2323 s->logs.logwait = 0;
Willy Tarreau148d0992010-01-10 10:21:21 +01002324 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002325 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002327
Willy Tarreaud787e662009-07-07 10:14:51 +02002328 /* OK now we have a complete HTTP request with indexed headers. Let's
2329 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002330 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2331 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2332 * points to the CRLF of the request line. req->lr points to the first
2333 * byte after the last LF. msg->col and msg->sov point to the first
2334 * byte of data. msg->eol cannot be trusted because it may have been
2335 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002336 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002337
Willy Tarreaud787e662009-07-07 10:14:51 +02002338 /* Maybe we found in invalid header name while we were configured not
2339 * to block on that, so we have to capture it now.
2340 */
2341 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002342 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /*
2345 * 1: identify the method
2346 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002347 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002348
2349 /* we can make use of server redirect on GET and HEAD */
2350 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2351 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /*
2354 * 2: check if the URI matches the monitor_uri.
2355 * We have to do this for every request which gets in, because
2356 * the monitor-uri is defined by the frontend.
2357 */
2358 if (unlikely((s->fe->monitor_uri_len != 0) &&
2359 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002360 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 s->fe->monitor_uri,
2362 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002363 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002365 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002367
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002369
Willy Tarreau59234e92008-11-30 23:51:27 +01002370 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002371 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2372 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002373
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 ret = acl_pass(ret);
2375 if (cond->pol == ACL_COND_UNLESS)
2376 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002377
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 if (ret) {
2379 /* we fail this request, let's return 503 service unavail */
2380 txn->status = 503;
2381 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2382 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002383 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002385
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 /* nothing to fail, let's reply normaly */
2387 txn->status = 200;
2388 stream_int_retnclose(req->prod, &http_200_chunk);
2389 goto return_prx_cond;
2390 }
2391
2392 /*
2393 * 3: Maybe we have to copy the original REQURI for the logs ?
2394 * Note: we cannot log anymore if the request has been
2395 * classified as invalid.
2396 */
2397 if (unlikely(s->logs.logwait & LW_REQ)) {
2398 /* we have a complete HTTP request that we must log */
2399 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2400 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002401
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 if (urilen >= REQURI_LEN)
2403 urilen = REQURI_LEN - 1;
2404 memcpy(txn->uri, &req->data[msg->som], urilen);
2405 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 if (!(s->logs.logwait &= ~LW_REQ))
2408 s->do_log(s);
2409 } else {
2410 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002411 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002412 }
Willy Tarreau06619262006-12-17 08:37:22 +01002413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002415 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2416 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002417
Willy Tarreau5b154472009-12-21 20:11:07 +01002418 /* ... and check if the request is HTTP/1.1 or above */
2419 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002420 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2421 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2422 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002423 txn->flags |= TX_REQ_VER_11;
2424
2425 /* "connection" has not been parsed yet */
2426 txn->flags &= ~TX_CON_HDR_PARS;
2427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 /* transfer length unknown*/
2429 txn->flags &= ~TX_REQ_XFER_LEN;
2430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /* 5: we may need to capture headers */
2432 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002433 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002435
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436 /* 6: determine the transfer-length.
2437 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2438 * the presence of a message-body in a REQUEST and its transfer length
2439 * must be determined that way (in order of precedence) :
2440 * 1. The presence of a message-body in a request is signaled by the
2441 * inclusion of a Content-Length or Transfer-Encoding header field
2442 * in the request's header fields. When a request message contains
2443 * both a message-body of non-zero length and a method that does
2444 * not define any semantics for that request message-body, then an
2445 * origin server SHOULD either ignore the message-body or respond
2446 * with an appropriate error message (e.g., 413). A proxy or
2447 * gateway, when presented the same request, SHOULD either forward
2448 * the request inbound with the message- body or ignore the
2449 * message-body when determining a response.
2450 *
2451 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2452 * and the "chunked" transfer-coding (Section 6.2) is used, the
2453 * transfer-length is defined by the use of this transfer-coding.
2454 * If a Transfer-Encoding header field is present and the "chunked"
2455 * transfer-coding is not present, the transfer-length is defined
2456 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002457 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002458 * 3. If a Content-Length header field is present, its decimal value in
2459 * OCTETs represents both the entity-length and the transfer-length.
2460 * If a message is received with both a Transfer-Encoding header
2461 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 * 4. By the server closing the connection. (Closing the connection
2464 * cannot be used to indicate the end of a request body, since that
2465 * would leave no possibility for the server to send back a response.)
2466 *
2467 * Whenever a transfer-coding is applied to a message-body, the set of
2468 * transfer-codings MUST include "chunked", unless the message indicates
2469 * it is terminated by closing the connection. When the "chunked"
2470 * transfer-coding is used, it MUST be the last transfer-coding applied
2471 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472 */
2473
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002474 /* CONNECT sets a tunnel and ignores everything else */
2475 if (txn->meth == HTTP_METH_CONNECT)
2476 goto skip_xfer_len;
2477
2478 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002479 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002480 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002481 while ((txn->flags & TX_REQ_VER_11) &&
2482 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002483 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2484 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2485 else if (txn->flags & TX_REQ_TE_CHNK) {
2486 /* bad transfer-encoding (chunked followed by something else) */
2487 use_close_only = 1;
2488 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2489 break;
2490 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002491 }
2492
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002494 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2496 signed long long cl;
2497
2498 if (!ctx.vlen)
2499 goto return_bad_req;
2500
2501 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2502 goto return_bad_req; /* parse failure */
2503
2504 if (cl < 0)
2505 goto return_bad_req;
2506
2507 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2508 goto return_bad_req; /* already specified, was different */
2509
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002510 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002511 msg->hdr_content_len = cl;
2512 }
2513
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002514 /* bodyless requests have a known length */
2515 if (!use_close_only)
2516 txn->flags |= TX_REQ_XFER_LEN;
2517
2518 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002519 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002520 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 req->analyse_exp = TICK_ETERNITY;
2522 return 1;
2523
2524 return_bad_req:
2525 /* We centralize bad requests processing here */
2526 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2527 /* we detected a parsing error. We want to archive this request
2528 * in the dedicated proxy area for later troubleshooting.
2529 */
2530 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2531 }
2532
2533 txn->req.msg_state = HTTP_MSG_ERROR;
2534 txn->status = 400;
2535 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002536
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002537 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002538 if (s->listener->counters)
2539 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002540
2541 return_prx_cond:
2542 if (!(s->flags & SN_ERR_MASK))
2543 s->flags |= SN_ERR_PRXCOND;
2544 if (!(s->flags & SN_FINST_MASK))
2545 s->flags |= SN_FINST_R;
2546
2547 req->analysers = 0;
2548 req->analyse_exp = TICK_ETERNITY;
2549 return 0;
2550}
2551
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002552/* This stream analyser runs all HTTP request processing which is common to
2553 * frontends and backends, which means blocking ACLs, filters, connection-close,
2554 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002555 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002556 * either needs more data or wants to immediately abort the request (eg: deny,
2557 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002559int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002560{
Willy Tarreaud787e662009-07-07 10:14:51 +02002561 struct http_txn *txn = &s->txn;
2562 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002563 struct acl_cond *cond;
2564 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002565 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002566 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002567
Willy Tarreau655dce92009-11-08 13:10:58 +01002568 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002569 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002570 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002571 return 0;
2572 }
2573
Willy Tarreau3a816292009-07-07 10:55:49 +02002574 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002575 req->analyse_exp = TICK_ETERNITY;
2576
2577 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2578 now_ms, __FUNCTION__,
2579 s,
2580 req,
2581 req->rex, req->wex,
2582 req->flags,
2583 req->l,
2584 req->analysers);
2585
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002586 /* first check whether we have some ACLs set to block this request */
2587 list_for_each_entry(cond, &px->block_cond, list) {
2588 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002589
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002590 ret = acl_pass(ret);
2591 if (cond->pol == ACL_COND_UNLESS)
2592 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002593
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002594 if (ret) {
2595 txn->status = 403;
2596 /* let's log the request time */
2597 s->logs.tv_request = now;
2598 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2599 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002601 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002602
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002603 /* try headers filters */
2604 if (px->req_exp != NULL) {
2605 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2606 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 /* has the request been denied ? */
2609 if (txn->flags & TX_CLDENY) {
2610 /* no need to go further */
2611 txn->status = 403;
2612 /* let's log the request time */
2613 s->logs.tv_request = now;
2614 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2615 goto return_prx_cond;
2616 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002617
2618 /* When a connection is tarpitted, we use the tarpit timeout,
2619 * which may be the same as the connect timeout if unspecified.
2620 * If unset, then set it to zero because we really want it to
2621 * eventually expire. We build the tarpit as an analyser.
2622 */
2623 if (txn->flags & TX_CLTARPIT) {
2624 buffer_erase(s->req);
2625 /* wipe the request out so that we can drop the connection early
2626 * if the client closes first.
2627 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002628 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002629 req->analysers = 0; /* remove switching rules etc... */
2630 req->analysers |= AN_REQ_HTTP_TARPIT;
2631 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2632 if (!req->analyse_exp)
2633 req->analyse_exp = tick_add(now_ms, 0);
2634 return 1;
2635 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002636 }
Willy Tarreau06619262006-12-17 08:37:22 +01002637
Willy Tarreau5b154472009-12-21 20:11:07 +01002638 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2639 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002640 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2641 * in which headers are mangled. However, if another mode is set, it will
2642 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002643 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002644
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002645 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002646 ((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 +01002647 int tmp = TX_CON_WANT_TUN;
2648 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2649 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002650 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2651 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002652 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002653 tmp = TX_CON_WANT_CLO;
2654
Willy Tarreau5b154472009-12-21 20:11:07 +01002655 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2656 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002657
Willy Tarreau8db1c172010-01-05 23:12:12 +01002658 if (!(txn->flags & TX_CON_HDR_PARS))
2659 http_req_parse_connection_header(txn);
2660
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002661 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2662 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2663 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2664 if (!(txn->flags & TX_REQ_XFER_LEN))
2665 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2666 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002667 }
2668
2669 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2670 * once we know the backend, because the tunnel mode can be implied by the
2671 * lack of any close/keepalive options in both the FE and the BE. Since
2672 * this information can evolve with time, we proceed by trying to make the
2673 * header status match the desired status. For this, we'll have to adjust
2674 * the "Connection" header. The test for persistent connections has already
2675 * been performed, so we only enter here if there is a risk the connection
2676 * is considered as persistent and we want it to be closed on the server
2677 * side. It would be nice if we could enter this place only when a
2678 * Connection header exists. Note that a CONNECT method will not enter
2679 * here.
2680 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002681 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2682 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2683 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002684 char *cur_ptr, *cur_end, *cur_next;
2685 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002686 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002687 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002688
Willy Tarreau5b154472009-12-21 20:11:07 +01002689 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002690 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002691
Willy Tarreau5b154472009-12-21 20:11:07 +01002692 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002693 cur_hdr = &txn->hdr_idx.v[cur_idx];
2694 cur_ptr = cur_next;
2695 cur_end = cur_ptr + cur_hdr->len;
2696 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002697
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002698 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002699 if (!val)
2700 continue;
2701
2702 /* 3 possibilities :
2703 * - we have already set "Connection: close" or we're in
2704 * HTTP/1.0, so we remove this line.
2705 * - we have not yet set "Connection: close", but this line
2706 * indicates close. We leave it untouched and set the flag.
2707 * - we have not yet set "Connection: close", and this line
2708 * indicates non-close. We replace it and set the flag.
2709 */
2710 if (must_delete) {
2711 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2712 http_msg_move_end(&txn->req, delta);
2713 cur_next += delta;
2714 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2715 txn->hdr_idx.used--;
2716 cur_hdr->len = 0;
2717 txn->flags |= TX_REQ_CONN_CLO;
2718 } else {
2719 if (cur_end - cur_ptr - val != 5 ||
2720 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2721 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2722 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002723 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002724 cur_hdr->len += delta;
2725 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002726 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002727 txn->flags |= TX_REQ_CONN_CLO;
2728 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002729 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002730 } /* for loop */
2731 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002732
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002734 list_for_each_entry(wl, &px->req_add, list) {
2735 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002736 goto return_bad_req;
2737 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002738
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002739 /* check if stats URI was requested, and if an auth is needed */
2740 if (px->uri_auth != NULL &&
2741 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2742 /* we have to check the URI and auth for this request.
2743 * FIXME!!! that one is rather dangerous, we want to
2744 * make it follow standard rules (eg: clear req->analysers).
2745 */
2746 if (stats_check_uri_auth(s, px)) {
2747 req->analysers = 0;
2748 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 /* check whether we have some ACLs set to redirect this request */
2753 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002754 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002755
Willy Tarreauf285f542010-01-03 20:03:03 +01002756 if (rule->cond) {
2757 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2758 ret = acl_pass(ret);
2759 if (rule->cond->pol == ACL_COND_UNLESS)
2760 ret = !ret;
2761 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002762
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002764 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002766
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 /* build redirect message */
2768 switch(rule->code) {
2769 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770 msg_fmt = HTTP_303;
2771 break;
2772 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 msg_fmt = HTTP_301;
2774 break;
2775 case 302:
2776 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002777 msg_fmt = HTTP_302;
2778 break;
2779 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002780
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002781 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002782 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002783
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002784 switch(rule->type) {
2785 case REDIRECT_TYPE_PREFIX: {
2786 const char *path;
2787 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002788
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002789 path = http_get_path(txn);
2790 /* build message using path */
2791 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002792 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002793 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2794 int qs = 0;
2795 while (qs < pathlen) {
2796 if (path[qs] == '?') {
2797 pathlen = qs;
2798 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002799 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002800 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002801 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002802 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 } else {
2804 path = "/";
2805 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002806 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002807
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002808 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002809 goto return_bad_req;
2810
2811 /* add prefix. Note that if prefix == "/", we don't want to
2812 * add anything, otherwise it makes it hard for the user to
2813 * configure a self-redirection.
2814 */
2815 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002816 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2817 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002818 }
2819
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820 /* add path */
2821 memcpy(rdr.str + rdr.len, path, pathlen);
2822 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01002823
2824 /* append a slash at the end of the location is needed and missing */
2825 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
2826 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2827 if (rdr.len > rdr.size - 5)
2828 goto return_bad_req;
2829 rdr.str[rdr.len] = '/';
2830 rdr.len++;
2831 }
2832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 break;
2834 }
2835 case REDIRECT_TYPE_LOCATION:
2836 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002837 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002838 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002839
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002840 /* add location */
2841 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2842 rdr.len += rule->rdr_len;
2843 break;
2844 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002845
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002846 if (rule->cookie_len) {
2847 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2848 rdr.len += 14;
2849 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2850 rdr.len += rule->cookie_len;
2851 memcpy(rdr.str + rdr.len, "\r\n", 2);
2852 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002853 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002854
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002855 /* add end of headers and the keep-alive/close status.
2856 * We may choose to set keep-alive if the Location begins
2857 * with a slash, because the client will come back to the
2858 * same server.
2859 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002860 txn->status = rule->code;
2861 /* let's log the request time */
2862 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002863
2864 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2865 (txn->flags & TX_REQ_XFER_LEN) &&
2866 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2867 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2868 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2869 /* keep-alive possible */
2870 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2871 rdr.len += 28;
2872 buffer_write(req->prod->ob, rdr.str, rdr.len);
2873 /* "eat" the request */
2874 buffer_ignore(req, msg->sov - msg->som);
2875 msg->som = msg->sov;
2876 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002877 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2878 txn->req.msg_state = HTTP_MSG_CLOSED;
2879 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002880 break;
2881 } else {
2882 /* keep-alive not possible */
2883 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2884 rdr.len += 23;
Willy Tarreau148d0992010-01-10 10:21:21 +01002885 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002886 goto return_prx_cond;
2887 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002888 }
2889 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002890
Willy Tarreau2be39392010-01-03 17:24:51 +01002891 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2892 * If this happens, then the data will not come immediately, so we must
2893 * send all what we have without waiting. Note that due to the small gain
2894 * in waiting for the body of the request, it's easier to simply put the
2895 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2896 * itself once used.
2897 */
2898 req->flags |= BF_SEND_DONTWAIT;
2899
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002900 /* that's OK for us now, let's move on to next analysers */
2901 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002902
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002903 return_bad_req:
2904 /* We centralize bad requests processing here */
2905 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2906 /* we detected a parsing error. We want to archive this request
2907 * in the dedicated proxy area for later troubleshooting.
2908 */
2909 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2910 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002911
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002912 txn->req.msg_state = HTTP_MSG_ERROR;
2913 txn->status = 400;
2914 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002915
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002916 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002917 if (s->listener->counters)
2918 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002919
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002920 return_prx_cond:
2921 if (!(s->flags & SN_ERR_MASK))
2922 s->flags |= SN_ERR_PRXCOND;
2923 if (!(s->flags & SN_FINST_MASK))
2924 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002925
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002926 req->analysers = 0;
2927 req->analyse_exp = TICK_ETERNITY;
2928 return 0;
2929}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002930
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002931/* This function performs all the processing enabled for the current request.
2932 * It returns 1 if the processing can continue on next analysers, or zero if it
2933 * needs more data, encounters an error, or wants to immediately abort the
2934 * request. It relies on buffers flags, and updates s->req->analysers.
2935 */
2936int http_process_request(struct session *s, struct buffer *req, int an_bit)
2937{
2938 struct http_txn *txn = &s->txn;
2939 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002940
Willy Tarreau655dce92009-11-08 13:10:58 +01002941 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002942 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002943 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002944 return 0;
2945 }
2946
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002947 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2948 now_ms, __FUNCTION__,
2949 s,
2950 req,
2951 req->rex, req->wex,
2952 req->flags,
2953 req->l,
2954 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002955
Willy Tarreau59234e92008-11-30 23:51:27 +01002956 /*
2957 * Right now, we know that we have processed the entire headers
2958 * and that unwanted requests have been filtered out. We can do
2959 * whatever we want with the remaining request. Also, now we
2960 * may have separate values for ->fe, ->be.
2961 */
Willy Tarreau06619262006-12-17 08:37:22 +01002962
Willy Tarreau59234e92008-11-30 23:51:27 +01002963 /*
2964 * If HTTP PROXY is set we simply get remote server address
2965 * parsing incoming request.
2966 */
2967 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002968 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01002969 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002970
Willy Tarreau59234e92008-11-30 23:51:27 +01002971 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002972 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 * Note that doing so might move headers in the request, but
2974 * the fields will stay coherent and the URI will not move.
2975 * This should only be performed in the backend.
2976 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002977 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002978 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2979 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002980
Willy Tarreau59234e92008-11-30 23:51:27 +01002981 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002982 * 8: the appsession cookie was looked up very early in 1.2,
2983 * so let's do the same now.
2984 */
2985
2986 /* It needs to look into the URI */
2987 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002988 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002989 }
2990
2991 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 * 9: add X-Forwarded-For if either the frontend or the backend
2993 * asks for it.
2994 */
2995 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2996 if (s->cli_addr.ss_family == AF_INET) {
2997 /* Add an X-Forwarded-For header unless the source IP is
2998 * in the 'except' network range.
2999 */
3000 if ((!s->fe->except_mask.s_addr ||
3001 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3002 != s->fe->except_net.s_addr) &&
3003 (!s->be->except_mask.s_addr ||
3004 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3005 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003006 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003007 unsigned char *pn;
3008 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003009
3010 /* Note: we rely on the backend to get the header name to be used for
3011 * x-forwarded-for, because the header is really meant for the backends.
3012 * However, if the backend did not specify any option, we have to rely
3013 * on the frontend's header name.
3014 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003015 if (s->be->fwdfor_hdr_len) {
3016 len = s->be->fwdfor_hdr_len;
3017 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003018 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003019 len = s->fe->fwdfor_hdr_len;
3020 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003021 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003022 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003023
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003024 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003025 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003026 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003027 }
3028 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003029 else if (s->cli_addr.ss_family == AF_INET6) {
3030 /* FIXME: for the sake of completeness, we should also support
3031 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003032 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 int len;
3034 char pn[INET6_ADDRSTRLEN];
3035 inet_ntop(AF_INET6,
3036 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3037 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003038
Willy Tarreau59234e92008-11-30 23:51:27 +01003039 /* Note: we rely on the backend to get the header name to be used for
3040 * x-forwarded-for, because the header is really meant for the backends.
3041 * However, if the backend did not specify any option, we have to rely
3042 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003043 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003044 if (s->be->fwdfor_hdr_len) {
3045 len = s->be->fwdfor_hdr_len;
3046 memcpy(trash, s->be->fwdfor_hdr_name, len);
3047 } else {
3048 len = s->fe->fwdfor_hdr_len;
3049 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003050 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003051 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003052
Willy Tarreau59234e92008-11-30 23:51:27 +01003053 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003054 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003055 goto return_bad_req;
3056 }
3057 }
3058
3059 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003060 * 10: add X-Original-To if either the frontend or the backend
3061 * asks for it.
3062 */
3063 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3064
3065 /* FIXME: don't know if IPv6 can handle that case too. */
3066 if (s->cli_addr.ss_family == AF_INET) {
3067 /* Add an X-Original-To header unless the destination IP is
3068 * in the 'except' network range.
3069 */
3070 if (!(s->flags & SN_FRT_ADDR_SET))
3071 get_frt_addr(s);
3072
3073 if ((!s->fe->except_mask_to.s_addr ||
3074 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3075 != s->fe->except_to.s_addr) &&
3076 (!s->be->except_mask_to.s_addr ||
3077 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3078 != s->be->except_to.s_addr)) {
3079 int len;
3080 unsigned char *pn;
3081 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3082
3083 /* Note: we rely on the backend to get the header name to be used for
3084 * x-original-to, because the header is really meant for the backends.
3085 * However, if the backend did not specify any option, we have to rely
3086 * on the frontend's header name.
3087 */
3088 if (s->be->orgto_hdr_len) {
3089 len = s->be->orgto_hdr_len;
3090 memcpy(trash, s->be->orgto_hdr_name, len);
3091 } else {
3092 len = s->fe->orgto_hdr_len;
3093 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003094 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003095 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3096
3097 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003098 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003099 goto return_bad_req;
3100 }
3101 }
3102 }
3103
Willy Tarreau78599912009-10-17 20:12:21 +02003104 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003105 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3106 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3107 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003108 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003109 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003110 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003111 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003112 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003113
3114 /* If we have no server assigned yet and we're balancing on url_param
3115 * with a POST request, we may be interested in checking the body for
3116 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003117 */
3118 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3119 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003120 s->be->url_param_post_limit != 0 &&
3121 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003122 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003123 buffer_dont_connect(req);
3124 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003125 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003126
Willy Tarreaud98cf932009-12-27 22:54:55 +01003127 if (txn->flags & TX_REQ_XFER_LEN)
3128 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003129
Willy Tarreau59234e92008-11-30 23:51:27 +01003130 /*************************************************************
3131 * OK, that's finished for the headers. We have done what we *
3132 * could. Let's switch to the DATA state. *
3133 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003134 req->analyse_exp = TICK_ETERNITY;
3135 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003136
Willy Tarreau59234e92008-11-30 23:51:27 +01003137 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003138 /* OK let's go on with the BODY now */
3139 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003140
Willy Tarreau59234e92008-11-30 23:51:27 +01003141 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003142 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003143 /* we detected a parsing error. We want to archive this request
3144 * in the dedicated proxy area for later troubleshooting.
3145 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003146 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003147 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003148
Willy Tarreau59234e92008-11-30 23:51:27 +01003149 txn->req.msg_state = HTTP_MSG_ERROR;
3150 txn->status = 400;
3151 req->analysers = 0;
3152 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003153
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003154 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003155 if (s->listener->counters)
3156 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003157
Willy Tarreau59234e92008-11-30 23:51:27 +01003158 if (!(s->flags & SN_ERR_MASK))
3159 s->flags |= SN_ERR_PRXCOND;
3160 if (!(s->flags & SN_FINST_MASK))
3161 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003162 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003163}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003164
Willy Tarreau60b85b02008-11-30 23:28:40 +01003165/* This function is an analyser which processes the HTTP tarpit. It always
3166 * returns zero, at the beginning because it prevents any other processing
3167 * from occurring, and at the end because it terminates the request.
3168 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003169int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003170{
3171 struct http_txn *txn = &s->txn;
3172
3173 /* This connection is being tarpitted. The CLIENT side has
3174 * already set the connect expiration date to the right
3175 * timeout. We just have to check that the client is still
3176 * there and that the timeout has not expired.
3177 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003178 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003179 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3180 !tick_is_expired(req->analyse_exp, now_ms))
3181 return 0;
3182
3183 /* We will set the queue timer to the time spent, just for
3184 * logging purposes. We fake a 500 server error, so that the
3185 * attacker will not suspect his connection has been tarpitted.
3186 * It will not cause trouble to the logs because we can exclude
3187 * the tarpitted connections by filtering on the 'PT' status flags.
3188 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003189 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3190
3191 txn->status = 500;
3192 if (req->flags != BF_READ_ERROR)
3193 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3194
3195 req->analysers = 0;
3196 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003197
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003198 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003199 if (s->listener->counters)
3200 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003201
Willy Tarreau60b85b02008-11-30 23:28:40 +01003202 if (!(s->flags & SN_ERR_MASK))
3203 s->flags |= SN_ERR_PRXCOND;
3204 if (!(s->flags & SN_FINST_MASK))
3205 s->flags |= SN_FINST_T;
3206 return 0;
3207}
3208
Willy Tarreaud34af782008-11-30 23:36:37 +01003209/* This function is an analyser which processes the HTTP request body. It looks
3210 * for parameters to be used for the load balancing algorithm (url_param). It
3211 * must only be called after the standard HTTP request processing has occurred,
3212 * because it expects the request to be parsed. It returns zero if it needs to
3213 * read more data, or 1 once it has completed its analysis.
3214 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003215int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003216{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003217 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003218 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003219 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003220
3221 /* We have to parse the HTTP request body to find any required data.
3222 * "balance url_param check_post" should have been the only way to get
3223 * into this. We were brought here after HTTP header analysis, so all
3224 * related structures are ready.
3225 */
3226
Willy Tarreau522d6c02009-12-06 18:49:18 +01003227 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3228 goto missing_data;
3229
3230 if (msg->msg_state < HTTP_MSG_100_SENT) {
3231 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3232 * send an HTTP/1.1 100 Continue intermediate response.
3233 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003234 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003235 struct hdr_ctx ctx;
3236 ctx.idx = 0;
3237 /* Expect is allowed in 1.1, look for it */
3238 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3239 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3240 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3241 }
3242 }
3243 msg->msg_state = HTTP_MSG_100_SENT;
3244 }
3245
3246 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003247 /* we have msg->col and msg->sov which both point to the first
3248 * byte of message body. msg->som still points to the beginning
3249 * of the message. We must save the body in req->lr because it
3250 * survives buffer re-alignments.
3251 */
3252 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003253 if (txn->flags & TX_REQ_TE_CHNK)
3254 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3255 else
3256 msg->msg_state = HTTP_MSG_DATA;
3257 }
3258
3259 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003260 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003261 * set ->sov and ->lr to point to the body and switch to DATA or
3262 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003263 */
3264 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003265
Willy Tarreau115acb92009-12-26 13:56:06 +01003266 if (!ret)
3267 goto missing_data;
3268 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003269 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003270 }
3271
Willy Tarreaud98cf932009-12-27 22:54:55 +01003272 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003273 * We have the first non-header byte in msg->col, which is either the
3274 * beginning of the chunk size or of the data. The first data byte is in
3275 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3276 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003277 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003278
3279 if (msg->hdr_content_len < limit)
3280 limit = msg->hdr_content_len;
3281
Willy Tarreau7c96f672009-12-27 22:47:25 +01003282 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003283 goto http_end;
3284
3285 missing_data:
3286 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003287 if (req->flags & BF_FULL)
3288 goto return_bad_req;
3289
Willy Tarreau522d6c02009-12-06 18:49:18 +01003290 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3291 txn->status = 408;
3292 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3293 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003294 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003295
3296 /* we get here if we need to wait for more data */
3297 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003298 /* Not enough data. We'll re-use the http-request
3299 * timeout here. Ideally, we should set the timeout
3300 * relative to the accept() date. We just set the
3301 * request timeout once at the beginning of the
3302 * request.
3303 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003304 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003305 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003306 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003307 return 0;
3308 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003309
3310 http_end:
3311 /* The situation will not evolve, so let's give up on the analysis. */
3312 s->logs.tv_request = now; /* update the request timer to reflect full request */
3313 req->analysers &= ~an_bit;
3314 req->analyse_exp = TICK_ETERNITY;
3315 return 1;
3316
3317 return_bad_req: /* let's centralize all bad requests */
3318 txn->req.msg_state = HTTP_MSG_ERROR;
3319 txn->status = 400;
3320 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3321
3322 return_err_msg:
3323 req->analysers = 0;
3324 s->fe->counters.failed_req++;
3325 if (s->listener->counters)
3326 s->listener->counters->failed_req++;
3327
3328 if (!(s->flags & SN_ERR_MASK))
3329 s->flags |= SN_ERR_PRXCOND;
3330 if (!(s->flags & SN_FINST_MASK))
3331 s->flags |= SN_FINST_R;
3332 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003333}
3334
Willy Tarreau610ecce2010-01-04 21:15:02 +01003335/* Terminate current transaction and prepare a new one. This is very tricky
3336 * right now but it works.
3337 */
3338void http_end_txn_clean_session(struct session *s)
3339{
3340 /* FIXME: We need a more portable way of releasing a backend's and a
3341 * server's connections. We need a safer way to reinitialize buffer
3342 * flags. We also need a more accurate method for computing per-request
3343 * data.
3344 */
3345 http_silent_debug(__LINE__, s);
3346
3347 s->req->cons->flags |= SI_FL_NOLINGER;
3348 s->req->cons->shutr(s->req->cons);
3349 s->req->cons->shutw(s->req->cons);
3350
3351 http_silent_debug(__LINE__, s);
3352
3353 if (s->flags & SN_BE_ASSIGNED)
3354 s->be->beconn--;
3355
3356 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3357 session_process_counters(s);
3358
3359 if (s->txn.status) {
3360 int n;
3361
3362 n = s->txn.status / 100;
3363 if (n < 1 || n > 5)
3364 n = 0;
3365
3366 if (s->fe->mode == PR_MODE_HTTP)
3367 s->fe->counters.p.http.rsp[n]++;
3368
3369 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3370 (s->be->mode == PR_MODE_HTTP))
3371 s->be->counters.p.http.rsp[n]++;
3372 }
3373
3374 /* don't count other requests' data */
3375 s->logs.bytes_in -= s->req->l - s->req->send_max;
3376 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3377
3378 /* let's do a final log if we need it */
3379 if (s->logs.logwait &&
3380 !(s->flags & SN_MONITOR) &&
3381 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3382 s->do_log(s);
3383 }
3384
3385 s->logs.accept_date = date; /* user-visible date for logging */
3386 s->logs.tv_accept = now; /* corrected date for internal use */
3387 tv_zero(&s->logs.tv_request);
3388 s->logs.t_queue = -1;
3389 s->logs.t_connect = -1;
3390 s->logs.t_data = -1;
3391 s->logs.t_close = 0;
3392 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3393 s->logs.srv_queue_size = 0; /* we will get this number soon */
3394
3395 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3396 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3397
3398 if (s->pend_pos)
3399 pendconn_free(s->pend_pos);
3400
3401 if (s->srv) {
3402 if (s->flags & SN_CURR_SESS) {
3403 s->flags &= ~SN_CURR_SESS;
3404 s->srv->cur_sess--;
3405 }
3406 if (may_dequeue_tasks(s->srv, s->be))
3407 process_srv_queue(s->srv);
3408 }
3409
3410 if (unlikely(s->srv_conn))
3411 sess_change_server(s, NULL);
3412 s->srv = NULL;
3413
3414 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3415 s->req->cons->fd = -1; /* just to help with debugging */
3416 s->req->cons->err_type = SI_ET_NONE;
3417 s->req->cons->err_loc = NULL;
3418 s->req->cons->exp = TICK_ETERNITY;
3419 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003420 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3421 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 +01003422 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3423 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3424 s->txn.meth = 0;
3425 http_reset_txn(s);
3426 s->txn.flags |= TX_NOT_FIRST;
3427 if (s->be->options2 & PR_O2_INDEPSTR)
3428 s->req->cons->flags |= SI_FL_INDEP_STR;
3429
3430 /* if the request buffer is not empty, it means we're
3431 * about to process another request, so send pending
3432 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003433 * Just don't do this if the buffer is close to be full,
3434 * because the request will wait for it to flush a little
3435 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003436 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003437 if (s->req->l > s->req->send_max) {
3438 if (s->rep->send_max &&
3439 !(s->rep->flags & BF_FULL) &&
3440 s->rep->lr <= s->rep->r &&
3441 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3442 s->rep->flags |= BF_EXPECT_MORE;
3443 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003444
3445 /* we're removing the analysers, we MUST re-enable events detection */
3446 buffer_auto_read(s->req);
3447 buffer_auto_close(s->req);
3448 buffer_auto_read(s->rep);
3449 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003450
3451 /* make ->lr point to the first non-forwarded byte */
3452 s->req->lr = s->req->w + s->req->send_max;
3453 if (s->req->lr >= s->req->data + s->req->size)
3454 s->req->lr -= s->req->size;
3455 s->rep->lr = s->rep->w + s->rep->send_max;
3456 if (s->rep->lr >= s->rep->data + s->rep->size)
3457 s->rep->lr -= s->req->size;
3458
3459 s->req->analysers |= s->fe->fe_req_ana;
3460 s->rep->analysers = 0;
3461
3462 http_silent_debug(__LINE__, s);
3463}
3464
3465
3466/* This function updates the request state machine according to the response
3467 * state machine and buffer flags. It returns 1 if it changes anything (flag
3468 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3469 * it is only used to find when a request/response couple is complete. Both
3470 * this function and its equivalent should loop until both return zero. It
3471 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3472 */
3473int http_sync_req_state(struct session *s)
3474{
3475 struct buffer *buf = s->req;
3476 struct http_txn *txn = &s->txn;
3477 unsigned int old_flags = buf->flags;
3478 unsigned int old_state = txn->req.msg_state;
3479
3480 http_silent_debug(__LINE__, s);
3481 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3482 return 0;
3483
3484 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003485 /* No need to read anymore, the request was completely parsed.
3486 * We can shut the read side unless we want to abort_on_close.
3487 */
3488 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3489 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003490
3491 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3492 goto wait_other_side;
3493
3494 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3495 /* The server has not finished to respond, so we
3496 * don't want to move in order not to upset it.
3497 */
3498 goto wait_other_side;
3499 }
3500
3501 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3502 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003503 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003504 txn->req.msg_state = HTTP_MSG_TUNNEL;
3505 goto wait_other_side;
3506 }
3507
3508 /* When we get here, it means that both the request and the
3509 * response have finished receiving. Depending on the connection
3510 * mode, we'll have to wait for the last bytes to leave in either
3511 * direction, and sometimes for a close to be effective.
3512 */
3513
3514 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3515 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3516 /* Server-close mode : queue a connection close to the server */
3517 buffer_shutw_now(buf);
3518 buf->cons->flags |= SI_FL_NOLINGER;
3519 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003520 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003521 /* Option forceclose is set, let's enforce it now
3522 * that we're not expecting any new data to come.
3523 */
3524 buffer_shutr_now(buf);
3525 buffer_shutw_now(buf);
3526 buf->cons->flags |= SI_FL_NOLINGER;
3527 }
3528 /* other modes include httpclose (no action) and keepalive (not implemented) */
3529 }
3530
3531 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3532 /* if we've just closed an output, let's switch */
3533 if (!(buf->flags & BF_OUT_EMPTY)) {
3534 txn->req.msg_state = HTTP_MSG_CLOSING;
3535 goto http_msg_closing;
3536 }
3537 else {
3538 txn->req.msg_state = HTTP_MSG_CLOSED;
3539 goto http_msg_closed;
3540 }
3541 }
3542 else {
3543 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003544 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003545 txn->req.msg_state = HTTP_MSG_TUNNEL;
3546 goto wait_other_side;
3547 }
3548 }
3549
3550 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3551 http_msg_closing:
3552 /* nothing else to forward, just waiting for the output buffer
3553 * to be empty and for the shutw_now to take effect.
3554 */
3555 if (buf->flags & BF_OUT_EMPTY) {
3556 txn->req.msg_state = HTTP_MSG_CLOSED;
3557 goto http_msg_closed;
3558 }
3559 else if (buf->flags & BF_SHUTW) {
3560 txn->req.msg_state = HTTP_MSG_ERROR;
3561 goto wait_other_side;
3562 }
3563 }
3564
3565 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3566 http_msg_closed:
3567 goto wait_other_side;
3568 }
3569
3570 wait_other_side:
3571 http_silent_debug(__LINE__, s);
3572 return txn->req.msg_state != old_state || buf->flags != old_flags;
3573}
3574
3575
3576/* This function updates the response state machine according to the request
3577 * state machine and buffer flags. It returns 1 if it changes anything (flag
3578 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3579 * it is only used to find when a request/response couple is complete. Both
3580 * this function and its equivalent should loop until both return zero. It
3581 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3582 */
3583int http_sync_res_state(struct session *s)
3584{
3585 struct buffer *buf = s->rep;
3586 struct http_txn *txn = &s->txn;
3587 unsigned int old_flags = buf->flags;
3588 unsigned int old_state = txn->rsp.msg_state;
3589
3590 http_silent_debug(__LINE__, s);
3591 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3592 return 0;
3593
3594 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3595 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003596 * still monitor the server connection for a possible close
3597 * while the request is being uploaded, so we don't disable
3598 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003599 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003600 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003601
3602 if (txn->req.msg_state == HTTP_MSG_ERROR)
3603 goto wait_other_side;
3604
3605 if (txn->req.msg_state < HTTP_MSG_DONE) {
3606 /* The client seems to still be sending data, probably
3607 * because we got an error response during an upload.
3608 * We have the choice of either breaking the connection
3609 * or letting it pass through. Let's do the later.
3610 */
3611 goto wait_other_side;
3612 }
3613
3614 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3615 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003616 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003617 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3618 goto wait_other_side;
3619 }
3620
3621 /* When we get here, it means that both the request and the
3622 * response have finished receiving. Depending on the connection
3623 * mode, we'll have to wait for the last bytes to leave in either
3624 * direction, and sometimes for a close to be effective.
3625 */
3626
3627 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3628 /* Server-close mode : shut read and wait for the request
3629 * side to close its output buffer. The caller will detect
3630 * when we're in DONE and the other is in CLOSED and will
3631 * catch that for the final cleanup.
3632 */
3633 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3634 buffer_shutr_now(buf);
3635 goto wait_other_side;
3636 }
3637 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3638 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3639 /* Option forceclose is set, let's enforce it now
3640 * that we're not expecting any new data to come.
3641 * The caller knows the session is complete once
3642 * both states are CLOSED.
3643 */
3644 buffer_shutr_now(buf);
3645 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003646 }
3647 else {
3648 /* other modes include httpclose (no action) and keepalive
3649 * (not implemented). These modes are used as a tunnel right
3650 * now.
3651 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003652 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003653 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3654 goto wait_other_side;
3655 }
3656
3657 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3658 /* if we've just closed an output, let's switch */
3659 if (!(buf->flags & BF_OUT_EMPTY)) {
3660 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3661 goto http_msg_closing;
3662 }
3663 else {
3664 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3665 goto http_msg_closed;
3666 }
3667 }
3668 goto wait_other_side;
3669 }
3670
3671 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3672 http_msg_closing:
3673 /* nothing else to forward, just waiting for the output buffer
3674 * to be empty and for the shutw_now to take effect.
3675 */
3676 if (buf->flags & BF_OUT_EMPTY) {
3677 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3678 goto http_msg_closed;
3679 }
3680 else if (buf->flags & BF_SHUTW) {
3681 txn->rsp.msg_state = HTTP_MSG_ERROR;
3682 goto wait_other_side;
3683 }
3684 }
3685
3686 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3687 http_msg_closed:
3688 /* drop any pending data */
3689 buffer_ignore(buf, buf->l - buf->send_max);
3690 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003691 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003692 goto wait_other_side;
3693 }
3694
3695 wait_other_side:
3696 http_silent_debug(__LINE__, s);
3697 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3698}
3699
3700
3701/* Resync the request and response state machines. Return 1 if either state
3702 * changes.
3703 */
3704int http_resync_states(struct session *s)
3705{
3706 struct http_txn *txn = &s->txn;
3707 int old_req_state = txn->req.msg_state;
3708 int old_res_state = txn->rsp.msg_state;
3709
3710 http_silent_debug(__LINE__, s);
3711 http_sync_req_state(s);
3712 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003713 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714 if (!http_sync_res_state(s))
3715 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003716 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717 if (!http_sync_req_state(s))
3718 break;
3719 }
3720 http_silent_debug(__LINE__, s);
3721 /* OK, both state machines agree on a compatible state.
3722 * There are a few cases we're interested in :
3723 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3724 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3725 * directions, so let's simply disable both analysers.
3726 * - HTTP_MSG_CLOSED on the response only means we must abort the
3727 * request.
3728 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3729 * with server-close mode means we've completed one request and we
3730 * must re-initialize the server connection.
3731 */
3732
3733 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3734 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3735 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3736 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3737 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003738 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003739 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003740 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003741 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003742 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003744 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3745 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3746 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003747 s->rep->analysers = 0;
3748 buffer_auto_close(s->rep);
3749 buffer_auto_read(s->rep);
3750 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751 buffer_abort(s->req);
3752 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003753 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003754 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003755 }
3756 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3757 txn->rsp.msg_state == HTTP_MSG_DONE &&
3758 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3759 /* server-close: terminate this server connection and
3760 * reinitialize a fresh-new transaction.
3761 */
3762 http_end_txn_clean_session(s);
3763 }
3764
3765 http_silent_debug(__LINE__, s);
3766 return txn->req.msg_state != old_req_state ||
3767 txn->rsp.msg_state != old_res_state;
3768}
3769
Willy Tarreaud98cf932009-12-27 22:54:55 +01003770/* This function is an analyser which forwards request body (including chunk
3771 * sizes if any). It is called as soon as we must forward, even if we forward
3772 * zero byte. The only situation where it must not be called is when we're in
3773 * tunnel mode and we want to forward till the close. It's used both to forward
3774 * remaining data and to resync after end of body. It expects the msg_state to
3775 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3776 * read more data, or 1 once we can go on with next request or end the session.
3777 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3778 * bytes of pending data + the headers if not already done (between som and sov).
3779 * It eventually adjusts som to match sov after the data in between have been sent.
3780 */
3781int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3782{
3783 struct http_txn *txn = &s->txn;
3784 struct http_msg *msg = &s->txn.req;
3785
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003786 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3787 return 0;
3788
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003789 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3790 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3791 /* Output closed while we were sending data. We must abort. */
3792 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003793 buffer_auto_read(req);
3794 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003795 req->analysers &= ~an_bit;
3796 return 1;
3797 }
3798
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003799 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003800
3801 /* Note that we don't have to send 100-continue back because we don't
3802 * need the data to complete our job, and it's up to the server to
3803 * decide whether to return 100, 417 or anything else in return of
3804 * an "Expect: 100-continue" header.
3805 */
3806
3807 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3808 /* we have msg->col and msg->sov which both point to the first
3809 * byte of message body. msg->som still points to the beginning
3810 * of the message. We must save the body in req->lr because it
3811 * survives buffer re-alignments.
3812 */
3813 req->lr = req->data + msg->sov;
3814 if (txn->flags & TX_REQ_TE_CHNK)
3815 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3816 else {
3817 msg->msg_state = HTTP_MSG_DATA;
3818 }
3819 }
3820
Willy Tarreaud98cf932009-12-27 22:54:55 +01003821 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003822 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003823 /* we may have some data pending */
3824 if (msg->hdr_content_len || msg->som != msg->sov) {
3825 int bytes = msg->sov - msg->som;
3826 if (bytes < 0) /* sov may have wrapped at the end */
3827 bytes += req->size;
3828 buffer_forward(req, bytes + msg->hdr_content_len);
3829 msg->hdr_content_len = 0; /* don't forward that again */
3830 msg->som = msg->sov;
3831 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003832
Willy Tarreaucaabe412010-01-03 23:08:28 +01003833 if (msg->msg_state == HTTP_MSG_DATA) {
3834 /* must still forward */
3835 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003836 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003837
3838 /* nothing left to forward */
3839 if (txn->flags & TX_REQ_TE_CHNK)
3840 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003841 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003842 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003843 }
3844 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003845 /* read the chunk size and assign it to ->hdr_content_len, then
3846 * set ->sov and ->lr to point to the body and switch to DATA or
3847 * TRAILERS state.
3848 */
3849 int ret = http_parse_chunk_size(req, msg);
3850
3851 if (!ret)
3852 goto missing_data;
3853 else if (ret < 0)
3854 goto return_bad_req;
3855 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003856 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003857 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3858 /* we want the CRLF after the data */
3859 int ret;
3860
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003861 req->lr = req->w + req->send_max;
3862 if (req->lr >= req->data + req->size)
3863 req->lr -= req->size;
3864
Willy Tarreaud98cf932009-12-27 22:54:55 +01003865 ret = http_skip_chunk_crlf(req, msg);
3866
3867 if (ret == 0)
3868 goto missing_data;
3869 else if (ret < 0)
3870 goto return_bad_req;
3871 /* we're in MSG_CHUNK_SIZE now */
3872 }
3873 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3874 int ret = http_forward_trailers(req, msg);
3875
3876 if (ret == 0)
3877 goto missing_data;
3878 else if (ret < 0)
3879 goto return_bad_req;
3880 /* we're in HTTP_MSG_DONE now */
3881 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 else {
3883 /* other states, DONE...TUNNEL */
3884 if (http_resync_states(s)) {
3885 /* some state changes occurred, maybe the analyser
3886 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003887 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3889 goto return_bad_req;
3890 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003891 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003892 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003893 }
3894 }
3895
Willy Tarreaud98cf932009-12-27 22:54:55 +01003896 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003897 /* stop waiting for data if the input is closed before the end */
3898 if (req->flags & BF_SHUTR)
3899 goto return_bad_req;
3900
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003901 /* waiting for the last bits to leave the buffer */
3902 if (req->flags & BF_SHUTW)
3903 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904
3905 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003906 return 0;
3907
Willy Tarreaud98cf932009-12-27 22:54:55 +01003908 return_bad_req: /* let's centralize all bad requests */
3909 txn->req.msg_state = HTTP_MSG_ERROR;
3910 txn->status = 400;
3911 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01003912 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 +01003913 req->analysers = 0;
3914 s->fe->counters.failed_req++;
3915 if (s->listener->counters)
3916 s->listener->counters->failed_req++;
3917
3918 if (!(s->flags & SN_ERR_MASK))
3919 s->flags |= SN_ERR_PRXCOND;
3920 if (!(s->flags & SN_FINST_MASK))
3921 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003922 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003923 return 0;
3924}
3925
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003926/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3927 * processing can continue on next analysers, or zero if it either needs more
3928 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3929 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3930 * when it has nothing left to do, and may remove any analyser when it wants to
3931 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003932 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003933int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003934{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003935 struct http_txn *txn = &s->txn;
3936 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003937 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003938 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003939 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003940 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003941
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003942 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 +02003943 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003944 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003945 rep,
3946 rep->rex, rep->wex,
3947 rep->flags,
3948 rep->l,
3949 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003950
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003951 /*
3952 * Now parse the partial (or complete) lines.
3953 * We will check the response syntax, and also join multi-line
3954 * headers. An index of all the lines will be elaborated while
3955 * parsing.
3956 *
3957 * For the parsing, we use a 28 states FSM.
3958 *
3959 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003960 * rep->data + msg->som = beginning of response
3961 * rep->data + msg->eoh = end of processed headers / start of current one
3962 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003963 * rep->lr = first non-visited byte
3964 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01003965 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003966 */
3967
Willy Tarreau83e3af02009-12-28 17:39:57 +01003968 /* There's a protected area at the end of the buffer for rewriting
3969 * purposes. We don't want to start to parse the request if the
3970 * protected area is affected, because we may have to move processed
3971 * data later, which is much more complicated.
3972 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003973 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3974 if (unlikely((rep->flags & BF_FULL) ||
3975 rep->r < rep->lr ||
3976 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3977 if (rep->send_max) {
3978 /* some data has still not left the buffer, wake us once that's done */
3979 buffer_dont_close(rep);
3980 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3981 return 0;
3982 }
3983 if (rep->l <= rep->size - global.tune.maxrewrite)
3984 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003985 }
3986
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003987 if (likely(rep->lr < rep->r))
3988 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003989 }
3990
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003991 /* 1: we might have to print this header in debug mode */
3992 if (unlikely((global.mode & MODE_DEBUG) &&
3993 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003994 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003995 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003996
Willy Tarreau962c3f42010-01-10 00:15:35 +01003997 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003998 eol = sol + msg->sl.rq.l;
3999 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004000
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004001 sol += hdr_idx_first_pos(&txn->hdr_idx);
4002 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004003
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004004 while (cur_idx) {
4005 eol = sol + txn->hdr_idx.v[cur_idx].len;
4006 debug_hdr("srvhdr", s, sol, eol);
4007 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4008 cur_idx = txn->hdr_idx.v[cur_idx].next;
4009 }
4010 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004011
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004012 /*
4013 * Now we quickly check if we have found a full valid response.
4014 * If not so, we check the FD and buffer states before leaving.
4015 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004016 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004017 * responses are checked first.
4018 *
4019 * Depending on whether the client is still there or not, we
4020 * may send an error response back or not. Note that normally
4021 * we should only check for HTTP status there, and check I/O
4022 * errors somewhere else.
4023 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004024
Willy Tarreau655dce92009-11-08 13:10:58 +01004025 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004026 /* Invalid response */
4027 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4028 /* we detected a parsing error. We want to archive this response
4029 * in the dedicated proxy area for later troubleshooting.
4030 */
4031 hdr_response_bad:
4032 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4033 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4034
4035 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004036 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004037 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004038 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4039 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004040
Willy Tarreau90deb182010-01-07 00:20:41 +01004041 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004042 rep->analysers = 0;
4043 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004044 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004045 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4046
4047 if (!(s->flags & SN_ERR_MASK))
4048 s->flags |= SN_ERR_PRXCOND;
4049 if (!(s->flags & SN_FINST_MASK))
4050 s->flags |= SN_FINST_H;
4051
4052 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004053 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004054
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004055 /* too large response does not fit in buffer. */
4056 else if (rep->flags & BF_FULL) {
4057 goto hdr_response_bad;
4058 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004059
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004060 /* read error */
4061 else if (rep->flags & BF_READ_ERROR) {
4062 if (msg->err_pos >= 0)
4063 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004064
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004065 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004066 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004067 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004068 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4069 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004070
Willy Tarreau90deb182010-01-07 00:20:41 +01004071 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004072 rep->analysers = 0;
4073 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004074 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004075 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004076
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004077 if (!(s->flags & SN_ERR_MASK))
4078 s->flags |= SN_ERR_SRVCL;
4079 if (!(s->flags & SN_FINST_MASK))
4080 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004081 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004082 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004083
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004084 /* read timeout : return a 504 to the client. */
4085 else if (rep->flags & BF_READ_TIMEOUT) {
4086 if (msg->err_pos >= 0)
4087 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004089 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004090 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004091 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004092 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4093 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004094
Willy Tarreau90deb182010-01-07 00:20:41 +01004095 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004096 rep->analysers = 0;
4097 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004098 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004099 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004100
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004101 if (!(s->flags & SN_ERR_MASK))
4102 s->flags |= SN_ERR_SRVTO;
4103 if (!(s->flags & SN_FINST_MASK))
4104 s->flags |= SN_FINST_H;
4105 return 0;
4106 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004107
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004108 /* close from server */
4109 else if (rep->flags & BF_SHUTR) {
4110 if (msg->err_pos >= 0)
4111 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004112
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004113 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004114 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004115 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004116 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4117 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004118
Willy Tarreau90deb182010-01-07 00:20:41 +01004119 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004120 rep->analysers = 0;
4121 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004122 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004123 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004124
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004125 if (!(s->flags & SN_ERR_MASK))
4126 s->flags |= SN_ERR_SRVCL;
4127 if (!(s->flags & SN_FINST_MASK))
4128 s->flags |= SN_FINST_H;
4129 return 0;
4130 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004131
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004132 /* write error to client (we don't send any message then) */
4133 else if (rep->flags & BF_WRITE_ERROR) {
4134 if (msg->err_pos >= 0)
4135 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004136
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004137 s->be->counters.failed_resp++;
4138 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004139 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004140
4141 if (!(s->flags & SN_ERR_MASK))
4142 s->flags |= SN_ERR_CLICL;
4143 if (!(s->flags & SN_FINST_MASK))
4144 s->flags |= SN_FINST_H;
4145
4146 /* process_session() will take care of the error */
4147 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004148 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004149
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004150 buffer_dont_close(rep);
4151 return 0;
4152 }
4153
4154 /* More interesting part now : we know that we have a complete
4155 * response which at least looks like HTTP. We have an indicator
4156 * of each header's length, so we can parse them quickly.
4157 */
4158
4159 if (unlikely(msg->err_pos >= 0))
4160 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4161
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004162 /*
4163 * 1: get the status code
4164 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004165 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004166 if (n < 1 || n > 5)
4167 n = 0;
4168 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004169
Willy Tarreau5b154472009-12-21 20:11:07 +01004170 /* check if the response is HTTP/1.1 or above */
4171 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004172 ((msg->sol[5] > '1') ||
4173 ((msg->sol[5] == '1') &&
4174 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004175 txn->flags |= TX_RES_VER_11;
4176
4177 /* "connection" has not been parsed yet */
4178 txn->flags &= ~TX_CON_HDR_PARS;
4179
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004180 /* transfer length unknown*/
4181 txn->flags &= ~TX_RES_XFER_LEN;
4182
Willy Tarreau962c3f42010-01-10 00:15:35 +01004183 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004184
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004185 if (txn->status >= 100 && txn->status < 500)
4186 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4187 else
4188 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4189
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004190 /*
4191 * 2: check for cacheability.
4192 */
4193
4194 switch (txn->status) {
4195 case 200:
4196 case 203:
4197 case 206:
4198 case 300:
4199 case 301:
4200 case 410:
4201 /* RFC2616 @13.4:
4202 * "A response received with a status code of
4203 * 200, 203, 206, 300, 301 or 410 MAY be stored
4204 * by a cache (...) unless a cache-control
4205 * directive prohibits caching."
4206 *
4207 * RFC2616 @9.5: POST method :
4208 * "Responses to this method are not cacheable,
4209 * unless the response includes appropriate
4210 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004211 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004212 if (likely(txn->meth != HTTP_METH_POST) &&
4213 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4214 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4215 break;
4216 default:
4217 break;
4218 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004219
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004220 /*
4221 * 3: we may need to capture headers
4222 */
4223 s->logs.logwait &= ~LW_RESP;
4224 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004225 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004226 txn->rsp.cap, s->fe->rsp_cap);
4227
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004228 /* 4: determine the transfer-length.
4229 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4230 * the presence of a message-body in a RESPONSE and its transfer length
4231 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004232 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004233 * All responses to the HEAD request method MUST NOT include a
4234 * message-body, even though the presence of entity-header fields
4235 * might lead one to believe they do. All 1xx (informational), 204
4236 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4237 * message-body. All other responses do include a message-body,
4238 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004239 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004240 * 1. Any response which "MUST NOT" include a message-body (such as the
4241 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4242 * always terminated by the first empty line after the header fields,
4243 * regardless of the entity-header fields present in the message.
4244 *
4245 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4246 * the "chunked" transfer-coding (Section 6.2) is used, the
4247 * transfer-length is defined by the use of this transfer-coding.
4248 * If a Transfer-Encoding header field is present and the "chunked"
4249 * transfer-coding is not present, the transfer-length is defined by
4250 * the sender closing the connection.
4251 *
4252 * 3. If a Content-Length header field is present, its decimal value in
4253 * OCTETs represents both the entity-length and the transfer-length.
4254 * If a message is received with both a Transfer-Encoding header
4255 * field and a Content-Length header field, the latter MUST be ignored.
4256 *
4257 * 4. If the message uses the media type "multipart/byteranges", and
4258 * the transfer-length is not otherwise specified, then this self-
4259 * delimiting media type defines the transfer-length. This media
4260 * type MUST NOT be used unless the sender knows that the recipient
4261 * can parse it; the presence in a request of a Range header with
4262 * multiple byte-range specifiers from a 1.1 client implies that the
4263 * client can parse multipart/byteranges responses.
4264 *
4265 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004266 */
4267
4268 /* Skip parsing if no content length is possible. The response flags
4269 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004270 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004271 * FIXME: should we parse anyway and return an error on chunked encoding ?
4272 */
4273 if (txn->meth == HTTP_METH_HEAD ||
4274 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004275 txn->status == 204 || txn->status == 304) {
4276 txn->flags |= TX_RES_XFER_LEN;
4277 goto skip_content_length;
4278 }
4279
4280 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004281 goto skip_content_length;
4282
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004283 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004284 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004285 while ((txn->flags & TX_RES_VER_11) &&
4286 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004287 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4288 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4289 else if (txn->flags & TX_RES_TE_CHNK) {
4290 /* bad transfer-encoding (chunked followed by something else) */
4291 use_close_only = 1;
4292 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4293 break;
4294 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004295 }
4296
4297 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4298 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004299 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004300 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4301 signed long long cl;
4302
4303 if (!ctx.vlen)
4304 goto hdr_response_bad;
4305
4306 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4307 goto hdr_response_bad; /* parse failure */
4308
4309 if (cl < 0)
4310 goto hdr_response_bad;
4311
4312 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4313 goto hdr_response_bad; /* already specified, was different */
4314
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004315 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004316 msg->hdr_content_len = cl;
4317 }
4318
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004319 /* FIXME: we should also implement the multipart/byterange method.
4320 * For now on, we resort to close mode in this case (unknown length).
4321 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004322skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004323
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004324 /* end of job, return OK */
4325 rep->analysers &= ~an_bit;
4326 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004327 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004328 return 1;
4329}
4330
4331/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004332 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4333 * and updates t->rep->analysers. It might make sense to explode it into several
4334 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004335 */
4336int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4337{
4338 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004339 struct http_msg *msg = &txn->rsp;
4340 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004341 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004342 int conn_ka = 0, conn_cl = 0;
4343 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004344 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004345
4346 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4347 now_ms, __FUNCTION__,
4348 t,
4349 rep,
4350 rep->rex, rep->wex,
4351 rep->flags,
4352 rep->l,
4353 rep->analysers);
4354
Willy Tarreau655dce92009-11-08 13:10:58 +01004355 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004356 return 0;
4357
4358 rep->analysers &= ~an_bit;
4359 rep->analyse_exp = TICK_ETERNITY;
4360
Willy Tarreau5b154472009-12-21 20:11:07 +01004361 /* Now we have to check if we need to modify the Connection header.
4362 * This is more difficult on the response than it is on the request,
4363 * because we can have two different HTTP versions and we don't know
4364 * how the client will interprete a response. For instance, let's say
4365 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4366 * HTTP/1.1 response without any header. Maybe it will bound itself to
4367 * HTTP/1.0 because it only knows about it, and will consider the lack
4368 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4369 * the lack of header as a keep-alive. Thus we will use two flags
4370 * indicating how a request MAY be understood by the client. In case
4371 * of multiple possibilities, we'll fix the header to be explicit. If
4372 * ambiguous cases such as both close and keepalive are seen, then we
4373 * will fall back to explicit close. Note that we won't take risks with
4374 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4375 */
4376
4377 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004378 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4379 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4380 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004381 int may_keep = 0, may_close = 0; /* how it may be understood */
4382 struct hdr_ctx ctx;
4383
4384 ctx.idx = 0;
4385 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4386 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4387 conn_cl = 1;
4388 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4389 conn_ka = 1;
4390 }
4391
4392 if (conn_cl) {
4393 /* close header present */
4394 may_close = 1;
4395 if (conn_ka) /* we have both close and keep-alive */
4396 may_keep = 1;
4397 }
4398 else if (conn_ka) {
4399 /* keep-alive alone */
4400 may_keep = 1;
4401 }
4402 else {
4403 /* no close nor keep-alive header */
4404 if (txn->flags & TX_RES_VER_11)
4405 may_keep = 1;
4406 else
4407 may_close = 1;
4408
4409 if (txn->flags & TX_REQ_VER_11)
4410 may_keep = 1;
4411 else
4412 may_close = 1;
4413 }
4414
4415 /* let's update the transaction status to reflect any close.
4416 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004417 * handled. We also explicitly state that we will close in
4418 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004419 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004420 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004421 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4422 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004423 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4424
4425 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004426 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004427 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4428 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004429 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004430 if (may_keep &&
4431 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4432 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004433 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004434 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4435 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4436 must_del_close = 1;
4437 if (!(txn->flags & TX_REQ_VER_11))
4438 must_keep = 1;
4439 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004440
4441 txn->flags |= TX_CON_HDR_PARS;
4442 }
4443
4444 /* We might have to check for "Connection:" if the server
4445 * returns a connection status that is not compatible with
4446 * the client's or with the config.
4447 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004448 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004449 char *cur_ptr, *cur_end, *cur_next;
4450 int cur_idx, old_idx, delta, val;
4451 int must_delete;
4452 struct hdr_idx_elem *cur_hdr;
4453
4454 /* we just have to remove the headers if both sides are 1.0 */
4455 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004456
4457 /* same if we want to re-enable keep-alive on 1.1 */
4458 must_delete |= must_del_close;
4459
Willy Tarreau962c3f42010-01-10 00:15:35 +01004460 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau5b154472009-12-21 20:11:07 +01004461
4462 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4463 cur_hdr = &txn->hdr_idx.v[cur_idx];
4464 cur_ptr = cur_next;
4465 cur_end = cur_ptr + cur_hdr->len;
4466 cur_next = cur_end + cur_hdr->cr + 1;
4467
4468 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4469 if (!val)
4470 continue;
4471
4472 /* 3 possibilities :
4473 * - we have already set "Connection: close" or we're in
4474 * HTTP/1.0, so we remove this line.
4475 * - we have not yet set "Connection: close", but this line
4476 * indicates close. We leave it untouched and set the flag.
4477 * - we have not yet set "Connection: close", and this line
4478 * indicates non-close. We replace it and set the flag.
4479 */
4480 if (must_delete) {
4481 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4482 http_msg_move_end(&txn->rsp, delta);
4483 cur_next += delta;
4484 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4485 txn->hdr_idx.used--;
4486 cur_hdr->len = 0;
4487 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004488 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004489 } else {
4490 if (cur_end - cur_ptr - val != 5 ||
4491 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4492 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4493 "close", 5);
4494 cur_next += delta;
4495 cur_hdr->len += delta;
4496 http_msg_move_end(&txn->rsp, delta);
4497 }
4498 must_delete = 1;
4499 must_close = 0;
4500 }
4501 } /* for loop */
4502 } /* if must close keep-alive */
4503
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004504 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004505 /*
4506 * 3: we will have to evaluate the filters.
4507 * As opposed to version 1.2, now they will be evaluated in the
4508 * filters order and not in the header order. This means that
4509 * each filter has to be validated among all headers.
4510 *
4511 * Filters are tried with ->be first, then with ->fe if it is
4512 * different from ->be.
4513 */
4514
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004515 cur_proxy = t->be;
4516 while (1) {
4517 struct proxy *rule_set = cur_proxy;
4518
4519 /* try headers filters */
4520 if (rule_set->rsp_exp != NULL) {
4521 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4522 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004523 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004524 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004525 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4526 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004527 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004528 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004529 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004530 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004531 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004532 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004533 if (!(t->flags & SN_ERR_MASK))
4534 t->flags |= SN_ERR_PRXCOND;
4535 if (!(t->flags & SN_FINST_MASK))
4536 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004537 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004538 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004539 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004541 /* has the response been denied ? */
4542 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004543 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004544 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004545
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004546 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004547 if (t->listener->counters)
4548 t->listener->counters->denied_resp++;
4549
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004550 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004551 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004552
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004553 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004554 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004555 if (txn->status < 200)
4556 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004557 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004558 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004559 }
4560
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004561 /* check whether we're already working on the frontend */
4562 if (cur_proxy == t->fe)
4563 break;
4564 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004565 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004566
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004567 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004568 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4569 * in which case this is not the right response, and we're waiting for the
4570 * next one. Let's allow this response to go to the client and wait for the
4571 * next one.
4572 */
4573 if (txn->status < 200) {
4574 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004575 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004576 msg->msg_state = HTTP_MSG_RPBEFORE;
4577 txn->status = 0;
4578 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4579 return 1;
4580 }
4581
4582 /* we don't have any 1xx status code now */
4583
4584 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585 * 4: check for server cookie.
4586 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004587 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4588 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004590
Willy Tarreaubaaee002006-06-26 02:48:02 +02004591
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004593 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004594 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004595 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004597
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004598 /*
4599 * 6: add server cookie in the response if needed
4600 */
4601 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004602 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004603 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004604
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004605 /* the server is known, it's not the one the client requested, we have to
4606 * insert a set-cookie here, except if we want to insert only on POST
4607 * requests and this one isn't. Note that servers which don't have cookies
4608 * (eg: some backup servers) will return a full cookie removal request.
4609 */
4610 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4611 t->be->cookie_name,
4612 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004613
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004614 if (t->be->cookie_domain)
4615 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004616
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004617 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004618 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004619 goto return_bad_resp;
4620 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004621
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004622 /* Here, we will tell an eventual cache on the client side that we don't
4623 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4624 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4625 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4626 */
4627 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004628
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004629 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4630
4631 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004632 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004633 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004634 }
4635 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004636
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004637 /*
4638 * 7: check if result will be cacheable with a cookie.
4639 * We'll block the response if security checks have caught
4640 * nasty things such as a cacheable cookie.
4641 */
4642 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4643 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004644 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004645
4646 /* we're in presence of a cacheable response containing
4647 * a set-cookie header. We'll block it as requested by
4648 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004649 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004650 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004651 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004652
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004653 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004654 if (t->listener->counters)
4655 t->listener->counters->denied_resp++;
4656
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004657 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4658 t->be->id, t->srv?t->srv->id:"<dispatch>");
4659 send_log(t->be, LOG_ALERT,
4660 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4661 t->be->id, t->srv?t->srv->id:"<dispatch>");
4662 goto return_srv_prx_502;
4663 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004664
4665 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004666 * 8: add "Connection: close" if needed and not yet set. This is
4667 * only needed for 1.1 responses since we know there is no other
4668 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004669 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004670 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004671 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004672 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004673 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004674 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004675 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004676 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4677 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4678 "Connection: keep-alive", 22) < 0))
4679 goto return_bad_resp;
4680 must_keep = 0;
4681 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004682
Willy Tarreaud98cf932009-12-27 22:54:55 +01004683 if (txn->flags & TX_RES_XFER_LEN)
4684 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004685
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004686 /*************************************************************
4687 * OK, that's finished for the headers. We have done what we *
4688 * could. Let's switch to the DATA state. *
4689 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004690
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004691 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004692
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004693 /* if the user wants to log as soon as possible, without counting
4694 * bytes from the server, then this is the right moment. We have
4695 * to temporarily assign bytes_out to log what we currently have.
4696 */
4697 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4698 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4699 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004700 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004701 t->logs.bytes_out = 0;
4702 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004703
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004704 /* Note: we must not try to cheat by jumping directly to DATA,
4705 * otherwise we would not let the client side wake up.
4706 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004707
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004708 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004709 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004710 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004711}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004712
Willy Tarreaud98cf932009-12-27 22:54:55 +01004713/* This function is an analyser which forwards response body (including chunk
4714 * sizes if any). It is called as soon as we must forward, even if we forward
4715 * zero byte. The only situation where it must not be called is when we're in
4716 * tunnel mode and we want to forward till the close. It's used both to forward
4717 * remaining data and to resync after end of body. It expects the msg_state to
4718 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4719 * read more data, or 1 once we can go on with next request or end the session.
4720 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4721 * bytes of pending data + the headers if not already done (between som and sov).
4722 * It eventually adjusts som to match sov after the data in between have been sent.
4723 */
4724int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4725{
4726 struct http_txn *txn = &s->txn;
4727 struct http_msg *msg = &s->txn.rsp;
4728
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004729 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4730 return 0;
4731
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004732 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004733 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004734 !s->req->analysers) {
4735 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4736 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004737 buffer_auto_read(res);
4738 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004739 res->analysers &= ~an_bit;
4740 return 1;
4741 }
4742
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004743 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004744
Willy Tarreaud98cf932009-12-27 22:54:55 +01004745 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4746 /* we have msg->col and msg->sov which both point to the first
4747 * byte of message body. msg->som still points to the beginning
4748 * of the message. We must save the body in req->lr because it
4749 * survives buffer re-alignments.
4750 */
4751 res->lr = res->data + msg->sov;
4752 if (txn->flags & TX_RES_TE_CHNK)
4753 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4754 else {
4755 msg->msg_state = HTTP_MSG_DATA;
4756 }
4757 }
4758
Willy Tarreaud98cf932009-12-27 22:54:55 +01004759 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004760 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004761 /* we may have some data pending */
4762 if (msg->hdr_content_len || msg->som != msg->sov) {
4763 int bytes = msg->sov - msg->som;
4764 if (bytes < 0) /* sov may have wrapped at the end */
4765 bytes += res->size;
4766 buffer_forward(res, bytes + msg->hdr_content_len);
4767 msg->hdr_content_len = 0; /* don't forward that again */
4768 msg->som = msg->sov;
4769 }
4770
Willy Tarreaucaabe412010-01-03 23:08:28 +01004771 if (msg->msg_state == HTTP_MSG_DATA) {
4772 /* must still forward */
4773 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004774 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004775
4776 /* nothing left to forward */
4777 if (txn->flags & TX_RES_TE_CHNK)
4778 msg->msg_state = HTTP_MSG_DATA_CRLF;
4779 else
4780 msg->msg_state = HTTP_MSG_DONE;
4781 }
4782 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004783 /* read the chunk size and assign it to ->hdr_content_len, then
4784 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4785 */
4786 int ret = http_parse_chunk_size(res, msg);
4787
4788 if (!ret)
4789 goto missing_data;
4790 else if (ret < 0)
4791 goto return_bad_res;
4792 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004793 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004794 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4795 /* we want the CRLF after the data */
4796 int ret;
4797
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004798 res->lr = res->w + res->send_max;
4799 if (res->lr >= res->data + res->size)
4800 res->lr -= res->size;
4801
Willy Tarreaud98cf932009-12-27 22:54:55 +01004802 ret = http_skip_chunk_crlf(res, msg);
4803
4804 if (!ret)
4805 goto missing_data;
4806 else if (ret < 0)
4807 goto return_bad_res;
4808 /* we're in MSG_CHUNK_SIZE now */
4809 }
4810 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4811 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004812
Willy Tarreaud98cf932009-12-27 22:54:55 +01004813 if (ret == 0)
4814 goto missing_data;
4815 else if (ret < 0)
4816 goto return_bad_res;
4817 /* we're in HTTP_MSG_DONE now */
4818 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004819 else {
4820 /* other states, DONE...TUNNEL */
4821 if (http_resync_states(s)) {
4822 http_silent_debug(__LINE__, s);
4823 /* some state changes occurred, maybe the analyser
4824 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004825 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004826 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4827 goto return_bad_res;
4828 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004829 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004830 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004831 }
4832 }
4833
Willy Tarreaud98cf932009-12-27 22:54:55 +01004834 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004835 /* stop waiting for data if the input is closed before the end */
4836 if (res->flags & BF_SHUTR)
4837 goto return_bad_res;
4838
Willy Tarreau610ecce2010-01-04 21:15:02 +01004839 if (!s->req->analysers)
4840 goto return_bad_res;
4841
Willy Tarreaud98cf932009-12-27 22:54:55 +01004842 /* forward the chunk size as well as any pending data */
4843 if (msg->hdr_content_len || msg->som != msg->sov) {
4844 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4845 msg->hdr_content_len = 0; /* don't forward that again */
4846 msg->som = msg->sov;
4847 }
4848
Willy Tarreaud98cf932009-12-27 22:54:55 +01004849 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004850 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004851 return 0;
4852
4853 return_bad_res: /* let's centralize all bad resuests */
4854 txn->rsp.msg_state = HTTP_MSG_ERROR;
4855 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01004856 /* don't send any error message as we're in the body */
4857 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004858 res->analysers = 0;
4859 s->be->counters.failed_resp++;
4860 if (s->srv) {
4861 s->srv->counters.failed_resp++;
4862 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4863 }
4864
4865 if (!(s->flags & SN_ERR_MASK))
4866 s->flags |= SN_ERR_PRXCOND;
4867 if (!(s->flags & SN_FINST_MASK))
4868 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004870 return 0;
4871}
4872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004873/* Iterate the same filter through all request headers.
4874 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004875 * Since it can manage the switch to another backend, it updates the per-proxy
4876 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004877 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004878int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004879{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004880 char term;
4881 char *cur_ptr, *cur_end, *cur_next;
4882 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004883 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004884 struct hdr_idx_elem *cur_hdr;
4885 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004887 last_hdr = 0;
4888
Willy Tarreau962c3f42010-01-10 00:15:35 +01004889 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004890 old_idx = 0;
4891
4892 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004893 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004894 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004895 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004896 (exp->action == ACT_ALLOW ||
4897 exp->action == ACT_DENY ||
4898 exp->action == ACT_TARPIT))
4899 return 0;
4900
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004901 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004902 if (!cur_idx)
4903 break;
4904
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004905 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004906 cur_ptr = cur_next;
4907 cur_end = cur_ptr + cur_hdr->len;
4908 cur_next = cur_end + cur_hdr->cr + 1;
4909
4910 /* Now we have one header between cur_ptr and cur_end,
4911 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004912 */
4913
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004914 /* The annoying part is that pattern matching needs
4915 * that we modify the contents to null-terminate all
4916 * strings before testing them.
4917 */
4918
4919 term = *cur_end;
4920 *cur_end = '\0';
4921
4922 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4923 switch (exp->action) {
4924 case ACT_SETBE:
4925 /* It is not possible to jump a second time.
4926 * FIXME: should we return an HTTP/500 here so that
4927 * the admin knows there's a problem ?
4928 */
4929 if (t->be != t->fe)
4930 break;
4931
4932 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004933 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004934 last_hdr = 1;
4935 break;
4936
4937 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004938 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004939 last_hdr = 1;
4940 break;
4941
4942 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004943 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004944 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004945
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004946 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004947 if (t->listener->counters)
4948 t->listener->counters->denied_resp++;
4949
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004950 break;
4951
4952 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004953 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004955
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004956 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004957 if (t->listener->counters)
4958 t->listener->counters->denied_resp++;
4959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004960 break;
4961
4962 case ACT_REPLACE:
4963 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4964 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4965 /* FIXME: if the user adds a newline in the replacement, the
4966 * index will not be recalculated for now, and the new line
4967 * will not be counted as a new header.
4968 */
4969
4970 cur_end += delta;
4971 cur_next += delta;
4972 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004973 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004974 break;
4975
4976 case ACT_REMOVE:
4977 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4978 cur_next += delta;
4979
Willy Tarreaufa355d42009-11-29 18:12:29 +01004980 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004981 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4982 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004983 cur_hdr->len = 0;
4984 cur_end = NULL; /* null-term has been rewritten */
4985 break;
4986
4987 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004988 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004989 if (cur_end)
4990 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004992 /* keep the link from this header to next one in case of later
4993 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004994 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004995 old_idx = cur_idx;
4996 }
4997 return 0;
4998}
4999
5000
5001/* Apply the filter to the request line.
5002 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5003 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005004 * Since it can manage the switch to another backend, it updates the per-proxy
5005 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005006 */
5007int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5008{
5009 char term;
5010 char *cur_ptr, *cur_end;
5011 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005012 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005013 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005014
Willy Tarreau58f10d72006-12-04 02:26:12 +01005015
Willy Tarreau3d300592007-03-18 18:34:41 +01005016 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005017 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005018 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005019 (exp->action == ACT_ALLOW ||
5020 exp->action == ACT_DENY ||
5021 exp->action == ACT_TARPIT))
5022 return 0;
5023 else if (exp->action == ACT_REMOVE)
5024 return 0;
5025
5026 done = 0;
5027
Willy Tarreau962c3f42010-01-10 00:15:35 +01005028 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005029 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005030
5031 /* Now we have the request line between cur_ptr and cur_end */
5032
5033 /* The annoying part is that pattern matching needs
5034 * that we modify the contents to null-terminate all
5035 * strings before testing them.
5036 */
5037
5038 term = *cur_end;
5039 *cur_end = '\0';
5040
5041 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5042 switch (exp->action) {
5043 case ACT_SETBE:
5044 /* It is not possible to jump a second time.
5045 * FIXME: should we return an HTTP/500 here so that
5046 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005047 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005048 if (t->be != t->fe)
5049 break;
5050
5051 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005052 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005053 done = 1;
5054 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005056 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005057 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005058 done = 1;
5059 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005061 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005062 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005063
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005064 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005065 if (t->listener->counters)
5066 t->listener->counters->denied_resp++;
5067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005068 done = 1;
5069 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005071 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005072 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005073
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005074 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005075 if (t->listener->counters)
5076 t->listener->counters->denied_resp++;
5077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005078 done = 1;
5079 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005081 case ACT_REPLACE:
5082 *cur_end = term; /* restore the string terminator */
5083 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5084 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5085 /* FIXME: if the user adds a newline in the replacement, the
5086 * index will not be recalculated for now, and the new line
5087 * will not be counted as a new header.
5088 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005089
Willy Tarreaufa355d42009-11-29 18:12:29 +01005090 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005091 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005092 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005093 HTTP_MSG_RQMETH,
5094 cur_ptr, cur_end + 1,
5095 NULL, NULL);
5096 if (unlikely(!cur_end))
5097 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005099 /* we have a full request and we know that we have either a CR
5100 * or an LF at <ptr>.
5101 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005102 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5103 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005104 /* there is no point trying this regex on headers */
5105 return 1;
5106 }
5107 }
5108 *cur_end = term; /* restore the string terminator */
5109 return done;
5110}
Willy Tarreau97de6242006-12-27 17:18:38 +01005111
Willy Tarreau58f10d72006-12-04 02:26:12 +01005112
Willy Tarreau58f10d72006-12-04 02:26:12 +01005113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005114/*
5115 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5116 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005117 * unparsable request. Since it can manage the switch to another backend, it
5118 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005119 */
5120int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5121{
Willy Tarreau3d300592007-03-18 18:34:41 +01005122 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005123 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005124 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005125 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127 /*
5128 * The interleaving of transformations and verdicts
5129 * makes it difficult to decide to continue or stop
5130 * the evaluation.
5131 */
5132
Willy Tarreau3d300592007-03-18 18:34:41 +01005133 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005134 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5135 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5136 exp = exp->next;
5137 continue;
5138 }
5139
5140 /* Apply the filter to the request line. */
5141 ret = apply_filter_to_req_line(t, req, exp);
5142 if (unlikely(ret < 0))
5143 return -1;
5144
5145 if (likely(ret == 0)) {
5146 /* The filter did not match the request, it can be
5147 * iterated through all headers.
5148 */
5149 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005150 }
5151 exp = exp->next;
5152 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005153 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005154}
5155
5156
Willy Tarreaua15645d2007-03-18 16:22:39 +01005157
Willy Tarreau58f10d72006-12-04 02:26:12 +01005158/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005159 * Try to retrieve the server associated to the appsession.
5160 * If the server is found, it's assigned to the session.
5161 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005162void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005163 struct http_txn *txn = &t->txn;
5164 appsess *asession = NULL;
5165 char *sessid_temp = NULL;
5166
Cyril Bontéb21570a2009-11-29 20:04:48 +01005167 if (len > t->be->appsession_len) {
5168 len = t->be->appsession_len;
5169 }
5170
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005171 if (t->be->options2 & PR_O2_AS_REQL) {
5172 /* request-learn option is enabled : store the sessid in the session for future use */
5173 if (t->sessid != NULL) {
5174 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5175 pool_free2(apools.sessid, t->sessid);
5176 }
5177
5178 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5179 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5180 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5181 return;
5182 }
5183
Cyril Bontéb21570a2009-11-29 20:04:48 +01005184 memcpy(t->sessid, buf, len);
5185 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005186 }
5187
5188 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5189 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5190 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5191 return;
5192 }
5193
Cyril Bontéb21570a2009-11-29 20:04:48 +01005194 memcpy(sessid_temp, buf, len);
5195 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005196
5197 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5198 /* free previously allocated memory */
5199 pool_free2(apools.sessid, sessid_temp);
5200
5201 if (asession != NULL) {
5202 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5203 if (!(t->be->options2 & PR_O2_AS_REQL))
5204 asession->request_count++;
5205
5206 if (asession->serverid != NULL) {
5207 struct server *srv = t->be->srv;
5208 while (srv) {
5209 if (strcmp(srv->id, asession->serverid) == 0) {
5210 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5211 /* we found the server and it's usable */
5212 txn->flags &= ~TX_CK_MASK;
5213 txn->flags |= TX_CK_VALID;
5214 t->flags |= SN_DIRECT | SN_ASSIGNED;
5215 t->srv = srv;
5216 break;
5217 } else {
5218 txn->flags &= ~TX_CK_MASK;
5219 txn->flags |= TX_CK_DOWN;
5220 }
5221 }
5222 srv = srv->next;
5223 }
5224 }
5225 }
5226}
5227
5228/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005229 * Manage client-side cookie. It can impact performance by about 2% so it is
5230 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005231 */
5232void manage_client_side_cookies(struct session *t, struct buffer *req)
5233{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005234 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005235 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005236 char *del_colon, *del_cookie, *colon;
5237 int app_cookies;
5238
Willy Tarreau58f10d72006-12-04 02:26:12 +01005239 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005240 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005241
Willy Tarreau2a324282006-12-05 00:05:46 +01005242 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005243 * we start with the start line.
5244 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005245 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005246 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005247
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005248 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005250 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005251
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005252 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005253 cur_ptr = cur_next;
5254 cur_end = cur_ptr + cur_hdr->len;
5255 cur_next = cur_end + cur_hdr->cr + 1;
5256
5257 /* We have one full header between cur_ptr and cur_end, and the
5258 * next header starts at cur_next. We're only interested in
5259 * "Cookie:" headers.
5260 */
5261
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005262 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5263 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005264 old_idx = cur_idx;
5265 continue;
5266 }
5267
5268 /* Now look for cookies. Conforming to RFC2109, we have to support
5269 * attributes whose name begin with a '$', and associate them with
5270 * the right cookie, if we want to delete this cookie.
5271 * So there are 3 cases for each cookie read :
5272 * 1) it's a special attribute, beginning with a '$' : ignore it.
5273 * 2) it's a server id cookie that we *MAY* want to delete : save
5274 * some pointers on it (last semi-colon, beginning of cookie...)
5275 * 3) it's an application cookie : we *MAY* have to delete a previous
5276 * "special" cookie.
5277 * At the end of loop, if a "special" cookie remains, we may have to
5278 * remove it. If no application cookie persists in the header, we
5279 * *MUST* delete it
5280 */
5281
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005282 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005283
Willy Tarreau58f10d72006-12-04 02:26:12 +01005284 /* del_cookie == NULL => nothing to be deleted */
5285 del_colon = del_cookie = NULL;
5286 app_cookies = 0;
5287
5288 while (p1 < cur_end) {
5289 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005290 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005291 while (p1 < cur_end) {
5292 if (*p1 == ';' || *p1 == ',')
5293 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005294 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005295 break;
5296 p1++;
5297 }
5298
5299 if (p1 == cur_end)
5300 break;
5301
5302 /* p1 is at the beginning of the cookie name */
5303 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005304 while (p2 < cur_end && *p2 != '=') {
5305 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5306 /* oops, the cookie name was truncated, resync */
5307 p1 = p2;
5308 goto resync_name;
5309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005310 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005311 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005312
5313 if (p2 == cur_end)
5314 break;
5315
5316 p3 = p2 + 1; /* skips the '=' sign */
5317 if (p3 == cur_end)
5318 break;
5319
Willy Tarreau305ae852010-01-03 19:45:54 +01005320 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5321 p5 = p4 = p3;
5322 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5323 if (!isspace((unsigned char)*p5))
5324 p4 = p5 + 1;
5325 p5++;
5326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005327
5328 /* here, we have the cookie name between p1 and p2,
5329 * and its value between p3 and p4.
5330 * we can process it :
5331 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005332 * Cookie: NAME=VALUE ;
5333 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005334 * | || || +--> p4
5335 * | || |+-------> p3
5336 * | || +--------> p2
5337 * | |+------------> p1
5338 * | +-------------> colon
5339 * +--------------------> cur_ptr
5340 */
5341
5342 if (*p1 == '$') {
5343 /* skip this one */
5344 }
5345 else {
5346 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005347 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005348 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005349 (p4 - p1 >= t->fe->capture_namelen) &&
5350 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005351 int log_len = p4 - p1;
5352
Willy Tarreau086b3b42007-05-13 21:45:51 +02005353 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005354 Alert("HTTP logging : out of memory.\n");
5355 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005356 if (log_len > t->fe->capture_len)
5357 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005358 memcpy(txn->cli_cookie, p1, log_len);
5359 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005360 }
5361 }
5362
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005363 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5364 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005365 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005366 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005367 char *delim;
5368
5369 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5370 * have the server ID betweek p3 and delim, and the original cookie between
5371 * delim+1 and p4. Otherwise, delim==p4 :
5372 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005373 * Cookie: NAME=SRV~VALUE ;
5374 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005375 * | || || | +--> p4
5376 * | || || +--------> delim
5377 * | || |+-----------> p3
5378 * | || +------------> p2
5379 * | |+----------------> p1
5380 * | +-----------------> colon
5381 * +------------------------> cur_ptr
5382 */
5383
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005384 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005385 for (delim = p3; delim < p4; delim++)
5386 if (*delim == COOKIE_DELIM)
5387 break;
5388 }
5389 else
5390 delim = p4;
5391
5392
5393 /* Here, we'll look for the first running server which supports the cookie.
5394 * This allows to share a same cookie between several servers, for example
5395 * to dedicate backup servers to specific servers only.
5396 * However, to prevent clients from sticking to cookie-less backup server
5397 * when they have incidentely learned an empty cookie, we simply ignore
5398 * empty cookies and mark them as invalid.
5399 */
5400 if (delim == p3)
5401 srv = NULL;
5402
5403 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005404 if (srv->cookie && (srv->cklen == delim - p3) &&
5405 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005406 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005407 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005408 txn->flags &= ~TX_CK_MASK;
5409 txn->flags |= TX_CK_VALID;
5410 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005411 t->srv = srv;
5412 break;
5413 } else {
5414 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005415 txn->flags &= ~TX_CK_MASK;
5416 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005417 }
5418 }
5419 srv = srv->next;
5420 }
5421
Willy Tarreau3d300592007-03-18 18:34:41 +01005422 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005423 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005424 txn->flags &= ~TX_CK_MASK;
5425 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005426 }
5427
5428 /* depending on the cookie mode, we may have to either :
5429 * - delete the complete cookie if we're in insert+indirect mode, so that
5430 * the server never sees it ;
5431 * - remove the server id from the cookie value, and tag the cookie as an
5432 * application cookie so that it does not get accidentely removed later,
5433 * if we're in cookie prefix mode
5434 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005435 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005436 int delta; /* negative */
5437
5438 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5439 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005440 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005441 cur_end += delta;
5442 cur_next += delta;
5443 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005444 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005445
5446 del_cookie = del_colon = NULL;
5447 app_cookies++; /* protect the header from deletion */
5448 }
5449 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005450 (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 +01005451 del_cookie = p1;
5452 del_colon = colon;
5453 }
5454 } else {
5455 /* now we know that we must keep this cookie since it's
5456 * not ours. But if we wanted to delete our cookie
5457 * earlier, we cannot remove the complete header, but we
5458 * can remove the previous block itself.
5459 */
5460 app_cookies++;
5461
5462 if (del_cookie != NULL) {
5463 int delta; /* negative */
5464
5465 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5466 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005467 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005468 cur_end += delta;
5469 cur_next += delta;
5470 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005471 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005472 del_cookie = del_colon = NULL;
5473 }
5474 }
5475
Cyril Bontéb21570a2009-11-29 20:04:48 +01005476 if (t->be->appsession_name != NULL) {
5477 int cmp_len, value_len;
5478 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005479
Cyril Bontéb21570a2009-11-29 20:04:48 +01005480 if (t->be->options2 & PR_O2_AS_PFX) {
5481 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5482 value_begin = p1 + t->be->appsession_name_len;
5483 value_len = p4 - p1 - t->be->appsession_name_len;
5484 } else {
5485 cmp_len = p2 - p1;
5486 value_begin = p3;
5487 value_len = p4 - p3;
5488 }
5489
5490 /* let's see if the cookie is our appcookie */
5491 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5492 /* Cool... it's the right one */
5493 manage_client_side_appsession(t, value_begin, value_len);
5494 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005495#if defined(DEBUG_HASH)
5496 Alert("manage_client_side_cookies\n");
5497 appsession_hash_dump(&(t->be->htbl_proxy));
5498#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005499 }/* end if ((t->proxy->appsession_name != NULL) ... */
5500 }
5501
5502 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005503 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005504 } /* while (p1 < cur_end) */
5505
5506 /* There's no more cookie on this line.
5507 * We may have marked the last one(s) for deletion.
5508 * We must do this now in two ways :
5509 * - if there is no app cookie, we simply delete the header ;
5510 * - if there are app cookies, we must delete the end of the
5511 * string properly, including the colon/semi-colon before
5512 * the cookie name.
5513 */
5514 if (del_cookie != NULL) {
5515 int delta;
5516 if (app_cookies) {
5517 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5518 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005519 cur_hdr->len += delta;
5520 } else {
5521 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005522
5523 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005524 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5525 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005526 cur_hdr->len = 0;
5527 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005528 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005529 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005530 }
5531
5532 /* keep the link from this header to next one */
5533 old_idx = cur_idx;
5534 } /* end of cookie processing on this header */
5535}
5536
5537
Willy Tarreaua15645d2007-03-18 16:22:39 +01005538/* Iterate the same filter through all response headers contained in <rtr>.
5539 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5540 */
5541int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5542{
5543 char term;
5544 char *cur_ptr, *cur_end, *cur_next;
5545 int cur_idx, old_idx, last_hdr;
5546 struct http_txn *txn = &t->txn;
5547 struct hdr_idx_elem *cur_hdr;
5548 int len, delta;
5549
5550 last_hdr = 0;
5551
Willy Tarreau962c3f42010-01-10 00:15:35 +01005552 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005553 old_idx = 0;
5554
5555 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005556 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005557 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005558 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005559 (exp->action == ACT_ALLOW ||
5560 exp->action == ACT_DENY))
5561 return 0;
5562
5563 cur_idx = txn->hdr_idx.v[old_idx].next;
5564 if (!cur_idx)
5565 break;
5566
5567 cur_hdr = &txn->hdr_idx.v[cur_idx];
5568 cur_ptr = cur_next;
5569 cur_end = cur_ptr + cur_hdr->len;
5570 cur_next = cur_end + cur_hdr->cr + 1;
5571
5572 /* Now we have one header between cur_ptr and cur_end,
5573 * and the next header starts at cur_next.
5574 */
5575
5576 /* The annoying part is that pattern matching needs
5577 * that we modify the contents to null-terminate all
5578 * strings before testing them.
5579 */
5580
5581 term = *cur_end;
5582 *cur_end = '\0';
5583
5584 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5585 switch (exp->action) {
5586 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005587 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005588 last_hdr = 1;
5589 break;
5590
5591 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005592 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005593 last_hdr = 1;
5594 break;
5595
5596 case ACT_REPLACE:
5597 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5598 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5599 /* FIXME: if the user adds a newline in the replacement, the
5600 * index will not be recalculated for now, and the new line
5601 * will not be counted as a new header.
5602 */
5603
5604 cur_end += delta;
5605 cur_next += delta;
5606 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005607 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005608 break;
5609
5610 case ACT_REMOVE:
5611 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5612 cur_next += delta;
5613
Willy Tarreaufa355d42009-11-29 18:12:29 +01005614 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005615 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5616 txn->hdr_idx.used--;
5617 cur_hdr->len = 0;
5618 cur_end = NULL; /* null-term has been rewritten */
5619 break;
5620
5621 }
5622 }
5623 if (cur_end)
5624 *cur_end = term; /* restore the string terminator */
5625
5626 /* keep the link from this header to next one in case of later
5627 * removal of next header.
5628 */
5629 old_idx = cur_idx;
5630 }
5631 return 0;
5632}
5633
5634
5635/* Apply the filter to the status line in the response buffer <rtr>.
5636 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5637 * or -1 if a replacement resulted in an invalid status line.
5638 */
5639int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5640{
5641 char term;
5642 char *cur_ptr, *cur_end;
5643 int done;
5644 struct http_txn *txn = &t->txn;
5645 int len, delta;
5646
5647
Willy Tarreau3d300592007-03-18 18:34:41 +01005648 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005649 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005650 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005651 (exp->action == ACT_ALLOW ||
5652 exp->action == ACT_DENY))
5653 return 0;
5654 else if (exp->action == ACT_REMOVE)
5655 return 0;
5656
5657 done = 0;
5658
Willy Tarreau962c3f42010-01-10 00:15:35 +01005659 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005660 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5661
5662 /* Now we have the status line between cur_ptr and cur_end */
5663
5664 /* The annoying part is that pattern matching needs
5665 * that we modify the contents to null-terminate all
5666 * strings before testing them.
5667 */
5668
5669 term = *cur_end;
5670 *cur_end = '\0';
5671
5672 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5673 switch (exp->action) {
5674 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005675 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005676 done = 1;
5677 break;
5678
5679 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005680 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005681 done = 1;
5682 break;
5683
5684 case ACT_REPLACE:
5685 *cur_end = term; /* restore the string terminator */
5686 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5687 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5688 /* FIXME: if the user adds a newline in the replacement, the
5689 * index will not be recalculated for now, and the new line
5690 * will not be counted as a new header.
5691 */
5692
Willy Tarreaufa355d42009-11-29 18:12:29 +01005693 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005695 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005696 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005697 cur_ptr, cur_end + 1,
5698 NULL, NULL);
5699 if (unlikely(!cur_end))
5700 return -1;
5701
5702 /* we have a full respnse and we know that we have either a CR
5703 * or an LF at <ptr>.
5704 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005705 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005706 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5707 /* there is no point trying this regex on headers */
5708 return 1;
5709 }
5710 }
5711 *cur_end = term; /* restore the string terminator */
5712 return done;
5713}
5714
5715
5716
5717/*
5718 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5719 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5720 * unparsable response.
5721 */
5722int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5723{
Willy Tarreau3d300592007-03-18 18:34:41 +01005724 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005725 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005726 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005727 int ret;
5728
5729 /*
5730 * The interleaving of transformations and verdicts
5731 * makes it difficult to decide to continue or stop
5732 * the evaluation.
5733 */
5734
Willy Tarreau3d300592007-03-18 18:34:41 +01005735 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005736 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5737 exp->action == ACT_PASS)) {
5738 exp = exp->next;
5739 continue;
5740 }
5741
5742 /* Apply the filter to the status line. */
5743 ret = apply_filter_to_sts_line(t, rtr, exp);
5744 if (unlikely(ret < 0))
5745 return -1;
5746
5747 if (likely(ret == 0)) {
5748 /* The filter did not match the response, it can be
5749 * iterated through all headers.
5750 */
5751 apply_filter_to_resp_headers(t, rtr, exp);
5752 }
5753 exp = exp->next;
5754 }
5755 return 0;
5756}
5757
5758
5759
5760/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005761 * Manage server-side cookies. It can impact performance by about 2% so it is
5762 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005763 */
5764void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5765{
5766 struct http_txn *txn = &t->txn;
5767 char *p1, *p2, *p3, *p4;
5768
Willy Tarreaua15645d2007-03-18 16:22:39 +01005769 char *cur_ptr, *cur_end, *cur_next;
5770 int cur_idx, old_idx, delta;
5771
Willy Tarreaua15645d2007-03-18 16:22:39 +01005772 /* Iterate through the headers.
5773 * we start with the start line.
5774 */
5775 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005776 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005777
5778 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5779 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005780 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005781
5782 cur_hdr = &txn->hdr_idx.v[cur_idx];
5783 cur_ptr = cur_next;
5784 cur_end = cur_ptr + cur_hdr->len;
5785 cur_next = cur_end + cur_hdr->cr + 1;
5786
5787 /* We have one full header between cur_ptr and cur_end, and the
5788 * next header starts at cur_next. We're only interested in
5789 * "Cookie:" headers.
5790 */
5791
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005792 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5793 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 old_idx = cur_idx;
5795 continue;
5796 }
5797
5798 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005799 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005800
5801
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005802 /* maybe we only wanted to see if there was a set-cookie. Note that
5803 * the cookie capture is declared in the fronend.
5804 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005805 if (t->be->cookie_name == NULL &&
5806 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005807 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005808 return;
5809
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005810 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005811
5812 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005813 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5814 break;
5815
5816 /* p1 is at the beginning of the cookie name */
5817 p2 = p1;
5818
5819 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5820 p2++;
5821
5822 if (p2 == cur_end || *p2 == ';') /* next cookie */
5823 break;
5824
5825 p3 = p2 + 1; /* skip the '=' sign */
5826 if (p3 == cur_end)
5827 break;
5828
5829 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005830 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005831 p4++;
5832
5833 /* here, we have the cookie name between p1 and p2,
5834 * and its value between p3 and p4.
5835 * we can process it.
5836 */
5837
5838 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005839 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005840 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005841 (p4 - p1 >= t->fe->capture_namelen) &&
5842 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005843 int log_len = p4 - p1;
5844
Willy Tarreau086b3b42007-05-13 21:45:51 +02005845 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005846 Alert("HTTP logging : out of memory.\n");
5847 }
5848
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005849 if (log_len > t->fe->capture_len)
5850 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005851 memcpy(txn->srv_cookie, p1, log_len);
5852 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005853 }
5854
5855 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005856 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5857 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005858 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005859 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005860
5861 /* If the cookie is in insert mode on a known server, we'll delete
5862 * this occurrence because we'll insert another one later.
5863 * We'll delete it too if the "indirect" option is set and we're in
5864 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005865 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5866 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005867 /* this header must be deleted */
5868 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5869 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5870 txn->hdr_idx.used--;
5871 cur_hdr->len = 0;
5872 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005873 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005874
Willy Tarreau3d300592007-03-18 18:34:41 +01005875 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005876 }
5877 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005878 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879 /* replace bytes p3->p4 with the cookie name associated
5880 * with this server since we know it.
5881 */
5882 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5883 cur_hdr->len += delta;
5884 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005885 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005886
Willy Tarreau3d300592007-03-18 18:34:41 +01005887 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005888 }
5889 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005890 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891 /* insert the cookie name associated with this server
5892 * before existing cookie, and insert a delimitor between them..
5893 */
5894 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5895 cur_hdr->len += delta;
5896 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005897 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005898
5899 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005900 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005901 }
5902 }
5903 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005904 else if (t->be->appsession_name != NULL) {
5905 int cmp_len, value_len;
5906 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005907
Cyril Bontéb21570a2009-11-29 20:04:48 +01005908 if (t->be->options2 & PR_O2_AS_PFX) {
5909 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5910 value_begin = p1 + t->be->appsession_name_len;
5911 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5912 } else {
5913 cmp_len = p2 - p1;
5914 value_begin = p3;
5915 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005916 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005917
5918 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5919 /* Cool... it's the right one */
5920 if (t->sessid != NULL) {
5921 /* free previously allocated memory as we don't need it anymore */
5922 pool_free2(apools.sessid, t->sessid);
5923 }
5924 /* Store the sessid in the session for future use */
5925 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5926 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5927 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5928 return;
5929 }
5930 memcpy(t->sessid, value_begin, value_len);
5931 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005932 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005933 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005934 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5935 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005936 /* keep the link from this header to next one */
5937 old_idx = cur_idx;
5938 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005939
5940 if (t->sessid != NULL) {
5941 appsess *asession = NULL;
5942 /* only do insert, if lookup fails */
5943 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5944 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01005945 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005946 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5947 Alert("Not enough Memory process_srv():asession:calloc().\n");
5948 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5949 return;
5950 }
5951 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5952 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5953 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005954 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005955 return;
5956 }
5957 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5958 asession->sessid[t->be->appsession_len] = 0;
5959
Willy Tarreau1fac7532010-01-09 19:23:06 +01005960 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005961 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5962 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5963 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005964 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005965 return;
5966 }
5967 asession->serverid[0] = '\0';
5968 memcpy(asession->serverid, t->srv->id, server_id_len);
5969
5970 asession->request_count = 0;
5971 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5972 }
5973
5974 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5975 asession->request_count++;
5976 }
5977
5978#if defined(DEBUG_HASH)
5979 Alert("manage_server_side_cookies\n");
5980 appsession_hash_dump(&(t->be->htbl_proxy));
5981#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005982}
5983
5984
5985
5986/*
5987 * Check if response is cacheable or not. Updates t->flags.
5988 */
5989void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5990{
5991 struct http_txn *txn = &t->txn;
5992 char *p1, *p2;
5993
5994 char *cur_ptr, *cur_end, *cur_next;
5995 int cur_idx;
5996
Willy Tarreau5df51872007-11-25 16:20:08 +01005997 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005998 return;
5999
6000 /* Iterate through the headers.
6001 * we start with the start line.
6002 */
6003 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006004 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006005
6006 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6007 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006008 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006009
6010 cur_hdr = &txn->hdr_idx.v[cur_idx];
6011 cur_ptr = cur_next;
6012 cur_end = cur_ptr + cur_hdr->len;
6013 cur_next = cur_end + cur_hdr->cr + 1;
6014
6015 /* We have one full header between cur_ptr and cur_end, and the
6016 * next header starts at cur_next. We're only interested in
6017 * "Cookie:" headers.
6018 */
6019
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006020 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6021 if (val) {
6022 if ((cur_end - (cur_ptr + val) >= 8) &&
6023 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6024 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6025 return;
6026 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006027 }
6028
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006029 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6030 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006031 continue;
6032
6033 /* OK, right now we know we have a cache-control header at cur_ptr */
6034
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006035 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006036
6037 if (p1 >= cur_end) /* no more info */
6038 continue;
6039
6040 /* p1 is at the beginning of the value */
6041 p2 = p1;
6042
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006043 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006044 p2++;
6045
6046 /* we have a complete value between p1 and p2 */
6047 if (p2 < cur_end && *p2 == '=') {
6048 /* we have something of the form no-cache="set-cookie" */
6049 if ((cur_end - p1 >= 21) &&
6050 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6051 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006052 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006053 continue;
6054 }
6055
6056 /* OK, so we know that either p2 points to the end of string or to a comma */
6057 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6058 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6059 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6060 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006061 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006062 return;
6063 }
6064
6065 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006066 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006067 continue;
6068 }
6069 }
6070}
6071
6072
Willy Tarreau58f10d72006-12-04 02:26:12 +01006073/*
6074 * Try to retrieve a known appsession in the URI, then the associated server.
6075 * If the server is found, it's assigned to the session.
6076 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006079 char *end_params, *first_param, *cur_param, *next_param;
6080 char separator;
6081 int value_len;
6082
6083 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006085 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006086 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006088 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089
Cyril Bontéb21570a2009-11-29 20:04:48 +01006090 first_param = NULL;
6091 switch (mode) {
6092 case PR_O2_AS_M_PP:
6093 first_param = memchr(begin, ';', len);
6094 break;
6095 case PR_O2_AS_M_QS:
6096 first_param = memchr(begin, '?', len);
6097 break;
6098 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006099
Cyril Bontéb21570a2009-11-29 20:04:48 +01006100 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006101 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103
Cyril Bontéb21570a2009-11-29 20:04:48 +01006104 switch (mode) {
6105 case PR_O2_AS_M_PP:
6106 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6107 end_params = (char *) begin + len;
6108 }
6109 separator = ';';
6110 break;
6111 case PR_O2_AS_M_QS:
6112 end_params = (char *) begin + len;
6113 separator = '&';
6114 break;
6115 default:
6116 /* unknown mode, shouldn't happen */
6117 return;
6118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006119
Cyril Bontéb21570a2009-11-29 20:04:48 +01006120 cur_param = next_param = end_params;
6121 while (cur_param > first_param) {
6122 cur_param--;
6123 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6124 /* let's see if this is the appsession parameter */
6125 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6126 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6127 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6128 /* Cool... it's the right one */
6129 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6130 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6131 if (value_len > 0) {
6132 manage_client_side_appsession(t, cur_param, value_len);
6133 }
6134 break;
6135 }
6136 next_param = cur_param;
6137 }
6138 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006139#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006140 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006141 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006142#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006143}
6144
6145
Willy Tarreaub2513902006-12-17 14:52:38 +01006146/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006147 * In a GET or HEAD request, check if the requested URI matches the stats uri
6148 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006149 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006150 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006151 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006152 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006153 *
6154 * Returns 1 if the session's state changes, otherwise 0.
6155 */
6156int stats_check_uri_auth(struct session *t, struct proxy *backend)
6157{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006158 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006159 struct uri_auth *uri_auth = backend->uri_auth;
6160 struct user_auth *user;
6161 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006162 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006163
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006164 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006166 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006167 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006168 return 0;
6169
Willy Tarreau962c3f42010-01-10 00:15:35 +01006170 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006171
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006172 /* the URI is in h */
6173 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006174 return 0;
6175
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006176 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006177 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006178 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006179 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006180 break;
6181 }
6182 h++;
6183 }
6184
6185 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006186 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6187 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006188 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006189 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006190 break;
6191 }
6192 h++;
6193 }
6194 }
6195
Willy Tarreau962c3f42010-01-10 00:15:35 +01006196 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6197 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006198 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006199 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006200 break;
6201 }
6202 h++;
6203 }
6204
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006205 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6206
Willy Tarreaub2513902006-12-17 14:52:38 +01006207 /* we are in front of a interceptable URI. Let's check
6208 * if there's an authentication and if it's valid.
6209 */
6210 user = uri_auth->users;
6211 if (!user) {
6212 /* no user auth required, it's OK */
6213 authenticated = 1;
6214 } else {
6215 authenticated = 0;
6216
6217 /* a user list is defined, we have to check.
6218 * skip 21 chars for "Authorization: Basic ".
6219 */
6220
6221 /* FIXME: this should move to an earlier place */
6222 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006223 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006224 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6225 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006226 if (len > 14 &&
6227 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006228 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006229 break;
6230 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006231 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006232 }
6233
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006234 if (txn->auth_hdr.len < 21 ||
6235 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006236 user = NULL;
6237
6238 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006239 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6240 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006241 user->user_pwd, user->user_len)) {
6242 authenticated = 1;
6243 break;
6244 }
6245 user = user->next;
6246 }
6247 }
6248
6249 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006250 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006251
6252 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006253 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6254 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006255 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006256 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006257 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006258 if (!(t->flags & SN_ERR_MASK))
6259 t->flags |= SN_ERR_PRXCOND;
6260 if (!(t->flags & SN_FINST_MASK))
6261 t->flags |= SN_FINST_R;
6262 return 1;
6263 }
6264
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006265 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006266 * data.
6267 */
Willy Tarreau70089872008-06-13 21:12:51 +02006268 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006269 t->data_source = DATA_SRC_STATS;
6270 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006271 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006272 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6273 t->rep->prod->private = t;
6274 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006275 return 1;
6276}
6277
Willy Tarreau4076a152009-04-02 15:18:36 +02006278/*
6279 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006280 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6281 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006282 */
6283void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6284 struct buffer *buf, struct http_msg *msg,
6285 struct proxy *other_end)
6286{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006287 es->len = buf->r - (buf->data + msg->som);
6288 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006289 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006290 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006291 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006292 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006293 es->when = date; // user-visible date
6294 es->sid = s->uniq_id;
6295 es->srv = s->srv;
6296 es->oe = other_end;
6297 es->src = s->cli_addr;
6298}
Willy Tarreaub2513902006-12-17 14:52:38 +01006299
Willy Tarreaubaaee002006-06-26 02:48:02 +02006300/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301 * Print a debug line with a header
6302 */
6303void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6304{
6305 int len, max;
6306 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006307 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308 max = end - start;
6309 UBOUND(max, sizeof(trash) - len - 1);
6310 len += strlcpy2(trash + len, start, max + 1);
6311 trash[len++] = '\n';
6312 write(1, trash, len);
6313}
6314
Willy Tarreau0937bc42009-12-22 15:03:09 +01006315/*
6316 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6317 * the required fields are properly allocated and that we only need to (re)init
6318 * them. This should be used before processing any new request.
6319 */
6320void http_init_txn(struct session *s)
6321{
6322 struct http_txn *txn = &s->txn;
6323 struct proxy *fe = s->fe;
6324
6325 txn->flags = 0;
6326 txn->status = -1;
6327
6328 txn->req.sol = txn->req.eol = NULL;
6329 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6330 txn->rsp.sol = txn->rsp.eol = NULL;
6331 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6332 txn->req.hdr_content_len = 0LL;
6333 txn->rsp.hdr_content_len = 0LL;
6334 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6335 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6336 chunk_reset(&txn->auth_hdr);
6337
6338 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6339 if (fe->options2 & PR_O2_REQBUG_OK)
6340 txn->req.err_pos = -1; /* let buggy requests pass */
6341
Willy Tarreau46023632010-01-07 22:51:47 +01006342 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006343 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6344
Willy Tarreau46023632010-01-07 22:51:47 +01006345 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006346 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6347
6348 if (txn->hdr_idx.v)
6349 hdr_idx_init(&txn->hdr_idx);
6350}
6351
6352/* to be used at the end of a transaction */
6353void http_end_txn(struct session *s)
6354{
6355 struct http_txn *txn = &s->txn;
6356
6357 /* these ones will have been dynamically allocated */
6358 pool_free2(pool2_requri, txn->uri);
6359 pool_free2(pool2_capture, txn->cli_cookie);
6360 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau762a2362010-01-09 13:57:26 +01006361 pool_free2(apools.sessid, s->sessid);
6362 s->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006363 txn->uri = NULL;
6364 txn->srv_cookie = NULL;
6365 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006366
6367 if (txn->req.cap) {
6368 struct cap_hdr *h;
6369 for (h = s->fe->req_cap; h; h = h->next)
6370 pool_free2(h->pool, txn->req.cap[h->index]);
6371 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6372 }
6373
6374 if (txn->rsp.cap) {
6375 struct cap_hdr *h;
6376 for (h = s->fe->rsp_cap; h; h = h->next)
6377 pool_free2(h->pool, txn->rsp.cap[h->index]);
6378 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6379 }
6380
Willy Tarreau0937bc42009-12-22 15:03:09 +01006381}
6382
6383/* to be used at the end of a transaction to prepare a new one */
6384void http_reset_txn(struct session *s)
6385{
6386 http_end_txn(s);
6387 http_init_txn(s);
6388
6389 s->be = s->fe;
6390 s->req->analysers = s->listener->analysers;
6391 s->logs.logwait = s->fe->to_log;
6392 s->srv = s->prev_srv = s->srv_conn = NULL;
6393 s->pend_pos = NULL;
6394 s->conn_retries = s->be->conn_retries;
6395
6396 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6397
6398 s->req->rto = s->fe->timeout.client;
6399 s->req->wto = s->be->timeout.server;
6400 s->req->cto = s->be->timeout.connect;
6401
6402 s->rep->rto = s->be->timeout.server;
6403 s->rep->wto = s->fe->timeout.client;
6404 s->rep->cto = TICK_ETERNITY;
6405
6406 s->req->rex = TICK_ETERNITY;
6407 s->req->wex = TICK_ETERNITY;
6408 s->req->analyse_exp = TICK_ETERNITY;
6409 s->rep->rex = TICK_ETERNITY;
6410 s->rep->wex = TICK_ETERNITY;
6411 s->rep->analyse_exp = TICK_ETERNITY;
6412}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006413
Willy Tarreau8797c062007-05-07 00:55:35 +02006414/************************************************************************/
6415/* The code below is dedicated to ACL parsing and matching */
6416/************************************************************************/
6417
6418
6419
6420
6421/* 1. Check on METHOD
6422 * We use the pre-parsed method if it is known, and store its number as an
6423 * integer. If it is unknown, we use the pointer and the length.
6424 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006425static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006426{
6427 int len, meth;
6428
Willy Tarreauae8b7962007-06-09 23:10:04 +02006429 len = strlen(*text);
6430 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006431
6432 pattern->val.i = meth;
6433 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006434 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006435 if (!pattern->ptr.str)
6436 return 0;
6437 pattern->len = len;
6438 }
6439 return 1;
6440}
6441
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006442static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006443acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6444 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006445{
6446 int meth;
6447 struct http_txn *txn = l7;
6448
Willy Tarreaub6866442008-07-14 23:54:42 +02006449 if (!txn)
6450 return 0;
6451
Willy Tarreau655dce92009-11-08 13:10:58 +01006452 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006453 return 0;
6454
Willy Tarreau8797c062007-05-07 00:55:35 +02006455 meth = txn->meth;
6456 test->i = meth;
6457 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006458 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6459 /* ensure the indexes are not affected */
6460 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006461 test->len = txn->req.sl.rq.m_l;
6462 test->ptr = txn->req.sol;
6463 }
6464 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6465 return 1;
6466}
6467
6468static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6469{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006470 int icase;
6471
Willy Tarreau8797c062007-05-07 00:55:35 +02006472 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006473 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006474
6475 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006476 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006477
6478 /* Other method, we must compare the strings */
6479 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006480 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006481
6482 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6483 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6484 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006485 return ACL_PAT_FAIL;
6486 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006487}
6488
6489/* 2. Check on Request/Status Version
6490 * We simply compare strings here.
6491 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006492static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006493{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006494 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006495 if (!pattern->ptr.str)
6496 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006497 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006498 return 1;
6499}
6500
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006501static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006502acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6503 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006504{
6505 struct http_txn *txn = l7;
6506 char *ptr;
6507 int len;
6508
Willy Tarreaub6866442008-07-14 23:54:42 +02006509 if (!txn)
6510 return 0;
6511
Willy Tarreau655dce92009-11-08 13:10:58 +01006512 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006513 return 0;
6514
Willy Tarreau8797c062007-05-07 00:55:35 +02006515 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006516 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006517
6518 while ((len-- > 0) && (*ptr++ != '/'));
6519 if (len <= 0)
6520 return 0;
6521
6522 test->ptr = ptr;
6523 test->len = len;
6524
6525 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6526 return 1;
6527}
6528
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006529static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006530acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6531 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006532{
6533 struct http_txn *txn = l7;
6534 char *ptr;
6535 int len;
6536
Willy Tarreaub6866442008-07-14 23:54:42 +02006537 if (!txn)
6538 return 0;
6539
Willy Tarreau655dce92009-11-08 13:10:58 +01006540 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006541 return 0;
6542
Willy Tarreau8797c062007-05-07 00:55:35 +02006543 len = txn->rsp.sl.st.v_l;
6544 ptr = txn->rsp.sol;
6545
6546 while ((len-- > 0) && (*ptr++ != '/'));
6547 if (len <= 0)
6548 return 0;
6549
6550 test->ptr = ptr;
6551 test->len = len;
6552
6553 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6554 return 1;
6555}
6556
6557/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006558static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006559acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6560 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006561{
6562 struct http_txn *txn = l7;
6563 char *ptr;
6564 int len;
6565
Willy Tarreaub6866442008-07-14 23:54:42 +02006566 if (!txn)
6567 return 0;
6568
Willy Tarreau655dce92009-11-08 13:10:58 +01006569 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006570 return 0;
6571
Willy Tarreau8797c062007-05-07 00:55:35 +02006572 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006573 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006574
6575 test->i = __strl2ui(ptr, len);
6576 test->flags = ACL_TEST_F_VOL_1ST;
6577 return 1;
6578}
6579
6580/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006581static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006582acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6583 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006584{
6585 struct http_txn *txn = l7;
6586
Willy Tarreaub6866442008-07-14 23:54:42 +02006587 if (!txn)
6588 return 0;
6589
Willy Tarreau655dce92009-11-08 13:10:58 +01006590 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006591 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006592
Willy Tarreauc11416f2007-06-17 16:58:38 +02006593 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6594 /* ensure the indexes are not affected */
6595 return 0;
6596
Willy Tarreau8797c062007-05-07 00:55:35 +02006597 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006598 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006599
Willy Tarreauf3d25982007-05-08 22:45:09 +02006600 /* we do not need to set READ_ONLY because the data is in a buffer */
6601 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006602 return 1;
6603}
6604
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006605static int
6606acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6607 struct acl_expr *expr, struct acl_test *test)
6608{
6609 struct http_txn *txn = l7;
6610
Willy Tarreaub6866442008-07-14 23:54:42 +02006611 if (!txn)
6612 return 0;
6613
Willy Tarreau655dce92009-11-08 13:10:58 +01006614 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006615 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006616
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006617 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6618 /* ensure the indexes are not affected */
6619 return 0;
6620
6621 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006622 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 +01006623 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6624 test->i = AF_INET;
6625
6626 /*
6627 * If we are parsing url in frontend space, we prepare backend stage
6628 * to not parse again the same url ! optimization lazyness...
6629 */
6630 if (px->options & PR_O_HTTP_PROXY)
6631 l4->flags |= SN_ADDR_SET;
6632
6633 test->flags = ACL_TEST_F_READ_ONLY;
6634 return 1;
6635}
6636
6637static int
6638acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6639 struct acl_expr *expr, struct acl_test *test)
6640{
6641 struct http_txn *txn = l7;
6642
Willy Tarreaub6866442008-07-14 23:54:42 +02006643 if (!txn)
6644 return 0;
6645
Willy Tarreau655dce92009-11-08 13:10:58 +01006646 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006647 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006648
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006649 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6650 /* ensure the indexes are not affected */
6651 return 0;
6652
6653 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006654 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 +01006655 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6656
6657 if (px->options & PR_O_HTTP_PROXY)
6658 l4->flags |= SN_ADDR_SET;
6659
6660 test->flags = ACL_TEST_F_READ_ONLY;
6661 return 1;
6662}
6663
Willy Tarreauc11416f2007-06-17 16:58:38 +02006664/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6665 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6666 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006667static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006668acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006669 struct acl_expr *expr, struct acl_test *test)
6670{
6671 struct http_txn *txn = l7;
6672 struct hdr_idx *idx = &txn->hdr_idx;
6673 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006674
Willy Tarreaub6866442008-07-14 23:54:42 +02006675 if (!txn)
6676 return 0;
6677
Willy Tarreau33a7e692007-06-10 19:45:56 +02006678 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6679 /* search for header from the beginning */
6680 ctx->idx = 0;
6681
Willy Tarreau33a7e692007-06-10 19:45:56 +02006682 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6683 test->flags |= ACL_TEST_F_FETCH_MORE;
6684 test->flags |= ACL_TEST_F_VOL_HDR;
6685 test->len = ctx->vlen;
6686 test->ptr = (char *)ctx->line + ctx->val;
6687 return 1;
6688 }
6689
6690 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6691 test->flags |= ACL_TEST_F_VOL_HDR;
6692 return 0;
6693}
6694
Willy Tarreau33a7e692007-06-10 19:45:56 +02006695static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006696acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6697 struct acl_expr *expr, struct acl_test *test)
6698{
6699 struct http_txn *txn = l7;
6700
Willy Tarreaub6866442008-07-14 23:54:42 +02006701 if (!txn)
6702 return 0;
6703
Willy Tarreau655dce92009-11-08 13:10:58 +01006704 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006705 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006706
Willy Tarreauc11416f2007-06-17 16:58:38 +02006707 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6708 /* ensure the indexes are not affected */
6709 return 0;
6710
6711 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6712}
6713
6714static int
6715acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6716 struct acl_expr *expr, struct acl_test *test)
6717{
6718 struct http_txn *txn = l7;
6719
Willy Tarreaub6866442008-07-14 23:54:42 +02006720 if (!txn)
6721 return 0;
6722
Willy Tarreau655dce92009-11-08 13:10:58 +01006723 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006724 return 0;
6725
6726 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6727}
6728
6729/* 6. Check on HTTP header count. The number of occurrences is returned.
6730 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6731 */
6732static int
6733acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006734 struct acl_expr *expr, struct acl_test *test)
6735{
6736 struct http_txn *txn = l7;
6737 struct hdr_idx *idx = &txn->hdr_idx;
6738 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006739 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006740
Willy Tarreaub6866442008-07-14 23:54:42 +02006741 if (!txn)
6742 return 0;
6743
Willy Tarreau33a7e692007-06-10 19:45:56 +02006744 ctx.idx = 0;
6745 cnt = 0;
6746 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6747 cnt++;
6748
6749 test->i = cnt;
6750 test->flags = ACL_TEST_F_VOL_HDR;
6751 return 1;
6752}
6753
Willy Tarreauc11416f2007-06-17 16:58:38 +02006754static int
6755acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6756 struct acl_expr *expr, struct acl_test *test)
6757{
6758 struct http_txn *txn = l7;
6759
Willy Tarreaub6866442008-07-14 23:54:42 +02006760 if (!txn)
6761 return 0;
6762
Willy Tarreau655dce92009-11-08 13:10:58 +01006763 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006764 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006765
Willy Tarreauc11416f2007-06-17 16:58:38 +02006766 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6767 /* ensure the indexes are not affected */
6768 return 0;
6769
6770 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6771}
6772
6773static int
6774acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6775 struct acl_expr *expr, struct acl_test *test)
6776{
6777 struct http_txn *txn = l7;
6778
Willy Tarreaub6866442008-07-14 23:54:42 +02006779 if (!txn)
6780 return 0;
6781
Willy Tarreau655dce92009-11-08 13:10:58 +01006782 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006783 return 0;
6784
6785 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6786}
6787
Willy Tarreau33a7e692007-06-10 19:45:56 +02006788/* 7. Check on HTTP header's integer value. The integer value is returned.
6789 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006790 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006791 */
6792static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006793acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006794 struct acl_expr *expr, struct acl_test *test)
6795{
6796 struct http_txn *txn = l7;
6797 struct hdr_idx *idx = &txn->hdr_idx;
6798 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006799
Willy Tarreaub6866442008-07-14 23:54:42 +02006800 if (!txn)
6801 return 0;
6802
Willy Tarreau33a7e692007-06-10 19:45:56 +02006803 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6804 /* search for header from the beginning */
6805 ctx->idx = 0;
6806
Willy Tarreau33a7e692007-06-10 19:45:56 +02006807 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6808 test->flags |= ACL_TEST_F_FETCH_MORE;
6809 test->flags |= ACL_TEST_F_VOL_HDR;
6810 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6811 return 1;
6812 }
6813
6814 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6815 test->flags |= ACL_TEST_F_VOL_HDR;
6816 return 0;
6817}
6818
Willy Tarreauc11416f2007-06-17 16:58:38 +02006819static int
6820acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6821 struct acl_expr *expr, struct acl_test *test)
6822{
6823 struct http_txn *txn = l7;
6824
Willy Tarreaub6866442008-07-14 23:54:42 +02006825 if (!txn)
6826 return 0;
6827
Willy Tarreau655dce92009-11-08 13:10:58 +01006828 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006829 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006830
Willy Tarreauc11416f2007-06-17 16:58:38 +02006831 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6832 /* ensure the indexes are not affected */
6833 return 0;
6834
6835 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6836}
6837
6838static int
6839acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6840 struct acl_expr *expr, struct acl_test *test)
6841{
6842 struct http_txn *txn = l7;
6843
Willy Tarreaub6866442008-07-14 23:54:42 +02006844 if (!txn)
6845 return 0;
6846
Willy Tarreau655dce92009-11-08 13:10:58 +01006847 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006848 return 0;
6849
6850 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6851}
6852
Willy Tarreau106f9792009-09-19 07:54:16 +02006853/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6854 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6855 */
6856static int
6857acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6858 struct acl_expr *expr, struct acl_test *test)
6859{
6860 struct http_txn *txn = l7;
6861 struct hdr_idx *idx = &txn->hdr_idx;
6862 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6863
6864 if (!txn)
6865 return 0;
6866
6867 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6868 /* search for header from the beginning */
6869 ctx->idx = 0;
6870
6871 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6872 test->flags |= ACL_TEST_F_FETCH_MORE;
6873 test->flags |= ACL_TEST_F_VOL_HDR;
6874 /* Same optimization as url_ip */
6875 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6876 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6877 test->ptr = (void *)&l4->srv_addr.sin_addr;
6878 test->i = AF_INET;
6879 return 1;
6880 }
6881
6882 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6883 test->flags |= ACL_TEST_F_VOL_HDR;
6884 return 0;
6885}
6886
6887static int
6888acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6889 struct acl_expr *expr, struct acl_test *test)
6890{
6891 struct http_txn *txn = l7;
6892
6893 if (!txn)
6894 return 0;
6895
Willy Tarreau655dce92009-11-08 13:10:58 +01006896 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006897 return 0;
6898
6899 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6900 /* ensure the indexes are not affected */
6901 return 0;
6902
6903 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6904}
6905
6906static int
6907acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6908 struct acl_expr *expr, struct acl_test *test)
6909{
6910 struct http_txn *txn = l7;
6911
6912 if (!txn)
6913 return 0;
6914
Willy Tarreau655dce92009-11-08 13:10:58 +01006915 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006916 return 0;
6917
6918 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6919}
6920
Willy Tarreau737b0c12007-06-10 21:28:46 +02006921/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6922 * the first '/' after the possible hostname, and ends before the possible '?'.
6923 */
6924static int
6925acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6926 struct acl_expr *expr, struct acl_test *test)
6927{
6928 struct http_txn *txn = l7;
6929 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006930
Willy Tarreaub6866442008-07-14 23:54:42 +02006931 if (!txn)
6932 return 0;
6933
Willy Tarreau655dce92009-11-08 13:10:58 +01006934 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006935 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006936
Willy Tarreauc11416f2007-06-17 16:58:38 +02006937 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6938 /* ensure the indexes are not affected */
6939 return 0;
6940
Willy Tarreau962c3f42010-01-10 00:15:35 +01006941 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006942 ptr = http_get_path(txn);
6943 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006944 return 0;
6945
6946 /* OK, we got the '/' ! */
6947 test->ptr = ptr;
6948
6949 while (ptr < end && *ptr != '?')
6950 ptr++;
6951
6952 test->len = ptr - test->ptr;
6953
6954 /* we do not need to set READ_ONLY because the data is in a buffer */
6955 test->flags = ACL_TEST_F_VOL_1ST;
6956 return 1;
6957}
6958
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006959static int
6960acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6961 struct acl_expr *expr, struct acl_test *test)
6962{
6963 struct buffer *req = s->req;
6964 struct http_txn *txn = &s->txn;
6965 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006966
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006967 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6968 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6969 */
6970
6971 if (!s || !req)
6972 return 0;
6973
Willy Tarreau655dce92009-11-08 13:10:58 +01006974 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006975 /* Already decoded as OK */
6976 test->flags |= ACL_TEST_F_SET_RES_PASS;
6977 return 1;
6978 }
6979
6980 /* Try to decode HTTP request */
6981 if (likely(req->lr < req->r))
6982 http_msg_analyzer(req, msg, &txn->hdr_idx);
6983
Willy Tarreau655dce92009-11-08 13:10:58 +01006984 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006985 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6986 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6987 return 1;
6988 }
6989 /* wait for final state */
6990 test->flags |= ACL_TEST_F_MAY_CHANGE;
6991 return 0;
6992 }
6993
6994 /* OK we got a valid HTTP request. We have some minor preparation to
6995 * perform so that further checks can rely on HTTP tests.
6996 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006997 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006998 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6999 s->flags |= SN_REDIRECTABLE;
7000
7001 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7002 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7003 return 1;
7004 }
7005
7006 test->flags |= ACL_TEST_F_SET_RES_PASS;
7007 return 1;
7008}
7009
Willy Tarreau8797c062007-05-07 00:55:35 +02007010
7011/************************************************************************/
7012/* All supported keywords must be declared here. */
7013/************************************************************************/
7014
7015/* Note: must not be declared <const> as its list will be overwritten */
7016static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007017 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7018
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007019 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7020 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7021 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7022 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007023
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007024 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7025 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7026 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7027 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7028 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7029 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7030 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7031 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7032 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007033
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007034 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7035 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7036 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7037 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7038 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7039 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7040 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7041 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7042 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7043 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007044 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007045
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007046 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7047 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7048 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7049 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7050 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7051 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7052 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7053 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7054 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007055 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007056
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007057 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7058 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7059 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7060 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7061 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7062 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7063 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007064
Willy Tarreauf3d25982007-05-08 22:45:09 +02007065 { NULL, NULL, NULL, NULL },
7066
7067#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007068 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7069 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7070 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7071 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7072 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7073 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7074 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7075
Willy Tarreau8797c062007-05-07 00:55:35 +02007076 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7077 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7078 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7079 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7080 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7081 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7082 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7083 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7084
7085 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7086 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7087 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7088 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7089 { NULL, NULL, NULL, NULL },
7090#endif
7091}};
7092
7093
7094__attribute__((constructor))
7095static void __http_protocol_init(void)
7096{
7097 acl_register_keywords(&acl_kws);
7098}
7099
7100
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007102 * Local variables:
7103 * c-indent-level: 8
7104 * c-basic-offset: 8
7105 * End:
7106 */