blob: d3c2b52f86f2bf1ede8008a1995154bdf6235a35 [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 Tarreau68085d82010-01-18 14:54:04 +0100455/* Find the end of the header value contained between <s> and <e>. See RFC2616,
456 * par 2.2 for more information. Note that it requires a valid header to return
457 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200458 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100459char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200460{
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
Willy Tarreau68085d82010-01-18 14:54:04 +0100479 * 1 when it finds a value, and 0 when there is no more. It is designed to work
480 * with headers defined as comma-separated lists. As a special case, if ctx->val
481 * is NULL when searching for a new values of a header, the current header is
482 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200483 */
484int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100485 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200486 struct hdr_ctx *ctx)
487{
Willy Tarreau68085d82010-01-18 14:54:04 +0100488 char *eol, *sov;
489 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200490
Willy Tarreau68085d82010-01-18 14:54:04 +0100491 cur_idx = ctx->idx;
492 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200493 /* We have previously returned a value, let's search
494 * another one on the same line.
495 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200496 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100497 ctx->del = ctx->val + ctx->vlen;
498 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200499 eol = sol + idx->v[cur_idx].len;
500
501 if (sov >= eol)
502 /* no more values in this header */
503 goto next_hdr;
504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 /* values remaining for this header, skip the comma but save it
506 * for later use (eg: for header deletion).
507 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 sov++;
509 while (sov < eol && http_is_lws[(unsigned char)*sov])
510 sov++;
511
512 goto return_hdr;
513 }
514
515 /* first request for this header */
516 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100517 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 while (cur_idx) {
520 eol = sol + idx->v[cur_idx].len;
521
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200522 if (len == 0) {
523 /* No argument was passed, we want any header.
524 * To achieve this, we simply build a fake request. */
525 while (sol + len < eol && sol[len] != ':')
526 len++;
527 name = sol;
528 }
529
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 if ((len < eol - sol) &&
531 (sol[len] == ':') &&
532 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100533 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200534 sov = sol + len + 1;
535 while (sov < eol && http_is_lws[(unsigned char)*sov])
536 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100537
Willy Tarreau33a7e692007-06-10 19:45:56 +0200538 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100539 ctx->prev = old_idx;
540 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 ctx->idx = cur_idx;
542 ctx->val = sov - sol;
543
544 eol = find_hdr_value_end(sov, eol);
545 ctx->vlen = eol - sov;
546 return 1;
547 }
548 next_hdr:
549 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100550 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200551 cur_idx = idx->v[cur_idx].next;
552 }
553 return 0;
554}
555
556int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 struct hdr_ctx *ctx)
559{
560 return http_find_header2(name, strlen(name), sol, idx, ctx);
561}
562
Willy Tarreau68085d82010-01-18 14:54:04 +0100563/* Remove one value of a header. This only works on a <ctx> returned by one of
564 * the http_find_header functions. The value is removed, as well as surrounding
565 * commas if any. If the removed value was alone, the whole header is removed.
566 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
567 * message <msg>. The new index is returned. If it is zero, it means there is
568 * no more header, so any processing may stop. The ctx is always left in a form
569 * that can be handled by http_find_header2() to find next occurrence.
570 */
571int http_remove_header2(struct http_msg *msg, struct buffer *buf,
572 struct hdr_idx *idx, struct hdr_ctx *ctx)
573{
574 int cur_idx = ctx->idx;
575 char *sol = ctx->line;
576 struct hdr_idx_elem *hdr;
577 int delta, skip_comma;
578
579 if (!cur_idx)
580 return 0;
581
582 hdr = &idx->v[cur_idx];
583 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
584 /* This was the only value of the header, we must now remove it entirely. */
585 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
586 http_msg_move_end(msg, delta);
587 idx->used--;
588 hdr->len = 0; /* unused entry */
589 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
590 ctx->idx = ctx->prev; /* walk back to the end of previous header */
591 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
592 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
593 ctx->vlen = 0;
594 return ctx->idx;
595 }
596
597 /* This was not the only value of this header. We have to remove between
598 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
599 * of the list, we remove the last separator.
600 */
601
602 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
603 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
604 sol + ctx->val + ctx->vlen + skip_comma,
605 NULL, 0);
606 hdr->len += delta;
607 http_msg_move_end(msg, delta);
608 ctx->val = ctx->del;
609 ctx->vlen = 0;
610 return ctx->idx;
611}
612
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100613/* This function handles a server error at the stream interface level. The
614 * stream interface is assumed to be already in a closed state. An optional
615 * message is copied into the input buffer, and an HTTP status code stored.
616 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100617 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100619static void http_server_error(struct session *t, struct stream_interface *si,
620 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100622 buffer_auto_read(si->ob);
623 buffer_abort(si->ob);
624 buffer_auto_close(si->ob);
625 buffer_erase(si->ob);
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100626 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200627 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100628 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100629 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100630 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100631 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 }
633 if (!(t->flags & SN_ERR_MASK))
634 t->flags |= err;
635 if (!(t->flags & SN_FINST_MASK))
636 t->flags |= finst;
637}
638
Willy Tarreau80587432006-12-24 17:47:20 +0100639/* This function returns the appropriate error location for the given session
640 * and message.
641 */
642
643struct chunk *error_message(struct session *s, int msgnum)
644{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200645 if (s->be->errmsg[msgnum].str)
646 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100647 else if (s->fe->errmsg[msgnum].str)
648 return &s->fe->errmsg[msgnum];
649 else
650 return &http_err_chunks[msgnum];
651}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652
Willy Tarreau53b6c742006-12-17 13:37:46 +0100653/*
654 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
655 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
656 */
657static http_meth_t find_http_meth(const char *str, const int len)
658{
659 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100660 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100661
662 m = ((unsigned)*str - 'A');
663
664 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100665 for (h = http_methods[m]; h->len > 0; h++) {
666 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100667 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100668 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100669 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100670 };
671 return HTTP_METH_OTHER;
672 }
673 return HTTP_METH_NONE;
674
675}
676
Willy Tarreau21d2af32008-02-14 20:25:24 +0100677/* Parse the URI from the given transaction (which is assumed to be in request
678 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
679 * It is returned otherwise.
680 */
681static char *
682http_get_path(struct http_txn *txn)
683{
684 char *ptr, *end;
685
Willy Tarreau962c3f42010-01-10 00:15:35 +0100686 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100687 end = ptr + txn->req.sl.rq.u_l;
688
689 if (ptr >= end)
690 return NULL;
691
692 /* RFC2616, par. 5.1.2 :
693 * Request-URI = "*" | absuri | abspath | authority
694 */
695
696 if (*ptr == '*')
697 return NULL;
698
699 if (isalpha((unsigned char)*ptr)) {
700 /* this is a scheme as described by RFC3986, par. 3.1 */
701 ptr++;
702 while (ptr < end &&
703 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
704 ptr++;
705 /* skip '://' */
706 if (ptr == end || *ptr++ != ':')
707 return NULL;
708 if (ptr == end || *ptr++ != '/')
709 return NULL;
710 if (ptr == end || *ptr++ != '/')
711 return NULL;
712 }
713 /* skip [user[:passwd]@]host[:[port]] */
714
715 while (ptr < end && *ptr != '/')
716 ptr++;
717
718 if (ptr == end)
719 return NULL;
720
721 /* OK, we got the '/' ! */
722 return ptr;
723}
724
Willy Tarreauefb453c2008-10-26 20:49:47 +0100725/* Returns a 302 for a redirectable request. This may only be called just after
726 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
727 * left unchanged and will follow normal proxy processing.
728 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100730{
731 struct http_txn *txn;
732 struct chunk rdr;
733 char *path;
734 int len;
735
736 /* 1: create the response header */
737 rdr.len = strlen(HTTP_302);
738 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100739 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740 memcpy(rdr.str, HTTP_302, rdr.len);
741
742 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200743 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100744 return;
745
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100746 /* special prefix "/" means don't change URL */
747 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
748 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
749 rdr.len += s->srv->rdr_len;
750 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751
752 /* 3: add the request URI */
753 txn = &s->txn;
754 path = http_get_path(txn);
755 if (!path)
756 return;
757
Willy Tarreau962c3f42010-01-10 00:15:35 +0100758 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200759 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760 return;
761
762 memcpy(rdr.str + rdr.len, path, len);
763 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100764
765 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
766 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
767 rdr.len += 29;
768 } else {
769 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
770 rdr.len += 23;
771 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772
773 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100774 si->shutr(si);
775 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 si->err_type = SI_ET_NONE;
777 si->err_loc = NULL;
778 si->state = SI_ST_CLO;
779
780 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100781 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100782
783 /* FIXME: we should increase a counter of redirects per server and per backend. */
784 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100785 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100786}
787
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100788/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 * that the server side is closed. Note that err_type is actually a
790 * bitmask, where almost only aborts may be cumulated with other
791 * values. We consider that aborted operations are more important
792 * than timeouts or errors due to the fact that nobody else in the
793 * logs might explain incomplete retries. All others should avoid
794 * being cumulated. It should normally not be possible to have multiple
795 * aborts at once, but just in case, the first one in sequence is reported.
796 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100797void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100798{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100799 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800
801 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100802 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
803 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100805 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
806 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100808 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
809 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100810 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100811 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
812 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100814 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
815 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100817 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
818 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100819 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
821 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822}
823
Willy Tarreau42250582007-04-01 01:30:43 +0200824extern const char sess_term_cond[8];
825extern const char sess_fin_state[8];
826extern const char *monthname[12];
827const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
828const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
829 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
830 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200831struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200832struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100833
Emeric Brun3a058f32009-06-30 18:26:00 +0200834void http_sess_clflog(struct session *s)
835{
836 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
837 struct proxy *fe = s->fe;
838 struct proxy *be = s->be;
839 struct proxy *prx_log;
840 struct http_txn *txn = &s->txn;
841 int tolog, level, err;
842 char *uri, *h;
843 char *svid;
844 struct tm tm;
845 static char tmpline[MAX_SYSLOG_LEN];
846 int hdr;
847 size_t w;
848 int t_request;
849
850 prx_log = fe;
851 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
852 (s->conn_retries != be->conn_retries) ||
853 txn->status >= 500;
854
855 if (s->cli_addr.ss_family == AF_INET)
856 inet_ntop(AF_INET,
857 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
858 pn, sizeof(pn));
859 else
860 inet_ntop(AF_INET6,
861 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
862 pn, sizeof(pn));
863
864 get_gmtime(s->logs.accept_date.tv_sec, &tm);
865
866 /* FIXME: let's limit ourselves to frontend logging for now. */
867 tolog = fe->to_log;
868
869 h = tmpline;
870
871 w = snprintf(h, sizeof(tmpline),
872 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
873 pn,
874 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
875 tm.tm_hour, tm.tm_min, tm.tm_sec);
876 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
877 goto trunc;
878 h += w;
879
880 if (h >= tmpline + sizeof(tmpline) - 4)
881 goto trunc;
882
883 *(h++) = ' ';
884 *(h++) = '\"';
885 uri = txn->uri ? txn->uri : "<BADREQ>";
886 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
887 '#', url_encode_map, uri);
888 *(h++) = '\"';
889
890 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
891 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
892 goto trunc;
893 h += w;
894
895 if (h >= tmpline + sizeof(tmpline) - 9)
896 goto trunc;
897 memcpy(h, " \"-\" \"-\"", 8);
898 h += 8;
899
900 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
901 " %d %03d",
902 (s->cli_addr.ss_family == AF_INET) ?
903 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
904 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
905 (int)s->logs.accept_date.tv_usec/1000);
906 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
907 goto trunc;
908 h += w;
909
910 w = strlen(fe->id);
911 if (h >= tmpline + sizeof(tmpline) - 4 - w)
912 goto trunc;
913 *(h++) = ' ';
914 *(h++) = '\"';
915 memcpy(h, fe->id, w);
916 h += w;
917 *(h++) = '\"';
918
919 w = strlen(be->id);
920 if (h >= tmpline + sizeof(tmpline) - 4 - w)
921 goto trunc;
922 *(h++) = ' ';
923 *(h++) = '\"';
924 memcpy(h, be->id, w);
925 h += w;
926 *(h++) = '\"';
927
928 svid = (tolog & LW_SVID) ?
929 (s->data_source != DATA_SRC_STATS) ?
930 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
931
932 w = strlen(svid);
933 if (h >= tmpline + sizeof(tmpline) - 4 - w)
934 goto trunc;
935 *(h++) = ' ';
936 *(h++) = '\"';
937 memcpy(h, svid, w);
938 h += w;
939 *(h++) = '\"';
940
941 t_request = -1;
942 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
943 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
944 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
945 " %d %ld %ld %ld %ld",
946 t_request,
947 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
948 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
949 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
950 s->logs.t_close);
951 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
952 goto trunc;
953 h += w;
954
955 if (h >= tmpline + sizeof(tmpline) - 8)
956 goto trunc;
957 *(h++) = ' ';
958 *(h++) = '\"';
959 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
960 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
961 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
962 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
963 *(h++) = '\"';
964
965 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
966 " %d %d %d %d %d %ld %ld",
967 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
968 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
969 s->logs.srv_queue_size, s->logs.prx_queue_size);
970
971 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
972 goto trunc;
973 h += w;
974
975 if (txn->cli_cookie) {
976 w = strlen(txn->cli_cookie);
977 if (h >= tmpline + sizeof(tmpline) - 4 - w)
978 goto trunc;
979 *(h++) = ' ';
980 *(h++) = '\"';
981 memcpy(h, txn->cli_cookie, w);
982 h += w;
983 *(h++) = '\"';
984 } else {
985 if (h >= tmpline + sizeof(tmpline) - 5)
986 goto trunc;
987 memcpy(h, " \"-\"", 4);
988 h += 4;
989 }
990
991 if (txn->srv_cookie) {
992 w = strlen(txn->srv_cookie);
993 if (h >= tmpline + sizeof(tmpline) - 4 - w)
994 goto trunc;
995 *(h++) = ' ';
996 *(h++) = '\"';
997 memcpy(h, txn->srv_cookie, w);
998 h += w;
999 *(h++) = '\"';
1000 } else {
1001 if (h >= tmpline + sizeof(tmpline) - 5)
1002 goto trunc;
1003 memcpy(h, " \"-\"", 4);
1004 h += 4;
1005 }
1006
1007 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1008 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1009 if (h >= sizeof (tmpline) + tmpline - 4)
1010 goto trunc;
1011 *(h++) = ' ';
1012 *(h++) = '\"';
1013 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1014 '#', hdr_encode_map, txn->req.cap[hdr]);
1015 *(h++) = '\"';
1016 }
1017 }
1018
1019 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1020 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1021 if (h >= sizeof (tmpline) + tmpline - 4)
1022 goto trunc;
1023 *(h++) = ' ';
1024 *(h++) = '\"';
1025 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1026 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1027 *(h++) = '\"';
1028 }
1029 }
1030
1031trunc:
1032 *h = '\0';
1033
1034 level = LOG_INFO;
1035 if (err && (fe->options2 & PR_O2_LOGERRORS))
1036 level = LOG_ERR;
1037
1038 send_log(prx_log, level, "%s\n", tmpline);
1039
1040 s->logs.logwait = 0;
1041}
1042
Willy Tarreau42250582007-04-01 01:30:43 +02001043/*
1044 * send a log for the session when we have enough info about it.
1045 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001046 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001047void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001048{
1049 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1050 struct proxy *fe = s->fe;
1051 struct proxy *be = s->be;
1052 struct proxy *prx_log;
1053 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001054 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001055 char *uri, *h;
1056 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001057 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001058 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001059 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001060 int hdr;
1061
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001062 /* if we don't want to log normal traffic, return now */
1063 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1064 (s->conn_retries != be->conn_retries) ||
1065 txn->status >= 500;
1066 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1067 return;
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1070 return;
1071 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072
Emeric Brun3a058f32009-06-30 18:26:00 +02001073 if (prx_log->options2 & PR_O2_CLFLOG)
1074 return http_sess_clflog(s);
1075
Willy Tarreau42250582007-04-01 01:30:43 +02001076 if (s->cli_addr.ss_family == AF_INET)
1077 inet_ntop(AF_INET,
1078 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1079 pn, sizeof(pn));
1080 else
1081 inet_ntop(AF_INET6,
1082 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1083 pn, sizeof(pn));
1084
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001085 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001086
1087 /* FIXME: let's limit ourselves to frontend logging for now. */
1088 tolog = fe->to_log;
1089
1090 h = tmpline;
1091 if (fe->to_log & LW_REQHDR &&
1092 txn->req.cap &&
1093 (h < tmpline + sizeof(tmpline) - 10)) {
1094 *(h++) = ' ';
1095 *(h++) = '{';
1096 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1097 if (hdr)
1098 *(h++) = '|';
1099 if (txn->req.cap[hdr] != NULL)
1100 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1101 '#', hdr_encode_map, txn->req.cap[hdr]);
1102 }
1103 *(h++) = '}';
1104 }
1105
1106 if (fe->to_log & LW_RSPHDR &&
1107 txn->rsp.cap &&
1108 (h < tmpline + sizeof(tmpline) - 7)) {
1109 *(h++) = ' ';
1110 *(h++) = '{';
1111 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1112 if (hdr)
1113 *(h++) = '|';
1114 if (txn->rsp.cap[hdr] != NULL)
1115 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1116 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1117 }
1118 *(h++) = '}';
1119 }
1120
1121 if (h < tmpline + sizeof(tmpline) - 4) {
1122 *(h++) = ' ';
1123 *(h++) = '"';
1124 uri = txn->uri ? txn->uri : "<BADREQ>";
1125 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1126 '#', url_encode_map, uri);
1127 *(h++) = '"';
1128 }
1129 *h = '\0';
1130
1131 svid = (tolog & LW_SVID) ?
1132 (s->data_source != DATA_SRC_STATS) ?
1133 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1134
Willy Tarreau70089872008-06-13 21:12:51 +02001135 t_request = -1;
1136 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1137 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1138
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001139 level = LOG_INFO;
1140 if (err && (fe->options2 & PR_O2_LOGERRORS))
1141 level = LOG_ERR;
1142
1143 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001144 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001145 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1146 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001147 pn,
1148 (s->cli_addr.ss_family == AF_INET) ?
1149 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1150 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001151 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001152 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001153 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001154 t_request,
1155 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001156 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1157 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1158 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1159 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001160 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001161 txn->cli_cookie ? txn->cli_cookie : "-",
1162 txn->srv_cookie ? txn->srv_cookie : "-",
1163 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1164 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1165 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1166 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1167 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001168 (s->flags & SN_REDISP)?"+":"",
1169 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001170 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1171
1172 s->logs.logwait = 0;
1173}
1174
Willy Tarreau117f59e2007-03-04 18:17:17 +01001175
1176/*
1177 * Capture headers from message starting at <som> according to header list
1178 * <cap_hdr>, and fill the <idx> structure appropriately.
1179 */
1180void capture_headers(char *som, struct hdr_idx *idx,
1181 char **cap, struct cap_hdr *cap_hdr)
1182{
1183 char *eol, *sol, *col, *sov;
1184 int cur_idx;
1185 struct cap_hdr *h;
1186 int len;
1187
1188 sol = som + hdr_idx_first_pos(idx);
1189 cur_idx = hdr_idx_first_idx(idx);
1190
1191 while (cur_idx) {
1192 eol = sol + idx->v[cur_idx].len;
1193
1194 col = sol;
1195 while (col < eol && *col != ':')
1196 col++;
1197
1198 sov = col + 1;
1199 while (sov < eol && http_is_lws[(unsigned char)*sov])
1200 sov++;
1201
1202 for (h = cap_hdr; h; h = h->next) {
1203 if ((h->namelen == col - sol) &&
1204 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1205 if (cap[h->index] == NULL)
1206 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001207 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001208
1209 if (cap[h->index] == NULL) {
1210 Alert("HTTP capture : out of memory.\n");
1211 continue;
1212 }
1213
1214 len = eol - sov;
1215 if (len > h->len)
1216 len = h->len;
1217
1218 memcpy(cap[h->index], sov, len);
1219 cap[h->index][len]=0;
1220 }
1221 }
1222 sol = eol + idx->v[cur_idx].cr + 1;
1223 cur_idx = idx->v[cur_idx].next;
1224 }
1225}
1226
1227
Willy Tarreau42250582007-04-01 01:30:43 +02001228/* either we find an LF at <ptr> or we jump to <bad>.
1229 */
1230#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1231
1232/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1233 * otherwise to <http_msg_ood> with <state> set to <st>.
1234 */
1235#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1236 ptr++; \
1237 if (likely(ptr < end)) \
1238 goto good; \
1239 else { \
1240 state = (st); \
1241 goto http_msg_ood; \
1242 } \
1243 } while (0)
1244
1245
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001247 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1249 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1250 * will give undefined results.
1251 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1252 * and that msg->sol points to the beginning of the response.
1253 * If a complete line is found (which implies that at least one CR or LF is
1254 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1255 * returned indicating an incomplete line (which does not mean that parts have
1256 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1257 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1258 * upon next call.
1259 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001260 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001261 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1262 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001263 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001265const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1266 unsigned int state, const char *ptr, const char *end,
1267 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001268{
Willy Tarreau8973c702007-01-21 23:58:29 +01001269 switch (state) {
1270 http_msg_rpver:
1271 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001272 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001273 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1274
1275 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001276 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001277 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1278 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001279 state = HTTP_MSG_ERROR;
1280 break;
1281
Willy Tarreau8973c702007-01-21 23:58:29 +01001282 http_msg_rpver_sp:
1283 case HTTP_MSG_RPVER_SP:
1284 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001285 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001286 goto http_msg_rpcode;
1287 }
1288 if (likely(HTTP_IS_SPHT(*ptr)))
1289 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1290 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001291 state = HTTP_MSG_ERROR;
1292 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001293
1294 http_msg_rpcode:
1295 case HTTP_MSG_RPCODE:
1296 if (likely(!HTTP_IS_LWS(*ptr)))
1297 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1298
1299 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001300 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1302 }
1303
1304 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001305 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 http_msg_rsp_reason:
1307 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001308 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 msg->sl.st.r_l = 0;
1310 goto http_msg_rpline_eol;
1311
1312 http_msg_rpcode_sp:
1313 case HTTP_MSG_RPCODE_SP:
1314 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001315 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 goto http_msg_rpreason;
1317 }
1318 if (likely(HTTP_IS_SPHT(*ptr)))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1320 /* so it's a CR/LF, so there is no reason phrase */
1321 goto http_msg_rsp_reason;
1322
1323 http_msg_rpreason:
1324 case HTTP_MSG_RPREASON:
1325 if (likely(!HTTP_IS_CRLF(*ptr)))
1326 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001327 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 http_msg_rpline_eol:
1329 /* We have seen the end of line. Note that we do not
1330 * necessarily have the \n yet, but at least we know that we
1331 * have EITHER \r OR \n, otherwise the response would not be
1332 * complete. We can then record the response length and return
1333 * to the caller which will be able to register it.
1334 */
1335 msg->sl.st.l = ptr - msg->sol;
1336 return ptr;
1337
1338#ifdef DEBUG_FULL
1339 default:
1340 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1341 exit(1);
1342#endif
1343 }
1344
1345 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001346 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 if (ret_state)
1348 *ret_state = state;
1349 if (ret_ptr)
1350 *ret_ptr = (char *)ptr;
1351 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001352}
1353
1354
1355/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 * This function parses a request line between <ptr> and <end>, starting with
1357 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1358 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1359 * will give undefined results.
1360 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1361 * and that msg->sol points to the beginning of the request.
1362 * If a complete line is found (which implies that at least one CR or LF is
1363 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1364 * returned indicating an incomplete line (which does not mean that parts have
1365 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1366 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1367 * upon next call.
1368 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001369 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1371 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001372 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001373 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001374const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1375 unsigned int state, const char *ptr, const char *end,
1376 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 switch (state) {
1379 http_msg_rqmeth:
1380 case HTTP_MSG_RQMETH:
1381 if (likely(HTTP_IS_TOKEN(*ptr)))
1382 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001385 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1387 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (likely(HTTP_IS_CRLF(*ptr))) {
1390 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001391 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001393 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001395 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001397 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 msg->sl.rq.v_l = 0;
1399 goto http_msg_rqline_eol;
1400 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001401 state = HTTP_MSG_ERROR;
1402 break;
1403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 http_msg_rqmeth_sp:
1405 case HTTP_MSG_RQMETH_SP:
1406 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001407 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 goto http_msg_rquri;
1409 }
1410 if (likely(HTTP_IS_SPHT(*ptr)))
1411 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1412 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1413 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 http_msg_rquri:
1416 case HTTP_MSG_RQURI:
1417 if (likely(!HTTP_IS_LWS(*ptr)))
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001421 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1426 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 http_msg_rquri_sp:
1429 case HTTP_MSG_RQURI_SP:
1430 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001431 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 goto http_msg_rqver;
1433 }
1434 if (likely(HTTP_IS_SPHT(*ptr)))
1435 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1436 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1437 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 http_msg_rqver:
1440 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001441 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001443
1444 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001445 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001446 http_msg_rqline_eol:
1447 /* We have seen the end of line. Note that we do not
1448 * necessarily have the \n yet, but at least we know that we
1449 * have EITHER \r OR \n, otherwise the request would not be
1450 * complete. We can then record the request length and return
1451 * to the caller which will be able to register it.
1452 */
1453 msg->sl.rq.l = ptr - msg->sol;
1454 return ptr;
1455 }
1456
1457 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001458 state = HTTP_MSG_ERROR;
1459 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461#ifdef DEBUG_FULL
1462 default:
1463 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1464 exit(1);
1465#endif
1466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001469 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 if (ret_state)
1471 *ret_state = state;
1472 if (ret_ptr)
1473 *ret_ptr = (char *)ptr;
1474 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001476
1477
Willy Tarreau8973c702007-01-21 23:58:29 +01001478/*
1479 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 * when data are missing and recalled at the exact same location with no
1482 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001483 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001484 * fields. Note that msg->som and msg->sol will be initialized after completing
1485 * the first state, so that none of the msg pointers has to be initialized
1486 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1489{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001490 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001492
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001493 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 ptr = buf->lr;
1495 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (unlikely(ptr >= end))
1498 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 /*
1502 * First, states that are specific to the response only.
1503 * We check them first so that request and headers are
1504 * closer to each other (accessed more often).
1505 */
1506 http_msg_rpbefore:
1507 case HTTP_MSG_RPBEFORE:
1508 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001509 /* we have a start of message, but we have to check
1510 * first if we need to remove some CRLF. We can only
1511 * do this when send_max=0.
1512 */
1513 char *beg = buf->w + buf->send_max;
1514 if (beg >= buf->data + buf->size)
1515 beg -= buf->size;
1516 if (unlikely(ptr != beg)) {
1517 if (buf->send_max)
1518 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001519 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001520 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001522 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001523 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 hdr_idx_init(idx);
1525 state = HTTP_MSG_RPVER;
1526 goto http_msg_rpver;
1527 }
1528
1529 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1530 goto http_msg_invalid;
1531
1532 if (unlikely(*ptr == '\n'))
1533 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1535 /* stop here */
1536
1537 http_msg_rpbefore_cr:
1538 case HTTP_MSG_RPBEFORE_CR:
1539 EXPECT_LF_HERE(ptr, http_msg_invalid);
1540 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1541 /* stop here */
1542
1543 http_msg_rpver:
1544 case HTTP_MSG_RPVER:
1545 case HTTP_MSG_RPVER_SP:
1546 case HTTP_MSG_RPCODE:
1547 case HTTP_MSG_RPCODE_SP:
1548 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001549 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001550 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001551 if (unlikely(!ptr))
1552 return;
1553
1554 /* we have a full response and we know that we have either a CR
1555 * or an LF at <ptr>.
1556 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001557 //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 +01001558 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1559
1560 msg->sol = ptr;
1561 if (likely(*ptr == '\r'))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1563 goto http_msg_rpline_end;
1564
1565 http_msg_rpline_end:
1566 case HTTP_MSG_RPLINE_END:
1567 /* msg->sol must point to the first of CR or LF. */
1568 EXPECT_LF_HERE(ptr, http_msg_invalid);
1569 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1570 /* stop here */
1571
1572 /*
1573 * Second, states that are specific to the request only
1574 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 http_msg_rqbefore:
1576 case HTTP_MSG_RQBEFORE:
1577 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001578 /* we have a start of message, but we have to check
1579 * first if we need to remove some CRLF. We can only
1580 * do this when send_max=0.
1581 */
1582 char *beg = buf->w + buf->send_max;
1583 if (beg >= buf->data + buf->size)
1584 beg -= buf->size;
1585 if (likely(ptr != beg)) {
1586 if (buf->send_max)
1587 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001588 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001589 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001591 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001592 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001593 /* we will need this when keep-alive will be supported
1594 hdr_idx_init(idx);
1595 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001596 state = HTTP_MSG_RQMETH;
1597 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1601 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (unlikely(*ptr == '\n'))
1604 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1605 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001606 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 http_msg_rqbefore_cr:
1609 case HTTP_MSG_RQBEFORE_CR:
1610 EXPECT_LF_HERE(ptr, http_msg_invalid);
1611 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 http_msg_rqmeth:
1615 case HTTP_MSG_RQMETH:
1616 case HTTP_MSG_RQMETH_SP:
1617 case HTTP_MSG_RQURI:
1618 case HTTP_MSG_RQURI_SP:
1619 case HTTP_MSG_RQVER:
1620 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001621 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 if (unlikely(!ptr))
1623 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 /* we have a full request and we know that we have either a CR
1626 * or an LF at <ptr>.
1627 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001628 //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 +01001629 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 msg->sol = ptr;
1632 if (likely(*ptr == '\r'))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 http_msg_rqline_end:
1637 case HTTP_MSG_RQLINE_END:
1638 /* check for HTTP/0.9 request : no version information available.
1639 * msg->sol must point to the first of CR or LF.
1640 */
1641 if (unlikely(msg->sl.rq.v_l == 0))
1642 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 EXPECT_LF_HERE(ptr, http_msg_invalid);
1645 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8973c702007-01-21 23:58:29 +01001648 /*
1649 * Common states below
1650 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 http_msg_hdr_first:
1652 case HTTP_MSG_HDR_FIRST:
1653 msg->sol = ptr;
1654 if (likely(!HTTP_IS_CRLF(*ptr))) {
1655 goto http_msg_hdr_name;
1656 }
1657
1658 if (likely(*ptr == '\r'))
1659 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1660 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 http_msg_hdr_name:
1663 case HTTP_MSG_HDR_NAME:
1664 /* assumes msg->sol points to the first char */
1665 if (likely(HTTP_IS_TOKEN(*ptr)))
1666 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 if (likely(*ptr == ':')) {
1669 msg->col = ptr - buf->data;
1670 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1671 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001672
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001673 if (likely(msg->err_pos < -1) || *ptr == '\n')
1674 goto http_msg_invalid;
1675
1676 if (msg->err_pos == -1) /* capture error pointer */
1677 msg->err_pos = ptr - buf->data; /* >= 0 now */
1678
1679 /* and we still accept this non-token character */
1680 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 http_msg_hdr_l1_sp:
1683 case HTTP_MSG_HDR_L1_SP:
1684 /* assumes msg->sol points to the first char and msg->col to the colon */
1685 if (likely(HTTP_IS_SPHT(*ptr)))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 /* header value can be basically anything except CR/LF */
1689 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001690
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 if (likely(!HTTP_IS_CRLF(*ptr))) {
1692 goto http_msg_hdr_val;
1693 }
1694
1695 if (likely(*ptr == '\r'))
1696 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1697 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 http_msg_hdr_l1_lf:
1700 case HTTP_MSG_HDR_L1_LF:
1701 EXPECT_LF_HERE(ptr, http_msg_invalid);
1702 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001703
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 http_msg_hdr_l1_lws:
1705 case HTTP_MSG_HDR_L1_LWS:
1706 if (likely(HTTP_IS_SPHT(*ptr))) {
1707 /* replace HT,CR,LF with spaces */
1708 for (; buf->data+msg->sov < ptr; msg->sov++)
1709 buf->data[msg->sov] = ' ';
1710 goto http_msg_hdr_l1_sp;
1711 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001712 /* we had a header consisting only in spaces ! */
1713 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 goto http_msg_complete_header;
1715
1716 http_msg_hdr_val:
1717 case HTTP_MSG_HDR_VAL:
1718 /* assumes msg->sol points to the first char, msg->col to the
1719 * colon, and msg->sov points to the first character of the
1720 * value.
1721 */
1722 if (likely(!HTTP_IS_CRLF(*ptr)))
1723 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 msg->eol = ptr;
1726 /* Note: we could also copy eol into ->eoh so that we have the
1727 * real header end in case it ends with lots of LWS, but is this
1728 * really needed ?
1729 */
1730 if (likely(*ptr == '\r'))
1731 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1732 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 http_msg_hdr_l2_lf:
1735 case HTTP_MSG_HDR_L2_LF:
1736 EXPECT_LF_HERE(ptr, http_msg_invalid);
1737 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 http_msg_hdr_l2_lws:
1740 case HTTP_MSG_HDR_L2_LWS:
1741 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1742 /* LWS: replace HT,CR,LF with spaces */
1743 for (; msg->eol < ptr; msg->eol++)
1744 *msg->eol = ' ';
1745 goto http_msg_hdr_val;
1746 }
1747 http_msg_complete_header:
1748 /*
1749 * It was a new header, so the last one is finished.
1750 * Assumes msg->sol points to the first char, msg->col to the
1751 * colon, msg->sov points to the first character of the value
1752 * and msg->eol to the first CR or LF so we know how the line
1753 * ends. We insert last header into the index.
1754 */
1755 /*
1756 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1757 write(2, msg->sol, msg->eol-msg->sol);
1758 fprintf(stderr,"\n");
1759 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1762 idx, idx->tail) < 0))
1763 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001764
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 msg->sol = ptr;
1766 if (likely(!HTTP_IS_CRLF(*ptr))) {
1767 goto http_msg_hdr_name;
1768 }
1769
1770 if (likely(*ptr == '\r'))
1771 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1772 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 http_msg_last_lf:
1775 case HTTP_MSG_LAST_LF:
1776 /* Assumes msg->sol points to the first of either CR or LF */
1777 EXPECT_LF_HERE(ptr, http_msg_invalid);
1778 ptr++;
1779 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001780 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001781 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001782 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001783 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 return;
1785#ifdef DEBUG_FULL
1786 default:
1787 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1788 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001789#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 }
1791 http_msg_ood:
1792 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001793 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 buf->lr = ptr;
1795 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 http_msg_invalid:
1798 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001799 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001800 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 return;
1802}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001803
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001804/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1805 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1806 * nothing is done and 1 is returned.
1807 */
1808static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1809{
1810 int delta;
1811 char *cur_end;
1812
1813 if (msg->sl.rq.v_l != 0)
1814 return 1;
1815
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001816 cur_end = msg->sol + msg->sl.rq.l;
1817 delta = 0;
1818
1819 if (msg->sl.rq.u_l == 0) {
1820 /* if no URI was set, add "/" */
1821 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1822 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001823 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001824 }
1825 /* add HTTP version */
1826 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001827 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001828 cur_end += delta;
1829 cur_end = (char *)http_parse_reqline(msg, req->data,
1830 HTTP_MSG_RQMETH,
1831 msg->sol, cur_end + 1,
1832 NULL, NULL);
1833 if (unlikely(!cur_end))
1834 return 0;
1835
1836 /* we have a full HTTP/1.0 request now and we know that
1837 * we have either a CR or an LF at <ptr>.
1838 */
1839 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1840 return 1;
1841}
1842
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001843/* Parse the Connection: header of an HTTP request, looking for both "close"
1844 * and "keep-alive" values. If a buffer is provided and we already know that
1845 * some headers may safely be removed, we remove them now. The <to_del> flags
1846 * are used for that :
1847 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1848 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1849 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1850 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1851 * harmless combinations may be removed. Do not call that after changes have
1852 * been processed. If unused, the buffer can be NULL, and no data will be
1853 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001854 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001855void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001856{
Willy Tarreau5b154472009-12-21 20:11:07 +01001857 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001858 const char *hdr_val = "Connection";
1859 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001860
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001861 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001862 return;
1863
Willy Tarreau88d349d2010-01-25 12:15:43 +01001864 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1865 hdr_val = "Proxy-Connection";
1866 hdr_len = 16;
1867 }
1868
Willy Tarreau5b154472009-12-21 20:11:07 +01001869 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001871 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1873 txn->flags |= TX_HDR_CONN_KAL;
1874 if ((to_del & 2) && buf)
1875 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1876 else
1877 txn->flags |= TX_CON_KAL_SET;
1878 }
1879 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1880 txn->flags |= TX_HDR_CONN_CLO;
1881 if ((to_del & 1) && buf)
1882 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1883 else
1884 txn->flags |= TX_CON_CLO_SET;
1885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
1887
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001888 txn->flags |= TX_HDR_CONN_PRS;
1889 return;
1890}
Willy Tarreau5b154472009-12-21 20:11:07 +01001891
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892/* Apply desired changes on the Connection: header. Values may be removed and/or
1893 * added depending on the <wanted> flags, which are exclusively composed of
1894 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1895 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1896 */
1897void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1898{
1899 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001900 const char *hdr_val = "Connection";
1901 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001902
1903 ctx.idx = 0;
1904
Willy Tarreau88d349d2010-01-25 12:15:43 +01001905
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection";
1908 hdr_len = 16;
1909 }
1910
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001912 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1914 if (wanted & TX_CON_KAL_SET)
1915 txn->flags |= TX_CON_KAL_SET;
1916 else
1917 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001918 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001919 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1920 if (wanted & TX_CON_CLO_SET)
1921 txn->flags |= TX_CON_CLO_SET;
1922 else
1923 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001924 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001925 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001926
1927 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1928 return;
1929
1930 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1931 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001932 hdr_val = "Connection: close";
1933 hdr_len = 17;
1934 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1935 hdr_val = "Proxy-Connection: close";
1936 hdr_len = 23;
1937 }
1938 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001939 }
1940
1941 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1942 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001943 hdr_val = "Connection: keep-alive";
1944 hdr_len = 22;
1945 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1946 hdr_val = "Proxy-Connection: keep-alive";
1947 hdr_len = 28;
1948 }
1949 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001950 }
1951 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001952}
1953
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1955 * first byte of body, and increments msg->sov by the number of bytes parsed,
1956 * so that we know we can forward between ->som and ->sov. Note that due to
1957 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1958 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001960 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001961 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001963{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 char *ptr = buf->lr;
1965 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001966 unsigned int chunk = 0;
1967
1968 /* The chunk size is in the following form, though we are only
1969 * interested in the size and CRLF :
1970 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1971 */
1972 while (1) {
1973 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001975 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001977 if (c < 0) /* not a hex digit anymore */
1978 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 if (++ptr >= end)
1980 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001981 if (chunk & 0xF000000) /* overflow will occur */
1982 return -1;
1983 chunk = (chunk << 4) + c;
1984 }
1985
Willy Tarreaud98cf932009-12-27 22:54:55 +01001986 /* empty size not allowed */
1987 if (ptr == buf->lr)
1988 return -1;
1989
1990 while (http_is_spht[(unsigned char)*ptr]) {
1991 if (++ptr >= end)
1992 ptr = buf->data;
1993 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001995 }
1996
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 /* Up to there, we know that at least one byte is present at *ptr. Check
1998 * for the end of chunk size.
1999 */
2000 while (1) {
2001 if (likely(HTTP_IS_CRLF(*ptr))) {
2002 /* we now have a CR or an LF at ptr */
2003 if (likely(*ptr == '\r')) {
2004 if (++ptr >= end)
2005 ptr = buf->data;
2006 if (ptr == buf->r)
2007 return 0;
2008 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002009
Willy Tarreaud98cf932009-12-27 22:54:55 +01002010 if (*ptr != '\n')
2011 return -1;
2012 if (++ptr >= end)
2013 ptr = buf->data;
2014 /* done */
2015 break;
2016 }
2017 else if (*ptr == ';') {
2018 /* chunk extension, ends at next CRLF */
2019 if (++ptr >= end)
2020 ptr = buf->data;
2021 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002022 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002023
2024 while (!HTTP_IS_CRLF(*ptr)) {
2025 if (++ptr >= end)
2026 ptr = buf->data;
2027 if (ptr == buf->r)
2028 return 0;
2029 }
2030 /* we have a CRLF now, loop above */
2031 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002032 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002034 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002035 }
2036
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 /* OK we found our CRLF and now <ptr> points to the next byte,
2038 * which may or may not be present. We save that into ->lr and
2039 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002040 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041 msg->sov += ptr - buf->lr;
2042 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002043 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002044 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002045 return 1;
2046}
2047
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048/* This function skips trailers in the buffer <buf> associated with HTTP
2049 * message <msg>. The first visited position is buf->lr. If the end of
2050 * the trailers is found, it is automatically scheduled to be forwarded,
2051 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2052 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002053 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2054 * zero. If a parse error is encountered, the function returns < 0 and does not
2055 * change anything except maybe buf->lr and msg->sov. Note that the message
2056 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2057 * which implies that all non-trailers data have already been scheduled for
2058 * forwarding, and that the difference between msg->som and msg->sov exactly
2059 * matches the length of trailers already parsed and not forwarded. It is also
2060 * important to note that this function is designed to be able to parse wrapped
2061 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 */
2063int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2064{
2065 /* we have buf->lr which points to next line. Look for CRLF. */
2066 while (1) {
2067 char *p1 = NULL, *p2 = NULL;
2068 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002069 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070
2071 /* scan current line and stop at LF or CRLF */
2072 while (1) {
2073 if (ptr == buf->r)
2074 return 0;
2075
2076 if (*ptr == '\n') {
2077 if (!p1)
2078 p1 = ptr;
2079 p2 = ptr;
2080 break;
2081 }
2082
2083 if (*ptr == '\r') {
2084 if (p1)
2085 return -1;
2086 p1 = ptr;
2087 }
2088
2089 ptr++;
2090 if (ptr >= buf->data + buf->size)
2091 ptr = buf->data;
2092 }
2093
2094 /* after LF; point to beginning of next line */
2095 p2++;
2096 if (p2 >= buf->data + buf->size)
2097 p2 = buf->data;
2098
Willy Tarreau638cd022010-01-03 07:42:04 +01002099 bytes = p2 - buf->lr;
2100 if (bytes < 0)
2101 bytes += buf->size;
2102
2103 /* schedule this line for forwarding */
2104 msg->sov += bytes;
2105 if (msg->sov >= buf->size)
2106 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107
Willy Tarreau638cd022010-01-03 07:42:04 +01002108 if (p1 == buf->lr) {
2109 /* LF/CRLF at beginning of line => end of trailers at p2.
2110 * Everything was scheduled for forwarding, there's nothing
2111 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002112 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002113 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002114 msg->msg_state = HTTP_MSG_DONE;
2115 return 1;
2116 }
2117 /* OK, next line then */
2118 buf->lr = p2;
2119 }
2120}
2121
2122/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2123 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2124 * ->som, buf->lr in order to include this part into the next forwarding phase.
2125 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2126 * not enough data are available, the function does not change anything and
2127 * returns zero. If a parse error is encountered, the function returns < 0 and
2128 * does not change anything. Note: this function is designed to parse wrapped
2129 * CRLF at the end of the buffer.
2130 */
2131int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2132{
2133 char *ptr;
2134 int bytes;
2135
2136 /* NB: we'll check data availabilty at the end. It's not a
2137 * problem because whatever we match first will be checked
2138 * against the correct length.
2139 */
2140 bytes = 1;
2141 ptr = buf->lr;
2142 if (*ptr == '\r') {
2143 bytes++;
2144 ptr++;
2145 if (ptr >= buf->data + buf->size)
2146 ptr = buf->data;
2147 }
2148
2149 if (buf->l < bytes)
2150 return 0;
2151
2152 if (*ptr != '\n')
2153 return -1;
2154
2155 ptr++;
2156 if (ptr >= buf->data + buf->size)
2157 ptr = buf->data;
2158 buf->lr = ptr;
2159 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2160 msg->sov = ptr - buf->data;
2161 msg->som = msg->sov - bytes;
2162 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2163 return 1;
2164}
Willy Tarreau5b154472009-12-21 20:11:07 +01002165
Willy Tarreau83e3af02009-12-28 17:39:57 +01002166void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2167{
2168 char *end = buf->data + buf->size;
2169 int off = buf->data + buf->size - buf->w;
2170
2171 /* two possible cases :
2172 * - the buffer is in one contiguous block, we move it in-place
2173 * - the buffer is in two blocks, we move it via the trash
2174 */
2175 if (buf->l) {
2176 int block1 = buf->l;
2177 int block2 = 0;
2178 if (buf->r <= buf->w) {
2179 /* non-contiguous block */
2180 block1 = buf->data + buf->size - buf->w;
2181 block2 = buf->r - buf->data;
2182 }
2183 if (block2)
2184 memcpy(trash, buf->data, block2);
2185 memmove(buf->data, buf->w, block1);
2186 if (block2)
2187 memcpy(buf->data + block1, trash, block2);
2188 }
2189
2190 /* adjust all known pointers */
2191 buf->w = buf->data;
2192 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2193 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2194 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2195 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2196
2197 /* adjust relative pointers */
2198 msg->som = 0;
2199 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2200 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2201 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2202
Willy Tarreau83e3af02009-12-28 17:39:57 +01002203 if (msg->err_pos >= 0) {
2204 msg->err_pos += off;
2205 if (msg->err_pos >= buf->size)
2206 msg->err_pos -= buf->size;
2207 }
2208
2209 buf->flags &= ~BF_FULL;
2210 if (buf->l >= buffer_max_len(buf))
2211 buf->flags |= BF_FULL;
2212}
2213
Willy Tarreaud787e662009-07-07 10:14:51 +02002214/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2215 * processing can continue on next analysers, or zero if it either needs more
2216 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2217 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2218 * when it has nothing left to do, and may remove any analyser when it wants to
2219 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002220 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002221int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002222{
Willy Tarreau59234e92008-11-30 23:51:27 +01002223 /*
2224 * We will parse the partial (or complete) lines.
2225 * We will check the request syntax, and also join multi-line
2226 * headers. An index of all the lines will be elaborated while
2227 * parsing.
2228 *
2229 * For the parsing, we use a 28 states FSM.
2230 *
2231 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002232 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002233 * req->data + msg->eoh = end of processed headers / start of current one
2234 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002235 * req->lr = first non-visited byte
2236 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002237 *
2238 * At end of parsing, we may perform a capture of the error (if any), and
2239 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2240 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2241 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002242 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002243
Willy Tarreau59234e92008-11-30 23:51:27 +01002244 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002245 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 struct http_txn *txn = &s->txn;
2247 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002248 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002249
Willy Tarreau6bf17362009-02-24 10:48:35 +01002250 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2251 now_ms, __FUNCTION__,
2252 s,
2253 req,
2254 req->rex, req->wex,
2255 req->flags,
2256 req->l,
2257 req->analysers);
2258
Willy Tarreau52a0c602009-08-16 22:45:38 +02002259 /* we're speaking HTTP here, so let's speak HTTP to the client */
2260 s->srv_error = http_return_srv_error;
2261
Willy Tarreau83e3af02009-12-28 17:39:57 +01002262 /* There's a protected area at the end of the buffer for rewriting
2263 * purposes. We don't want to start to parse the request if the
2264 * protected area is affected, because we may have to move processed
2265 * data later, which is much more complicated.
2266 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002267 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002268 if ((txn->flags & TX_NOT_FIRST) &&
2269 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002270 req->r < req->lr ||
2271 req->r > req->data + req->size - global.tune.maxrewrite)) {
2272 if (req->send_max) {
2273 /* some data has still not left the buffer, wake us once that's done */
2274 buffer_dont_connect(req);
2275 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2276 return 0;
2277 }
2278 if (req->l <= req->size - global.tune.maxrewrite)
2279 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002280 }
2281
Willy Tarreau065e8332010-01-08 00:30:20 +01002282 /* Note that we have the same problem with the response ; we
2283 * may want to send a redirect, error or anything which requires
2284 * some spare space. So we'll ensure that we have at least
2285 * maxrewrite bytes available in the response buffer before
2286 * processing that one. This will only affect pipelined
2287 * keep-alive requests.
2288 */
2289 if ((txn->flags & TX_NOT_FIRST) &&
2290 unlikely((s->rep->flags & BF_FULL) ||
2291 s->rep->r < s->rep->lr ||
2292 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2293 if (s->rep->send_max) {
2294 /* don't let a connection request be initiated */
2295 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002296 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002297 return 0;
2298 }
2299 }
2300
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002301 if (likely(req->lr < req->r))
2302 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002303 }
2304
Willy Tarreau59234e92008-11-30 23:51:27 +01002305 /* 1: we might have to print this header in debug mode */
2306 if (unlikely((global.mode & MODE_DEBUG) &&
2307 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002308 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau962c3f42010-01-10 00:15:35 +01002311 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 eol = sol + msg->sl.rq.l;
2313 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002314
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 sol += hdr_idx_first_pos(&txn->hdr_idx);
2316 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002317
Willy Tarreau59234e92008-11-30 23:51:27 +01002318 while (cur_idx) {
2319 eol = sol + txn->hdr_idx.v[cur_idx].len;
2320 debug_hdr("clihdr", s, sol, eol);
2321 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2322 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002323 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 }
2325
Willy Tarreau58f10d72006-12-04 02:26:12 +01002326
Willy Tarreau59234e92008-11-30 23:51:27 +01002327 /*
2328 * Now we quickly check if we have found a full valid request.
2329 * If not so, we check the FD and buffer states before leaving.
2330 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002331 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002332 * requests are checked first. When waiting for a second request
2333 * on a keep-alive session, if we encounter and error, close, t/o,
2334 * we note the error in the session flags but don't set any state.
2335 * Since the error will be noted there, it will not be counted by
2336 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002338
Willy Tarreau655dce92009-11-08 13:10:58 +01002339 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002340 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2344 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 /* 1: Since we are in header mode, if there's no space
2347 * left for headers, we won't be able to free more
2348 * later, so the session will never terminate. We
2349 * must terminate it now.
2350 */
2351 if (unlikely(req->flags & BF_FULL)) {
2352 /* FIXME: check if URI is set and return Status
2353 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002354 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 goto return_bad_req;
2356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 /* 2: have we encountered a read error ? */
2359 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002360 if (!(s->flags & SN_ERR_MASK))
2361 s->flags |= SN_ERR_CLICL;
2362
Willy Tarreaufcffa692010-01-10 14:21:19 +01002363 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002364 goto failed_keep_alive;
2365
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002367 if (msg->err_pos >= 0)
2368 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 msg->msg_state = HTTP_MSG_ERROR;
2370 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002371
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002372 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002373 if (s->listener->counters)
2374 s->listener->counters->failed_req++;
2375
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 if (!(s->flags & SN_FINST_MASK))
2377 s->flags |= SN_FINST_R;
2378 return 0;
2379 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 /* 3: has the read timeout expired ? */
2382 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002383 if (!(s->flags & SN_ERR_MASK))
2384 s->flags |= SN_ERR_CLITO;
2385
Willy Tarreaufcffa692010-01-10 14:21:19 +01002386 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002387 goto failed_keep_alive;
2388
Willy Tarreau59234e92008-11-30 23:51:27 +01002389 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002390 if (msg->err_pos >= 0)
2391 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002392 txn->status = 408;
2393 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2394 msg->msg_state = HTTP_MSG_ERROR;
2395 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002396
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002397 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002398 if (s->listener->counters)
2399 s->listener->counters->failed_req++;
2400
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 if (!(s->flags & SN_FINST_MASK))
2402 s->flags |= SN_FINST_R;
2403 return 0;
2404 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002405
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 /* 4: have we encountered a close ? */
2407 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002408 if (!(s->flags & SN_ERR_MASK))
2409 s->flags |= SN_ERR_CLICL;
2410
Willy Tarreaufcffa692010-01-10 14:21:19 +01002411 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002412 goto failed_keep_alive;
2413
Willy Tarreau4076a152009-04-02 15:18:36 +02002414 if (msg->err_pos >= 0)
2415 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 txn->status = 400;
2417 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2418 msg->msg_state = HTTP_MSG_ERROR;
2419 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002420
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002421 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002422 if (s->listener->counters)
2423 s->listener->counters->failed_req++;
2424
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 if (!(s->flags & SN_FINST_MASK))
2426 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002427 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002428 }
2429
Willy Tarreau520d95e2009-09-19 21:04:57 +02002430 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002431 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002432 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002433
Willy Tarreaufcffa692010-01-10 14:21:19 +01002434 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2435 /* If the client starts to talk, let's fall back to
2436 * request timeout processing.
2437 */
2438 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002439 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002440 }
2441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002443 if (!tick_isset(req->analyse_exp)) {
2444 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2445 (txn->flags & TX_WAIT_NEXT_RQ) &&
2446 tick_isset(s->be->timeout.httpka))
2447 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2448 else
2449 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002451
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 /* we're not ready yet */
2453 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002454
2455 failed_keep_alive:
2456 /* Here we process low-level errors for keep-alive requests. In
2457 * short, if the request is not the first one and it experiences
2458 * a timeout, read error or shutdown, we just silently close so
2459 * that the client can try again.
2460 */
2461 txn->status = 0;
2462 msg->msg_state = HTTP_MSG_RQBEFORE;
2463 req->analysers = 0;
2464 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002465 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002466 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002467 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002469
Willy Tarreaud787e662009-07-07 10:14:51 +02002470 /* OK now we have a complete HTTP request with indexed headers. Let's
2471 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002472 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2473 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2474 * points to the CRLF of the request line. req->lr points to the first
2475 * byte after the last LF. msg->col and msg->sov point to the first
2476 * byte of data. msg->eol cannot be trusted because it may have been
2477 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002478 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002479
Willy Tarreaub16a5742010-01-10 14:46:16 +01002480 if (txn->flags & TX_WAIT_NEXT_RQ) {
2481 /* kill the pending keep-alive timeout */
2482 txn->flags &= ~TX_WAIT_NEXT_RQ;
2483 req->analyse_exp = TICK_ETERNITY;
2484 }
2485
2486
Willy Tarreaud787e662009-07-07 10:14:51 +02002487 /* Maybe we found in invalid header name while we were configured not
2488 * to block on that, so we have to capture it now.
2489 */
2490 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002491 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /*
2494 * 1: identify the method
2495 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002496 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002497
2498 /* we can make use of server redirect on GET and HEAD */
2499 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2500 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 /*
2503 * 2: check if the URI matches the monitor_uri.
2504 * We have to do this for every request which gets in, because
2505 * the monitor-uri is defined by the frontend.
2506 */
2507 if (unlikely((s->fe->monitor_uri_len != 0) &&
2508 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002509 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 s->fe->monitor_uri,
2511 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002512 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002514 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002520 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2521 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 ret = acl_pass(ret);
2524 if (cond->pol == ACL_COND_UNLESS)
2525 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 if (ret) {
2528 /* we fail this request, let's return 503 service unavail */
2529 txn->status = 503;
2530 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2531 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002532 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002533 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002534
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 /* nothing to fail, let's reply normaly */
2536 txn->status = 200;
2537 stream_int_retnclose(req->prod, &http_200_chunk);
2538 goto return_prx_cond;
2539 }
2540
2541 /*
2542 * 3: Maybe we have to copy the original REQURI for the logs ?
2543 * Note: we cannot log anymore if the request has been
2544 * classified as invalid.
2545 */
2546 if (unlikely(s->logs.logwait & LW_REQ)) {
2547 /* we have a complete HTTP request that we must log */
2548 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2549 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002550
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 if (urilen >= REQURI_LEN)
2552 urilen = REQURI_LEN - 1;
2553 memcpy(txn->uri, &req->data[msg->som], urilen);
2554 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002555
Willy Tarreau59234e92008-11-30 23:51:27 +01002556 if (!(s->logs.logwait &= ~LW_REQ))
2557 s->do_log(s);
2558 } else {
2559 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002560 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 }
Willy Tarreau06619262006-12-17 08:37:22 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002564 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2565 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002566
Willy Tarreau5b154472009-12-21 20:11:07 +01002567 /* ... and check if the request is HTTP/1.1 or above */
2568 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002569 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2570 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2571 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002572 txn->flags |= TX_REQ_VER_11;
2573
2574 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002575 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002576
Willy Tarreau88d349d2010-01-25 12:15:43 +01002577 /* if the frontend has "option http-use-proxy-header", we'll check if
2578 * we have what looks like a proxied connection instead of a connection,
2579 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2580 * Note that this is *not* RFC-compliant, however browsers and proxies
2581 * happen to do that despite being non-standard :-(
2582 * We consider that a request not beginning with either '/' or '*' is
2583 * a proxied connection, which covers both "scheme://location" and
2584 * CONNECT ip:port.
2585 */
2586 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2587 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2588 txn->flags |= TX_USE_PX_CONN;
2589
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002590 /* transfer length unknown*/
2591 txn->flags &= ~TX_REQ_XFER_LEN;
2592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /* 5: we may need to capture headers */
2594 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002595 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002597
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002598 /* 6: determine the transfer-length.
2599 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2600 * the presence of a message-body in a REQUEST and its transfer length
2601 * must be determined that way (in order of precedence) :
2602 * 1. The presence of a message-body in a request is signaled by the
2603 * inclusion of a Content-Length or Transfer-Encoding header field
2604 * in the request's header fields. When a request message contains
2605 * both a message-body of non-zero length and a method that does
2606 * not define any semantics for that request message-body, then an
2607 * origin server SHOULD either ignore the message-body or respond
2608 * with an appropriate error message (e.g., 413). A proxy or
2609 * gateway, when presented the same request, SHOULD either forward
2610 * the request inbound with the message- body or ignore the
2611 * message-body when determining a response.
2612 *
2613 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2614 * and the "chunked" transfer-coding (Section 6.2) is used, the
2615 * transfer-length is defined by the use of this transfer-coding.
2616 * If a Transfer-Encoding header field is present and the "chunked"
2617 * transfer-coding is not present, the transfer-length is defined
2618 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002619 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002620 * 3. If a Content-Length header field is present, its decimal value in
2621 * OCTETs represents both the entity-length and the transfer-length.
2622 * If a message is received with both a Transfer-Encoding header
2623 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002625 * 4. By the server closing the connection. (Closing the connection
2626 * cannot be used to indicate the end of a request body, since that
2627 * would leave no possibility for the server to send back a response.)
2628 *
2629 * Whenever a transfer-coding is applied to a message-body, the set of
2630 * transfer-codings MUST include "chunked", unless the message indicates
2631 * it is terminated by closing the connection. When the "chunked"
2632 * transfer-coding is used, it MUST be the last transfer-coding applied
2633 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002634 */
2635
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002636 /* CONNECT sets a tunnel and ignores everything else */
2637 if (txn->meth == HTTP_METH_CONNECT)
2638 goto skip_xfer_len;
2639
2640 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002641 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002642 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002643 while ((txn->flags & TX_REQ_VER_11) &&
2644 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002645 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2646 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2647 else if (txn->flags & TX_REQ_TE_CHNK) {
2648 /* bad transfer-encoding (chunked followed by something else) */
2649 use_close_only = 1;
2650 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2651 break;
2652 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002653 }
2654
Willy Tarreau32b47f42009-10-18 20:55:02 +02002655 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002656 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002657 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2658 signed long long cl;
2659
2660 if (!ctx.vlen)
2661 goto return_bad_req;
2662
2663 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2664 goto return_bad_req; /* parse failure */
2665
2666 if (cl < 0)
2667 goto return_bad_req;
2668
2669 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2670 goto return_bad_req; /* already specified, was different */
2671
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002672 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002673 msg->hdr_content_len = cl;
2674 }
2675
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002676 /* bodyless requests have a known length */
2677 if (!use_close_only)
2678 txn->flags |= TX_REQ_XFER_LEN;
2679
2680 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002681 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002682 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002683 req->analyse_exp = TICK_ETERNITY;
2684 return 1;
2685
2686 return_bad_req:
2687 /* We centralize bad requests processing here */
2688 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2689 /* we detected a parsing error. We want to archive this request
2690 * in the dedicated proxy area for later troubleshooting.
2691 */
2692 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2693 }
2694
2695 txn->req.msg_state = HTTP_MSG_ERROR;
2696 txn->status = 400;
2697 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002698
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002699 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002700 if (s->listener->counters)
2701 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002702
2703 return_prx_cond:
2704 if (!(s->flags & SN_ERR_MASK))
2705 s->flags |= SN_ERR_PRXCOND;
2706 if (!(s->flags & SN_FINST_MASK))
2707 s->flags |= SN_FINST_R;
2708
2709 req->analysers = 0;
2710 req->analyse_exp = TICK_ETERNITY;
2711 return 0;
2712}
2713
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714/* This stream analyser runs all HTTP request processing which is common to
2715 * frontends and backends, which means blocking ACLs, filters, connection-close,
2716 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002717 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002718 * either needs more data or wants to immediately abort the request (eg: deny,
2719 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002720 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002721int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002722{
Willy Tarreaud787e662009-07-07 10:14:51 +02002723 struct http_txn *txn = &s->txn;
2724 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002725 struct acl_cond *cond;
2726 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002727 struct wordlist *wl;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002728 int del_ka, del_cl;
Willy Tarreaud787e662009-07-07 10:14:51 +02002729
Willy Tarreau655dce92009-11-08 13:10:58 +01002730 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002731 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002732 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002733 return 0;
2734 }
2735
Willy Tarreau3a816292009-07-07 10:55:49 +02002736 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002737 req->analyse_exp = TICK_ETERNITY;
2738
2739 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2740 now_ms, __FUNCTION__,
2741 s,
2742 req,
2743 req->rex, req->wex,
2744 req->flags,
2745 req->l,
2746 req->analysers);
2747
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748 /* first check whether we have some ACLs set to block this request */
2749 list_for_each_entry(cond, &px->block_cond, list) {
2750 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 ret = acl_pass(ret);
2753 if (cond->pol == ACL_COND_UNLESS)
2754 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002755
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 if (ret) {
2757 txn->status = 403;
2758 /* let's log the request time */
2759 s->logs.tv_request = now;
2760 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2761 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002764
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 /* try headers filters */
2766 if (px->req_exp != NULL) {
2767 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2768 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* has the request been denied ? */
2771 if (txn->flags & TX_CLDENY) {
2772 /* no need to go further */
2773 txn->status = 403;
2774 /* let's log the request time */
2775 s->logs.tv_request = now;
2776 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2777 goto return_prx_cond;
2778 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002779
2780 /* When a connection is tarpitted, we use the tarpit timeout,
2781 * which may be the same as the connect timeout if unspecified.
2782 * If unset, then set it to zero because we really want it to
2783 * eventually expire. We build the tarpit as an analyser.
2784 */
2785 if (txn->flags & TX_CLTARPIT) {
2786 buffer_erase(s->req);
2787 /* wipe the request out so that we can drop the connection early
2788 * if the client closes first.
2789 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002790 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002791 req->analysers = 0; /* remove switching rules etc... */
2792 req->analysers |= AN_REQ_HTTP_TARPIT;
2793 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2794 if (!req->analyse_exp)
2795 req->analyse_exp = tick_add(now_ms, 0);
2796 return 1;
2797 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798 }
Willy Tarreau06619262006-12-17 08:37:22 +01002799
Willy Tarreau5b154472009-12-21 20:11:07 +01002800 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2801 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002802 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2803 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002804 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2805 * avoid to redo the same work if FE and BE have the same settings (common).
2806 * The method consists in checking if options changed between the two calls
2807 * (implying that either one is non-null, or one of them is non-null and we
2808 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002809 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002810
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002811 del_cl = del_ka = 0;
2812
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002813 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002814 ((!(txn->flags & TX_HDR_CONN_PRS) &&
2815 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2816 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2817 (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 +01002818 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002819
Willy Tarreau5b154472009-12-21 20:11:07 +01002820 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2821 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002822 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2823 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002824 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002825 tmp = TX_CON_WANT_CLO;
2826
Willy Tarreau5b154472009-12-21 20:11:07 +01002827 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2828 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002829
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002830 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2831 /* parse the Connection header and possibly clean it */
2832 int to_del = 0;
2833 if ((txn->flags & TX_REQ_VER_11) ||
2834 (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)
2835 to_del |= 2; /* remove "keep-alive" */
2836 if (!(txn->flags & TX_REQ_VER_11))
2837 to_del |= 1; /* remove "close" */
2838 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002839 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002840
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002841 /* check if client or config asks for explicit close in KAL/SCL */
2842 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2843 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2844 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2845 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2846 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002847 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2848 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002849 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2850 }
Willy Tarreau78599912009-10-17 20:12:21 +02002851
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002852 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002853 list_for_each_entry(wl, &px->req_add, list) {
2854 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002855 goto return_bad_req;
2856 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002857
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002858 /* check if stats URI was requested, and if an auth is needed */
2859 if (px->uri_auth != NULL &&
2860 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2861 /* we have to check the URI and auth for this request.
2862 * FIXME!!! that one is rather dangerous, we want to
2863 * make it follow standard rules (eg: clear req->analysers).
2864 */
2865 if (stats_check_uri_auth(s, px)) {
2866 req->analysers = 0;
2867 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002869 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002870
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002871 /* check whether we have some ACLs set to redirect this request */
2872 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002873 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002874
Willy Tarreauf285f542010-01-03 20:03:03 +01002875 if (rule->cond) {
2876 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2877 ret = acl_pass(ret);
2878 if (rule->cond->pol == ACL_COND_UNLESS)
2879 ret = !ret;
2880 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002881
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002882 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002883 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002884 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002885
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002886 /* build redirect message */
2887 switch(rule->code) {
2888 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002889 msg_fmt = HTTP_303;
2890 break;
2891 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002892 msg_fmt = HTTP_301;
2893 break;
2894 case 302:
2895 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002896 msg_fmt = HTTP_302;
2897 break;
2898 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002899
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002900 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002901 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002902
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002903 switch(rule->type) {
2904 case REDIRECT_TYPE_PREFIX: {
2905 const char *path;
2906 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002907
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002908 path = http_get_path(txn);
2909 /* build message using path */
2910 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002911 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002912 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2913 int qs = 0;
2914 while (qs < pathlen) {
2915 if (path[qs] == '?') {
2916 pathlen = qs;
2917 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002918 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002919 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002920 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002921 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002922 } else {
2923 path = "/";
2924 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002925 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002926
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002927 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002928 goto return_bad_req;
2929
2930 /* add prefix. Note that if prefix == "/", we don't want to
2931 * add anything, otherwise it makes it hard for the user to
2932 * configure a self-redirection.
2933 */
2934 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002935 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2936 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002937 }
2938
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002939 /* add path */
2940 memcpy(rdr.str + rdr.len, path, pathlen);
2941 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01002942
2943 /* append a slash at the end of the location is needed and missing */
2944 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
2945 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2946 if (rdr.len > rdr.size - 5)
2947 goto return_bad_req;
2948 rdr.str[rdr.len] = '/';
2949 rdr.len++;
2950 }
2951
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002952 break;
2953 }
2954 case REDIRECT_TYPE_LOCATION:
2955 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002956 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002957 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002958
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002959 /* add location */
2960 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2961 rdr.len += rule->rdr_len;
2962 break;
2963 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002964
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002965 if (rule->cookie_len) {
2966 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2967 rdr.len += 14;
2968 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2969 rdr.len += rule->cookie_len;
2970 memcpy(rdr.str + rdr.len, "\r\n", 2);
2971 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002972 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002973
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002974 /* add end of headers and the keep-alive/close status.
2975 * We may choose to set keep-alive if the Location begins
2976 * with a slash, because the client will come back to the
2977 * same server.
2978 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002979 txn->status = rule->code;
2980 /* let's log the request time */
2981 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002982
2983 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2984 (txn->flags & TX_REQ_XFER_LEN) &&
2985 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2986 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2987 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2988 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01002989 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01002990 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2991 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
2992 rdr.len += 30;
2993 } else {
2994 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
2995 rdr.len += 24;
2996 }
Willy Tarreau75661452010-01-10 10:35:01 +01002997 }
2998 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2999 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003000 buffer_write(req->prod->ob, rdr.str, rdr.len);
3001 /* "eat" the request */
3002 buffer_ignore(req, msg->sov - msg->som);
3003 msg->som = msg->sov;
3004 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003005 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3006 txn->req.msg_state = HTTP_MSG_CLOSED;
3007 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003008 break;
3009 } else {
3010 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003011 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3012 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3013 rdr.len += 29;
3014 } else {
3015 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3016 rdr.len += 23;
3017 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003018 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003019 goto return_prx_cond;
3020 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021 }
3022 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003023
Willy Tarreau2be39392010-01-03 17:24:51 +01003024 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3025 * If this happens, then the data will not come immediately, so we must
3026 * send all what we have without waiting. Note that due to the small gain
3027 * in waiting for the body of the request, it's easier to simply put the
3028 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3029 * itself once used.
3030 */
3031 req->flags |= BF_SEND_DONTWAIT;
3032
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003033 /* that's OK for us now, let's move on to next analysers */
3034 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 return_bad_req:
3037 /* We centralize bad requests processing here */
3038 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3039 /* we detected a parsing error. We want to archive this request
3040 * in the dedicated proxy area for later troubleshooting.
3041 */
3042 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3043 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 txn->req.msg_state = HTTP_MSG_ERROR;
3046 txn->status = 400;
3047 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003048
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003049 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003050 if (s->listener->counters)
3051 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 return_prx_cond:
3054 if (!(s->flags & SN_ERR_MASK))
3055 s->flags |= SN_ERR_PRXCOND;
3056 if (!(s->flags & SN_FINST_MASK))
3057 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 req->analysers = 0;
3060 req->analyse_exp = TICK_ETERNITY;
3061 return 0;
3062}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003063
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003064/* This function performs all the processing enabled for the current request.
3065 * It returns 1 if the processing can continue on next analysers, or zero if it
3066 * needs more data, encounters an error, or wants to immediately abort the
3067 * request. It relies on buffers flags, and updates s->req->analysers.
3068 */
3069int http_process_request(struct session *s, struct buffer *req, int an_bit)
3070{
3071 struct http_txn *txn = &s->txn;
3072 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003073
Willy Tarreau655dce92009-11-08 13:10:58 +01003074 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003075 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003076 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003077 return 0;
3078 }
3079
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3081 now_ms, __FUNCTION__,
3082 s,
3083 req,
3084 req->rex, req->wex,
3085 req->flags,
3086 req->l,
3087 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003088
Willy Tarreau59234e92008-11-30 23:51:27 +01003089 /*
3090 * Right now, we know that we have processed the entire headers
3091 * and that unwanted requests have been filtered out. We can do
3092 * whatever we want with the remaining request. Also, now we
3093 * may have separate values for ->fe, ->be.
3094 */
Willy Tarreau06619262006-12-17 08:37:22 +01003095
Willy Tarreau59234e92008-11-30 23:51:27 +01003096 /*
3097 * If HTTP PROXY is set we simply get remote server address
3098 * parsing incoming request.
3099 */
3100 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003101 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003103
Willy Tarreau59234e92008-11-30 23:51:27 +01003104 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003105 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 * Note that doing so might move headers in the request, but
3107 * the fields will stay coherent and the URI will not move.
3108 * This should only be performed in the backend.
3109 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003110 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003111 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3112 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003113
Willy Tarreau59234e92008-11-30 23:51:27 +01003114 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003115 * 8: the appsession cookie was looked up very early in 1.2,
3116 * so let's do the same now.
3117 */
3118
3119 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003120 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003121 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003122 }
3123
3124 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003125 * 9: add X-Forwarded-For if either the frontend or the backend
3126 * asks for it.
3127 */
3128 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3129 if (s->cli_addr.ss_family == AF_INET) {
3130 /* Add an X-Forwarded-For header unless the source IP is
3131 * in the 'except' network range.
3132 */
3133 if ((!s->fe->except_mask.s_addr ||
3134 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3135 != s->fe->except_net.s_addr) &&
3136 (!s->be->except_mask.s_addr ||
3137 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3138 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003139 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003140 unsigned char *pn;
3141 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003142
3143 /* Note: we rely on the backend to get the header name to be used for
3144 * x-forwarded-for, because the header is really meant for the backends.
3145 * However, if the backend did not specify any option, we have to rely
3146 * on the frontend's header name.
3147 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003148 if (s->be->fwdfor_hdr_len) {
3149 len = s->be->fwdfor_hdr_len;
3150 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003151 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003152 len = s->fe->fwdfor_hdr_len;
3153 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003154 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003155 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003156
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003157 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003158 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003159 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003160 }
3161 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003162 else if (s->cli_addr.ss_family == AF_INET6) {
3163 /* FIXME: for the sake of completeness, we should also support
3164 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003165 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003166 int len;
3167 char pn[INET6_ADDRSTRLEN];
3168 inet_ntop(AF_INET6,
3169 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3170 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003171
Willy Tarreau59234e92008-11-30 23:51:27 +01003172 /* Note: we rely on the backend to get the header name to be used for
3173 * x-forwarded-for, because the header is really meant for the backends.
3174 * However, if the backend did not specify any option, we have to rely
3175 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003176 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003177 if (s->be->fwdfor_hdr_len) {
3178 len = s->be->fwdfor_hdr_len;
3179 memcpy(trash, s->be->fwdfor_hdr_name, len);
3180 } else {
3181 len = s->fe->fwdfor_hdr_len;
3182 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003183 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003184 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003185
Willy Tarreau59234e92008-11-30 23:51:27 +01003186 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003187 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003188 goto return_bad_req;
3189 }
3190 }
3191
3192 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003193 * 10: add X-Original-To if either the frontend or the backend
3194 * asks for it.
3195 */
3196 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3197
3198 /* FIXME: don't know if IPv6 can handle that case too. */
3199 if (s->cli_addr.ss_family == AF_INET) {
3200 /* Add an X-Original-To header unless the destination IP is
3201 * in the 'except' network range.
3202 */
3203 if (!(s->flags & SN_FRT_ADDR_SET))
3204 get_frt_addr(s);
3205
3206 if ((!s->fe->except_mask_to.s_addr ||
3207 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3208 != s->fe->except_to.s_addr) &&
3209 (!s->be->except_mask_to.s_addr ||
3210 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3211 != s->be->except_to.s_addr)) {
3212 int len;
3213 unsigned char *pn;
3214 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3215
3216 /* Note: we rely on the backend to get the header name to be used for
3217 * x-original-to, because the header is really meant for the backends.
3218 * However, if the backend did not specify any option, we have to rely
3219 * on the frontend's header name.
3220 */
3221 if (s->be->orgto_hdr_len) {
3222 len = s->be->orgto_hdr_len;
3223 memcpy(trash, s->be->orgto_hdr_name, len);
3224 } else {
3225 len = s->fe->orgto_hdr_len;
3226 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003227 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003228 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3229
3230 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003231 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003232 goto return_bad_req;
3233 }
3234 }
3235 }
3236
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003237 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3238 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3239 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3240 unsigned int want_flags = 0;
3241
3242 if (txn->flags & TX_REQ_VER_11) {
3243 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3244 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3245 want_flags |= TX_CON_CLO_SET;
3246 } else {
3247 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
3248 want_flags |= TX_CON_KAL_SET;
3249 }
3250
3251 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3252 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003253 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003254
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003255
Willy Tarreau522d6c02009-12-06 18:49:18 +01003256 /* If we have no server assigned yet and we're balancing on url_param
3257 * with a POST request, we may be interested in checking the body for
3258 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 */
3260 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3261 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003262 s->be->url_param_post_limit != 0 &&
3263 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003264 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003265 buffer_dont_connect(req);
3266 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003268
Willy Tarreaud98cf932009-12-27 22:54:55 +01003269 if (txn->flags & TX_REQ_XFER_LEN)
3270 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003271
Willy Tarreau59234e92008-11-30 23:51:27 +01003272 /*************************************************************
3273 * OK, that's finished for the headers. We have done what we *
3274 * could. Let's switch to the DATA state. *
3275 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003276 req->analyse_exp = TICK_ETERNITY;
3277 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003278
Willy Tarreau59234e92008-11-30 23:51:27 +01003279 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003280 /* OK let's go on with the BODY now */
3281 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003282
Willy Tarreau59234e92008-11-30 23:51:27 +01003283 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003284 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003285 /* we detected a parsing error. We want to archive this request
3286 * in the dedicated proxy area for later troubleshooting.
3287 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003288 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003289 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003290
Willy Tarreau59234e92008-11-30 23:51:27 +01003291 txn->req.msg_state = HTTP_MSG_ERROR;
3292 txn->status = 400;
3293 req->analysers = 0;
3294 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003295
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003296 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003297 if (s->listener->counters)
3298 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003299
Willy Tarreau59234e92008-11-30 23:51:27 +01003300 if (!(s->flags & SN_ERR_MASK))
3301 s->flags |= SN_ERR_PRXCOND;
3302 if (!(s->flags & SN_FINST_MASK))
3303 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003304 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003305}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003306
Willy Tarreau60b85b02008-11-30 23:28:40 +01003307/* This function is an analyser which processes the HTTP tarpit. It always
3308 * returns zero, at the beginning because it prevents any other processing
3309 * from occurring, and at the end because it terminates the request.
3310 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003311int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003312{
3313 struct http_txn *txn = &s->txn;
3314
3315 /* This connection is being tarpitted. The CLIENT side has
3316 * already set the connect expiration date to the right
3317 * timeout. We just have to check that the client is still
3318 * there and that the timeout has not expired.
3319 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003320 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003321 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3322 !tick_is_expired(req->analyse_exp, now_ms))
3323 return 0;
3324
3325 /* We will set the queue timer to the time spent, just for
3326 * logging purposes. We fake a 500 server error, so that the
3327 * attacker will not suspect his connection has been tarpitted.
3328 * It will not cause trouble to the logs because we can exclude
3329 * the tarpitted connections by filtering on the 'PT' status flags.
3330 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003331 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3332
3333 txn->status = 500;
3334 if (req->flags != BF_READ_ERROR)
3335 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3336
3337 req->analysers = 0;
3338 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003339
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003340 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003341 if (s->listener->counters)
3342 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003343
Willy Tarreau60b85b02008-11-30 23:28:40 +01003344 if (!(s->flags & SN_ERR_MASK))
3345 s->flags |= SN_ERR_PRXCOND;
3346 if (!(s->flags & SN_FINST_MASK))
3347 s->flags |= SN_FINST_T;
3348 return 0;
3349}
3350
Willy Tarreaud34af782008-11-30 23:36:37 +01003351/* This function is an analyser which processes the HTTP request body. It looks
3352 * for parameters to be used for the load balancing algorithm (url_param). It
3353 * must only be called after the standard HTTP request processing has occurred,
3354 * because it expects the request to be parsed. It returns zero if it needs to
3355 * read more data, or 1 once it has completed its analysis.
3356 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003357int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003358{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003359 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003360 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003361 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003362
3363 /* We have to parse the HTTP request body to find any required data.
3364 * "balance url_param check_post" should have been the only way to get
3365 * into this. We were brought here after HTTP header analysis, so all
3366 * related structures are ready.
3367 */
3368
Willy Tarreau522d6c02009-12-06 18:49:18 +01003369 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3370 goto missing_data;
3371
3372 if (msg->msg_state < HTTP_MSG_100_SENT) {
3373 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3374 * send an HTTP/1.1 100 Continue intermediate response.
3375 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003376 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003377 struct hdr_ctx ctx;
3378 ctx.idx = 0;
3379 /* Expect is allowed in 1.1, look for it */
3380 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3381 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3382 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3383 }
3384 }
3385 msg->msg_state = HTTP_MSG_100_SENT;
3386 }
3387
3388 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003389 /* we have msg->col and msg->sov which both point to the first
3390 * byte of message body. msg->som still points to the beginning
3391 * of the message. We must save the body in req->lr because it
3392 * survives buffer re-alignments.
3393 */
3394 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003395 if (txn->flags & TX_REQ_TE_CHNK)
3396 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3397 else
3398 msg->msg_state = HTTP_MSG_DATA;
3399 }
3400
3401 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003402 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003403 * set ->sov and ->lr to point to the body and switch to DATA or
3404 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003405 */
3406 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003407
Willy Tarreau115acb92009-12-26 13:56:06 +01003408 if (!ret)
3409 goto missing_data;
3410 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003411 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003412 }
3413
Willy Tarreaud98cf932009-12-27 22:54:55 +01003414 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003415 * We have the first non-header byte in msg->col, which is either the
3416 * beginning of the chunk size or of the data. The first data byte is in
3417 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3418 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003419 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003420
3421 if (msg->hdr_content_len < limit)
3422 limit = msg->hdr_content_len;
3423
Willy Tarreau7c96f672009-12-27 22:47:25 +01003424 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003425 goto http_end;
3426
3427 missing_data:
3428 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003429 if (req->flags & BF_FULL)
3430 goto return_bad_req;
3431
Willy Tarreau522d6c02009-12-06 18:49:18 +01003432 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3433 txn->status = 408;
3434 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3435 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003436 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003437
3438 /* we get here if we need to wait for more data */
3439 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003440 /* Not enough data. We'll re-use the http-request
3441 * timeout here. Ideally, we should set the timeout
3442 * relative to the accept() date. We just set the
3443 * request timeout once at the beginning of the
3444 * request.
3445 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003446 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003447 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003448 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003449 return 0;
3450 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003451
3452 http_end:
3453 /* The situation will not evolve, so let's give up on the analysis. */
3454 s->logs.tv_request = now; /* update the request timer to reflect full request */
3455 req->analysers &= ~an_bit;
3456 req->analyse_exp = TICK_ETERNITY;
3457 return 1;
3458
3459 return_bad_req: /* let's centralize all bad requests */
3460 txn->req.msg_state = HTTP_MSG_ERROR;
3461 txn->status = 400;
3462 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3463
3464 return_err_msg:
3465 req->analysers = 0;
3466 s->fe->counters.failed_req++;
3467 if (s->listener->counters)
3468 s->listener->counters->failed_req++;
3469
3470 if (!(s->flags & SN_ERR_MASK))
3471 s->flags |= SN_ERR_PRXCOND;
3472 if (!(s->flags & SN_FINST_MASK))
3473 s->flags |= SN_FINST_R;
3474 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003475}
3476
Willy Tarreau610ecce2010-01-04 21:15:02 +01003477/* Terminate current transaction and prepare a new one. This is very tricky
3478 * right now but it works.
3479 */
3480void http_end_txn_clean_session(struct session *s)
3481{
3482 /* FIXME: We need a more portable way of releasing a backend's and a
3483 * server's connections. We need a safer way to reinitialize buffer
3484 * flags. We also need a more accurate method for computing per-request
3485 * data.
3486 */
3487 http_silent_debug(__LINE__, s);
3488
3489 s->req->cons->flags |= SI_FL_NOLINGER;
3490 s->req->cons->shutr(s->req->cons);
3491 s->req->cons->shutw(s->req->cons);
3492
3493 http_silent_debug(__LINE__, s);
3494
3495 if (s->flags & SN_BE_ASSIGNED)
3496 s->be->beconn--;
3497
3498 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3499 session_process_counters(s);
3500
3501 if (s->txn.status) {
3502 int n;
3503
3504 n = s->txn.status / 100;
3505 if (n < 1 || n > 5)
3506 n = 0;
3507
3508 if (s->fe->mode == PR_MODE_HTTP)
3509 s->fe->counters.p.http.rsp[n]++;
3510
3511 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3512 (s->be->mode == PR_MODE_HTTP))
3513 s->be->counters.p.http.rsp[n]++;
3514 }
3515
3516 /* don't count other requests' data */
3517 s->logs.bytes_in -= s->req->l - s->req->send_max;
3518 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3519
3520 /* let's do a final log if we need it */
3521 if (s->logs.logwait &&
3522 !(s->flags & SN_MONITOR) &&
3523 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3524 s->do_log(s);
3525 }
3526
3527 s->logs.accept_date = date; /* user-visible date for logging */
3528 s->logs.tv_accept = now; /* corrected date for internal use */
3529 tv_zero(&s->logs.tv_request);
3530 s->logs.t_queue = -1;
3531 s->logs.t_connect = -1;
3532 s->logs.t_data = -1;
3533 s->logs.t_close = 0;
3534 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3535 s->logs.srv_queue_size = 0; /* we will get this number soon */
3536
3537 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3538 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3539
3540 if (s->pend_pos)
3541 pendconn_free(s->pend_pos);
3542
3543 if (s->srv) {
3544 if (s->flags & SN_CURR_SESS) {
3545 s->flags &= ~SN_CURR_SESS;
3546 s->srv->cur_sess--;
3547 }
3548 if (may_dequeue_tasks(s->srv, s->be))
3549 process_srv_queue(s->srv);
3550 }
3551
3552 if (unlikely(s->srv_conn))
3553 sess_change_server(s, NULL);
3554 s->srv = NULL;
3555
3556 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3557 s->req->cons->fd = -1; /* just to help with debugging */
3558 s->req->cons->err_type = SI_ET_NONE;
3559 s->req->cons->err_loc = NULL;
3560 s->req->cons->exp = TICK_ETERNITY;
3561 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003562 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3563 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 Tarreau4de91492010-01-22 19:10:05 +01003564 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003565 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3566 s->txn.meth = 0;
3567 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003568 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003569 if (s->be->options2 & PR_O2_INDEPSTR)
3570 s->req->cons->flags |= SI_FL_INDEP_STR;
3571
3572 /* if the request buffer is not empty, it means we're
3573 * about to process another request, so send pending
3574 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003575 * Just don't do this if the buffer is close to be full,
3576 * because the request will wait for it to flush a little
3577 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003578 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003579 if (s->req->l > s->req->send_max) {
3580 if (s->rep->send_max &&
3581 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003582 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3583 s->rep->flags |= BF_EXPECT_MORE;
3584 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003585
3586 /* we're removing the analysers, we MUST re-enable events detection */
3587 buffer_auto_read(s->req);
3588 buffer_auto_close(s->req);
3589 buffer_auto_read(s->rep);
3590 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003591
3592 /* make ->lr point to the first non-forwarded byte */
3593 s->req->lr = s->req->w + s->req->send_max;
3594 if (s->req->lr >= s->req->data + s->req->size)
3595 s->req->lr -= s->req->size;
3596 s->rep->lr = s->rep->w + s->rep->send_max;
3597 if (s->rep->lr >= s->rep->data + s->rep->size)
3598 s->rep->lr -= s->req->size;
3599
3600 s->req->analysers |= s->fe->fe_req_ana;
3601 s->rep->analysers = 0;
3602
3603 http_silent_debug(__LINE__, s);
3604}
3605
3606
3607/* This function updates the request state machine according to the response
3608 * state machine and buffer flags. It returns 1 if it changes anything (flag
3609 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3610 * it is only used to find when a request/response couple is complete. Both
3611 * this function and its equivalent should loop until both return zero. It
3612 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3613 */
3614int http_sync_req_state(struct session *s)
3615{
3616 struct buffer *buf = s->req;
3617 struct http_txn *txn = &s->txn;
3618 unsigned int old_flags = buf->flags;
3619 unsigned int old_state = txn->req.msg_state;
3620
3621 http_silent_debug(__LINE__, s);
3622 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3623 return 0;
3624
3625 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003626 /* No need to read anymore, the request was completely parsed.
3627 * We can shut the read side unless we want to abort_on_close.
3628 */
3629 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3630 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003631
3632 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3633 goto wait_other_side;
3634
3635 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3636 /* The server has not finished to respond, so we
3637 * don't want to move in order not to upset it.
3638 */
3639 goto wait_other_side;
3640 }
3641
3642 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3643 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003644 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003645 txn->req.msg_state = HTTP_MSG_TUNNEL;
3646 goto wait_other_side;
3647 }
3648
3649 /* When we get here, it means that both the request and the
3650 * response have finished receiving. Depending on the connection
3651 * mode, we'll have to wait for the last bytes to leave in either
3652 * direction, and sometimes for a close to be effective.
3653 */
3654
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003655 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3656 /* Server-close mode : queue a connection close to the server */
3657 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003658 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003659 }
3660 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3661 /* Option forceclose is set, or either side wants to close,
3662 * let's enforce it now that we're not expecting any new
3663 * data to come. The caller knows the session is complete
3664 * once both states are CLOSED.
3665 */
3666 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003667 buffer_shutr_now(buf);
3668 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003669 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003670 }
3671 else {
3672 /* The last possible modes are keep-alive and tunnel. Since tunnel
3673 * mode does not set the body analyser, we can't reach this place
3674 * in tunnel mode, so we're left with keep-alive only.
3675 * This mode is currently not implemented, we switch to tunnel mode.
3676 */
3677 buffer_auto_read(buf);
3678 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003679 }
3680
3681 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3682 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003683 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3684
Willy Tarreau610ecce2010-01-04 21:15:02 +01003685 if (!(buf->flags & BF_OUT_EMPTY)) {
3686 txn->req.msg_state = HTTP_MSG_CLOSING;
3687 goto http_msg_closing;
3688 }
3689 else {
3690 txn->req.msg_state = HTTP_MSG_CLOSED;
3691 goto http_msg_closed;
3692 }
3693 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003694 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003695 }
3696
3697 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3698 http_msg_closing:
3699 /* nothing else to forward, just waiting for the output buffer
3700 * to be empty and for the shutw_now to take effect.
3701 */
3702 if (buf->flags & BF_OUT_EMPTY) {
3703 txn->req.msg_state = HTTP_MSG_CLOSED;
3704 goto http_msg_closed;
3705 }
3706 else if (buf->flags & BF_SHUTW) {
3707 txn->req.msg_state = HTTP_MSG_ERROR;
3708 goto wait_other_side;
3709 }
3710 }
3711
3712 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3713 http_msg_closed:
3714 goto wait_other_side;
3715 }
3716
3717 wait_other_side:
3718 http_silent_debug(__LINE__, s);
3719 return txn->req.msg_state != old_state || buf->flags != old_flags;
3720}
3721
3722
3723/* This function updates the response state machine according to the request
3724 * state machine and buffer flags. It returns 1 if it changes anything (flag
3725 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3726 * it is only used to find when a request/response couple is complete. Both
3727 * this function and its equivalent should loop until both return zero. It
3728 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3729 */
3730int http_sync_res_state(struct session *s)
3731{
3732 struct buffer *buf = s->rep;
3733 struct http_txn *txn = &s->txn;
3734 unsigned int old_flags = buf->flags;
3735 unsigned int old_state = txn->rsp.msg_state;
3736
3737 http_silent_debug(__LINE__, s);
3738 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3739 return 0;
3740
3741 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3742 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003743 * still monitor the server connection for a possible close
3744 * while the request is being uploaded, so we don't disable
3745 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003747 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748
3749 if (txn->req.msg_state == HTTP_MSG_ERROR)
3750 goto wait_other_side;
3751
3752 if (txn->req.msg_state < HTTP_MSG_DONE) {
3753 /* The client seems to still be sending data, probably
3754 * because we got an error response during an upload.
3755 * We have the choice of either breaking the connection
3756 * or letting it pass through. Let's do the later.
3757 */
3758 goto wait_other_side;
3759 }
3760
3761 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3762 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003763 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003764 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3765 goto wait_other_side;
3766 }
3767
3768 /* When we get here, it means that both the request and the
3769 * response have finished receiving. Depending on the connection
3770 * mode, we'll have to wait for the last bytes to leave in either
3771 * direction, and sometimes for a close to be effective.
3772 */
3773
3774 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3775 /* Server-close mode : shut read and wait for the request
3776 * side to close its output buffer. The caller will detect
3777 * when we're in DONE and the other is in CLOSED and will
3778 * catch that for the final cleanup.
3779 */
3780 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3781 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003783 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3784 /* Option forceclose is set, or either side wants to close,
3785 * let's enforce it now that we're not expecting any new
3786 * data to come. The caller knows the session is complete
3787 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003789 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3790 buffer_shutr_now(buf);
3791 buffer_shutw_now(buf);
3792 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793 }
3794 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003795 /* The last possible modes are keep-alive and tunnel. Since tunnel
3796 * mode does not set the body analyser, we can't reach this place
3797 * in tunnel mode, so we're left with keep-alive only.
3798 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003800 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802 }
3803
3804 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3805 /* if we've just closed an output, let's switch */
3806 if (!(buf->flags & BF_OUT_EMPTY)) {
3807 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3808 goto http_msg_closing;
3809 }
3810 else {
3811 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3812 goto http_msg_closed;
3813 }
3814 }
3815 goto wait_other_side;
3816 }
3817
3818 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3819 http_msg_closing:
3820 /* nothing else to forward, just waiting for the output buffer
3821 * to be empty and for the shutw_now to take effect.
3822 */
3823 if (buf->flags & BF_OUT_EMPTY) {
3824 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3825 goto http_msg_closed;
3826 }
3827 else if (buf->flags & BF_SHUTW) {
3828 txn->rsp.msg_state = HTTP_MSG_ERROR;
3829 goto wait_other_side;
3830 }
3831 }
3832
3833 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3834 http_msg_closed:
3835 /* drop any pending data */
3836 buffer_ignore(buf, buf->l - buf->send_max);
3837 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003838 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003839 goto wait_other_side;
3840 }
3841
3842 wait_other_side:
3843 http_silent_debug(__LINE__, s);
3844 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3845}
3846
3847
3848/* Resync the request and response state machines. Return 1 if either state
3849 * changes.
3850 */
3851int http_resync_states(struct session *s)
3852{
3853 struct http_txn *txn = &s->txn;
3854 int old_req_state = txn->req.msg_state;
3855 int old_res_state = txn->rsp.msg_state;
3856
3857 http_silent_debug(__LINE__, s);
3858 http_sync_req_state(s);
3859 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003860 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003861 if (!http_sync_res_state(s))
3862 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003863 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 if (!http_sync_req_state(s))
3865 break;
3866 }
3867 http_silent_debug(__LINE__, s);
3868 /* OK, both state machines agree on a compatible state.
3869 * There are a few cases we're interested in :
3870 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3871 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3872 * directions, so let's simply disable both analysers.
3873 * - HTTP_MSG_CLOSED on the response only means we must abort the
3874 * request.
3875 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3876 * with server-close mode means we've completed one request and we
3877 * must re-initialize the server connection.
3878 */
3879
3880 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3881 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3882 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3883 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3884 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003885 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003886 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003888 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003889 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003890 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003891 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3892 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3893 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003894 s->rep->analysers = 0;
3895 buffer_auto_close(s->rep);
3896 buffer_auto_read(s->rep);
3897 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 buffer_abort(s->req);
3899 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003900 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003901 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003902 }
3903 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3904 txn->rsp.msg_state == HTTP_MSG_DONE &&
3905 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3906 /* server-close: terminate this server connection and
3907 * reinitialize a fresh-new transaction.
3908 */
3909 http_end_txn_clean_session(s);
3910 }
3911
3912 http_silent_debug(__LINE__, s);
3913 return txn->req.msg_state != old_req_state ||
3914 txn->rsp.msg_state != old_res_state;
3915}
3916
Willy Tarreaud98cf932009-12-27 22:54:55 +01003917/* This function is an analyser which forwards request body (including chunk
3918 * sizes if any). It is called as soon as we must forward, even if we forward
3919 * zero byte. The only situation where it must not be called is when we're in
3920 * tunnel mode and we want to forward till the close. It's used both to forward
3921 * remaining data and to resync after end of body. It expects the msg_state to
3922 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3923 * read more data, or 1 once we can go on with next request or end the session.
3924 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3925 * bytes of pending data + the headers if not already done (between som and sov).
3926 * It eventually adjusts som to match sov after the data in between have been sent.
3927 */
3928int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3929{
3930 struct http_txn *txn = &s->txn;
3931 struct http_msg *msg = &s->txn.req;
3932
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003933 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3934 return 0;
3935
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003936 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3937 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3938 /* Output closed while we were sending data. We must abort. */
3939 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003940 buffer_auto_read(req);
3941 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003942 req->analysers &= ~an_bit;
3943 return 1;
3944 }
3945
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003946 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003947
3948 /* Note that we don't have to send 100-continue back because we don't
3949 * need the data to complete our job, and it's up to the server to
3950 * decide whether to return 100, 417 or anything else in return of
3951 * an "Expect: 100-continue" header.
3952 */
3953
3954 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3955 /* we have msg->col and msg->sov which both point to the first
3956 * byte of message body. msg->som still points to the beginning
3957 * of the message. We must save the body in req->lr because it
3958 * survives buffer re-alignments.
3959 */
3960 req->lr = req->data + msg->sov;
3961 if (txn->flags & TX_REQ_TE_CHNK)
3962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3963 else {
3964 msg->msg_state = HTTP_MSG_DATA;
3965 }
3966 }
3967
Willy Tarreaud98cf932009-12-27 22:54:55 +01003968 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003970 /* we may have some data pending */
3971 if (msg->hdr_content_len || msg->som != msg->sov) {
3972 int bytes = msg->sov - msg->som;
3973 if (bytes < 0) /* sov may have wrapped at the end */
3974 bytes += req->size;
3975 buffer_forward(req, bytes + msg->hdr_content_len);
3976 msg->hdr_content_len = 0; /* don't forward that again */
3977 msg->som = msg->sov;
3978 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003979
Willy Tarreaucaabe412010-01-03 23:08:28 +01003980 if (msg->msg_state == HTTP_MSG_DATA) {
3981 /* must still forward */
3982 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003983 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003984
3985 /* nothing left to forward */
3986 if (txn->flags & TX_REQ_TE_CHNK)
3987 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003989 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003990 }
3991 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003992 /* read the chunk size and assign it to ->hdr_content_len, then
3993 * set ->sov and ->lr to point to the body and switch to DATA or
3994 * TRAILERS state.
3995 */
3996 int ret = http_parse_chunk_size(req, msg);
3997
3998 if (!ret)
3999 goto missing_data;
4000 else if (ret < 0)
4001 goto return_bad_req;
4002 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004003 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004004 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4005 /* we want the CRLF after the data */
4006 int ret;
4007
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004008 req->lr = req->w + req->send_max;
4009 if (req->lr >= req->data + req->size)
4010 req->lr -= req->size;
4011
Willy Tarreaud98cf932009-12-27 22:54:55 +01004012 ret = http_skip_chunk_crlf(req, msg);
4013
4014 if (ret == 0)
4015 goto missing_data;
4016 else if (ret < 0)
4017 goto return_bad_req;
4018 /* we're in MSG_CHUNK_SIZE now */
4019 }
4020 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4021 int ret = http_forward_trailers(req, msg);
4022
4023 if (ret == 0)
4024 goto missing_data;
4025 else if (ret < 0)
4026 goto return_bad_req;
4027 /* we're in HTTP_MSG_DONE now */
4028 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004029 else {
4030 /* other states, DONE...TUNNEL */
4031 if (http_resync_states(s)) {
4032 /* some state changes occurred, maybe the analyser
4033 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004034 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4036 goto return_bad_req;
4037 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004038 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004040 }
4041 }
4042
Willy Tarreaud98cf932009-12-27 22:54:55 +01004043 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004044 /* stop waiting for data if the input is closed before the end */
4045 if (req->flags & BF_SHUTR)
4046 goto return_bad_req;
4047
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004048 /* waiting for the last bits to leave the buffer */
4049 if (req->flags & BF_SHUTW)
4050 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051
4052 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004053 return 0;
4054
Willy Tarreaud98cf932009-12-27 22:54:55 +01004055 return_bad_req: /* let's centralize all bad requests */
4056 txn->req.msg_state = HTTP_MSG_ERROR;
4057 txn->status = 400;
4058 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004059 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 +01004060 req->analysers = 0;
4061 s->fe->counters.failed_req++;
4062 if (s->listener->counters)
4063 s->listener->counters->failed_req++;
4064
4065 if (!(s->flags & SN_ERR_MASK))
4066 s->flags |= SN_ERR_PRXCOND;
4067 if (!(s->flags & SN_FINST_MASK))
4068 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004069 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004070 return 0;
4071}
4072
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004073/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4074 * processing can continue on next analysers, or zero if it either needs more
4075 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4076 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4077 * when it has nothing left to do, and may remove any analyser when it wants to
4078 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004079 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004080int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004081{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004082 struct http_txn *txn = &s->txn;
4083 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004084 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004085 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004086 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004087 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004088
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004089 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 +02004090 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004091 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004092 rep,
4093 rep->rex, rep->wex,
4094 rep->flags,
4095 rep->l,
4096 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004097
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004098 /*
4099 * Now parse the partial (or complete) lines.
4100 * We will check the response syntax, and also join multi-line
4101 * headers. An index of all the lines will be elaborated while
4102 * parsing.
4103 *
4104 * For the parsing, we use a 28 states FSM.
4105 *
4106 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004107 * rep->data + msg->som = beginning of response
4108 * rep->data + msg->eoh = end of processed headers / start of current one
4109 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004110 * rep->lr = first non-visited byte
4111 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004112 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004113 */
4114
Willy Tarreau83e3af02009-12-28 17:39:57 +01004115 /* There's a protected area at the end of the buffer for rewriting
4116 * purposes. We don't want to start to parse the request if the
4117 * protected area is affected, because we may have to move processed
4118 * data later, which is much more complicated.
4119 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004120 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4121 if (unlikely((rep->flags & BF_FULL) ||
4122 rep->r < rep->lr ||
4123 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4124 if (rep->send_max) {
4125 /* some data has still not left the buffer, wake us once that's done */
4126 buffer_dont_close(rep);
4127 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4128 return 0;
4129 }
4130 if (rep->l <= rep->size - global.tune.maxrewrite)
4131 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004132 }
4133
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004134 if (likely(rep->lr < rep->r))
4135 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004136 }
4137
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004138 /* 1: we might have to print this header in debug mode */
4139 if (unlikely((global.mode & MODE_DEBUG) &&
4140 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004141 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004142 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004143
Willy Tarreau962c3f42010-01-10 00:15:35 +01004144 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004145 eol = sol + msg->sl.rq.l;
4146 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004147
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004148 sol += hdr_idx_first_pos(&txn->hdr_idx);
4149 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004150
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004151 while (cur_idx) {
4152 eol = sol + txn->hdr_idx.v[cur_idx].len;
4153 debug_hdr("srvhdr", s, sol, eol);
4154 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4155 cur_idx = txn->hdr_idx.v[cur_idx].next;
4156 }
4157 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004158
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004159 /*
4160 * Now we quickly check if we have found a full valid response.
4161 * If not so, we check the FD and buffer states before leaving.
4162 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004163 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004164 * responses are checked first.
4165 *
4166 * Depending on whether the client is still there or not, we
4167 * may send an error response back or not. Note that normally
4168 * we should only check for HTTP status there, and check I/O
4169 * errors somewhere else.
4170 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004171
Willy Tarreau655dce92009-11-08 13:10:58 +01004172 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004173 /* Invalid response */
4174 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4175 /* we detected a parsing error. We want to archive this response
4176 * in the dedicated proxy area for later troubleshooting.
4177 */
4178 hdr_response_bad:
4179 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4180 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4181
4182 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004183 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004184 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004185 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4186 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004187
Willy Tarreau90deb182010-01-07 00:20:41 +01004188 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004189 rep->analysers = 0;
4190 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004191 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004192 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4193
4194 if (!(s->flags & SN_ERR_MASK))
4195 s->flags |= SN_ERR_PRXCOND;
4196 if (!(s->flags & SN_FINST_MASK))
4197 s->flags |= SN_FINST_H;
4198
4199 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004200 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004201
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004202 /* too large response does not fit in buffer. */
4203 else if (rep->flags & BF_FULL) {
4204 goto hdr_response_bad;
4205 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004206
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004207 /* read error */
4208 else if (rep->flags & BF_READ_ERROR) {
4209 if (msg->err_pos >= 0)
4210 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004211
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004212 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004213 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004214 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004215 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4216 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004217
Willy Tarreau90deb182010-01-07 00:20:41 +01004218 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004219 rep->analysers = 0;
4220 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004221 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004222 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004223
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004224 if (!(s->flags & SN_ERR_MASK))
4225 s->flags |= SN_ERR_SRVCL;
4226 if (!(s->flags & SN_FINST_MASK))
4227 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004228 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004229 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004230
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004231 /* read timeout : return a 504 to the client. */
4232 else if (rep->flags & BF_READ_TIMEOUT) {
4233 if (msg->err_pos >= 0)
4234 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004235
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004236 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004237 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004238 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004239 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4240 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004241
Willy Tarreau90deb182010-01-07 00:20:41 +01004242 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004243 rep->analysers = 0;
4244 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004245 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004246 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004247
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004248 if (!(s->flags & SN_ERR_MASK))
4249 s->flags |= SN_ERR_SRVTO;
4250 if (!(s->flags & SN_FINST_MASK))
4251 s->flags |= SN_FINST_H;
4252 return 0;
4253 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004254
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004255 /* close from server */
4256 else if (rep->flags & BF_SHUTR) {
4257 if (msg->err_pos >= 0)
4258 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004259
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004260 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004261 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004262 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004263 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4264 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004265
Willy Tarreau90deb182010-01-07 00:20:41 +01004266 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004267 rep->analysers = 0;
4268 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004269 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004270 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004271
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004272 if (!(s->flags & SN_ERR_MASK))
4273 s->flags |= SN_ERR_SRVCL;
4274 if (!(s->flags & SN_FINST_MASK))
4275 s->flags |= SN_FINST_H;
4276 return 0;
4277 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004278
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004279 /* write error to client (we don't send any message then) */
4280 else if (rep->flags & BF_WRITE_ERROR) {
4281 if (msg->err_pos >= 0)
4282 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004283
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004284 s->be->counters.failed_resp++;
4285 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004286 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004287
4288 if (!(s->flags & SN_ERR_MASK))
4289 s->flags |= SN_ERR_CLICL;
4290 if (!(s->flags & SN_FINST_MASK))
4291 s->flags |= SN_FINST_H;
4292
4293 /* process_session() will take care of the error */
4294 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004295 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004296
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004297 buffer_dont_close(rep);
4298 return 0;
4299 }
4300
4301 /* More interesting part now : we know that we have a complete
4302 * response which at least looks like HTTP. We have an indicator
4303 * of each header's length, so we can parse them quickly.
4304 */
4305
4306 if (unlikely(msg->err_pos >= 0))
4307 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4308
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004309 /*
4310 * 1: get the status code
4311 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004312 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004313 if (n < 1 || n > 5)
4314 n = 0;
4315 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004316
Willy Tarreau5b154472009-12-21 20:11:07 +01004317 /* check if the response is HTTP/1.1 or above */
4318 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004319 ((msg->sol[5] > '1') ||
4320 ((msg->sol[5] == '1') &&
4321 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004322 txn->flags |= TX_RES_VER_11;
4323
4324 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004325 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004326
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004327 /* transfer length unknown*/
4328 txn->flags &= ~TX_RES_XFER_LEN;
4329
Willy Tarreau962c3f42010-01-10 00:15:35 +01004330 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004331
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004332 if (txn->status >= 100 && txn->status < 500)
4333 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4334 else
4335 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4336
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004337 /*
4338 * 2: check for cacheability.
4339 */
4340
4341 switch (txn->status) {
4342 case 200:
4343 case 203:
4344 case 206:
4345 case 300:
4346 case 301:
4347 case 410:
4348 /* RFC2616 @13.4:
4349 * "A response received with a status code of
4350 * 200, 203, 206, 300, 301 or 410 MAY be stored
4351 * by a cache (...) unless a cache-control
4352 * directive prohibits caching."
4353 *
4354 * RFC2616 @9.5: POST method :
4355 * "Responses to this method are not cacheable,
4356 * unless the response includes appropriate
4357 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004358 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004359 if (likely(txn->meth != HTTP_METH_POST) &&
4360 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4361 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4362 break;
4363 default:
4364 break;
4365 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004366
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004367 /*
4368 * 3: we may need to capture headers
4369 */
4370 s->logs.logwait &= ~LW_RESP;
4371 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004372 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004373 txn->rsp.cap, s->fe->rsp_cap);
4374
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004375 /* 4: determine the transfer-length.
4376 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4377 * the presence of a message-body in a RESPONSE and its transfer length
4378 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004379 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004380 * All responses to the HEAD request method MUST NOT include a
4381 * message-body, even though the presence of entity-header fields
4382 * might lead one to believe they do. All 1xx (informational), 204
4383 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4384 * message-body. All other responses do include a message-body,
4385 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004386 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004387 * 1. Any response which "MUST NOT" include a message-body (such as the
4388 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4389 * always terminated by the first empty line after the header fields,
4390 * regardless of the entity-header fields present in the message.
4391 *
4392 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4393 * the "chunked" transfer-coding (Section 6.2) is used, the
4394 * transfer-length is defined by the use of this transfer-coding.
4395 * If a Transfer-Encoding header field is present and the "chunked"
4396 * transfer-coding is not present, the transfer-length is defined by
4397 * the sender closing the connection.
4398 *
4399 * 3. If a Content-Length header field is present, its decimal value in
4400 * OCTETs represents both the entity-length and the transfer-length.
4401 * If a message is received with both a Transfer-Encoding header
4402 * field and a Content-Length header field, the latter MUST be ignored.
4403 *
4404 * 4. If the message uses the media type "multipart/byteranges", and
4405 * the transfer-length is not otherwise specified, then this self-
4406 * delimiting media type defines the transfer-length. This media
4407 * type MUST NOT be used unless the sender knows that the recipient
4408 * can parse it; the presence in a request of a Range header with
4409 * multiple byte-range specifiers from a 1.1 client implies that the
4410 * client can parse multipart/byteranges responses.
4411 *
4412 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004413 */
4414
4415 /* Skip parsing if no content length is possible. The response flags
4416 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004417 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004418 * FIXME: should we parse anyway and return an error on chunked encoding ?
4419 */
4420 if (txn->meth == HTTP_METH_HEAD ||
4421 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004422 txn->status == 204 || txn->status == 304) {
4423 txn->flags |= TX_RES_XFER_LEN;
4424 goto skip_content_length;
4425 }
4426
4427 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004428 goto skip_content_length;
4429
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004430 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004431 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004432 while ((txn->flags & TX_RES_VER_11) &&
4433 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004434 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4435 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4436 else if (txn->flags & TX_RES_TE_CHNK) {
4437 /* bad transfer-encoding (chunked followed by something else) */
4438 use_close_only = 1;
4439 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4440 break;
4441 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004442 }
4443
4444 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4445 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004446 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004447 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4448 signed long long cl;
4449
4450 if (!ctx.vlen)
4451 goto hdr_response_bad;
4452
4453 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4454 goto hdr_response_bad; /* parse failure */
4455
4456 if (cl < 0)
4457 goto hdr_response_bad;
4458
4459 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4460 goto hdr_response_bad; /* already specified, was different */
4461
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004462 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004463 msg->hdr_content_len = cl;
4464 }
4465
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004466 /* FIXME: we should also implement the multipart/byterange method.
4467 * For now on, we resort to close mode in this case (unknown length).
4468 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004469skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004470
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471 /* end of job, return OK */
4472 rep->analysers &= ~an_bit;
4473 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004474 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004475 return 1;
4476}
4477
4478/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004479 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4480 * and updates t->rep->analysers. It might make sense to explode it into several
4481 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004482 */
4483int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4484{
4485 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004486 struct http_msg *msg = &txn->rsp;
4487 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004488 struct wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489
4490 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4491 now_ms, __FUNCTION__,
4492 t,
4493 rep,
4494 rep->rex, rep->wex,
4495 rep->flags,
4496 rep->l,
4497 rep->analysers);
4498
Willy Tarreau655dce92009-11-08 13:10:58 +01004499 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004500 return 0;
4501
4502 rep->analysers &= ~an_bit;
4503 rep->analyse_exp = TICK_ETERNITY;
4504
Willy Tarreau5b154472009-12-21 20:11:07 +01004505 /* Now we have to check if we need to modify the Connection header.
4506 * This is more difficult on the response than it is on the request,
4507 * because we can have two different HTTP versions and we don't know
4508 * how the client will interprete a response. For instance, let's say
4509 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4510 * HTTP/1.1 response without any header. Maybe it will bound itself to
4511 * HTTP/1.0 because it only knows about it, and will consider the lack
4512 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4513 * the lack of header as a keep-alive. Thus we will use two flags
4514 * indicating how a request MAY be understood by the client. In case
4515 * of multiple possibilities, we'll fix the header to be explicit. If
4516 * ambiguous cases such as both close and keepalive are seen, then we
4517 * will fall back to explicit close. Note that we won't take risks with
4518 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004519 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004520 */
4521
4522 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004523 (txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004524 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4525 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004526 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004527
Willy Tarreau60466522010-01-18 19:08:45 +01004528 /* on unknown transfer length, we must close */
4529 if (!(txn->flags & TX_RES_XFER_LEN) &&
4530 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4531 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004532
Willy Tarreau60466522010-01-18 19:08:45 +01004533 /* now adjust header transformations depending on current state */
4534 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4535 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4536 to_del |= 2; /* remove "keep-alive" on any response */
4537 if (!(txn->flags & TX_RES_VER_11))
4538 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004539 }
Willy Tarreau60466522010-01-18 19:08:45 +01004540 else { /* SCL / KAL */
4541 to_del |= 1; /* remove "close" on any response */
4542 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4543 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004544 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004545
Willy Tarreau60466522010-01-18 19:08:45 +01004546 /* Parse and remove some headers from the connection header */
4547 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004548
Willy Tarreau60466522010-01-18 19:08:45 +01004549 /* Some keep-alive responses are converted to Server-close if
4550 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004551 */
Willy Tarreau60466522010-01-18 19:08:45 +01004552 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4553 if ((txn->flags & TX_HDR_CONN_CLO) ||
4554 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4555 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004556 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004557 }
4558
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004559 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004560 /*
4561 * 3: we will have to evaluate the filters.
4562 * As opposed to version 1.2, now they will be evaluated in the
4563 * filters order and not in the header order. This means that
4564 * each filter has to be validated among all headers.
4565 *
4566 * Filters are tried with ->be first, then with ->fe if it is
4567 * different from ->be.
4568 */
4569
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004570 cur_proxy = t->be;
4571 while (1) {
4572 struct proxy *rule_set = cur_proxy;
4573
4574 /* try headers filters */
4575 if (rule_set->rsp_exp != NULL) {
4576 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4577 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004578 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004579 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004580 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4581 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004582 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004583 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004584 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004586 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004587 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004588 if (!(t->flags & SN_ERR_MASK))
4589 t->flags |= SN_ERR_PRXCOND;
4590 if (!(t->flags & SN_FINST_MASK))
4591 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004592 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004593 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004594 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004595
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596 /* has the response been denied ? */
4597 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004598 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004599 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004600
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004601 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004602 if (t->listener->counters)
4603 t->listener->counters->denied_resp++;
4604
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004605 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004606 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004607
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004608 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004609 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004610 if (txn->status < 200)
4611 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004612 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004614 }
4615
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004616 /* check whether we're already working on the frontend */
4617 if (cur_proxy == t->fe)
4618 break;
4619 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004620 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004621
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004622 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004623 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4624 * in which case this is not the right response, and we're waiting for the
4625 * next one. Let's allow this response to go to the client and wait for the
4626 * next one.
4627 */
4628 if (txn->status < 200) {
4629 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004630 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004631 msg->msg_state = HTTP_MSG_RPBEFORE;
4632 txn->status = 0;
4633 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4634 return 1;
4635 }
4636
4637 /* we don't have any 1xx status code now */
4638
4639 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004640 * 4: check for server cookie.
4641 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004642 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4643 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004644 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004645
Willy Tarreaubaaee002006-06-26 02:48:02 +02004646
Willy Tarreaua15645d2007-03-18 16:22:39 +01004647 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004648 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004649 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004650 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004651 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004652
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004653 /*
4654 * 6: add server cookie in the response if needed
4655 */
4656 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004657 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004658 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004659
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004660 /* the server is known, it's not the one the client requested, we have to
4661 * insert a set-cookie here, except if we want to insert only on POST
4662 * requests and this one isn't. Note that servers which don't have cookies
4663 * (eg: some backup servers) will return a full cookie removal request.
4664 */
4665 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4666 t->be->cookie_name,
4667 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004668
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004669 if (t->be->cookie_domain)
4670 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004671
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004672 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004673 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004674 goto return_bad_resp;
4675 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004676
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004677 /* Here, we will tell an eventual cache on the client side that we don't
4678 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4679 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4680 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4681 */
4682 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004683
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004684 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4685
4686 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004687 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004688 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004689 }
4690 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004691
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004692 /*
4693 * 7: check if result will be cacheable with a cookie.
4694 * We'll block the response if security checks have caught
4695 * nasty things such as a cacheable cookie.
4696 */
4697 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4698 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004699 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004700
4701 /* we're in presence of a cacheable response containing
4702 * a set-cookie header. We'll block it as requested by
4703 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004704 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004705 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004706 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004707
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004708 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004709 if (t->listener->counters)
4710 t->listener->counters->denied_resp++;
4711
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004712 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4713 t->be->id, t->srv?t->srv->id:"<dispatch>");
4714 send_log(t->be, LOG_ALERT,
4715 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4716 t->be->id, t->srv?t->srv->id:"<dispatch>");
4717 goto return_srv_prx_502;
4718 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004719
4720 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004721 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004722 */
Willy Tarreau60466522010-01-18 19:08:45 +01004723 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4724 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4725 unsigned int want_flags = 0;
4726
4727 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4728 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4729 /* we want a keep-alive response here. Keep-alive header
4730 * required if either side is not 1.1.
4731 */
4732 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4733 want_flags |= TX_CON_KAL_SET;
4734 }
4735 else {
4736 /* we want a close response here. Close header required if
4737 * the server is 1.1, regardless of the client.
4738 */
4739 if (txn->flags & TX_RES_VER_11)
4740 want_flags |= TX_CON_CLO_SET;
4741 }
4742
4743 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4744 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004745 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004746
Willy Tarreaud98cf932009-12-27 22:54:55 +01004747 if (txn->flags & TX_RES_XFER_LEN)
4748 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004749
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004750 /*************************************************************
4751 * OK, that's finished for the headers. We have done what we *
4752 * could. Let's switch to the DATA state. *
4753 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004754
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004755 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004756
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004757 /* if the user wants to log as soon as possible, without counting
4758 * bytes from the server, then this is the right moment. We have
4759 * to temporarily assign bytes_out to log what we currently have.
4760 */
4761 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4762 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4763 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004764 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004765 t->logs.bytes_out = 0;
4766 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004767
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004768 /* Note: we must not try to cheat by jumping directly to DATA,
4769 * otherwise we would not let the client side wake up.
4770 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004771
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004772 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004773 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004774 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004775}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004776
Willy Tarreaud98cf932009-12-27 22:54:55 +01004777/* This function is an analyser which forwards response body (including chunk
4778 * sizes if any). It is called as soon as we must forward, even if we forward
4779 * zero byte. The only situation where it must not be called is when we're in
4780 * tunnel mode and we want to forward till the close. It's used both to forward
4781 * remaining data and to resync after end of body. It expects the msg_state to
4782 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4783 * read more data, or 1 once we can go on with next request or end the session.
4784 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4785 * bytes of pending data + the headers if not already done (between som and sov).
4786 * It eventually adjusts som to match sov after the data in between have been sent.
4787 */
4788int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4789{
4790 struct http_txn *txn = &s->txn;
4791 struct http_msg *msg = &s->txn.rsp;
4792
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004793 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4794 return 0;
4795
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004796 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004797 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004798 !s->req->analysers) {
4799 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4800 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004801 buffer_auto_read(res);
4802 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004803 res->analysers &= ~an_bit;
4804 return 1;
4805 }
4806
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004807 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004808
Willy Tarreaud98cf932009-12-27 22:54:55 +01004809 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4810 /* we have msg->col and msg->sov which both point to the first
4811 * byte of message body. msg->som still points to the beginning
4812 * of the message. We must save the body in req->lr because it
4813 * survives buffer re-alignments.
4814 */
4815 res->lr = res->data + msg->sov;
4816 if (txn->flags & TX_RES_TE_CHNK)
4817 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4818 else {
4819 msg->msg_state = HTTP_MSG_DATA;
4820 }
4821 }
4822
Willy Tarreaud98cf932009-12-27 22:54:55 +01004823 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004824 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004825 /* we may have some data pending */
4826 if (msg->hdr_content_len || msg->som != msg->sov) {
4827 int bytes = msg->sov - msg->som;
4828 if (bytes < 0) /* sov may have wrapped at the end */
4829 bytes += res->size;
4830 buffer_forward(res, bytes + msg->hdr_content_len);
4831 msg->hdr_content_len = 0; /* don't forward that again */
4832 msg->som = msg->sov;
4833 }
4834
Willy Tarreaucaabe412010-01-03 23:08:28 +01004835 if (msg->msg_state == HTTP_MSG_DATA) {
4836 /* must still forward */
4837 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004838 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004839
4840 /* nothing left to forward */
4841 if (txn->flags & TX_RES_TE_CHNK)
4842 msg->msg_state = HTTP_MSG_DATA_CRLF;
4843 else
4844 msg->msg_state = HTTP_MSG_DONE;
4845 }
4846 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847 /* read the chunk size and assign it to ->hdr_content_len, then
4848 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4849 */
4850 int ret = http_parse_chunk_size(res, msg);
4851
4852 if (!ret)
4853 goto missing_data;
4854 else if (ret < 0)
4855 goto return_bad_res;
4856 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004857 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004858 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4859 /* we want the CRLF after the data */
4860 int ret;
4861
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004862 res->lr = res->w + res->send_max;
4863 if (res->lr >= res->data + res->size)
4864 res->lr -= res->size;
4865
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866 ret = http_skip_chunk_crlf(res, msg);
4867
4868 if (!ret)
4869 goto missing_data;
4870 else if (ret < 0)
4871 goto return_bad_res;
4872 /* we're in MSG_CHUNK_SIZE now */
4873 }
4874 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4875 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004876
Willy Tarreaud98cf932009-12-27 22:54:55 +01004877 if (ret == 0)
4878 goto missing_data;
4879 else if (ret < 0)
4880 goto return_bad_res;
4881 /* we're in HTTP_MSG_DONE now */
4882 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004883 else {
4884 /* other states, DONE...TUNNEL */
4885 if (http_resync_states(s)) {
4886 http_silent_debug(__LINE__, s);
4887 /* some state changes occurred, maybe the analyser
4888 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004889 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004890 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4891 goto return_bad_res;
4892 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004893 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004895 }
4896 }
4897
Willy Tarreaud98cf932009-12-27 22:54:55 +01004898 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004899 /* stop waiting for data if the input is closed before the end */
4900 if (res->flags & BF_SHUTR)
4901 goto return_bad_res;
4902
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 if (!s->req->analysers)
4904 goto return_bad_res;
4905
Willy Tarreaud98cf932009-12-27 22:54:55 +01004906 /* forward the chunk size as well as any pending data */
4907 if (msg->hdr_content_len || msg->som != msg->sov) {
4908 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4909 msg->hdr_content_len = 0; /* don't forward that again */
4910 msg->som = msg->sov;
4911 }
4912
Willy Tarreaud98cf932009-12-27 22:54:55 +01004913 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004914 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004915 return 0;
4916
4917 return_bad_res: /* let's centralize all bad resuests */
4918 txn->rsp.msg_state = HTTP_MSG_ERROR;
4919 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01004920 /* don't send any error message as we're in the body */
4921 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004922 res->analysers = 0;
4923 s->be->counters.failed_resp++;
4924 if (s->srv) {
4925 s->srv->counters.failed_resp++;
4926 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4927 }
4928
4929 if (!(s->flags & SN_ERR_MASK))
4930 s->flags |= SN_ERR_PRXCOND;
4931 if (!(s->flags & SN_FINST_MASK))
4932 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004934 return 0;
4935}
4936
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004937/* Iterate the same filter through all request headers.
4938 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004939 * Since it can manage the switch to another backend, it updates the per-proxy
4940 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004941 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004942int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004943{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004944 char term;
4945 char *cur_ptr, *cur_end, *cur_next;
4946 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004947 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004948 struct hdr_idx_elem *cur_hdr;
4949 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004950
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004951 last_hdr = 0;
4952
Willy Tarreau962c3f42010-01-10 00:15:35 +01004953 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954 old_idx = 0;
4955
4956 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004957 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004958 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004959 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004960 (exp->action == ACT_ALLOW ||
4961 exp->action == ACT_DENY ||
4962 exp->action == ACT_TARPIT))
4963 return 0;
4964
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004965 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004966 if (!cur_idx)
4967 break;
4968
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004969 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004970 cur_ptr = cur_next;
4971 cur_end = cur_ptr + cur_hdr->len;
4972 cur_next = cur_end + cur_hdr->cr + 1;
4973
4974 /* Now we have one header between cur_ptr and cur_end,
4975 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004976 */
4977
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004978 /* The annoying part is that pattern matching needs
4979 * that we modify the contents to null-terminate all
4980 * strings before testing them.
4981 */
4982
4983 term = *cur_end;
4984 *cur_end = '\0';
4985
4986 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4987 switch (exp->action) {
4988 case ACT_SETBE:
4989 /* It is not possible to jump a second time.
4990 * FIXME: should we return an HTTP/500 here so that
4991 * the admin knows there's a problem ?
4992 */
4993 if (t->be != t->fe)
4994 break;
4995
4996 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004997 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004998 last_hdr = 1;
4999 break;
5000
5001 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005002 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005003 last_hdr = 1;
5004 break;
5005
5006 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005007 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005008 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005009
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005010 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005011 if (t->listener->counters)
5012 t->listener->counters->denied_resp++;
5013
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 break;
5015
5016 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005017 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005018 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005019
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005020 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005021 if (t->listener->counters)
5022 t->listener->counters->denied_resp++;
5023
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005024 break;
5025
5026 case ACT_REPLACE:
5027 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5028 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5029 /* FIXME: if the user adds a newline in the replacement, the
5030 * index will not be recalculated for now, and the new line
5031 * will not be counted as a new header.
5032 */
5033
5034 cur_end += delta;
5035 cur_next += delta;
5036 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005037 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005038 break;
5039
5040 case ACT_REMOVE:
5041 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5042 cur_next += delta;
5043
Willy Tarreaufa355d42009-11-29 18:12:29 +01005044 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005045 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5046 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005047 cur_hdr->len = 0;
5048 cur_end = NULL; /* null-term has been rewritten */
5049 break;
5050
5051 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005052 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005053 if (cur_end)
5054 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005056 /* keep the link from this header to next one in case of later
5057 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005058 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005059 old_idx = cur_idx;
5060 }
5061 return 0;
5062}
5063
5064
5065/* Apply the filter to the request line.
5066 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5067 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005068 * Since it can manage the switch to another backend, it updates the per-proxy
5069 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005070 */
5071int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5072{
5073 char term;
5074 char *cur_ptr, *cur_end;
5075 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005076 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005077 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005078
Willy Tarreau58f10d72006-12-04 02:26:12 +01005079
Willy Tarreau3d300592007-03-18 18:34:41 +01005080 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005081 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005082 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005083 (exp->action == ACT_ALLOW ||
5084 exp->action == ACT_DENY ||
5085 exp->action == ACT_TARPIT))
5086 return 0;
5087 else if (exp->action == ACT_REMOVE)
5088 return 0;
5089
5090 done = 0;
5091
Willy Tarreau962c3f42010-01-10 00:15:35 +01005092 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005093 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005094
5095 /* Now we have the request line between cur_ptr and cur_end */
5096
5097 /* The annoying part is that pattern matching needs
5098 * that we modify the contents to null-terminate all
5099 * strings before testing them.
5100 */
5101
5102 term = *cur_end;
5103 *cur_end = '\0';
5104
5105 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5106 switch (exp->action) {
5107 case ACT_SETBE:
5108 /* It is not possible to jump a second time.
5109 * FIXME: should we return an HTTP/500 here so that
5110 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005111 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005112 if (t->be != t->fe)
5113 break;
5114
5115 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005116 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005117 done = 1;
5118 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005120 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005121 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005122 done = 1;
5123 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005125 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005126 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005127
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005128 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005129 if (t->listener->counters)
5130 t->listener->counters->denied_resp++;
5131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005132 done = 1;
5133 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005135 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005136 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005137
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005138 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005139 if (t->listener->counters)
5140 t->listener->counters->denied_resp++;
5141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005142 done = 1;
5143 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005145 case ACT_REPLACE:
5146 *cur_end = term; /* restore the string terminator */
5147 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5148 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5149 /* FIXME: if the user adds a newline in the replacement, the
5150 * index will not be recalculated for now, and the new line
5151 * will not be counted as a new header.
5152 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005153
Willy Tarreaufa355d42009-11-29 18:12:29 +01005154 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005155 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005156 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005157 HTTP_MSG_RQMETH,
5158 cur_ptr, cur_end + 1,
5159 NULL, NULL);
5160 if (unlikely(!cur_end))
5161 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005162
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005163 /* we have a full request and we know that we have either a CR
5164 * or an LF at <ptr>.
5165 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005166 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5167 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005168 /* there is no point trying this regex on headers */
5169 return 1;
5170 }
5171 }
5172 *cur_end = term; /* restore the string terminator */
5173 return done;
5174}
Willy Tarreau97de6242006-12-27 17:18:38 +01005175
Willy Tarreau58f10d72006-12-04 02:26:12 +01005176
Willy Tarreau58f10d72006-12-04 02:26:12 +01005177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005178/*
5179 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5180 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005181 * unparsable request. Since it can manage the switch to another backend, it
5182 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005183 */
5184int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5185{
Willy Tarreau3d300592007-03-18 18:34:41 +01005186 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005187 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005188 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005189 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005191 /*
5192 * The interleaving of transformations and verdicts
5193 * makes it difficult to decide to continue or stop
5194 * the evaluation.
5195 */
5196
Willy Tarreau3d300592007-03-18 18:34:41 +01005197 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005198 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5199 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5200 exp = exp->next;
5201 continue;
5202 }
5203
5204 /* Apply the filter to the request line. */
5205 ret = apply_filter_to_req_line(t, req, exp);
5206 if (unlikely(ret < 0))
5207 return -1;
5208
5209 if (likely(ret == 0)) {
5210 /* The filter did not match the request, it can be
5211 * iterated through all headers.
5212 */
5213 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214 }
5215 exp = exp->next;
5216 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005217 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005218}
5219
5220
Willy Tarreaua15645d2007-03-18 16:22:39 +01005221
Willy Tarreau58f10d72006-12-04 02:26:12 +01005222/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005223 * Try to retrieve the server associated to the appsession.
5224 * If the server is found, it's assigned to the session.
5225 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005226void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005227 struct http_txn *txn = &t->txn;
5228 appsess *asession = NULL;
5229 char *sessid_temp = NULL;
5230
Cyril Bontéb21570a2009-11-29 20:04:48 +01005231 if (len > t->be->appsession_len) {
5232 len = t->be->appsession_len;
5233 }
5234
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005235 if (t->be->options2 & PR_O2_AS_REQL) {
5236 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005237 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005238 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005239 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005240 }
5241
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005242 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005243 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5244 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5245 return;
5246 }
5247
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005248 memcpy(txn->sessid, buf, len);
5249 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005250 }
5251
5252 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5253 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5254 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5255 return;
5256 }
5257
Cyril Bontéb21570a2009-11-29 20:04:48 +01005258 memcpy(sessid_temp, buf, len);
5259 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005260
5261 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5262 /* free previously allocated memory */
5263 pool_free2(apools.sessid, sessid_temp);
5264
5265 if (asession != NULL) {
5266 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5267 if (!(t->be->options2 & PR_O2_AS_REQL))
5268 asession->request_count++;
5269
5270 if (asession->serverid != NULL) {
5271 struct server *srv = t->be->srv;
5272 while (srv) {
5273 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005274 if ((srv->state & SRV_RUNNING) ||
5275 (t->be->options & PR_O_PERSIST) ||
5276 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005277 /* we found the server and it's usable */
5278 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005279 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005280 t->flags |= SN_DIRECT | SN_ASSIGNED;
5281 t->srv = srv;
5282 break;
5283 } else {
5284 txn->flags &= ~TX_CK_MASK;
5285 txn->flags |= TX_CK_DOWN;
5286 }
5287 }
5288 srv = srv->next;
5289 }
5290 }
5291 }
5292}
5293
5294/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005295 * Manage client-side cookie. It can impact performance by about 2% so it is
5296 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005297 */
5298void manage_client_side_cookies(struct session *t, struct buffer *req)
5299{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005300 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005301 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005302 char *del_colon, *del_cookie, *colon;
5303 int app_cookies;
5304
Willy Tarreau58f10d72006-12-04 02:26:12 +01005305 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005306 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005307
Willy Tarreau2a324282006-12-05 00:05:46 +01005308 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005309 * we start with the start line.
5310 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005311 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005312 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005313
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005314 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005315 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005316 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005317
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005318 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005319 cur_ptr = cur_next;
5320 cur_end = cur_ptr + cur_hdr->len;
5321 cur_next = cur_end + cur_hdr->cr + 1;
5322
5323 /* We have one full header between cur_ptr and cur_end, and the
5324 * next header starts at cur_next. We're only interested in
5325 * "Cookie:" headers.
5326 */
5327
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005328 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5329 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005330 old_idx = cur_idx;
5331 continue;
5332 }
5333
5334 /* Now look for cookies. Conforming to RFC2109, we have to support
5335 * attributes whose name begin with a '$', and associate them with
5336 * the right cookie, if we want to delete this cookie.
5337 * So there are 3 cases for each cookie read :
5338 * 1) it's a special attribute, beginning with a '$' : ignore it.
5339 * 2) it's a server id cookie that we *MAY* want to delete : save
5340 * some pointers on it (last semi-colon, beginning of cookie...)
5341 * 3) it's an application cookie : we *MAY* have to delete a previous
5342 * "special" cookie.
5343 * At the end of loop, if a "special" cookie remains, we may have to
5344 * remove it. If no application cookie persists in the header, we
5345 * *MUST* delete it
5346 */
5347
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005348 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005349
Willy Tarreau58f10d72006-12-04 02:26:12 +01005350 /* del_cookie == NULL => nothing to be deleted */
5351 del_colon = del_cookie = NULL;
5352 app_cookies = 0;
5353
5354 while (p1 < cur_end) {
5355 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005356 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005357 while (p1 < cur_end) {
5358 if (*p1 == ';' || *p1 == ',')
5359 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005360 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005361 break;
5362 p1++;
5363 }
5364
5365 if (p1 == cur_end)
5366 break;
5367
5368 /* p1 is at the beginning of the cookie name */
5369 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005370 while (p2 < cur_end && *p2 != '=') {
5371 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5372 /* oops, the cookie name was truncated, resync */
5373 p1 = p2;
5374 goto resync_name;
5375 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005376 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005377 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005378
5379 if (p2 == cur_end)
5380 break;
5381
5382 p3 = p2 + 1; /* skips the '=' sign */
5383 if (p3 == cur_end)
5384 break;
5385
Willy Tarreau305ae852010-01-03 19:45:54 +01005386 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5387 p5 = p4 = p3;
5388 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5389 if (!isspace((unsigned char)*p5))
5390 p4 = p5 + 1;
5391 p5++;
5392 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005393
5394 /* here, we have the cookie name between p1 and p2,
5395 * and its value between p3 and p4.
5396 * we can process it :
5397 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005398 * Cookie: NAME=VALUE ;
5399 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005400 * | || || +--> p4
5401 * | || |+-------> p3
5402 * | || +--------> p2
5403 * | |+------------> p1
5404 * | +-------------> colon
5405 * +--------------------> cur_ptr
5406 */
5407
5408 if (*p1 == '$') {
5409 /* skip this one */
5410 }
5411 else {
5412 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005413 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005414 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005415 (p4 - p1 >= t->fe->capture_namelen) &&
5416 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005417 int log_len = p4 - p1;
5418
Willy Tarreau086b3b42007-05-13 21:45:51 +02005419 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005420 Alert("HTTP logging : out of memory.\n");
5421 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005422 if (log_len > t->fe->capture_len)
5423 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005424 memcpy(txn->cli_cookie, p1, log_len);
5425 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005426 }
5427 }
5428
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005429 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5430 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005431 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005432 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005433 char *delim;
5434
5435 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5436 * have the server ID betweek p3 and delim, and the original cookie between
5437 * delim+1 and p4. Otherwise, delim==p4 :
5438 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005439 * Cookie: NAME=SRV~VALUE ;
5440 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005441 * | || || | +--> p4
5442 * | || || +--------> delim
5443 * | || |+-----------> p3
5444 * | || +------------> p2
5445 * | |+----------------> p1
5446 * | +-----------------> colon
5447 * +------------------------> cur_ptr
5448 */
5449
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005450 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005451 for (delim = p3; delim < p4; delim++)
5452 if (*delim == COOKIE_DELIM)
5453 break;
5454 }
5455 else
5456 delim = p4;
5457
5458
5459 /* Here, we'll look for the first running server which supports the cookie.
5460 * This allows to share a same cookie between several servers, for example
5461 * to dedicate backup servers to specific servers only.
5462 * However, to prevent clients from sticking to cookie-less backup server
5463 * when they have incidentely learned an empty cookie, we simply ignore
5464 * empty cookies and mark them as invalid.
5465 */
5466 if (delim == p3)
5467 srv = NULL;
5468
5469 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005470 if (srv->cookie && (srv->cklen == delim - p3) &&
5471 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005472 if ((srv->state & SRV_RUNNING) ||
5473 (t->be->options & PR_O_PERSIST) ||
5474 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005475 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005476 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005477 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005478 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005479 t->srv = srv;
5480 break;
5481 } else {
5482 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005483 txn->flags &= ~TX_CK_MASK;
5484 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005485 }
5486 }
5487 srv = srv->next;
5488 }
5489
Willy Tarreau3d300592007-03-18 18:34:41 +01005490 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005491 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005492 txn->flags &= ~TX_CK_MASK;
5493 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005494 }
5495
5496 /* depending on the cookie mode, we may have to either :
5497 * - delete the complete cookie if we're in insert+indirect mode, so that
5498 * the server never sees it ;
5499 * - remove the server id from the cookie value, and tag the cookie as an
5500 * application cookie so that it does not get accidentely removed later,
5501 * if we're in cookie prefix mode
5502 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005503 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005504 int delta; /* negative */
5505
5506 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5507 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005508 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005509 cur_end += delta;
5510 cur_next += delta;
5511 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005512 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005513
5514 del_cookie = del_colon = NULL;
5515 app_cookies++; /* protect the header from deletion */
5516 }
5517 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005518 (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 +01005519 del_cookie = p1;
5520 del_colon = colon;
5521 }
5522 } else {
5523 /* now we know that we must keep this cookie since it's
5524 * not ours. But if we wanted to delete our cookie
5525 * earlier, we cannot remove the complete header, but we
5526 * can remove the previous block itself.
5527 */
5528 app_cookies++;
5529
5530 if (del_cookie != NULL) {
5531 int delta; /* negative */
5532
5533 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5534 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005535 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005536 cur_end += delta;
5537 cur_next += delta;
5538 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005539 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005540 del_cookie = del_colon = NULL;
5541 }
5542 }
5543
Cyril Bontéb21570a2009-11-29 20:04:48 +01005544 if (t->be->appsession_name != NULL) {
5545 int cmp_len, value_len;
5546 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005547
Cyril Bontéb21570a2009-11-29 20:04:48 +01005548 if (t->be->options2 & PR_O2_AS_PFX) {
5549 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5550 value_begin = p1 + t->be->appsession_name_len;
5551 value_len = p4 - p1 - t->be->appsession_name_len;
5552 } else {
5553 cmp_len = p2 - p1;
5554 value_begin = p3;
5555 value_len = p4 - p3;
5556 }
5557
5558 /* let's see if the cookie is our appcookie */
5559 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5560 /* Cool... it's the right one */
5561 manage_client_side_appsession(t, value_begin, value_len);
5562 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005563#if defined(DEBUG_HASH)
5564 Alert("manage_client_side_cookies\n");
5565 appsession_hash_dump(&(t->be->htbl_proxy));
5566#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005567 }/* end if ((t->proxy->appsession_name != NULL) ... */
5568 }
5569
5570 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005571 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005572 } /* while (p1 < cur_end) */
5573
5574 /* There's no more cookie on this line.
5575 * We may have marked the last one(s) for deletion.
5576 * We must do this now in two ways :
5577 * - if there is no app cookie, we simply delete the header ;
5578 * - if there are app cookies, we must delete the end of the
5579 * string properly, including the colon/semi-colon before
5580 * the cookie name.
5581 */
5582 if (del_cookie != NULL) {
5583 int delta;
5584 if (app_cookies) {
5585 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5586 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005587 cur_hdr->len += delta;
5588 } else {
5589 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005590
5591 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005592 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5593 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005594 cur_hdr->len = 0;
5595 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005596 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005597 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005598 }
5599
5600 /* keep the link from this header to next one */
5601 old_idx = cur_idx;
5602 } /* end of cookie processing on this header */
5603}
5604
5605
Willy Tarreaua15645d2007-03-18 16:22:39 +01005606/* Iterate the same filter through all response headers contained in <rtr>.
5607 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5608 */
5609int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5610{
5611 char term;
5612 char *cur_ptr, *cur_end, *cur_next;
5613 int cur_idx, old_idx, last_hdr;
5614 struct http_txn *txn = &t->txn;
5615 struct hdr_idx_elem *cur_hdr;
5616 int len, delta;
5617
5618 last_hdr = 0;
5619
Willy Tarreau962c3f42010-01-10 00:15:35 +01005620 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005621 old_idx = 0;
5622
5623 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005624 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005625 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005626 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005627 (exp->action == ACT_ALLOW ||
5628 exp->action == ACT_DENY))
5629 return 0;
5630
5631 cur_idx = txn->hdr_idx.v[old_idx].next;
5632 if (!cur_idx)
5633 break;
5634
5635 cur_hdr = &txn->hdr_idx.v[cur_idx];
5636 cur_ptr = cur_next;
5637 cur_end = cur_ptr + cur_hdr->len;
5638 cur_next = cur_end + cur_hdr->cr + 1;
5639
5640 /* Now we have one header between cur_ptr and cur_end,
5641 * and the next header starts at cur_next.
5642 */
5643
5644 /* The annoying part is that pattern matching needs
5645 * that we modify the contents to null-terminate all
5646 * strings before testing them.
5647 */
5648
5649 term = *cur_end;
5650 *cur_end = '\0';
5651
5652 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5653 switch (exp->action) {
5654 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005655 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005656 last_hdr = 1;
5657 break;
5658
5659 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005660 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005661 last_hdr = 1;
5662 break;
5663
5664 case ACT_REPLACE:
5665 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5666 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5667 /* FIXME: if the user adds a newline in the replacement, the
5668 * index will not be recalculated for now, and the new line
5669 * will not be counted as a new header.
5670 */
5671
5672 cur_end += delta;
5673 cur_next += delta;
5674 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005675 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005676 break;
5677
5678 case ACT_REMOVE:
5679 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5680 cur_next += delta;
5681
Willy Tarreaufa355d42009-11-29 18:12:29 +01005682 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005683 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5684 txn->hdr_idx.used--;
5685 cur_hdr->len = 0;
5686 cur_end = NULL; /* null-term has been rewritten */
5687 break;
5688
5689 }
5690 }
5691 if (cur_end)
5692 *cur_end = term; /* restore the string terminator */
5693
5694 /* keep the link from this header to next one in case of later
5695 * removal of next header.
5696 */
5697 old_idx = cur_idx;
5698 }
5699 return 0;
5700}
5701
5702
5703/* Apply the filter to the status line in the response buffer <rtr>.
5704 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5705 * or -1 if a replacement resulted in an invalid status line.
5706 */
5707int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5708{
5709 char term;
5710 char *cur_ptr, *cur_end;
5711 int done;
5712 struct http_txn *txn = &t->txn;
5713 int len, delta;
5714
5715
Willy Tarreau3d300592007-03-18 18:34:41 +01005716 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005717 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005718 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005719 (exp->action == ACT_ALLOW ||
5720 exp->action == ACT_DENY))
5721 return 0;
5722 else if (exp->action == ACT_REMOVE)
5723 return 0;
5724
5725 done = 0;
5726
Willy Tarreau962c3f42010-01-10 00:15:35 +01005727 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005728 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5729
5730 /* Now we have the status line between cur_ptr and cur_end */
5731
5732 /* The annoying part is that pattern matching needs
5733 * that we modify the contents to null-terminate all
5734 * strings before testing them.
5735 */
5736
5737 term = *cur_end;
5738 *cur_end = '\0';
5739
5740 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5741 switch (exp->action) {
5742 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005743 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005744 done = 1;
5745 break;
5746
5747 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005748 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005749 done = 1;
5750 break;
5751
5752 case ACT_REPLACE:
5753 *cur_end = term; /* restore the string terminator */
5754 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5755 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5756 /* FIXME: if the user adds a newline in the replacement, the
5757 * index will not be recalculated for now, and the new line
5758 * will not be counted as a new header.
5759 */
5760
Willy Tarreaufa355d42009-11-29 18:12:29 +01005761 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005762 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005763 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005764 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005765 cur_ptr, cur_end + 1,
5766 NULL, NULL);
5767 if (unlikely(!cur_end))
5768 return -1;
5769
5770 /* we have a full respnse and we know that we have either a CR
5771 * or an LF at <ptr>.
5772 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005773 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005774 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5775 /* there is no point trying this regex on headers */
5776 return 1;
5777 }
5778 }
5779 *cur_end = term; /* restore the string terminator */
5780 return done;
5781}
5782
5783
5784
5785/*
5786 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5787 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5788 * unparsable response.
5789 */
5790int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5791{
Willy Tarreau3d300592007-03-18 18:34:41 +01005792 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005793 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005794 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005795 int ret;
5796
5797 /*
5798 * The interleaving of transformations and verdicts
5799 * makes it difficult to decide to continue or stop
5800 * the evaluation.
5801 */
5802
Willy Tarreau3d300592007-03-18 18:34:41 +01005803 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005804 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5805 exp->action == ACT_PASS)) {
5806 exp = exp->next;
5807 continue;
5808 }
5809
5810 /* Apply the filter to the status line. */
5811 ret = apply_filter_to_sts_line(t, rtr, exp);
5812 if (unlikely(ret < 0))
5813 return -1;
5814
5815 if (likely(ret == 0)) {
5816 /* The filter did not match the response, it can be
5817 * iterated through all headers.
5818 */
5819 apply_filter_to_resp_headers(t, rtr, exp);
5820 }
5821 exp = exp->next;
5822 }
5823 return 0;
5824}
5825
5826
5827
5828/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005829 * Manage server-side cookies. It can impact performance by about 2% so it is
5830 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005831 */
5832void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5833{
5834 struct http_txn *txn = &t->txn;
5835 char *p1, *p2, *p3, *p4;
5836
Willy Tarreaua15645d2007-03-18 16:22:39 +01005837 char *cur_ptr, *cur_end, *cur_next;
5838 int cur_idx, old_idx, delta;
5839
Willy Tarreaua15645d2007-03-18 16:22:39 +01005840 /* Iterate through the headers.
5841 * we start with the start line.
5842 */
5843 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005844 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005845
5846 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5847 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005848 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005849
5850 cur_hdr = &txn->hdr_idx.v[cur_idx];
5851 cur_ptr = cur_next;
5852 cur_end = cur_ptr + cur_hdr->len;
5853 cur_next = cur_end + cur_hdr->cr + 1;
5854
5855 /* We have one full header between cur_ptr and cur_end, and the
5856 * next header starts at cur_next. We're only interested in
5857 * "Cookie:" headers.
5858 */
5859
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005860 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5861 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005862 old_idx = cur_idx;
5863 continue;
5864 }
5865
5866 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005867 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868
5869
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005870 /* maybe we only wanted to see if there was a set-cookie. Note that
5871 * the cookie capture is declared in the fronend.
5872 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005873 if (t->be->cookie_name == NULL &&
5874 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005875 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005876 return;
5877
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005878 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879
5880 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005881 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5882 break;
5883
5884 /* p1 is at the beginning of the cookie name */
5885 p2 = p1;
5886
5887 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5888 p2++;
5889
5890 if (p2 == cur_end || *p2 == ';') /* next cookie */
5891 break;
5892
5893 p3 = p2 + 1; /* skip the '=' sign */
5894 if (p3 == cur_end)
5895 break;
5896
5897 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005898 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005899 p4++;
5900
5901 /* here, we have the cookie name between p1 and p2,
5902 * and its value between p3 and p4.
5903 * we can process it.
5904 */
5905
5906 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005907 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005908 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005909 (p4 - p1 >= t->fe->capture_namelen) &&
5910 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005911 int log_len = p4 - p1;
5912
Willy Tarreau086b3b42007-05-13 21:45:51 +02005913 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005914 Alert("HTTP logging : out of memory.\n");
5915 }
5916
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005917 if (log_len > t->fe->capture_len)
5918 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005919 memcpy(txn->srv_cookie, p1, log_len);
5920 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005921 }
5922
5923 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005924 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5925 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005926 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005927 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005928
5929 /* If the cookie is in insert mode on a known server, we'll delete
5930 * this occurrence because we'll insert another one later.
5931 * We'll delete it too if the "indirect" option is set and we're in
5932 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005933 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5934 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005935 /* this header must be deleted */
5936 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5937 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5938 txn->hdr_idx.used--;
5939 cur_hdr->len = 0;
5940 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005941 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005942
Willy Tarreau3d300592007-03-18 18:34:41 +01005943 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944 }
5945 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005946 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005947 /* replace bytes p3->p4 with the cookie name associated
5948 * with this server since we know it.
5949 */
5950 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5951 cur_hdr->len += delta;
5952 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005953 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005954
Willy Tarreau3d300592007-03-18 18:34:41 +01005955 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005956 }
5957 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005958 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959 /* insert the cookie name associated with this server
5960 * before existing cookie, and insert a delimitor between them..
5961 */
5962 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5963 cur_hdr->len += delta;
5964 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005965 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005966
5967 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005968 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005969 }
5970 }
5971 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005972 else if (t->be->appsession_name != NULL) {
5973 int cmp_len, value_len;
5974 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005975
Cyril Bontéb21570a2009-11-29 20:04:48 +01005976 if (t->be->options2 & PR_O2_AS_PFX) {
5977 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5978 value_begin = p1 + t->be->appsession_name_len;
5979 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5980 } else {
5981 cmp_len = p2 - p1;
5982 value_begin = p3;
5983 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005984 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005985
5986 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5987 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005988 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005989 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005990 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01005991 }
5992 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005993 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005994 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5995 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5996 return;
5997 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005998 memcpy(txn->sessid, value_begin, value_len);
5999 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006000 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006001 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006002 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6003 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006004 /* keep the link from this header to next one */
6005 old_idx = cur_idx;
6006 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006007
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006008 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006009 appsess *asession = NULL;
6010 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006011 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006012 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006013 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006014 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6015 Alert("Not enough Memory process_srv():asession:calloc().\n");
6016 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6017 return;
6018 }
6019 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6020 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6021 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006022 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006023 return;
6024 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006025 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006026 asession->sessid[t->be->appsession_len] = 0;
6027
Willy Tarreau1fac7532010-01-09 19:23:06 +01006028 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006029 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6030 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6031 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006032 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006033 return;
6034 }
6035 asession->serverid[0] = '\0';
6036 memcpy(asession->serverid, t->srv->id, server_id_len);
6037
6038 asession->request_count = 0;
6039 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6040 }
6041
6042 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6043 asession->request_count++;
6044 }
6045
6046#if defined(DEBUG_HASH)
6047 Alert("manage_server_side_cookies\n");
6048 appsession_hash_dump(&(t->be->htbl_proxy));
6049#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006050}
6051
6052
6053
6054/*
6055 * Check if response is cacheable or not. Updates t->flags.
6056 */
6057void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6058{
6059 struct http_txn *txn = &t->txn;
6060 char *p1, *p2;
6061
6062 char *cur_ptr, *cur_end, *cur_next;
6063 int cur_idx;
6064
Willy Tarreau5df51872007-11-25 16:20:08 +01006065 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006066 return;
6067
6068 /* Iterate through the headers.
6069 * we start with the start line.
6070 */
6071 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006072 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006073
6074 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6075 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006076 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006077
6078 cur_hdr = &txn->hdr_idx.v[cur_idx];
6079 cur_ptr = cur_next;
6080 cur_end = cur_ptr + cur_hdr->len;
6081 cur_next = cur_end + cur_hdr->cr + 1;
6082
6083 /* We have one full header between cur_ptr and cur_end, and the
6084 * next header starts at cur_next. We're only interested in
6085 * "Cookie:" headers.
6086 */
6087
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006088 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6089 if (val) {
6090 if ((cur_end - (cur_ptr + val) >= 8) &&
6091 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6092 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6093 return;
6094 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006095 }
6096
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006097 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6098 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006099 continue;
6100
6101 /* OK, right now we know we have a cache-control header at cur_ptr */
6102
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006103 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006104
6105 if (p1 >= cur_end) /* no more info */
6106 continue;
6107
6108 /* p1 is at the beginning of the value */
6109 p2 = p1;
6110
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006111 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006112 p2++;
6113
6114 /* we have a complete value between p1 and p2 */
6115 if (p2 < cur_end && *p2 == '=') {
6116 /* we have something of the form no-cache="set-cookie" */
6117 if ((cur_end - p1 >= 21) &&
6118 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6119 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006120 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006121 continue;
6122 }
6123
6124 /* OK, so we know that either p2 points to the end of string or to a comma */
6125 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6126 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6127 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6128 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006129 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006130 return;
6131 }
6132
6133 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006134 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006135 continue;
6136 }
6137 }
6138}
6139
6140
Willy Tarreau58f10d72006-12-04 02:26:12 +01006141/*
6142 * Try to retrieve a known appsession in the URI, then the associated server.
6143 * If the server is found, it's assigned to the session.
6144 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006145void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006146{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006147 char *end_params, *first_param, *cur_param, *next_param;
6148 char separator;
6149 int value_len;
6150
6151 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006152
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006153 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006154 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006155 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006156 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006157
Cyril Bontéb21570a2009-11-29 20:04:48 +01006158 first_param = NULL;
6159 switch (mode) {
6160 case PR_O2_AS_M_PP:
6161 first_param = memchr(begin, ';', len);
6162 break;
6163 case PR_O2_AS_M_QS:
6164 first_param = memchr(begin, '?', len);
6165 break;
6166 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006167
Cyril Bontéb21570a2009-11-29 20:04:48 +01006168 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006169 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006170 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006171
Cyril Bontéb21570a2009-11-29 20:04:48 +01006172 switch (mode) {
6173 case PR_O2_AS_M_PP:
6174 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6175 end_params = (char *) begin + len;
6176 }
6177 separator = ';';
6178 break;
6179 case PR_O2_AS_M_QS:
6180 end_params = (char *) begin + len;
6181 separator = '&';
6182 break;
6183 default:
6184 /* unknown mode, shouldn't happen */
6185 return;
6186 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006187
Cyril Bontéb21570a2009-11-29 20:04:48 +01006188 cur_param = next_param = end_params;
6189 while (cur_param > first_param) {
6190 cur_param--;
6191 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6192 /* let's see if this is the appsession parameter */
6193 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6194 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6195 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6196 /* Cool... it's the right one */
6197 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6198 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6199 if (value_len > 0) {
6200 manage_client_side_appsession(t, cur_param, value_len);
6201 }
6202 break;
6203 }
6204 next_param = cur_param;
6205 }
6206 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006207#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006208 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006209 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006210#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211}
6212
6213
Willy Tarreaub2513902006-12-17 14:52:38 +01006214/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006215 * In a GET or HEAD request, check if the requested URI matches the stats uri
6216 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006217 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006218 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006219 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006220 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006221 *
6222 * Returns 1 if the session's state changes, otherwise 0.
6223 */
6224int stats_check_uri_auth(struct session *t, struct proxy *backend)
6225{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006226 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006227 struct uri_auth *uri_auth = backend->uri_auth;
6228 struct user_auth *user;
6229 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006230 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006231
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006232 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006234 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006235 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006236 return 0;
6237
Willy Tarreau962c3f42010-01-10 00:15:35 +01006238 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006239
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006240 /* the URI is in h */
6241 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006242 return 0;
6243
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006244 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006245 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006246 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006247 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006248 break;
6249 }
6250 h++;
6251 }
6252
6253 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006254 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6255 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006256 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006257 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006258 break;
6259 }
6260 h++;
6261 }
6262 }
6263
Willy Tarreau962c3f42010-01-10 00:15:35 +01006264 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6265 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006266 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006267 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006268 break;
6269 }
6270 h++;
6271 }
6272
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006273 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6274
Willy Tarreaub2513902006-12-17 14:52:38 +01006275 /* we are in front of a interceptable URI. Let's check
6276 * if there's an authentication and if it's valid.
6277 */
6278 user = uri_auth->users;
6279 if (!user) {
6280 /* no user auth required, it's OK */
6281 authenticated = 1;
6282 } else {
6283 authenticated = 0;
6284
6285 /* a user list is defined, we have to check.
6286 * skip 21 chars for "Authorization: Basic ".
6287 */
6288
6289 /* FIXME: this should move to an earlier place */
6290 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006291 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006292 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6293 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006294 if (len > 14 &&
6295 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006296 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006297 break;
6298 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006299 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006300 }
6301
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006302 if (txn->auth_hdr.len < 21 ||
6303 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006304 user = NULL;
6305
6306 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006307 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6308 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006309 user->user_pwd, user->user_len)) {
6310 authenticated = 1;
6311 break;
6312 }
6313 user = user->next;
6314 }
6315 }
6316
6317 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006318 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006319
6320 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006321 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6322 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006323 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006324 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006325 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006326 if (!(t->flags & SN_ERR_MASK))
6327 t->flags |= SN_ERR_PRXCOND;
6328 if (!(t->flags & SN_FINST_MASK))
6329 t->flags |= SN_FINST_R;
6330 return 1;
6331 }
6332
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006333 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006334 * data.
6335 */
Willy Tarreau70089872008-06-13 21:12:51 +02006336 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006337 t->data_source = DATA_SRC_STATS;
6338 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006339 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006340 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6341 t->rep->prod->private = t;
6342 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006343 return 1;
6344}
6345
Willy Tarreau4076a152009-04-02 15:18:36 +02006346/*
6347 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006348 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6349 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006350 */
6351void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6352 struct buffer *buf, struct http_msg *msg,
6353 struct proxy *other_end)
6354{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006355 es->len = buf->r - (buf->data + msg->som);
6356 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006357 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006358 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006359 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006360 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006361 es->when = date; // user-visible date
6362 es->sid = s->uniq_id;
6363 es->srv = s->srv;
6364 es->oe = other_end;
6365 es->src = s->cli_addr;
6366}
Willy Tarreaub2513902006-12-17 14:52:38 +01006367
Willy Tarreaubaaee002006-06-26 02:48:02 +02006368/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006369 * Print a debug line with a header
6370 */
6371void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6372{
6373 int len, max;
6374 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006375 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376 max = end - start;
6377 UBOUND(max, sizeof(trash) - len - 1);
6378 len += strlcpy2(trash + len, start, max + 1);
6379 trash[len++] = '\n';
6380 write(1, trash, len);
6381}
6382
Willy Tarreau0937bc42009-12-22 15:03:09 +01006383/*
6384 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6385 * the required fields are properly allocated and that we only need to (re)init
6386 * them. This should be used before processing any new request.
6387 */
6388void http_init_txn(struct session *s)
6389{
6390 struct http_txn *txn = &s->txn;
6391 struct proxy *fe = s->fe;
6392
6393 txn->flags = 0;
6394 txn->status = -1;
6395
6396 txn->req.sol = txn->req.eol = NULL;
6397 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6398 txn->rsp.sol = txn->rsp.eol = NULL;
6399 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6400 txn->req.hdr_content_len = 0LL;
6401 txn->rsp.hdr_content_len = 0LL;
6402 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6403 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6404 chunk_reset(&txn->auth_hdr);
6405
6406 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6407 if (fe->options2 & PR_O2_REQBUG_OK)
6408 txn->req.err_pos = -1; /* let buggy requests pass */
6409
Willy Tarreau46023632010-01-07 22:51:47 +01006410 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006411 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6412
Willy Tarreau46023632010-01-07 22:51:47 +01006413 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006414 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6415
6416 if (txn->hdr_idx.v)
6417 hdr_idx_init(&txn->hdr_idx);
6418}
6419
6420/* to be used at the end of a transaction */
6421void http_end_txn(struct session *s)
6422{
6423 struct http_txn *txn = &s->txn;
6424
6425 /* these ones will have been dynamically allocated */
6426 pool_free2(pool2_requri, txn->uri);
6427 pool_free2(pool2_capture, txn->cli_cookie);
6428 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006429 pool_free2(apools.sessid, txn->sessid);
6430 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006431 txn->uri = NULL;
6432 txn->srv_cookie = NULL;
6433 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006434
6435 if (txn->req.cap) {
6436 struct cap_hdr *h;
6437 for (h = s->fe->req_cap; h; h = h->next)
6438 pool_free2(h->pool, txn->req.cap[h->index]);
6439 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6440 }
6441
6442 if (txn->rsp.cap) {
6443 struct cap_hdr *h;
6444 for (h = s->fe->rsp_cap; h; h = h->next)
6445 pool_free2(h->pool, txn->rsp.cap[h->index]);
6446 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6447 }
6448
Willy Tarreau0937bc42009-12-22 15:03:09 +01006449}
6450
6451/* to be used at the end of a transaction to prepare a new one */
6452void http_reset_txn(struct session *s)
6453{
6454 http_end_txn(s);
6455 http_init_txn(s);
6456
6457 s->be = s->fe;
6458 s->req->analysers = s->listener->analysers;
6459 s->logs.logwait = s->fe->to_log;
6460 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006461 /* re-init store persistence */
6462 s->store_count = 0;
6463
Willy Tarreau0937bc42009-12-22 15:03:09 +01006464 s->pend_pos = NULL;
6465 s->conn_retries = s->be->conn_retries;
6466
6467 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6468
Willy Tarreau739cfba2010-01-25 23:11:14 +01006469 /* We must trim any excess data from the response buffer, because we
6470 * may have blocked an invalid response from a server that we don't
6471 * want to accidentely forward once we disable the analysers, nor do
6472 * we want those data to come along with next response. A typical
6473 * example of such data would be from a buggy server responding to
6474 * a HEAD with some data, or sending more than the advertised
6475 * content-length.
6476 */
6477 if (unlikely(s->rep->l > s->rep->send_max)) {
6478 s->rep->l = s->rep->send_max;
6479 s->rep->r = s->rep->w + s->rep->l;
6480 if (s->rep->r >= s->rep->data + s->rep->size)
6481 s->rep->r -= s->rep->size;
6482 }
6483
Willy Tarreau0937bc42009-12-22 15:03:09 +01006484 s->req->rto = s->fe->timeout.client;
6485 s->req->wto = s->be->timeout.server;
6486 s->req->cto = s->be->timeout.connect;
6487
6488 s->rep->rto = s->be->timeout.server;
6489 s->rep->wto = s->fe->timeout.client;
6490 s->rep->cto = TICK_ETERNITY;
6491
6492 s->req->rex = TICK_ETERNITY;
6493 s->req->wex = TICK_ETERNITY;
6494 s->req->analyse_exp = TICK_ETERNITY;
6495 s->rep->rex = TICK_ETERNITY;
6496 s->rep->wex = TICK_ETERNITY;
6497 s->rep->analyse_exp = TICK_ETERNITY;
6498}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006499
Willy Tarreau8797c062007-05-07 00:55:35 +02006500/************************************************************************/
6501/* The code below is dedicated to ACL parsing and matching */
6502/************************************************************************/
6503
6504
6505
6506
6507/* 1. Check on METHOD
6508 * We use the pre-parsed method if it is known, and store its number as an
6509 * integer. If it is unknown, we use the pointer and the length.
6510 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006511static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006512{
6513 int len, meth;
6514
Willy Tarreauae8b7962007-06-09 23:10:04 +02006515 len = strlen(*text);
6516 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006517
6518 pattern->val.i = meth;
6519 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006520 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006521 if (!pattern->ptr.str)
6522 return 0;
6523 pattern->len = len;
6524 }
6525 return 1;
6526}
6527
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006528static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006529acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6530 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006531{
6532 int meth;
6533 struct http_txn *txn = l7;
6534
Willy Tarreaub6866442008-07-14 23:54:42 +02006535 if (!txn)
6536 return 0;
6537
Willy Tarreau655dce92009-11-08 13:10:58 +01006538 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006539 return 0;
6540
Willy Tarreau8797c062007-05-07 00:55:35 +02006541 meth = txn->meth;
6542 test->i = meth;
6543 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006544 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6545 /* ensure the indexes are not affected */
6546 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006547 test->len = txn->req.sl.rq.m_l;
6548 test->ptr = txn->req.sol;
6549 }
6550 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6551 return 1;
6552}
6553
6554static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6555{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006556 int icase;
6557
Willy Tarreau8797c062007-05-07 00:55:35 +02006558 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006559 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006560
6561 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006562 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006563
6564 /* Other method, we must compare the strings */
6565 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006566 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006567
6568 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6569 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6570 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006571 return ACL_PAT_FAIL;
6572 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006573}
6574
6575/* 2. Check on Request/Status Version
6576 * We simply compare strings here.
6577 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006578static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006579{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006580 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006581 if (!pattern->ptr.str)
6582 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006583 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006584 return 1;
6585}
6586
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006587static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006588acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6589 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006590{
6591 struct http_txn *txn = l7;
6592 char *ptr;
6593 int len;
6594
Willy Tarreaub6866442008-07-14 23:54:42 +02006595 if (!txn)
6596 return 0;
6597
Willy Tarreau655dce92009-11-08 13:10:58 +01006598 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006599 return 0;
6600
Willy Tarreau8797c062007-05-07 00:55:35 +02006601 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006602 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006603
6604 while ((len-- > 0) && (*ptr++ != '/'));
6605 if (len <= 0)
6606 return 0;
6607
6608 test->ptr = ptr;
6609 test->len = len;
6610
6611 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6612 return 1;
6613}
6614
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006615static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006616acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6617 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006618{
6619 struct http_txn *txn = l7;
6620 char *ptr;
6621 int len;
6622
Willy Tarreaub6866442008-07-14 23:54:42 +02006623 if (!txn)
6624 return 0;
6625
Willy Tarreau655dce92009-11-08 13:10:58 +01006626 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006627 return 0;
6628
Willy Tarreau8797c062007-05-07 00:55:35 +02006629 len = txn->rsp.sl.st.v_l;
6630 ptr = txn->rsp.sol;
6631
6632 while ((len-- > 0) && (*ptr++ != '/'));
6633 if (len <= 0)
6634 return 0;
6635
6636 test->ptr = ptr;
6637 test->len = len;
6638
6639 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6640 return 1;
6641}
6642
6643/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006644static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006645acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6646 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006647{
6648 struct http_txn *txn = l7;
6649 char *ptr;
6650 int len;
6651
Willy Tarreaub6866442008-07-14 23:54:42 +02006652 if (!txn)
6653 return 0;
6654
Willy Tarreau655dce92009-11-08 13:10:58 +01006655 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006656 return 0;
6657
Willy Tarreau8797c062007-05-07 00:55:35 +02006658 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006659 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006660
6661 test->i = __strl2ui(ptr, len);
6662 test->flags = ACL_TEST_F_VOL_1ST;
6663 return 1;
6664}
6665
6666/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006667static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006668acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6669 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006670{
6671 struct http_txn *txn = l7;
6672
Willy Tarreaub6866442008-07-14 23:54:42 +02006673 if (!txn)
6674 return 0;
6675
Willy Tarreau655dce92009-11-08 13:10:58 +01006676 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006677 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006678
Willy Tarreauc11416f2007-06-17 16:58:38 +02006679 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6680 /* ensure the indexes are not affected */
6681 return 0;
6682
Willy Tarreau8797c062007-05-07 00:55:35 +02006683 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006684 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006685
Willy Tarreauf3d25982007-05-08 22:45:09 +02006686 /* we do not need to set READ_ONLY because the data is in a buffer */
6687 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006688 return 1;
6689}
6690
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006691static int
6692acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6693 struct acl_expr *expr, struct acl_test *test)
6694{
6695 struct http_txn *txn = l7;
6696
Willy Tarreaub6866442008-07-14 23:54:42 +02006697 if (!txn)
6698 return 0;
6699
Willy Tarreau655dce92009-11-08 13:10:58 +01006700 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006701 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006702
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006703 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6704 /* ensure the indexes are not affected */
6705 return 0;
6706
6707 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006708 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 +01006709 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6710 test->i = AF_INET;
6711
6712 /*
6713 * If we are parsing url in frontend space, we prepare backend stage
6714 * to not parse again the same url ! optimization lazyness...
6715 */
6716 if (px->options & PR_O_HTTP_PROXY)
6717 l4->flags |= SN_ADDR_SET;
6718
6719 test->flags = ACL_TEST_F_READ_ONLY;
6720 return 1;
6721}
6722
6723static int
6724acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6725 struct acl_expr *expr, struct acl_test *test)
6726{
6727 struct http_txn *txn = l7;
6728
Willy Tarreaub6866442008-07-14 23:54:42 +02006729 if (!txn)
6730 return 0;
6731
Willy Tarreau655dce92009-11-08 13:10:58 +01006732 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006733 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006734
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006735 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6736 /* ensure the indexes are not affected */
6737 return 0;
6738
6739 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006740 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 +01006741 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6742
6743 if (px->options & PR_O_HTTP_PROXY)
6744 l4->flags |= SN_ADDR_SET;
6745
6746 test->flags = ACL_TEST_F_READ_ONLY;
6747 return 1;
6748}
6749
Willy Tarreauc11416f2007-06-17 16:58:38 +02006750/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6751 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6752 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006753static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006754acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006755 struct acl_expr *expr, struct acl_test *test)
6756{
6757 struct http_txn *txn = l7;
6758 struct hdr_idx *idx = &txn->hdr_idx;
6759 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006760
Willy Tarreaub6866442008-07-14 23:54:42 +02006761 if (!txn)
6762 return 0;
6763
Willy Tarreau33a7e692007-06-10 19:45:56 +02006764 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6765 /* search for header from the beginning */
6766 ctx->idx = 0;
6767
Willy Tarreau33a7e692007-06-10 19:45:56 +02006768 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6769 test->flags |= ACL_TEST_F_FETCH_MORE;
6770 test->flags |= ACL_TEST_F_VOL_HDR;
6771 test->len = ctx->vlen;
6772 test->ptr = (char *)ctx->line + ctx->val;
6773 return 1;
6774 }
6775
6776 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6777 test->flags |= ACL_TEST_F_VOL_HDR;
6778 return 0;
6779}
6780
Willy Tarreau33a7e692007-06-10 19:45:56 +02006781static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006782acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6783 struct acl_expr *expr, struct acl_test *test)
6784{
6785 struct http_txn *txn = l7;
6786
Willy Tarreaub6866442008-07-14 23:54:42 +02006787 if (!txn)
6788 return 0;
6789
Willy Tarreau655dce92009-11-08 13:10:58 +01006790 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006791 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006792
Willy Tarreauc11416f2007-06-17 16:58:38 +02006793 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6794 /* ensure the indexes are not affected */
6795 return 0;
6796
6797 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6798}
6799
6800static int
6801acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6802 struct acl_expr *expr, struct acl_test *test)
6803{
6804 struct http_txn *txn = l7;
6805
Willy Tarreaub6866442008-07-14 23:54:42 +02006806 if (!txn)
6807 return 0;
6808
Willy Tarreau655dce92009-11-08 13:10:58 +01006809 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006810 return 0;
6811
6812 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6813}
6814
6815/* 6. Check on HTTP header count. The number of occurrences is returned.
6816 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6817 */
6818static int
6819acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006820 struct acl_expr *expr, struct acl_test *test)
6821{
6822 struct http_txn *txn = l7;
6823 struct hdr_idx *idx = &txn->hdr_idx;
6824 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006825 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006826
Willy Tarreaub6866442008-07-14 23:54:42 +02006827 if (!txn)
6828 return 0;
6829
Willy Tarreau33a7e692007-06-10 19:45:56 +02006830 ctx.idx = 0;
6831 cnt = 0;
6832 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6833 cnt++;
6834
6835 test->i = cnt;
6836 test->flags = ACL_TEST_F_VOL_HDR;
6837 return 1;
6838}
6839
Willy Tarreauc11416f2007-06-17 16:58:38 +02006840static int
6841acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6842 struct acl_expr *expr, struct acl_test *test)
6843{
6844 struct http_txn *txn = l7;
6845
Willy Tarreaub6866442008-07-14 23:54:42 +02006846 if (!txn)
6847 return 0;
6848
Willy Tarreau655dce92009-11-08 13:10:58 +01006849 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006850 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006851
Willy Tarreauc11416f2007-06-17 16:58:38 +02006852 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6853 /* ensure the indexes are not affected */
6854 return 0;
6855
6856 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6857}
6858
6859static int
6860acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6861 struct acl_expr *expr, struct acl_test *test)
6862{
6863 struct http_txn *txn = l7;
6864
Willy Tarreaub6866442008-07-14 23:54:42 +02006865 if (!txn)
6866 return 0;
6867
Willy Tarreau655dce92009-11-08 13:10:58 +01006868 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006869 return 0;
6870
6871 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6872}
6873
Willy Tarreau33a7e692007-06-10 19:45:56 +02006874/* 7. Check on HTTP header's integer value. The integer value is returned.
6875 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006876 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006877 */
6878static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006879acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006880 struct acl_expr *expr, struct acl_test *test)
6881{
6882 struct http_txn *txn = l7;
6883 struct hdr_idx *idx = &txn->hdr_idx;
6884 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006885
Willy Tarreaub6866442008-07-14 23:54:42 +02006886 if (!txn)
6887 return 0;
6888
Willy Tarreau33a7e692007-06-10 19:45:56 +02006889 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6890 /* search for header from the beginning */
6891 ctx->idx = 0;
6892
Willy Tarreau33a7e692007-06-10 19:45:56 +02006893 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6894 test->flags |= ACL_TEST_F_FETCH_MORE;
6895 test->flags |= ACL_TEST_F_VOL_HDR;
6896 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6897 return 1;
6898 }
6899
6900 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6901 test->flags |= ACL_TEST_F_VOL_HDR;
6902 return 0;
6903}
6904
Willy Tarreauc11416f2007-06-17 16:58:38 +02006905static int
6906acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6907 struct acl_expr *expr, struct acl_test *test)
6908{
6909 struct http_txn *txn = l7;
6910
Willy Tarreaub6866442008-07-14 23:54:42 +02006911 if (!txn)
6912 return 0;
6913
Willy Tarreau655dce92009-11-08 13:10:58 +01006914 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006915 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006916
Willy Tarreauc11416f2007-06-17 16:58:38 +02006917 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6918 /* ensure the indexes are not affected */
6919 return 0;
6920
6921 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6922}
6923
6924static int
6925acl_fetch_shdr_val(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
Willy Tarreaub6866442008-07-14 23:54:42 +02006930 if (!txn)
6931 return 0;
6932
Willy Tarreau655dce92009-11-08 13:10:58 +01006933 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006934 return 0;
6935
6936 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6937}
6938
Willy Tarreau106f9792009-09-19 07:54:16 +02006939/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6940 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6941 */
6942static int
6943acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6944 struct acl_expr *expr, struct acl_test *test)
6945{
6946 struct http_txn *txn = l7;
6947 struct hdr_idx *idx = &txn->hdr_idx;
6948 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6949
6950 if (!txn)
6951 return 0;
6952
6953 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6954 /* search for header from the beginning */
6955 ctx->idx = 0;
6956
6957 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6958 test->flags |= ACL_TEST_F_FETCH_MORE;
6959 test->flags |= ACL_TEST_F_VOL_HDR;
6960 /* Same optimization as url_ip */
6961 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6962 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6963 test->ptr = (void *)&l4->srv_addr.sin_addr;
6964 test->i = AF_INET;
6965 return 1;
6966 }
6967
6968 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6969 test->flags |= ACL_TEST_F_VOL_HDR;
6970 return 0;
6971}
6972
6973static int
6974acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6975 struct acl_expr *expr, struct acl_test *test)
6976{
6977 struct http_txn *txn = l7;
6978
6979 if (!txn)
6980 return 0;
6981
Willy Tarreau655dce92009-11-08 13:10:58 +01006982 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006983 return 0;
6984
6985 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6986 /* ensure the indexes are not affected */
6987 return 0;
6988
6989 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6990}
6991
6992static int
6993acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6994 struct acl_expr *expr, struct acl_test *test)
6995{
6996 struct http_txn *txn = l7;
6997
6998 if (!txn)
6999 return 0;
7000
Willy Tarreau655dce92009-11-08 13:10:58 +01007001 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007002 return 0;
7003
7004 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7005}
7006
Willy Tarreau737b0c12007-06-10 21:28:46 +02007007/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7008 * the first '/' after the possible hostname, and ends before the possible '?'.
7009 */
7010static int
7011acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7012 struct acl_expr *expr, struct acl_test *test)
7013{
7014 struct http_txn *txn = l7;
7015 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007016
Willy Tarreaub6866442008-07-14 23:54:42 +02007017 if (!txn)
7018 return 0;
7019
Willy Tarreau655dce92009-11-08 13:10:58 +01007020 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007021 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007022
Willy Tarreauc11416f2007-06-17 16:58:38 +02007023 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7024 /* ensure the indexes are not affected */
7025 return 0;
7026
Willy Tarreau962c3f42010-01-10 00:15:35 +01007027 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007028 ptr = http_get_path(txn);
7029 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007030 return 0;
7031
7032 /* OK, we got the '/' ! */
7033 test->ptr = ptr;
7034
7035 while (ptr < end && *ptr != '?')
7036 ptr++;
7037
7038 test->len = ptr - test->ptr;
7039
7040 /* we do not need to set READ_ONLY because the data is in a buffer */
7041 test->flags = ACL_TEST_F_VOL_1ST;
7042 return 1;
7043}
7044
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007045static int
7046acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7047 struct acl_expr *expr, struct acl_test *test)
7048{
7049 struct buffer *req = s->req;
7050 struct http_txn *txn = &s->txn;
7051 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007052
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007053 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7054 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7055 */
7056
7057 if (!s || !req)
7058 return 0;
7059
Willy Tarreau655dce92009-11-08 13:10:58 +01007060 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007061 /* Already decoded as OK */
7062 test->flags |= ACL_TEST_F_SET_RES_PASS;
7063 return 1;
7064 }
7065
7066 /* Try to decode HTTP request */
7067 if (likely(req->lr < req->r))
7068 http_msg_analyzer(req, msg, &txn->hdr_idx);
7069
Willy Tarreau655dce92009-11-08 13:10:58 +01007070 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007071 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7072 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7073 return 1;
7074 }
7075 /* wait for final state */
7076 test->flags |= ACL_TEST_F_MAY_CHANGE;
7077 return 0;
7078 }
7079
7080 /* OK we got a valid HTTP request. We have some minor preparation to
7081 * perform so that further checks can rely on HTTP tests.
7082 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007083 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007084 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7085 s->flags |= SN_REDIRECTABLE;
7086
7087 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7088 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7089 return 1;
7090 }
7091
7092 test->flags |= ACL_TEST_F_SET_RES_PASS;
7093 return 1;
7094}
7095
Willy Tarreau8797c062007-05-07 00:55:35 +02007096
7097/************************************************************************/
7098/* All supported keywords must be declared here. */
7099/************************************************************************/
7100
7101/* Note: must not be declared <const> as its list will be overwritten */
7102static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007103 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7104
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007105 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7106 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7107 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7108 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007109
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007110 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7111 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7112 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7113 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7114 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7115 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7116 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7117 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7118 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007119
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007120 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7121 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7122 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7123 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7124 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7125 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7126 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7127 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7128 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7129 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007130 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007131
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007132 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7133 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7134 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7135 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7136 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7137 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7138 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7139 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7140 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007141 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007142
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007143 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7144 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7145 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7146 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7147 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7148 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7149 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007150
Willy Tarreauf3d25982007-05-08 22:45:09 +02007151 { NULL, NULL, NULL, NULL },
7152
7153#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007154 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7155 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7156 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7157 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7158 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7159 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7160 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7161
Willy Tarreau8797c062007-05-07 00:55:35 +02007162 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7163 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7164 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7165 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7166 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7167 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7168 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7169 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7170
7171 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7172 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7173 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7174 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7175 { NULL, NULL, NULL, NULL },
7176#endif
7177}};
7178
7179
7180__attribute__((constructor))
7181static void __http_protocol_init(void)
7182{
7183 acl_register_keywords(&acl_kws);
7184}
7185
7186
Willy Tarreau58f10d72006-12-04 02:26:12 +01007187/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007188 * Local variables:
7189 * c-indent-level: 8
7190 * c-basic-offset: 8
7191 * End:
7192 */