blob: 07e701de9accfd5821309c629a6e3e41947e0ba8 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaua9679ac2010-01-03 17:32:57 +010082/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020083const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
103const char *HTTP_401_fmt =
104 "HTTP/1.0 401 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200107 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
114 [HTTP_ERR_400] = 400,
115 [HTTP_ERR_403] = 403,
116 [HTTP_ERR_408] = 408,
117 [HTTP_ERR_500] = 500,
118 [HTTP_ERR_502] = 502,
119 [HTTP_ERR_503] = 503,
120 [HTTP_ERR_504] = 504,
121};
122
Willy Tarreau80587432006-12-24 17:47:20 +0100123static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100125 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 "Cache-Control: no-cache\r\n"
127 "Connection: close\r\n"
128 "Content-Type: text/html\r\n"
129 "\r\n"
130 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
131
132 [HTTP_ERR_403] =
133 "HTTP/1.0 403 Forbidden\r\n"
134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
139
140 [HTTP_ERR_408] =
141 "HTTP/1.0 408 Request Time-out\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
147
148 [HTTP_ERR_500] =
149 "HTTP/1.0 500 Server Error\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
155
156 [HTTP_ERR_502] =
157 "HTTP/1.0 502 Bad Gateway\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
163
164 [HTTP_ERR_503] =
165 "HTTP/1.0 503 Service Unavailable\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
171
172 [HTTP_ERR_504] =
173 "HTTP/1.0 504 Gateway Time-out\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
179
180};
181
Willy Tarreau80587432006-12-24 17:47:20 +0100182/* We must put the messages here since GCC cannot initialize consts depending
183 * on strlen().
184 */
185struct chunk http_err_chunks[HTTP_ERR_SIZE];
186
Willy Tarreau42250582007-04-01 01:30:43 +0200187#define FD_SETS_ARE_BITFIELDS
188#ifdef FD_SETS_ARE_BITFIELDS
189/*
190 * This map is used with all the FD_* macros to check whether a particular bit
191 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
192 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
193 * byte should be encoded. Be careful to always pass bytes from 0 to 255
194 * exclusively to the macros.
195 */
196fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
198
199#else
200#error "Check if your OS uses bitfields for fd_sets"
201#endif
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203void init_proto_http()
204{
Willy Tarreau42250582007-04-01 01:30:43 +0200205 int i;
206 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100207 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200208
Willy Tarreau80587432006-12-24 17:47:20 +0100209 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
210 if (!http_err_msgs[msg]) {
211 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
212 abort();
213 }
214
215 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
216 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
217 }
Willy Tarreau42250582007-04-01 01:30:43 +0200218
219 /* initialize the log header encoding map : '{|}"#' should be encoded with
220 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
221 * URL encoding only requires '"', '#' to be encoded as well as non-
222 * printable characters above.
223 */
224 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
225 memset(url_encode_map, 0, sizeof(url_encode_map));
226 for (i = 0; i < 32; i++) {
227 FD_SET(i, hdr_encode_map);
228 FD_SET(i, url_encode_map);
229 }
230 for (i = 127; i < 256; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234
235 tmp = "\"#{|}";
236 while (*tmp) {
237 FD_SET(*tmp, hdr_encode_map);
238 tmp++;
239 }
240
241 tmp = "\"#";
242 while (*tmp) {
243 FD_SET(*tmp, url_encode_map);
244 tmp++;
245 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200246
247 /* memory allocations */
248 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200249 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100250}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
Willy Tarreau53b6c742006-12-17 13:37:46 +0100252/*
253 * We have 26 list of methods (1 per first letter), each of which can have
254 * up to 3 entries (2 valid, 1 null).
255 */
256struct http_method_desc {
257 http_meth_t meth;
258 int len;
259 const char text[8];
260};
261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100262const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263 ['C' - 'A'] = {
264 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
265 },
266 ['D' - 'A'] = {
267 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
268 },
269 ['G' - 'A'] = {
270 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
271 },
272 ['H' - 'A'] = {
273 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
274 },
275 ['P' - 'A'] = {
276 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
277 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
278 },
279 ['T' - 'A'] = {
280 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
281 },
282 /* rest is empty like this :
283 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
284 */
285};
286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200288 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289 * RFC2616 for those chars.
290 */
291
292const char http_is_spht[256] = {
293 [' '] = 1, ['\t'] = 1,
294};
295
296const char http_is_crlf[256] = {
297 ['\r'] = 1, ['\n'] = 1,
298};
299
300const char http_is_lws[256] = {
301 [' '] = 1, ['\t'] = 1,
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_sep[256] = {
306 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
307 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
308 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
309 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
310 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
311};
312
313const char http_is_ctl[256] = {
314 [0 ... 31] = 1,
315 [127] = 1,
316};
317
318/*
319 * A token is any ASCII char that is neither a separator nor a CTL char.
320 * Do not overwrite values in assignment since gcc-2.95 will not handle
321 * them correctly. Instead, define every non-CTL char's status.
322 */
323const char http_is_token[256] = {
324 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
325 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
326 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
327 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
328 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
329 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
330 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
331 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
332 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
333 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
334 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
335 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
336 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
337 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
338 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
339 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
340 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
341 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
342 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
343 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
344 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
345 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
346 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
347 ['|'] = 1, ['}'] = 0, ['~'] = 1,
348};
349
350
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100351/*
352 * An http ver_token is any ASCII which can be found in an HTTP version,
353 * which includes 'H', 'T', 'P', '/', '.' and any digit.
354 */
355const char http_is_ver_token[256] = {
356 ['.'] = 1, ['/'] = 1,
357 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
358 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
359 ['H'] = 1, ['P'] = 1, ['T'] = 1,
360};
361
362
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100363/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100364 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
365 */
366#if defined(DEBUG_FSM)
367static void http_silent_debug(int line, struct session *s)
368{
369 int size = 0;
370 size += snprintf(trash + size, sizeof(trash) - size,
371 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
372 line,
373 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
374 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
375 write(-1, trash, size);
376 size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
379 line,
380 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
381 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
382
383 write(-1, trash, size);
384}
385#else
386#define http_silent_debug(l,s) do { } while (0)
387#endif
388
389/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100390 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
391 * CRLF. Text length is measured first, so it cannot be NULL.
392 * The header is also automatically added to the index <hdr_idx>, and the end
393 * of headers is automatically adjusted. The number of bytes added is returned
394 * on success, otherwise <0 is returned indicating an error.
395 */
396int http_header_add_tail(struct buffer *b, struct http_msg *msg,
397 struct hdr_idx *hdr_idx, const char *text)
398{
399 int bytes, len;
400
401 len = strlen(text);
402 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
403 if (!bytes)
404 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100405 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100406 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
407}
408
409/*
410 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
411 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
412 * the buffer is only opened and the space reserved, but nothing is copied.
413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
417int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
418 struct hdr_idx *hdr_idx, const char *text, int len)
419{
420 int bytes;
421
422 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428
429/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100430 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
431 * If so, returns the position of the first non-space character relative to
432 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
433 * to return a pointer to the place after the first space. Returns 0 if the
434 * header name does not match. Checks are case-insensitive.
435 */
436int http_header_match2(const char *hdr, const char *end,
437 const char *name, int len)
438{
439 const char *val;
440
441 if (hdr + len >= end)
442 return 0;
443 if (hdr[len] != ':')
444 return 0;
445 if (strncasecmp(hdr, name, len) != 0)
446 return 0;
447 val = hdr + len + 1;
448 while (val < end && HTTP_IS_SPHT(*val))
449 val++;
450 if ((val >= end) && (len + 2 <= end - hdr))
451 return len + 2; /* we may replace starting from second space */
452 return val - hdr;
453}
454
Willy Tarreau33a7e692007-06-10 19:45:56 +0200455/* Find the end of the header value contained between <s> and <e>.
456 * See RFC2616, par 2.2 for more information. Note that it requires
457 * a valid header to return a valid result.
458 */
459const char *find_hdr_value_end(const char *s, const char *e)
460{
461 int quoted, qdpair;
462
463 quoted = qdpair = 0;
464 for (; s < e; s++) {
465 if (qdpair) qdpair = 0;
466 else if (quoted && *s == '\\') qdpair = 1;
467 else if (quoted && *s == '"') quoted = 0;
468 else if (*s == '"') quoted = 1;
469 else if (*s == ',') return s;
470 }
471 return s;
472}
473
474/* Find the first or next occurrence of header <name> in message buffer <sol>
475 * using headers index <idx>, and return it in the <ctx> structure. This
476 * structure holds everything necessary to use the header and find next
477 * occurrence. If its <idx> member is 0, the header is searched from the
478 * beginning. Otherwise, the next occurrence is returned. The function returns
479 * 1 when it finds a value, and 0 when there is no more.
480 */
481int http_find_header2(const char *name, int len,
482 const char *sol, struct hdr_idx *idx,
483 struct hdr_ctx *ctx)
484{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200485 const char *eol, *sov;
486 int cur_idx;
487
488 if (ctx->idx) {
489 /* We have previously returned a value, let's search
490 * another one on the same line.
491 */
492 cur_idx = ctx->idx;
493 sol = ctx->line;
494 sov = sol + ctx->val + ctx->vlen;
495 eol = sol + idx->v[cur_idx].len;
496
497 if (sov >= eol)
498 /* no more values in this header */
499 goto next_hdr;
500
501 /* values remaining for this header, skip the comma */
502 sov++;
503 while (sov < eol && http_is_lws[(unsigned char)*sov])
504 sov++;
505
506 goto return_hdr;
507 }
508
509 /* first request for this header */
510 sol += hdr_idx_first_pos(idx);
511 cur_idx = hdr_idx_first_idx(idx);
512
513 while (cur_idx) {
514 eol = sol + idx->v[cur_idx].len;
515
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200516 if (len == 0) {
517 /* No argument was passed, we want any header.
518 * To achieve this, we simply build a fake request. */
519 while (sol + len < eol && sol[len] != ':')
520 len++;
521 name = sol;
522 }
523
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 if ((len < eol - sol) &&
525 (sol[len] == ':') &&
526 (strncasecmp(sol, name, len) == 0)) {
527
528 sov = sol + len + 1;
529 while (sov < eol && http_is_lws[(unsigned char)*sov])
530 sov++;
531 return_hdr:
532 ctx->line = sol;
533 ctx->idx = cur_idx;
534 ctx->val = sov - sol;
535
536 eol = find_hdr_value_end(sov, eol);
537 ctx->vlen = eol - sov;
538 return 1;
539 }
540 next_hdr:
541 sol = eol + idx->v[cur_idx].cr + 1;
542 cur_idx = idx->v[cur_idx].next;
543 }
544 return 0;
545}
546
547int http_find_header(const char *name,
548 const char *sol, struct hdr_idx *idx,
549 struct hdr_ctx *ctx)
550{
551 return http_find_header2(name, strlen(name), sol, idx, ctx);
552}
553
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100554/* This function handles a server error at the stream interface level. The
555 * stream interface is assumed to be already in a closed state. An optional
556 * message is copied into the input buffer, and an HTTP status code stored.
557 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100558 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100560static void http_server_error(struct session *t, struct stream_interface *si,
561 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100563 buffer_erase(si->ob);
564 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200565 buffer_auto_close(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100566 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100567 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100568 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 }
570 if (!(t->flags & SN_ERR_MASK))
571 t->flags |= err;
572 if (!(t->flags & SN_FINST_MASK))
573 t->flags |= finst;
574}
575
Willy Tarreau80587432006-12-24 17:47:20 +0100576/* This function returns the appropriate error location for the given session
577 * and message.
578 */
579
580struct chunk *error_message(struct session *s, int msgnum)
581{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200582 if (s->be->errmsg[msgnum].str)
583 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100584 else if (s->fe->errmsg[msgnum].str)
585 return &s->fe->errmsg[msgnum];
586 else
587 return &http_err_chunks[msgnum];
588}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200589
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590/*
591 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
592 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
593 */
594static http_meth_t find_http_meth(const char *str, const int len)
595{
596 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100597 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100598
599 m = ((unsigned)*str - 'A');
600
601 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100602 for (h = http_methods[m]; h->len > 0; h++) {
603 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100604 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100605 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100606 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100607 };
608 return HTTP_METH_OTHER;
609 }
610 return HTTP_METH_NONE;
611
612}
613
Willy Tarreau21d2af32008-02-14 20:25:24 +0100614/* Parse the URI from the given transaction (which is assumed to be in request
615 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
616 * It is returned otherwise.
617 */
618static char *
619http_get_path(struct http_txn *txn)
620{
621 char *ptr, *end;
622
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100623 ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100624 end = ptr + txn->req.sl.rq.u_l;
625
626 if (ptr >= end)
627 return NULL;
628
629 /* RFC2616, par. 5.1.2 :
630 * Request-URI = "*" | absuri | abspath | authority
631 */
632
633 if (*ptr == '*')
634 return NULL;
635
636 if (isalpha((unsigned char)*ptr)) {
637 /* this is a scheme as described by RFC3986, par. 3.1 */
638 ptr++;
639 while (ptr < end &&
640 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
641 ptr++;
642 /* skip '://' */
643 if (ptr == end || *ptr++ != ':')
644 return NULL;
645 if (ptr == end || *ptr++ != '/')
646 return NULL;
647 if (ptr == end || *ptr++ != '/')
648 return NULL;
649 }
650 /* skip [user[:passwd]@]host[:[port]] */
651
652 while (ptr < end && *ptr != '/')
653 ptr++;
654
655 if (ptr == end)
656 return NULL;
657
658 /* OK, we got the '/' ! */
659 return ptr;
660}
661
Willy Tarreauefb453c2008-10-26 20:49:47 +0100662/* Returns a 302 for a redirectable request. This may only be called just after
663 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
664 * left unchanged and will follow normal proxy processing.
665 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100667{
668 struct http_txn *txn;
669 struct chunk rdr;
670 char *path;
671 int len;
672
673 /* 1: create the response header */
674 rdr.len = strlen(HTTP_302);
675 rdr.str = trash;
676 memcpy(rdr.str, HTTP_302, rdr.len);
677
678 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200679 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100680 return;
681
682 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
683 rdr.len += s->srv->rdr_len;
684
685 /* 3: add the request URI */
686 txn = &s->txn;
687 path = http_get_path(txn);
688 if (!path)
689 return;
690
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100691 len = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200692 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100693 return;
694
695 memcpy(rdr.str + rdr.len, path, len);
696 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100697 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
698 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699
700 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100701 si->shutr(si);
702 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100703 si->err_type = SI_ET_NONE;
704 si->err_loc = NULL;
705 si->state = SI_ST_CLO;
706
707 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100708 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100709
710 /* FIXME: we should increase a counter of redirects per server and per backend. */
711 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100712 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713}
714
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100715/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100716 * that the server side is closed. Note that err_type is actually a
717 * bitmask, where almost only aborts may be cumulated with other
718 * values. We consider that aborted operations are more important
719 * than timeouts or errors due to the fact that nobody else in the
720 * logs might explain incomplete retries. All others should avoid
721 * being cumulated. It should normally not be possible to have multiple
722 * aborts at once, but just in case, the first one in sequence is reported.
723 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100724void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100725{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100726 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100727
728 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100729 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
730 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100731 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100732 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
733 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100734 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100735 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
736 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100737 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100738 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
739 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100741 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
742 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100743 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100744 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
745 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100746 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100747 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
748 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100749}
750
Willy Tarreau42250582007-04-01 01:30:43 +0200751extern const char sess_term_cond[8];
752extern const char sess_fin_state[8];
753extern const char *monthname[12];
754const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
755const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
756 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
757 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200758struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200759struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100760
Emeric Brun3a058f32009-06-30 18:26:00 +0200761void http_sess_clflog(struct session *s)
762{
763 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
764 struct proxy *fe = s->fe;
765 struct proxy *be = s->be;
766 struct proxy *prx_log;
767 struct http_txn *txn = &s->txn;
768 int tolog, level, err;
769 char *uri, *h;
770 char *svid;
771 struct tm tm;
772 static char tmpline[MAX_SYSLOG_LEN];
773 int hdr;
774 size_t w;
775 int t_request;
776
777 prx_log = fe;
778 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
779 (s->conn_retries != be->conn_retries) ||
780 txn->status >= 500;
781
782 if (s->cli_addr.ss_family == AF_INET)
783 inet_ntop(AF_INET,
784 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
785 pn, sizeof(pn));
786 else
787 inet_ntop(AF_INET6,
788 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
789 pn, sizeof(pn));
790
791 get_gmtime(s->logs.accept_date.tv_sec, &tm);
792
793 /* FIXME: let's limit ourselves to frontend logging for now. */
794 tolog = fe->to_log;
795
796 h = tmpline;
797
798 w = snprintf(h, sizeof(tmpline),
799 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
800 pn,
801 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
802 tm.tm_hour, tm.tm_min, tm.tm_sec);
803 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
804 goto trunc;
805 h += w;
806
807 if (h >= tmpline + sizeof(tmpline) - 4)
808 goto trunc;
809
810 *(h++) = ' ';
811 *(h++) = '\"';
812 uri = txn->uri ? txn->uri : "<BADREQ>";
813 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
814 '#', url_encode_map, uri);
815 *(h++) = '\"';
816
817 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
818 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
819 goto trunc;
820 h += w;
821
822 if (h >= tmpline + sizeof(tmpline) - 9)
823 goto trunc;
824 memcpy(h, " \"-\" \"-\"", 8);
825 h += 8;
826
827 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
828 " %d %03d",
829 (s->cli_addr.ss_family == AF_INET) ?
830 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
831 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
832 (int)s->logs.accept_date.tv_usec/1000);
833 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
834 goto trunc;
835 h += w;
836
837 w = strlen(fe->id);
838 if (h >= tmpline + sizeof(tmpline) - 4 - w)
839 goto trunc;
840 *(h++) = ' ';
841 *(h++) = '\"';
842 memcpy(h, fe->id, w);
843 h += w;
844 *(h++) = '\"';
845
846 w = strlen(be->id);
847 if (h >= tmpline + sizeof(tmpline) - 4 - w)
848 goto trunc;
849 *(h++) = ' ';
850 *(h++) = '\"';
851 memcpy(h, be->id, w);
852 h += w;
853 *(h++) = '\"';
854
855 svid = (tolog & LW_SVID) ?
856 (s->data_source != DATA_SRC_STATS) ?
857 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
858
859 w = strlen(svid);
860 if (h >= tmpline + sizeof(tmpline) - 4 - w)
861 goto trunc;
862 *(h++) = ' ';
863 *(h++) = '\"';
864 memcpy(h, svid, w);
865 h += w;
866 *(h++) = '\"';
867
868 t_request = -1;
869 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
870 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
871 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
872 " %d %ld %ld %ld %ld",
873 t_request,
874 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
875 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
876 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
877 s->logs.t_close);
878 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
879 goto trunc;
880 h += w;
881
882 if (h >= tmpline + sizeof(tmpline) - 8)
883 goto trunc;
884 *(h++) = ' ';
885 *(h++) = '\"';
886 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
887 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
888 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
889 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
890 *(h++) = '\"';
891
892 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
893 " %d %d %d %d %d %ld %ld",
894 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
895 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
896 s->logs.srv_queue_size, s->logs.prx_queue_size);
897
898 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
899 goto trunc;
900 h += w;
901
902 if (txn->cli_cookie) {
903 w = strlen(txn->cli_cookie);
904 if (h >= tmpline + sizeof(tmpline) - 4 - w)
905 goto trunc;
906 *(h++) = ' ';
907 *(h++) = '\"';
908 memcpy(h, txn->cli_cookie, w);
909 h += w;
910 *(h++) = '\"';
911 } else {
912 if (h >= tmpline + sizeof(tmpline) - 5)
913 goto trunc;
914 memcpy(h, " \"-\"", 4);
915 h += 4;
916 }
917
918 if (txn->srv_cookie) {
919 w = strlen(txn->srv_cookie);
920 if (h >= tmpline + sizeof(tmpline) - 4 - w)
921 goto trunc;
922 *(h++) = ' ';
923 *(h++) = '\"';
924 memcpy(h, txn->srv_cookie, w);
925 h += w;
926 *(h++) = '\"';
927 } else {
928 if (h >= tmpline + sizeof(tmpline) - 5)
929 goto trunc;
930 memcpy(h, " \"-\"", 4);
931 h += 4;
932 }
933
934 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
935 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
936 if (h >= sizeof (tmpline) + tmpline - 4)
937 goto trunc;
938 *(h++) = ' ';
939 *(h++) = '\"';
940 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
941 '#', hdr_encode_map, txn->req.cap[hdr]);
942 *(h++) = '\"';
943 }
944 }
945
946 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
947 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
948 if (h >= sizeof (tmpline) + tmpline - 4)
949 goto trunc;
950 *(h++) = ' ';
951 *(h++) = '\"';
952 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
953 '#', hdr_encode_map, txn->rsp.cap[hdr]);
954 *(h++) = '\"';
955 }
956 }
957
958trunc:
959 *h = '\0';
960
961 level = LOG_INFO;
962 if (err && (fe->options2 & PR_O2_LOGERRORS))
963 level = LOG_ERR;
964
965 send_log(prx_log, level, "%s\n", tmpline);
966
967 s->logs.logwait = 0;
968}
969
Willy Tarreau42250582007-04-01 01:30:43 +0200970/*
971 * send a log for the session when we have enough info about it.
972 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100974void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200975{
976 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
977 struct proxy *fe = s->fe;
978 struct proxy *be = s->be;
979 struct proxy *prx_log;
980 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200981 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200982 char *uri, *h;
983 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200984 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200985 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200986 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200987 int hdr;
988
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200989 /* if we don't want to log normal traffic, return now */
990 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
991 (s->conn_retries != be->conn_retries) ||
992 txn->status >= 500;
993 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
994 return;
995
Willy Tarreau42250582007-04-01 01:30:43 +0200996 if (fe->logfac1 < 0 && fe->logfac2 < 0)
997 return;
998 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100999
Emeric Brun3a058f32009-06-30 18:26:00 +02001000 if (prx_log->options2 & PR_O2_CLFLOG)
1001 return http_sess_clflog(s);
1002
Willy Tarreau42250582007-04-01 01:30:43 +02001003 if (s->cli_addr.ss_family == AF_INET)
1004 inet_ntop(AF_INET,
1005 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1006 pn, sizeof(pn));
1007 else
1008 inet_ntop(AF_INET6,
1009 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1010 pn, sizeof(pn));
1011
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001012 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001013
1014 /* FIXME: let's limit ourselves to frontend logging for now. */
1015 tolog = fe->to_log;
1016
1017 h = tmpline;
1018 if (fe->to_log & LW_REQHDR &&
1019 txn->req.cap &&
1020 (h < tmpline + sizeof(tmpline) - 10)) {
1021 *(h++) = ' ';
1022 *(h++) = '{';
1023 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1024 if (hdr)
1025 *(h++) = '|';
1026 if (txn->req.cap[hdr] != NULL)
1027 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1028 '#', hdr_encode_map, txn->req.cap[hdr]);
1029 }
1030 *(h++) = '}';
1031 }
1032
1033 if (fe->to_log & LW_RSPHDR &&
1034 txn->rsp.cap &&
1035 (h < tmpline + sizeof(tmpline) - 7)) {
1036 *(h++) = ' ';
1037 *(h++) = '{';
1038 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1039 if (hdr)
1040 *(h++) = '|';
1041 if (txn->rsp.cap[hdr] != NULL)
1042 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1043 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1044 }
1045 *(h++) = '}';
1046 }
1047
1048 if (h < tmpline + sizeof(tmpline) - 4) {
1049 *(h++) = ' ';
1050 *(h++) = '"';
1051 uri = txn->uri ? txn->uri : "<BADREQ>";
1052 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1053 '#', url_encode_map, uri);
1054 *(h++) = '"';
1055 }
1056 *h = '\0';
1057
1058 svid = (tolog & LW_SVID) ?
1059 (s->data_source != DATA_SRC_STATS) ?
1060 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1061
Willy Tarreau70089872008-06-13 21:12:51 +02001062 t_request = -1;
1063 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1064 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1065
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001066 level = LOG_INFO;
1067 if (err && (fe->options2 & PR_O2_LOGERRORS))
1068 level = LOG_ERR;
1069
1070 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001071 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001072 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1073 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001074 pn,
1075 (s->cli_addr.ss_family == AF_INET) ?
1076 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1077 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001078 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001079 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001080 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001081 t_request,
1082 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001083 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1084 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1085 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1086 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001087 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001088 txn->cli_cookie ? txn->cli_cookie : "-",
1089 txn->srv_cookie ? txn->srv_cookie : "-",
1090 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1091 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1092 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1093 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1094 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001095 (s->flags & SN_REDISP)?"+":"",
1096 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001097 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1098
1099 s->logs.logwait = 0;
1100}
1101
Willy Tarreau117f59e2007-03-04 18:17:17 +01001102
1103/*
1104 * Capture headers from message starting at <som> according to header list
1105 * <cap_hdr>, and fill the <idx> structure appropriately.
1106 */
1107void capture_headers(char *som, struct hdr_idx *idx,
1108 char **cap, struct cap_hdr *cap_hdr)
1109{
1110 char *eol, *sol, *col, *sov;
1111 int cur_idx;
1112 struct cap_hdr *h;
1113 int len;
1114
1115 sol = som + hdr_idx_first_pos(idx);
1116 cur_idx = hdr_idx_first_idx(idx);
1117
1118 while (cur_idx) {
1119 eol = sol + idx->v[cur_idx].len;
1120
1121 col = sol;
1122 while (col < eol && *col != ':')
1123 col++;
1124
1125 sov = col + 1;
1126 while (sov < eol && http_is_lws[(unsigned char)*sov])
1127 sov++;
1128
1129 for (h = cap_hdr; h; h = h->next) {
1130 if ((h->namelen == col - sol) &&
1131 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1132 if (cap[h->index] == NULL)
1133 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001134 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001135
1136 if (cap[h->index] == NULL) {
1137 Alert("HTTP capture : out of memory.\n");
1138 continue;
1139 }
1140
1141 len = eol - sov;
1142 if (len > h->len)
1143 len = h->len;
1144
1145 memcpy(cap[h->index], sov, len);
1146 cap[h->index][len]=0;
1147 }
1148 }
1149 sol = eol + idx->v[cur_idx].cr + 1;
1150 cur_idx = idx->v[cur_idx].next;
1151 }
1152}
1153
1154
Willy Tarreau42250582007-04-01 01:30:43 +02001155/* either we find an LF at <ptr> or we jump to <bad>.
1156 */
1157#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1158
1159/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1160 * otherwise to <http_msg_ood> with <state> set to <st>.
1161 */
1162#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1163 ptr++; \
1164 if (likely(ptr < end)) \
1165 goto good; \
1166 else { \
1167 state = (st); \
1168 goto http_msg_ood; \
1169 } \
1170 } while (0)
1171
1172
Willy Tarreaubaaee002006-06-26 02:48:02 +02001173/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001174 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001175 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1176 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1177 * will give undefined results.
1178 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1179 * and that msg->sol points to the beginning of the response.
1180 * If a complete line is found (which implies that at least one CR or LF is
1181 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1182 * returned indicating an incomplete line (which does not mean that parts have
1183 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1184 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1185 * upon next call.
1186 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001187 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001188 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1189 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001190 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001192const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1193 unsigned int state, const char *ptr, const char *end,
1194 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001195{
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 switch (state) {
1197 http_msg_rpver:
1198 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001199 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1201
1202 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001203 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1205 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001206 state = HTTP_MSG_ERROR;
1207 break;
1208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 http_msg_rpver_sp:
1210 case HTTP_MSG_RPVER_SP:
1211 if (likely(!HTTP_IS_LWS(*ptr))) {
1212 msg->sl.st.c = ptr - msg_buf;
1213 goto http_msg_rpcode;
1214 }
1215 if (likely(HTTP_IS_SPHT(*ptr)))
1216 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1217 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001218 state = HTTP_MSG_ERROR;
1219 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001220
1221 http_msg_rpcode:
1222 case HTTP_MSG_RPCODE:
1223 if (likely(!HTTP_IS_LWS(*ptr)))
1224 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1225
1226 if (likely(HTTP_IS_SPHT(*ptr))) {
1227 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1228 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1229 }
1230
1231 /* so it's a CR/LF, so there is no reason phrase */
1232 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1233 http_msg_rsp_reason:
1234 /* FIXME: should we support HTTP responses without any reason phrase ? */
1235 msg->sl.st.r = ptr - msg_buf;
1236 msg->sl.st.r_l = 0;
1237 goto http_msg_rpline_eol;
1238
1239 http_msg_rpcode_sp:
1240 case HTTP_MSG_RPCODE_SP:
1241 if (likely(!HTTP_IS_LWS(*ptr))) {
1242 msg->sl.st.r = ptr - msg_buf;
1243 goto http_msg_rpreason;
1244 }
1245 if (likely(HTTP_IS_SPHT(*ptr)))
1246 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1247 /* so it's a CR/LF, so there is no reason phrase */
1248 goto http_msg_rsp_reason;
1249
1250 http_msg_rpreason:
1251 case HTTP_MSG_RPREASON:
1252 if (likely(!HTTP_IS_CRLF(*ptr)))
1253 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1254 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1255 http_msg_rpline_eol:
1256 /* We have seen the end of line. Note that we do not
1257 * necessarily have the \n yet, but at least we know that we
1258 * have EITHER \r OR \n, otherwise the response would not be
1259 * complete. We can then record the response length and return
1260 * to the caller which will be able to register it.
1261 */
1262 msg->sl.st.l = ptr - msg->sol;
1263 return ptr;
1264
1265#ifdef DEBUG_FULL
1266 default:
1267 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1268 exit(1);
1269#endif
1270 }
1271
1272 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001273 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 if (ret_state)
1275 *ret_state = state;
1276 if (ret_ptr)
1277 *ret_ptr = (char *)ptr;
1278 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001279}
1280
1281
1282/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 * This function parses a request line between <ptr> and <end>, starting with
1284 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1285 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1286 * will give undefined results.
1287 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1288 * and that msg->sol points to the beginning of the request.
1289 * If a complete line is found (which implies that at least one CR or LF is
1290 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1291 * returned indicating an incomplete line (which does not mean that parts have
1292 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1293 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1294 * upon next call.
1295 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001296 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1298 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001299 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001301const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1302 unsigned int state, const char *ptr, const char *end,
1303 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001304{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 switch (state) {
1306 http_msg_rqmeth:
1307 case HTTP_MSG_RQMETH:
1308 if (likely(HTTP_IS_TOKEN(*ptr)))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001312 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 if (likely(HTTP_IS_CRLF(*ptr))) {
1317 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001318 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 http_msg_req09_uri:
1320 msg->sl.rq.u = ptr - msg_buf;
1321 http_msg_req09_uri_e:
1322 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1323 http_msg_req09_ver:
1324 msg->sl.rq.v = ptr - msg_buf;
1325 msg->sl.rq.v_l = 0;
1326 goto http_msg_rqline_eol;
1327 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001328 state = HTTP_MSG_ERROR;
1329 break;
1330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 http_msg_rqmeth_sp:
1332 case HTTP_MSG_RQMETH_SP:
1333 if (likely(!HTTP_IS_LWS(*ptr))) {
1334 msg->sl.rq.u = ptr - msg_buf;
1335 goto http_msg_rquri;
1336 }
1337 if (likely(HTTP_IS_SPHT(*ptr)))
1338 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1339 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1340 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 http_msg_rquri:
1343 case HTTP_MSG_RQURI:
1344 if (likely(!HTTP_IS_LWS(*ptr)))
1345 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 if (likely(HTTP_IS_SPHT(*ptr))) {
1348 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1353 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 http_msg_rquri_sp:
1356 case HTTP_MSG_RQURI_SP:
1357 if (likely(!HTTP_IS_LWS(*ptr))) {
1358 msg->sl.rq.v = ptr - msg_buf;
1359 goto http_msg_rqver;
1360 }
1361 if (likely(HTTP_IS_SPHT(*ptr)))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1363 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1364 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 http_msg_rqver:
1367 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001368 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001370
1371 if (likely(HTTP_IS_CRLF(*ptr))) {
1372 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1373 http_msg_rqline_eol:
1374 /* We have seen the end of line. Note that we do not
1375 * necessarily have the \n yet, but at least we know that we
1376 * have EITHER \r OR \n, otherwise the request would not be
1377 * complete. We can then record the request length and return
1378 * to the caller which will be able to register it.
1379 */
1380 msg->sl.rq.l = ptr - msg->sol;
1381 return ptr;
1382 }
1383
1384 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001385 state = HTTP_MSG_ERROR;
1386 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388#ifdef DEBUG_FULL
1389 default:
1390 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1391 exit(1);
1392#endif
1393 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001396 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 if (ret_state)
1398 *ret_state = state;
1399 if (ret_ptr)
1400 *ret_ptr = (char *)ptr;
1401 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001403
1404
Willy Tarreau8973c702007-01-21 23:58:29 +01001405/*
1406 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001407 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 * when data are missing and recalled at the exact same location with no
1409 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001410 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001411 * fields. Note that msg->som and msg->sol will be initialized after completing
1412 * the first state, so that none of the msg pointers has to be initialized
1413 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1416{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001417 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001420 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 ptr = buf->lr;
1422 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 if (unlikely(ptr >= end))
1425 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001428 /*
1429 * First, states that are specific to the response only.
1430 * We check them first so that request and headers are
1431 * closer to each other (accessed more often).
1432 */
1433 http_msg_rpbefore:
1434 case HTTP_MSG_RPBEFORE:
1435 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001436 /* we have a start of message, but we have to check
1437 * first if we need to remove some CRLF. We can only
1438 * do this when send_max=0.
1439 */
1440 char *beg = buf->w + buf->send_max;
1441 if (beg >= buf->data + buf->size)
1442 beg -= buf->size;
1443 if (unlikely(ptr != beg)) {
1444 if (buf->send_max)
1445 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001446 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001447 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001449 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001450 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 hdr_idx_init(idx);
1452 state = HTTP_MSG_RPVER;
1453 goto http_msg_rpver;
1454 }
1455
1456 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1457 goto http_msg_invalid;
1458
1459 if (unlikely(*ptr == '\n'))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1461 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1462 /* stop here */
1463
1464 http_msg_rpbefore_cr:
1465 case HTTP_MSG_RPBEFORE_CR:
1466 EXPECT_LF_HERE(ptr, http_msg_invalid);
1467 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1468 /* stop here */
1469
1470 http_msg_rpver:
1471 case HTTP_MSG_RPVER:
1472 case HTTP_MSG_RPVER_SP:
1473 case HTTP_MSG_RPCODE:
1474 case HTTP_MSG_RPCODE_SP:
1475 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001476 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001477 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001478 if (unlikely(!ptr))
1479 return;
1480
1481 /* we have a full response and we know that we have either a CR
1482 * or an LF at <ptr>.
1483 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001484 //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 +01001485 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1486
1487 msg->sol = ptr;
1488 if (likely(*ptr == '\r'))
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1490 goto http_msg_rpline_end;
1491
1492 http_msg_rpline_end:
1493 case HTTP_MSG_RPLINE_END:
1494 /* msg->sol must point to the first of CR or LF. */
1495 EXPECT_LF_HERE(ptr, http_msg_invalid);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1497 /* stop here */
1498
1499 /*
1500 * Second, states that are specific to the request only
1501 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 http_msg_rqbefore:
1503 case HTTP_MSG_RQBEFORE:
1504 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001505 /* we have a start of message, but we have to check
1506 * first if we need to remove some CRLF. We can only
1507 * do this when send_max=0.
1508 */
1509 char *beg = buf->w + buf->send_max;
1510 if (beg >= buf->data + buf->size)
1511 beg -= buf->size;
1512 if (likely(ptr != beg)) {
1513 if (buf->send_max)
1514 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001515 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001516 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001518 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001519 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001520 /* we will need this when keep-alive will be supported
1521 hdr_idx_init(idx);
1522 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 state = HTTP_MSG_RQMETH;
1524 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1528 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 if (unlikely(*ptr == '\n'))
1531 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1532 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001533 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 http_msg_rqbefore_cr:
1536 case HTTP_MSG_RQBEFORE_CR:
1537 EXPECT_LF_HERE(ptr, http_msg_invalid);
1538 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001539 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001540
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 http_msg_rqmeth:
1542 case HTTP_MSG_RQMETH:
1543 case HTTP_MSG_RQMETH_SP:
1544 case HTTP_MSG_RQURI:
1545 case HTTP_MSG_RQURI_SP:
1546 case HTTP_MSG_RQVER:
1547 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001548 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 if (unlikely(!ptr))
1550 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001551
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 /* we have a full request and we know that we have either a CR
1553 * or an LF at <ptr>.
1554 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001555 //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 +01001556 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 msg->sol = ptr;
1559 if (likely(*ptr == '\r'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 http_msg_rqline_end:
1564 case HTTP_MSG_RQLINE_END:
1565 /* check for HTTP/0.9 request : no version information available.
1566 * msg->sol must point to the first of CR or LF.
1567 */
1568 if (unlikely(msg->sl.rq.v_l == 0))
1569 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 EXPECT_LF_HERE(ptr, http_msg_invalid);
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001573 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8973c702007-01-21 23:58:29 +01001575 /*
1576 * Common states below
1577 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 http_msg_hdr_first:
1579 case HTTP_MSG_HDR_FIRST:
1580 msg->sol = ptr;
1581 if (likely(!HTTP_IS_CRLF(*ptr))) {
1582 goto http_msg_hdr_name;
1583 }
1584
1585 if (likely(*ptr == '\r'))
1586 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1587 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 http_msg_hdr_name:
1590 case HTTP_MSG_HDR_NAME:
1591 /* assumes msg->sol points to the first char */
1592 if (likely(HTTP_IS_TOKEN(*ptr)))
1593 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001595 if (likely(*ptr == ':')) {
1596 msg->col = ptr - buf->data;
1597 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1598 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001600 if (likely(msg->err_pos < -1) || *ptr == '\n')
1601 goto http_msg_invalid;
1602
1603 if (msg->err_pos == -1) /* capture error pointer */
1604 msg->err_pos = ptr - buf->data; /* >= 0 now */
1605
1606 /* and we still accept this non-token character */
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 http_msg_hdr_l1_sp:
1610 case HTTP_MSG_HDR_L1_SP:
1611 /* assumes msg->sol points to the first char and msg->col to the colon */
1612 if (likely(HTTP_IS_SPHT(*ptr)))
1613 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 /* header value can be basically anything except CR/LF */
1616 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 if (likely(!HTTP_IS_CRLF(*ptr))) {
1619 goto http_msg_hdr_val;
1620 }
1621
1622 if (likely(*ptr == '\r'))
1623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1624 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 http_msg_hdr_l1_lf:
1627 case HTTP_MSG_HDR_L1_LF:
1628 EXPECT_LF_HERE(ptr, http_msg_invalid);
1629 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 http_msg_hdr_l1_lws:
1632 case HTTP_MSG_HDR_L1_LWS:
1633 if (likely(HTTP_IS_SPHT(*ptr))) {
1634 /* replace HT,CR,LF with spaces */
1635 for (; buf->data+msg->sov < ptr; msg->sov++)
1636 buf->data[msg->sov] = ' ';
1637 goto http_msg_hdr_l1_sp;
1638 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001639 /* we had a header consisting only in spaces ! */
1640 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 goto http_msg_complete_header;
1642
1643 http_msg_hdr_val:
1644 case HTTP_MSG_HDR_VAL:
1645 /* assumes msg->sol points to the first char, msg->col to the
1646 * colon, and msg->sov points to the first character of the
1647 * value.
1648 */
1649 if (likely(!HTTP_IS_CRLF(*ptr)))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 msg->eol = ptr;
1653 /* Note: we could also copy eol into ->eoh so that we have the
1654 * real header end in case it ends with lots of LWS, but is this
1655 * really needed ?
1656 */
1657 if (likely(*ptr == '\r'))
1658 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1659 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 http_msg_hdr_l2_lf:
1662 case HTTP_MSG_HDR_L2_LF:
1663 EXPECT_LF_HERE(ptr, http_msg_invalid);
1664 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 http_msg_hdr_l2_lws:
1667 case HTTP_MSG_HDR_L2_LWS:
1668 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1669 /* LWS: replace HT,CR,LF with spaces */
1670 for (; msg->eol < ptr; msg->eol++)
1671 *msg->eol = ' ';
1672 goto http_msg_hdr_val;
1673 }
1674 http_msg_complete_header:
1675 /*
1676 * It was a new header, so the last one is finished.
1677 * Assumes msg->sol points to the first char, msg->col to the
1678 * colon, msg->sov points to the first character of the value
1679 * and msg->eol to the first CR or LF so we know how the line
1680 * ends. We insert last header into the index.
1681 */
1682 /*
1683 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1684 write(2, msg->sol, msg->eol-msg->sol);
1685 fprintf(stderr,"\n");
1686 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1689 idx, idx->tail) < 0))
1690 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 msg->sol = ptr;
1693 if (likely(!HTTP_IS_CRLF(*ptr))) {
1694 goto http_msg_hdr_name;
1695 }
1696
1697 if (likely(*ptr == '\r'))
1698 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1699 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001700
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 http_msg_last_lf:
1702 case HTTP_MSG_LAST_LF:
1703 /* Assumes msg->sol points to the first of either CR or LF */
1704 EXPECT_LF_HERE(ptr, http_msg_invalid);
1705 ptr++;
1706 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001707 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001709 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 return;
1711#ifdef DEBUG_FULL
1712 default:
1713 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1714 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001715#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 }
1717 http_msg_ood:
1718 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001719 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 buf->lr = ptr;
1721 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 http_msg_invalid:
1724 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001725 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001726 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 return;
1728}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001729
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001730/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1731 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1732 * nothing is done and 1 is returned.
1733 */
1734static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1735{
1736 int delta;
1737 char *cur_end;
1738
1739 if (msg->sl.rq.v_l != 0)
1740 return 1;
1741
1742 msg->sol = req->data + msg->som;
1743 cur_end = msg->sol + msg->sl.rq.l;
1744 delta = 0;
1745
1746 if (msg->sl.rq.u_l == 0) {
1747 /* if no URI was set, add "/" */
1748 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1749 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001750 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001751 }
1752 /* add HTTP version */
1753 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001754 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001755 cur_end += delta;
1756 cur_end = (char *)http_parse_reqline(msg, req->data,
1757 HTTP_MSG_RQMETH,
1758 msg->sol, cur_end + 1,
1759 NULL, NULL);
1760 if (unlikely(!cur_end))
1761 return 0;
1762
1763 /* we have a full HTTP/1.0 request now and we know that
1764 * we have either a CR or an LF at <ptr>.
1765 */
1766 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1767 return 1;
1768}
1769
Willy Tarreau5b154472009-12-21 20:11:07 +01001770/* Parse the Connection: headaer of an HTTP request, and set the transaction
1771 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1772 * is also set so that we don't parse a second time. If some dangerous values
1773 * are encountered, we leave the status to indicate that the request might be
1774 * interpreted as keep-alive, but we also set the connection flags to indicate
1775 * that we WANT it to be a close, so that the header will be fixed. This
1776 * function should only be called when we know we're interested in checking
1777 * the request (not a CONNECT, and FE or BE mangles the header).
1778 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001779void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001780{
1781 struct http_msg *msg = &txn->req;
1782 struct hdr_ctx ctx;
1783 int conn_cl, conn_ka;
1784
1785 if (txn->flags & TX_CON_HDR_PARS)
1786 return;
1787
1788 conn_cl = 0;
1789 conn_ka = 0;
1790 ctx.idx = 0;
1791
1792 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1793 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1794 conn_cl = 1;
1795 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1796 conn_ka = 1;
1797 }
1798
1799 /* Determine if the client wishes keep-alive or close.
1800 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1801 * are persistent unless "Connection: close" is explicitly specified.
1802 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1803 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1804 * they explicitly specify "Connection: Keep-Alive", regardless of any
1805 * optional "Keep-Alive" header.
1806 * Note that if we find a request with both "Connection: close" and
1807 * "Connection: Keep-Alive", we indicate we want a close but don't have
1808 * it, so that it can be enforced later.
1809 */
1810
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001811 if (conn_cl && conn_ka) {
1812 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1813 }
1814 else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
Willy Tarreau5b154472009-12-21 20:11:07 +01001815 if (conn_cl) {
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001816 txn->flags |= TX_REQ_CONN_CLO;
1817 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1818 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01001819 }
1820 } else { /* HTTP/1.0 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001821 if (!conn_ka) {
1822 txn->flags |= TX_REQ_CONN_CLO;
1823 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1824 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1825 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001826 }
1827 txn->flags |= TX_CON_HDR_PARS;
1828}
1829
Willy Tarreaud98cf932009-12-27 22:54:55 +01001830/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1831 * first byte of body, and increments msg->sov by the number of bytes parsed,
1832 * so that we know we can forward between ->som and ->sov. Note that due to
1833 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1834 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001835 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001837 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001838int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001839{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840 char *ptr = buf->lr;
1841 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001842 unsigned int chunk = 0;
1843
1844 /* The chunk size is in the following form, though we are only
1845 * interested in the size and CRLF :
1846 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1847 */
1848 while (1) {
1849 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001850 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001851 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 if (c < 0) /* not a hex digit anymore */
1854 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855 if (++ptr >= end)
1856 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001857 if (chunk & 0xF000000) /* overflow will occur */
1858 return -1;
1859 chunk = (chunk << 4) + c;
1860 }
1861
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862 /* empty size not allowed */
1863 if (ptr == buf->lr)
1864 return -1;
1865
1866 while (http_is_spht[(unsigned char)*ptr]) {
1867 if (++ptr >= end)
1868 ptr = buf->data;
1869 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001870 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001871 }
1872
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873 /* Up to there, we know that at least one byte is present at *ptr. Check
1874 * for the end of chunk size.
1875 */
1876 while (1) {
1877 if (likely(HTTP_IS_CRLF(*ptr))) {
1878 /* we now have a CR or an LF at ptr */
1879 if (likely(*ptr == '\r')) {
1880 if (++ptr >= end)
1881 ptr = buf->data;
1882 if (ptr == buf->r)
1883 return 0;
1884 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001885
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 if (*ptr != '\n')
1887 return -1;
1888 if (++ptr >= end)
1889 ptr = buf->data;
1890 /* done */
1891 break;
1892 }
1893 else if (*ptr == ';') {
1894 /* chunk extension, ends at next CRLF */
1895 if (++ptr >= end)
1896 ptr = buf->data;
1897 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001898 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001899
1900 while (!HTTP_IS_CRLF(*ptr)) {
1901 if (++ptr >= end)
1902 ptr = buf->data;
1903 if (ptr == buf->r)
1904 return 0;
1905 }
1906 /* we have a CRLF now, loop above */
1907 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001908 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001910 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001911 }
1912
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 /* OK we found our CRLF and now <ptr> points to the next byte,
1914 * which may or may not be present. We save that into ->lr and
1915 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001916 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 msg->sov += ptr - buf->lr;
1918 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001919 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 return 1;
1922}
1923
Willy Tarreaud98cf932009-12-27 22:54:55 +01001924/* This function skips trailers in the buffer <buf> associated with HTTP
1925 * message <msg>. The first visited position is buf->lr. If the end of
1926 * the trailers is found, it is automatically scheduled to be forwarded,
1927 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1928 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001929 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1930 * zero. If a parse error is encountered, the function returns < 0 and does not
1931 * change anything except maybe buf->lr and msg->sov. Note that the message
1932 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1933 * which implies that all non-trailers data have already been scheduled for
1934 * forwarding, and that the difference between msg->som and msg->sov exactly
1935 * matches the length of trailers already parsed and not forwarded. It is also
1936 * important to note that this function is designed to be able to parse wrapped
1937 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001938 */
1939int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1940{
1941 /* we have buf->lr which points to next line. Look for CRLF. */
1942 while (1) {
1943 char *p1 = NULL, *p2 = NULL;
1944 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001945 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946
1947 /* scan current line and stop at LF or CRLF */
1948 while (1) {
1949 if (ptr == buf->r)
1950 return 0;
1951
1952 if (*ptr == '\n') {
1953 if (!p1)
1954 p1 = ptr;
1955 p2 = ptr;
1956 break;
1957 }
1958
1959 if (*ptr == '\r') {
1960 if (p1)
1961 return -1;
1962 p1 = ptr;
1963 }
1964
1965 ptr++;
1966 if (ptr >= buf->data + buf->size)
1967 ptr = buf->data;
1968 }
1969
1970 /* after LF; point to beginning of next line */
1971 p2++;
1972 if (p2 >= buf->data + buf->size)
1973 p2 = buf->data;
1974
Willy Tarreau638cd022010-01-03 07:42:04 +01001975 bytes = p2 - buf->lr;
1976 if (bytes < 0)
1977 bytes += buf->size;
1978
1979 /* schedule this line for forwarding */
1980 msg->sov += bytes;
1981 if (msg->sov >= buf->size)
1982 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001983
Willy Tarreau638cd022010-01-03 07:42:04 +01001984 if (p1 == buf->lr) {
1985 /* LF/CRLF at beginning of line => end of trailers at p2.
1986 * Everything was scheduled for forwarding, there's nothing
1987 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001988 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001989 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990 msg->msg_state = HTTP_MSG_DONE;
1991 return 1;
1992 }
1993 /* OK, next line then */
1994 buf->lr = p2;
1995 }
1996}
1997
1998/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1999 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2000 * ->som, buf->lr in order to include this part into the next forwarding phase.
2001 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2002 * not enough data are available, the function does not change anything and
2003 * returns zero. If a parse error is encountered, the function returns < 0 and
2004 * does not change anything. Note: this function is designed to parse wrapped
2005 * CRLF at the end of the buffer.
2006 */
2007int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2008{
2009 char *ptr;
2010 int bytes;
2011
2012 /* NB: we'll check data availabilty at the end. It's not a
2013 * problem because whatever we match first will be checked
2014 * against the correct length.
2015 */
2016 bytes = 1;
2017 ptr = buf->lr;
2018 if (*ptr == '\r') {
2019 bytes++;
2020 ptr++;
2021 if (ptr >= buf->data + buf->size)
2022 ptr = buf->data;
2023 }
2024
2025 if (buf->l < bytes)
2026 return 0;
2027
2028 if (*ptr != '\n')
2029 return -1;
2030
2031 ptr++;
2032 if (ptr >= buf->data + buf->size)
2033 ptr = buf->data;
2034 buf->lr = ptr;
2035 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2036 msg->sov = ptr - buf->data;
2037 msg->som = msg->sov - bytes;
2038 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2039 return 1;
2040}
Willy Tarreau5b154472009-12-21 20:11:07 +01002041
Willy Tarreau83e3af02009-12-28 17:39:57 +01002042void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2043{
2044 char *end = buf->data + buf->size;
2045 int off = buf->data + buf->size - buf->w;
2046
2047 /* two possible cases :
2048 * - the buffer is in one contiguous block, we move it in-place
2049 * - the buffer is in two blocks, we move it via the trash
2050 */
2051 if (buf->l) {
2052 int block1 = buf->l;
2053 int block2 = 0;
2054 if (buf->r <= buf->w) {
2055 /* non-contiguous block */
2056 block1 = buf->data + buf->size - buf->w;
2057 block2 = buf->r - buf->data;
2058 }
2059 if (block2)
2060 memcpy(trash, buf->data, block2);
2061 memmove(buf->data, buf->w, block1);
2062 if (block2)
2063 memcpy(buf->data + block1, trash, block2);
2064 }
2065
2066 /* adjust all known pointers */
2067 buf->w = buf->data;
2068 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2069 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2070 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2071 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2072
2073 /* adjust relative pointers */
2074 msg->som = 0;
2075 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2076 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2077 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2078
2079 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2080 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2081
2082 if (msg->err_pos >= 0) {
2083 msg->err_pos += off;
2084 if (msg->err_pos >= buf->size)
2085 msg->err_pos -= buf->size;
2086 }
2087
2088 buf->flags &= ~BF_FULL;
2089 if (buf->l >= buffer_max_len(buf))
2090 buf->flags |= BF_FULL;
2091}
2092
Willy Tarreaud787e662009-07-07 10:14:51 +02002093/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2094 * processing can continue on next analysers, or zero if it either needs more
2095 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2096 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2097 * when it has nothing left to do, and may remove any analyser when it wants to
2098 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002099 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002100int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002101{
Willy Tarreau59234e92008-11-30 23:51:27 +01002102 /*
2103 * We will parse the partial (or complete) lines.
2104 * We will check the request syntax, and also join multi-line
2105 * headers. An index of all the lines will be elaborated while
2106 * parsing.
2107 *
2108 * For the parsing, we use a 28 states FSM.
2109 *
2110 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002111 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002112 * req->data + msg->eoh = end of processed headers / start of current one
2113 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 * req->lr = first non-visited byte
2115 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002116 *
2117 * At end of parsing, we may perform a capture of the error (if any), and
2118 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2119 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2120 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002121 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002122
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002124 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 struct http_txn *txn = &s->txn;
2126 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002127 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002128
Willy Tarreau6bf17362009-02-24 10:48:35 +01002129 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2130 now_ms, __FUNCTION__,
2131 s,
2132 req,
2133 req->rex, req->wex,
2134 req->flags,
2135 req->l,
2136 req->analysers);
2137
Willy Tarreau52a0c602009-08-16 22:45:38 +02002138 /* we're speaking HTTP here, so let's speak HTTP to the client */
2139 s->srv_error = http_return_srv_error;
2140
Willy Tarreau83e3af02009-12-28 17:39:57 +01002141 /* There's a protected area at the end of the buffer for rewriting
2142 * purposes. We don't want to start to parse the request if the
2143 * protected area is affected, because we may have to move processed
2144 * data later, which is much more complicated.
2145 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002146 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
2147 if (unlikely((req->flags & BF_FULL) ||
2148 req->r < req->lr ||
2149 req->r > req->data + req->size - global.tune.maxrewrite)) {
2150 if (req->send_max) {
2151 /* some data has still not left the buffer, wake us once that's done */
2152 buffer_dont_connect(req);
2153 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2154 return 0;
2155 }
2156 if (req->l <= req->size - global.tune.maxrewrite)
2157 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002158 }
2159
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002160 if (likely(req->lr < req->r))
2161 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002162 }
2163
Willy Tarreau59234e92008-11-30 23:51:27 +01002164 /* 1: we might have to print this header in debug mode */
2165 if (unlikely((global.mode & MODE_DEBUG) &&
2166 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002167 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002168 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002169
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 sol = req->data + msg->som;
2171 eol = sol + msg->sl.rq.l;
2172 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002173
Willy Tarreau59234e92008-11-30 23:51:27 +01002174 sol += hdr_idx_first_pos(&txn->hdr_idx);
2175 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002176
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 while (cur_idx) {
2178 eol = sol + txn->hdr_idx.v[cur_idx].len;
2179 debug_hdr("clihdr", s, sol, eol);
2180 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2181 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002182 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002183 }
2184
Willy Tarreau58f10d72006-12-04 02:26:12 +01002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 /*
2187 * Now we quickly check if we have found a full valid request.
2188 * If not so, we check the FD and buffer states before leaving.
2189 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002190 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 * requests are checked first.
2192 *
2193 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002194
Willy Tarreau655dce92009-11-08 13:10:58 +01002195 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002196 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002197 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002198 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002199 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2200 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002201
Willy Tarreau59234e92008-11-30 23:51:27 +01002202 /* 1: Since we are in header mode, if there's no space
2203 * left for headers, we won't be able to free more
2204 * later, so the session will never terminate. We
2205 * must terminate it now.
2206 */
2207 if (unlikely(req->flags & BF_FULL)) {
2208 /* FIXME: check if URI is set and return Status
2209 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002210 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 goto return_bad_req;
2212 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002213
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 /* 2: have we encountered a read error ? */
2215 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002216 if (txn->flags & TX_NOT_FIRST)
2217 goto failed_keep_alive;
2218
Willy Tarreau59234e92008-11-30 23:51:27 +01002219 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002220 if (msg->err_pos >= 0)
2221 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002222 msg->msg_state = HTTP_MSG_ERROR;
2223 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002224
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002225 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002226 if (s->listener->counters)
2227 s->listener->counters->failed_req++;
2228
Willy Tarreau59234e92008-11-30 23:51:27 +01002229 if (!(s->flags & SN_ERR_MASK))
2230 s->flags |= SN_ERR_CLICL;
2231 if (!(s->flags & SN_FINST_MASK))
2232 s->flags |= SN_FINST_R;
2233 return 0;
2234 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002235
Willy Tarreau59234e92008-11-30 23:51:27 +01002236 /* 3: has the read timeout expired ? */
2237 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002238 if (txn->flags & TX_NOT_FIRST)
2239 goto failed_keep_alive;
2240
Willy Tarreau59234e92008-11-30 23:51:27 +01002241 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002242 if (msg->err_pos >= 0)
2243 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002244 txn->status = 408;
2245 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2246 msg->msg_state = HTTP_MSG_ERROR;
2247 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002248
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002249 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002250 if (s->listener->counters)
2251 s->listener->counters->failed_req++;
2252
Willy Tarreau59234e92008-11-30 23:51:27 +01002253 if (!(s->flags & SN_ERR_MASK))
2254 s->flags |= SN_ERR_CLITO;
2255 if (!(s->flags & SN_FINST_MASK))
2256 s->flags |= SN_FINST_R;
2257 return 0;
2258 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002259
Willy Tarreau59234e92008-11-30 23:51:27 +01002260 /* 4: have we encountered a close ? */
2261 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002262 if (txn->flags & TX_NOT_FIRST)
2263 goto failed_keep_alive;
2264
Willy Tarreau4076a152009-04-02 15:18:36 +02002265 if (msg->err_pos >= 0)
2266 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 txn->status = 400;
2268 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2269 msg->msg_state = HTTP_MSG_ERROR;
2270 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002271
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002272 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002273 if (s->listener->counters)
2274 s->listener->counters->failed_req++;
2275
Willy Tarreau59234e92008-11-30 23:51:27 +01002276 if (!(s->flags & SN_ERR_MASK))
2277 s->flags |= SN_ERR_CLICL;
2278 if (!(s->flags & SN_FINST_MASK))
2279 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002280 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002281 }
2282
Willy Tarreau520d95e2009-09-19 21:04:57 +02002283 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002284 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002285 req->flags &= ~BF_DONT_READ;
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002286
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 /* just set the request timeout once at the beginning of the request */
2288 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002289 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 /* we're not ready yet */
2292 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002293
2294 failed_keep_alive:
2295 /* Here we process low-level errors for keep-alive requests. In
2296 * short, if the request is not the first one and it experiences
2297 * a timeout, read error or shutdown, we just silently close so
2298 * that the client can try again.
2299 */
2300 txn->status = 0;
2301 msg->msg_state = HTTP_MSG_RQBEFORE;
2302 req->analysers = 0;
2303 s->logs.logwait = 0;
2304 stream_int_cond_close(req->prod, NULL);
2305 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002307
Willy Tarreaud787e662009-07-07 10:14:51 +02002308 /* OK now we have a complete HTTP request with indexed headers. Let's
2309 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002310 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2311 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2312 * points to the CRLF of the request line. req->lr points to the first
2313 * byte after the last LF. msg->col and msg->sov point to the first
2314 * byte of data. msg->eol cannot be trusted because it may have been
2315 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002316 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002317
Willy Tarreaud787e662009-07-07 10:14:51 +02002318 /* Maybe we found in invalid header name while we were configured not
2319 * to block on that, so we have to capture it now.
2320 */
2321 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002322 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2323
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 /* ensure we keep this pointer to the beginning of the message */
2325 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002326
Willy Tarreau59234e92008-11-30 23:51:27 +01002327 /*
2328 * 1: identify the method
2329 */
2330 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2331
2332 /* we can make use of server redirect on GET and HEAD */
2333 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2334 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002335
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 /*
2337 * 2: check if the URI matches the monitor_uri.
2338 * We have to do this for every request which gets in, because
2339 * the monitor-uri is defined by the frontend.
2340 */
2341 if (unlikely((s->fe->monitor_uri_len != 0) &&
2342 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2343 !memcmp(&req->data[msg->sl.rq.u],
2344 s->fe->monitor_uri,
2345 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002346 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002348 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002354 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2355 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 ret = acl_pass(ret);
2358 if (cond->pol == ACL_COND_UNLESS)
2359 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 if (ret) {
2362 /* we fail this request, let's return 503 service unavail */
2363 txn->status = 503;
2364 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2365 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002366 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002367 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 /* nothing to fail, let's reply normaly */
2370 txn->status = 200;
2371 stream_int_retnclose(req->prod, &http_200_chunk);
2372 goto return_prx_cond;
2373 }
2374
2375 /*
2376 * 3: Maybe we have to copy the original REQURI for the logs ?
2377 * Note: we cannot log anymore if the request has been
2378 * classified as invalid.
2379 */
2380 if (unlikely(s->logs.logwait & LW_REQ)) {
2381 /* we have a complete HTTP request that we must log */
2382 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2383 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 if (urilen >= REQURI_LEN)
2386 urilen = REQURI_LEN - 1;
2387 memcpy(txn->uri, &req->data[msg->som], urilen);
2388 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 if (!(s->logs.logwait &= ~LW_REQ))
2391 s->do_log(s);
2392 } else {
2393 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002394 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 }
Willy Tarreau06619262006-12-17 08:37:22 +01002396
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002398 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2399 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002400
Willy Tarreau5b154472009-12-21 20:11:07 +01002401 /* ... and check if the request is HTTP/1.1 or above */
2402 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002403 ((req->data[msg->sl.rq.v + 5] > '1') ||
2404 ((req->data[msg->sl.rq.v + 5] == '1') &&
2405 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002406 txn->flags |= TX_REQ_VER_11;
2407
2408 /* "connection" has not been parsed yet */
2409 txn->flags &= ~TX_CON_HDR_PARS;
2410
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002411 /* transfer length unknown*/
2412 txn->flags &= ~TX_REQ_XFER_LEN;
2413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 /* 5: we may need to capture headers */
2415 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2416 capture_headers(req->data + msg->som, &txn->hdr_idx,
2417 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002418
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002419 /* 6: determine the transfer-length.
2420 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2421 * the presence of a message-body in a REQUEST and its transfer length
2422 * must be determined that way (in order of precedence) :
2423 * 1. The presence of a message-body in a request is signaled by the
2424 * inclusion of a Content-Length or Transfer-Encoding header field
2425 * in the request's header fields. When a request message contains
2426 * both a message-body of non-zero length and a method that does
2427 * not define any semantics for that request message-body, then an
2428 * origin server SHOULD either ignore the message-body or respond
2429 * with an appropriate error message (e.g., 413). A proxy or
2430 * gateway, when presented the same request, SHOULD either forward
2431 * the request inbound with the message- body or ignore the
2432 * message-body when determining a response.
2433 *
2434 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2435 * and the "chunked" transfer-coding (Section 6.2) is used, the
2436 * transfer-length is defined by the use of this transfer-coding.
2437 * If a Transfer-Encoding header field is present and the "chunked"
2438 * transfer-coding is not present, the transfer-length is defined
2439 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002440 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002441 * 3. If a Content-Length header field is present, its decimal value in
2442 * OCTETs represents both the entity-length and the transfer-length.
2443 * If a message is received with both a Transfer-Encoding header
2444 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002445 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002446 * 4. By the server closing the connection. (Closing the connection
2447 * cannot be used to indicate the end of a request body, since that
2448 * would leave no possibility for the server to send back a response.)
2449 *
2450 * Whenever a transfer-coding is applied to a message-body, the set of
2451 * transfer-codings MUST include "chunked", unless the message indicates
2452 * it is terminated by closing the connection. When the "chunked"
2453 * transfer-coding is used, it MUST be the last transfer-coding applied
2454 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002455 */
2456
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002457 /* CONNECT sets a tunnel and ignores everything else */
2458 if (txn->meth == HTTP_METH_CONNECT)
2459 goto skip_xfer_len;
2460
2461 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002464 while ((txn->flags & TX_REQ_VER_11) &&
2465 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002466 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2467 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2468 else if (txn->flags & TX_REQ_TE_CHNK) {
2469 /* bad transfer-encoding (chunked followed by something else) */
2470 use_close_only = 1;
2471 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2472 break;
2473 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 }
2475
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2479 signed long long cl;
2480
2481 if (!ctx.vlen)
2482 goto return_bad_req;
2483
2484 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2485 goto return_bad_req; /* parse failure */
2486
2487 if (cl < 0)
2488 goto return_bad_req;
2489
2490 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2491 goto return_bad_req; /* already specified, was different */
2492
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002493 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002494 msg->hdr_content_len = cl;
2495 }
2496
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002497 /* bodyless requests have a known length */
2498 if (!use_close_only)
2499 txn->flags |= TX_REQ_XFER_LEN;
2500
2501 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002502 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002503 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002504 req->analyse_exp = TICK_ETERNITY;
2505 return 1;
2506
2507 return_bad_req:
2508 /* We centralize bad requests processing here */
2509 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2510 /* we detected a parsing error. We want to archive this request
2511 * in the dedicated proxy area for later troubleshooting.
2512 */
2513 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2514 }
2515
2516 txn->req.msg_state = HTTP_MSG_ERROR;
2517 txn->status = 400;
2518 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002519
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002520 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002521 if (s->listener->counters)
2522 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002523
2524 return_prx_cond:
2525 if (!(s->flags & SN_ERR_MASK))
2526 s->flags |= SN_ERR_PRXCOND;
2527 if (!(s->flags & SN_FINST_MASK))
2528 s->flags |= SN_FINST_R;
2529
2530 req->analysers = 0;
2531 req->analyse_exp = TICK_ETERNITY;
2532 return 0;
2533}
2534
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002535/* This stream analyser runs all HTTP request processing which is common to
2536 * frontends and backends, which means blocking ACLs, filters, connection-close,
2537 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002538 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002539 * either needs more data or wants to immediately abort the request (eg: deny,
2540 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002541 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002542int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002543{
Willy Tarreaud787e662009-07-07 10:14:51 +02002544 struct http_txn *txn = &s->txn;
2545 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002546 struct acl_cond *cond;
2547 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002548 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002549 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002550
Willy Tarreau655dce92009-11-08 13:10:58 +01002551 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002552 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002553 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002554 return 0;
2555 }
2556
Willy Tarreau3a816292009-07-07 10:55:49 +02002557 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 req->analyse_exp = TICK_ETERNITY;
2559
2560 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2561 now_ms, __FUNCTION__,
2562 s,
2563 req,
2564 req->rex, req->wex,
2565 req->flags,
2566 req->l,
2567 req->analysers);
2568
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002569 /* first check whether we have some ACLs set to block this request */
2570 list_for_each_entry(cond, &px->block_cond, list) {
2571 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002572
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002573 ret = acl_pass(ret);
2574 if (cond->pol == ACL_COND_UNLESS)
2575 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002576
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002577 if (ret) {
2578 txn->status = 403;
2579 /* let's log the request time */
2580 s->logs.tv_request = now;
2581 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2582 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002584 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002585
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002586 /* try headers filters */
2587 if (px->req_exp != NULL) {
2588 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2589 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002590
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 /* has the request been denied ? */
2592 if (txn->flags & TX_CLDENY) {
2593 /* no need to go further */
2594 txn->status = 403;
2595 /* let's log the request time */
2596 s->logs.tv_request = now;
2597 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2598 goto return_prx_cond;
2599 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002600
2601 /* When a connection is tarpitted, we use the tarpit timeout,
2602 * which may be the same as the connect timeout if unspecified.
2603 * If unset, then set it to zero because we really want it to
2604 * eventually expire. We build the tarpit as an analyser.
2605 */
2606 if (txn->flags & TX_CLTARPIT) {
2607 buffer_erase(s->req);
2608 /* wipe the request out so that we can drop the connection early
2609 * if the client closes first.
2610 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002611 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002612 req->analysers = 0; /* remove switching rules etc... */
2613 req->analysers |= AN_REQ_HTTP_TARPIT;
2614 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2615 if (!req->analyse_exp)
2616 req->analyse_exp = tick_add(now_ms, 0);
2617 return 1;
2618 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002619 }
Willy Tarreau06619262006-12-17 08:37:22 +01002620
Willy Tarreau5b154472009-12-21 20:11:07 +01002621 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2622 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002623 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2624 * in which headers are mangled. However, if another mode is set, it will
2625 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002626 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002627
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002628 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002629 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002630 int tmp = TX_CON_WANT_TUN;
2631 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2632 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002633 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2634 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002635 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002636 tmp = TX_CON_WANT_CLO;
2637
2638 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002639 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002640
2641 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2642 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002643
2644 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2645 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2646 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2647 if (!(txn->flags & TX_REQ_XFER_LEN))
2648 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2649 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002650 }
2651
2652 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2653 * once we know the backend, because the tunnel mode can be implied by the
2654 * lack of any close/keepalive options in both the FE and the BE. Since
2655 * this information can evolve with time, we proceed by trying to make the
2656 * header status match the desired status. For this, we'll have to adjust
2657 * the "Connection" header. The test for persistent connections has already
2658 * been performed, so we only enter here if there is a risk the connection
2659 * is considered as persistent and we want it to be closed on the server
2660 * side. It would be nice if we could enter this place only when a
2661 * Connection header exists. Note that a CONNECT method will not enter
2662 * here.
2663 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002664 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2665 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2666 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002667 char *cur_ptr, *cur_end, *cur_next;
2668 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002669 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002670 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002671
Willy Tarreau5b154472009-12-21 20:11:07 +01002672 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002673 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002674
Willy Tarreau5b154472009-12-21 20:11:07 +01002675 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002676 cur_hdr = &txn->hdr_idx.v[cur_idx];
2677 cur_ptr = cur_next;
2678 cur_end = cur_ptr + cur_hdr->len;
2679 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002680
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002681 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002682 if (!val)
2683 continue;
2684
2685 /* 3 possibilities :
2686 * - we have already set "Connection: close" or we're in
2687 * HTTP/1.0, so we remove this line.
2688 * - we have not yet set "Connection: close", but this line
2689 * indicates close. We leave it untouched and set the flag.
2690 * - we have not yet set "Connection: close", and this line
2691 * indicates non-close. We replace it and set the flag.
2692 */
2693 if (must_delete) {
2694 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2695 http_msg_move_end(&txn->req, delta);
2696 cur_next += delta;
2697 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2698 txn->hdr_idx.used--;
2699 cur_hdr->len = 0;
2700 txn->flags |= TX_REQ_CONN_CLO;
2701 } else {
2702 if (cur_end - cur_ptr - val != 5 ||
2703 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2704 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2705 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002707 cur_hdr->len += delta;
2708 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002709 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002710 txn->flags |= TX_REQ_CONN_CLO;
2711 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002712 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002713 } /* for loop */
2714 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002715
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002716 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002717 list_for_each_entry(wl, &px->req_add, list) {
2718 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719 goto return_bad_req;
2720 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002721
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722 /* check if stats URI was requested, and if an auth is needed */
2723 if (px->uri_auth != NULL &&
2724 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2725 /* we have to check the URI and auth for this request.
2726 * FIXME!!! that one is rather dangerous, we want to
2727 * make it follow standard rules (eg: clear req->analysers).
2728 */
2729 if (stats_check_uri_auth(s, px)) {
2730 req->analysers = 0;
2731 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002734
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002735 /* check whether we have some ACLs set to redirect this request */
2736 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002737 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002738
Willy Tarreauf285f542010-01-03 20:03:03 +01002739 if (rule->cond) {
2740 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2741 ret = acl_pass(ret);
2742 if (rule->cond->pol == ACL_COND_UNLESS)
2743 ret = !ret;
2744 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002745
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002746 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002747 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002749
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750 /* build redirect message */
2751 switch(rule->code) {
2752 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002753 msg_fmt = HTTP_303;
2754 break;
2755 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 msg_fmt = HTTP_301;
2757 break;
2758 case 302:
2759 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 msg_fmt = HTTP_302;
2761 break;
2762 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002763
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002764 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002766
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 switch(rule->type) {
2768 case REDIRECT_TYPE_PREFIX: {
2769 const char *path;
2770 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002771
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 path = http_get_path(txn);
2773 /* build message using path */
2774 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002775 pathlen = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002776 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2777 int qs = 0;
2778 while (qs < pathlen) {
2779 if (path[qs] == '?') {
2780 pathlen = qs;
2781 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002782 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002783 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002784 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002785 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 } else {
2787 path = "/";
2788 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002789 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002790
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002791 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002792 goto return_bad_req;
2793
2794 /* add prefix. Note that if prefix == "/", we don't want to
2795 * add anything, otherwise it makes it hard for the user to
2796 * configure a self-redirection.
2797 */
2798 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002799 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2800 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002801 }
2802
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 /* add path */
2804 memcpy(rdr.str + rdr.len, path, pathlen);
2805 rdr.len += pathlen;
2806 break;
2807 }
2808 case REDIRECT_TYPE_LOCATION:
2809 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002810 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002812
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002813 /* add location */
2814 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2815 rdr.len += rule->rdr_len;
2816 break;
2817 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002818
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002819 if (rule->cookie_len) {
2820 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2821 rdr.len += 14;
2822 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2823 rdr.len += rule->cookie_len;
2824 memcpy(rdr.str + rdr.len, "\r\n", 2);
2825 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002826 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002827
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002828 /* add end of headers and the keep-alive/close status.
2829 * We may choose to set keep-alive if the Location begins
2830 * with a slash, because the client will come back to the
2831 * same server.
2832 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 txn->status = rule->code;
2834 /* let's log the request time */
2835 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002836
2837 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2838 (txn->flags & TX_REQ_XFER_LEN) &&
2839 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2840 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2841 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2842 /* keep-alive possible */
2843 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2844 rdr.len += 28;
2845 buffer_write(req->prod->ob, rdr.str, rdr.len);
2846 /* "eat" the request */
2847 buffer_ignore(req, msg->sov - msg->som);
2848 msg->som = msg->sov;
2849 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002850 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2851 txn->req.msg_state = HTTP_MSG_CLOSED;
2852 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002853 break;
2854 } else {
2855 /* keep-alive not possible */
2856 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2857 rdr.len += 23;
2858 stream_int_cond_close(req->prod, &rdr);
2859 goto return_prx_cond;
2860 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 }
2862 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002863
Willy Tarreaud98cf932009-12-27 22:54:55 +01002864 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2865 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002866 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002867 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002868 req->flags |= BF_DONT_READ;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002869 else
2870 req->flags &= ~BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002871
Willy Tarreau2be39392010-01-03 17:24:51 +01002872 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2873 * If this happens, then the data will not come immediately, so we must
2874 * send all what we have without waiting. Note that due to the small gain
2875 * in waiting for the body of the request, it's easier to simply put the
2876 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2877 * itself once used.
2878 */
2879 req->flags |= BF_SEND_DONTWAIT;
2880
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002881 /* that's OK for us now, let's move on to next analysers */
2882 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002883
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002884 return_bad_req:
2885 /* We centralize bad requests processing here */
2886 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2887 /* we detected a parsing error. We want to archive this request
2888 * in the dedicated proxy area for later troubleshooting.
2889 */
2890 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2891 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002892
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 txn->req.msg_state = HTTP_MSG_ERROR;
2894 txn->status = 400;
2895 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002896
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002897 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002898 if (s->listener->counters)
2899 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002900
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002901 return_prx_cond:
2902 if (!(s->flags & SN_ERR_MASK))
2903 s->flags |= SN_ERR_PRXCOND;
2904 if (!(s->flags & SN_FINST_MASK))
2905 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002906
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002907 req->analysers = 0;
2908 req->analyse_exp = TICK_ETERNITY;
2909 return 0;
2910}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002911
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002912/* This function performs all the processing enabled for the current request.
2913 * It returns 1 if the processing can continue on next analysers, or zero if it
2914 * needs more data, encounters an error, or wants to immediately abort the
2915 * request. It relies on buffers flags, and updates s->req->analysers.
2916 */
2917int http_process_request(struct session *s, struct buffer *req, int an_bit)
2918{
2919 struct http_txn *txn = &s->txn;
2920 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002921
Willy Tarreau655dce92009-11-08 13:10:58 +01002922 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002923 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002924 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002925 return 0;
2926 }
2927
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002928 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2929 now_ms, __FUNCTION__,
2930 s,
2931 req,
2932 req->rex, req->wex,
2933 req->flags,
2934 req->l,
2935 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002936
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 /*
2938 * Right now, we know that we have processed the entire headers
2939 * and that unwanted requests have been filtered out. We can do
2940 * whatever we want with the remaining request. Also, now we
2941 * may have separate values for ->fe, ->be.
2942 */
Willy Tarreau06619262006-12-17 08:37:22 +01002943
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 /*
2945 * If HTTP PROXY is set we simply get remote server address
2946 * parsing incoming request.
2947 */
2948 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2949 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2950 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002951
Willy Tarreau59234e92008-11-30 23:51:27 +01002952 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002953 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002954 * Note that doing so might move headers in the request, but
2955 * the fields will stay coherent and the URI will not move.
2956 * This should only be performed in the backend.
2957 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002958 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2960 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002961
Willy Tarreau59234e92008-11-30 23:51:27 +01002962 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002963 * 8: the appsession cookie was looked up very early in 1.2,
2964 * so let's do the same now.
2965 */
2966
2967 /* It needs to look into the URI */
2968 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002969 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002970 }
2971
2972 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 * 9: add X-Forwarded-For if either the frontend or the backend
2974 * asks for it.
2975 */
2976 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2977 if (s->cli_addr.ss_family == AF_INET) {
2978 /* Add an X-Forwarded-For header unless the source IP is
2979 * in the 'except' network range.
2980 */
2981 if ((!s->fe->except_mask.s_addr ||
2982 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2983 != s->fe->except_net.s_addr) &&
2984 (!s->be->except_mask.s_addr ||
2985 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2986 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002987 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 unsigned char *pn;
2989 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002990
2991 /* Note: we rely on the backend to get the header name to be used for
2992 * x-forwarded-for, because the header is really meant for the backends.
2993 * However, if the backend did not specify any option, we have to rely
2994 * on the frontend's header name.
2995 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 if (s->be->fwdfor_hdr_len) {
2997 len = s->be->fwdfor_hdr_len;
2998 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002999 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003000 len = s->fe->fwdfor_hdr_len;
3001 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003002 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003004
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003005 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003006 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003007 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003008 }
3009 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003010 else if (s->cli_addr.ss_family == AF_INET6) {
3011 /* FIXME: for the sake of completeness, we should also support
3012 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003013 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003014 int len;
3015 char pn[INET6_ADDRSTRLEN];
3016 inet_ntop(AF_INET6,
3017 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3018 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003019
Willy Tarreau59234e92008-11-30 23:51:27 +01003020 /* Note: we rely on the backend to get the header name to be used for
3021 * x-forwarded-for, because the header is really meant for the backends.
3022 * However, if the backend did not specify any option, we have to rely
3023 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003024 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003025 if (s->be->fwdfor_hdr_len) {
3026 len = s->be->fwdfor_hdr_len;
3027 memcpy(trash, s->be->fwdfor_hdr_name, len);
3028 } else {
3029 len = s->fe->fwdfor_hdr_len;
3030 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003031 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003033
Willy Tarreau59234e92008-11-30 23:51:27 +01003034 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003035 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003036 goto return_bad_req;
3037 }
3038 }
3039
3040 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003041 * 10: add X-Original-To if either the frontend or the backend
3042 * asks for it.
3043 */
3044 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3045
3046 /* FIXME: don't know if IPv6 can handle that case too. */
3047 if (s->cli_addr.ss_family == AF_INET) {
3048 /* Add an X-Original-To header unless the destination IP is
3049 * in the 'except' network range.
3050 */
3051 if (!(s->flags & SN_FRT_ADDR_SET))
3052 get_frt_addr(s);
3053
3054 if ((!s->fe->except_mask_to.s_addr ||
3055 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3056 != s->fe->except_to.s_addr) &&
3057 (!s->be->except_mask_to.s_addr ||
3058 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3059 != s->be->except_to.s_addr)) {
3060 int len;
3061 unsigned char *pn;
3062 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3063
3064 /* Note: we rely on the backend to get the header name to be used for
3065 * x-original-to, because the header is really meant for the backends.
3066 * However, if the backend did not specify any option, we have to rely
3067 * on the frontend's header name.
3068 */
3069 if (s->be->orgto_hdr_len) {
3070 len = s->be->orgto_hdr_len;
3071 memcpy(trash, s->be->orgto_hdr_name, len);
3072 } else {
3073 len = s->fe->orgto_hdr_len;
3074 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003075 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003076 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3077
3078 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003079 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003080 goto return_bad_req;
3081 }
3082 }
3083 }
3084
Willy Tarreau78599912009-10-17 20:12:21 +02003085 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003086 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3087 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3088 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003089 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003090 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003091 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003092 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003093 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003094
3095 /* If we have no server assigned yet and we're balancing on url_param
3096 * with a POST request, we may be interested in checking the body for
3097 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003098 */
3099 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3100 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003101 s->be->url_param_post_limit != 0 &&
3102 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003103 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003104 buffer_dont_connect(req);
3105 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003107
Willy Tarreaud98cf932009-12-27 22:54:55 +01003108 if (txn->flags & TX_REQ_XFER_LEN)
3109 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003110
Willy Tarreau59234e92008-11-30 23:51:27 +01003111 /*************************************************************
3112 * OK, that's finished for the headers. We have done what we *
3113 * could. Let's switch to the DATA state. *
3114 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003115 req->analyse_exp = TICK_ETERNITY;
3116 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003117
Willy Tarreau59234e92008-11-30 23:51:27 +01003118 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003119 /* OK let's go on with the BODY now */
3120 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003121
Willy Tarreau59234e92008-11-30 23:51:27 +01003122 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003123 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003124 /* we detected a parsing error. We want to archive this request
3125 * in the dedicated proxy area for later troubleshooting.
3126 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003127 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003128 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003129
Willy Tarreau59234e92008-11-30 23:51:27 +01003130 txn->req.msg_state = HTTP_MSG_ERROR;
3131 txn->status = 400;
3132 req->analysers = 0;
3133 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003134
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003135 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003136 if (s->listener->counters)
3137 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003138
Willy Tarreau59234e92008-11-30 23:51:27 +01003139 if (!(s->flags & SN_ERR_MASK))
3140 s->flags |= SN_ERR_PRXCOND;
3141 if (!(s->flags & SN_FINST_MASK))
3142 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003143 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003144}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003145
Willy Tarreau60b85b02008-11-30 23:28:40 +01003146/* This function is an analyser which processes the HTTP tarpit. It always
3147 * returns zero, at the beginning because it prevents any other processing
3148 * from occurring, and at the end because it terminates the request.
3149 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003150int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003151{
3152 struct http_txn *txn = &s->txn;
3153
3154 /* This connection is being tarpitted. The CLIENT side has
3155 * already set the connect expiration date to the right
3156 * timeout. We just have to check that the client is still
3157 * there and that the timeout has not expired.
3158 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003159 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003160 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3161 !tick_is_expired(req->analyse_exp, now_ms))
3162 return 0;
3163
3164 /* We will set the queue timer to the time spent, just for
3165 * logging purposes. We fake a 500 server error, so that the
3166 * attacker will not suspect his connection has been tarpitted.
3167 * It will not cause trouble to the logs because we can exclude
3168 * the tarpitted connections by filtering on the 'PT' status flags.
3169 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003170 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3171
3172 txn->status = 500;
3173 if (req->flags != BF_READ_ERROR)
3174 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3175
3176 req->analysers = 0;
3177 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003178
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003179 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003180 if (s->listener->counters)
3181 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003182
Willy Tarreau60b85b02008-11-30 23:28:40 +01003183 if (!(s->flags & SN_ERR_MASK))
3184 s->flags |= SN_ERR_PRXCOND;
3185 if (!(s->flags & SN_FINST_MASK))
3186 s->flags |= SN_FINST_T;
3187 return 0;
3188}
3189
Willy Tarreaud34af782008-11-30 23:36:37 +01003190/* This function is an analyser which processes the HTTP request body. It looks
3191 * for parameters to be used for the load balancing algorithm (url_param). It
3192 * must only be called after the standard HTTP request processing has occurred,
3193 * because it expects the request to be parsed. It returns zero if it needs to
3194 * read more data, or 1 once it has completed its analysis.
3195 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003196int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003197{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003198 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003199 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003200 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003201
3202 /* We have to parse the HTTP request body to find any required data.
3203 * "balance url_param check_post" should have been the only way to get
3204 * into this. We were brought here after HTTP header analysis, so all
3205 * related structures are ready.
3206 */
3207
Willy Tarreau522d6c02009-12-06 18:49:18 +01003208 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3209 goto missing_data;
3210
3211 if (msg->msg_state < HTTP_MSG_100_SENT) {
3212 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3213 * send an HTTP/1.1 100 Continue intermediate response.
3214 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003215 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003216 struct hdr_ctx ctx;
3217 ctx.idx = 0;
3218 /* Expect is allowed in 1.1, look for it */
3219 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3220 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3221 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3222 }
3223 }
3224 msg->msg_state = HTTP_MSG_100_SENT;
3225 }
3226
3227 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003228 /* we have msg->col and msg->sov which both point to the first
3229 * byte of message body. msg->som still points to the beginning
3230 * of the message. We must save the body in req->lr because it
3231 * survives buffer re-alignments.
3232 */
3233 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003234 if (txn->flags & TX_REQ_TE_CHNK)
3235 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3236 else
3237 msg->msg_state = HTTP_MSG_DATA;
3238 }
3239
3240 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003241 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003242 * set ->sov and ->lr to point to the body and switch to DATA or
3243 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003244 */
3245 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003246
Willy Tarreau115acb92009-12-26 13:56:06 +01003247 if (!ret)
3248 goto missing_data;
3249 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003250 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003251 }
3252
Willy Tarreaud98cf932009-12-27 22:54:55 +01003253 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003254 * We have the first non-header byte in msg->col, which is either the
3255 * beginning of the chunk size or of the data. The first data byte is in
3256 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3257 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003258 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003259
3260 if (msg->hdr_content_len < limit)
3261 limit = msg->hdr_content_len;
3262
Willy Tarreau7c96f672009-12-27 22:47:25 +01003263 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003264 goto http_end;
3265
3266 missing_data:
3267 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003268 if (req->flags & BF_FULL)
3269 goto return_bad_req;
3270
Willy Tarreau522d6c02009-12-06 18:49:18 +01003271 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3272 txn->status = 408;
3273 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3274 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003275 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003276
3277 /* we get here if we need to wait for more data */
3278 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003279 /* Not enough data. We'll re-use the http-request
3280 * timeout here. Ideally, we should set the timeout
3281 * relative to the accept() date. We just set the
3282 * request timeout once at the beginning of the
3283 * request.
3284 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003285 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003286 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003287 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003288 return 0;
3289 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003290
3291 http_end:
3292 /* The situation will not evolve, so let's give up on the analysis. */
3293 s->logs.tv_request = now; /* update the request timer to reflect full request */
3294 req->analysers &= ~an_bit;
3295 req->analyse_exp = TICK_ETERNITY;
3296 return 1;
3297
3298 return_bad_req: /* let's centralize all bad requests */
3299 txn->req.msg_state = HTTP_MSG_ERROR;
3300 txn->status = 400;
3301 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3302
3303 return_err_msg:
3304 req->analysers = 0;
3305 s->fe->counters.failed_req++;
3306 if (s->listener->counters)
3307 s->listener->counters->failed_req++;
3308
3309 if (!(s->flags & SN_ERR_MASK))
3310 s->flags |= SN_ERR_PRXCOND;
3311 if (!(s->flags & SN_FINST_MASK))
3312 s->flags |= SN_FINST_R;
3313 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003314}
3315
Willy Tarreau610ecce2010-01-04 21:15:02 +01003316/* Terminate current transaction and prepare a new one. This is very tricky
3317 * right now but it works.
3318 */
3319void http_end_txn_clean_session(struct session *s)
3320{
3321 /* FIXME: We need a more portable way of releasing a backend's and a
3322 * server's connections. We need a safer way to reinitialize buffer
3323 * flags. We also need a more accurate method for computing per-request
3324 * data.
3325 */
3326 http_silent_debug(__LINE__, s);
3327
3328 s->req->cons->flags |= SI_FL_NOLINGER;
3329 s->req->cons->shutr(s->req->cons);
3330 s->req->cons->shutw(s->req->cons);
3331
3332 http_silent_debug(__LINE__, s);
3333
3334 if (s->flags & SN_BE_ASSIGNED)
3335 s->be->beconn--;
3336
3337 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3338 session_process_counters(s);
3339
3340 if (s->txn.status) {
3341 int n;
3342
3343 n = s->txn.status / 100;
3344 if (n < 1 || n > 5)
3345 n = 0;
3346
3347 if (s->fe->mode == PR_MODE_HTTP)
3348 s->fe->counters.p.http.rsp[n]++;
3349
3350 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3351 (s->be->mode == PR_MODE_HTTP))
3352 s->be->counters.p.http.rsp[n]++;
3353 }
3354
3355 /* don't count other requests' data */
3356 s->logs.bytes_in -= s->req->l - s->req->send_max;
3357 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3358
3359 /* let's do a final log if we need it */
3360 if (s->logs.logwait &&
3361 !(s->flags & SN_MONITOR) &&
3362 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3363 s->do_log(s);
3364 }
3365
3366 s->logs.accept_date = date; /* user-visible date for logging */
3367 s->logs.tv_accept = now; /* corrected date for internal use */
3368 tv_zero(&s->logs.tv_request);
3369 s->logs.t_queue = -1;
3370 s->logs.t_connect = -1;
3371 s->logs.t_data = -1;
3372 s->logs.t_close = 0;
3373 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3374 s->logs.srv_queue_size = 0; /* we will get this number soon */
3375
3376 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3377 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3378
3379 if (s->pend_pos)
3380 pendconn_free(s->pend_pos);
3381
3382 if (s->srv) {
3383 if (s->flags & SN_CURR_SESS) {
3384 s->flags &= ~SN_CURR_SESS;
3385 s->srv->cur_sess--;
3386 }
3387 if (may_dequeue_tasks(s->srv, s->be))
3388 process_srv_queue(s->srv);
3389 }
3390
3391 if (unlikely(s->srv_conn))
3392 sess_change_server(s, NULL);
3393 s->srv = NULL;
3394
3395 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3396 s->req->cons->fd = -1; /* just to help with debugging */
3397 s->req->cons->err_type = SI_ET_NONE;
3398 s->req->cons->err_loc = NULL;
3399 s->req->cons->exp = TICK_ETERNITY;
3400 s->req->cons->flags = SI_FL_NONE;
3401 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE);
3402 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE|BF_WRITE_PARTIAL);
3403 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3404 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3405 s->txn.meth = 0;
3406 http_reset_txn(s);
3407 s->txn.flags |= TX_NOT_FIRST;
3408 if (s->be->options2 & PR_O2_INDEPSTR)
3409 s->req->cons->flags |= SI_FL_INDEP_STR;
3410
3411 /* if the request buffer is not empty, it means we're
3412 * about to process another request, so send pending
3413 * data with MSG_MORE to merge TCP packets when possible.
3414 * Also, let's not start reading a small request packet,
3415 * we may prefer to read a larger one later.
3416 */
3417 s->req->flags &= ~BF_DONT_READ;
3418 if (s->req->l > s->req->send_max) {
3419 s->rep->flags |= BF_EXPECT_MORE;
3420 s->req->flags |= BF_DONT_READ;
3421 }
3422
3423 /* make ->lr point to the first non-forwarded byte */
3424 s->req->lr = s->req->w + s->req->send_max;
3425 if (s->req->lr >= s->req->data + s->req->size)
3426 s->req->lr -= s->req->size;
3427 s->rep->lr = s->rep->w + s->rep->send_max;
3428 if (s->rep->lr >= s->rep->data + s->rep->size)
3429 s->rep->lr -= s->req->size;
3430
3431 s->req->analysers |= s->fe->fe_req_ana;
3432 s->rep->analysers = 0;
3433
3434 http_silent_debug(__LINE__, s);
3435}
3436
3437
3438/* This function updates the request state machine according to the response
3439 * state machine and buffer flags. It returns 1 if it changes anything (flag
3440 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3441 * it is only used to find when a request/response couple is complete. Both
3442 * this function and its equivalent should loop until both return zero. It
3443 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3444 */
3445int http_sync_req_state(struct session *s)
3446{
3447 struct buffer *buf = s->req;
3448 struct http_txn *txn = &s->txn;
3449 unsigned int old_flags = buf->flags;
3450 unsigned int old_state = txn->req.msg_state;
3451
3452 http_silent_debug(__LINE__, s);
3453 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3454 return 0;
3455
3456 if (txn->req.msg_state == HTTP_MSG_DONE) {
3457 /* No need to read anymore, the request was completely parsed */
3458 buf->flags |= BF_DONT_READ;
3459
3460 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3461 goto wait_other_side;
3462
3463 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3464 /* The server has not finished to respond, so we
3465 * don't want to move in order not to upset it.
3466 */
3467 goto wait_other_side;
3468 }
3469
3470 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3471 /* if any side switches to tunnel mode, the other one does too */
3472 buf->flags &= ~BF_DONT_READ;
3473 txn->req.msg_state = HTTP_MSG_TUNNEL;
3474 goto wait_other_side;
3475 }
3476
3477 /* When we get here, it means that both the request and the
3478 * response have finished receiving. Depending on the connection
3479 * mode, we'll have to wait for the last bytes to leave in either
3480 * direction, and sometimes for a close to be effective.
3481 */
3482
3483 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3484 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3485 /* Server-close mode : queue a connection close to the server */
3486 buffer_shutw_now(buf);
3487 buf->cons->flags |= SI_FL_NOLINGER;
3488 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003489 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003490 /* Option forceclose is set, let's enforce it now
3491 * that we're not expecting any new data to come.
3492 */
3493 buffer_shutr_now(buf);
3494 buffer_shutw_now(buf);
3495 buf->cons->flags |= SI_FL_NOLINGER;
3496 }
3497 /* other modes include httpclose (no action) and keepalive (not implemented) */
3498 }
3499
3500 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3501 /* if we've just closed an output, let's switch */
3502 if (!(buf->flags & BF_OUT_EMPTY)) {
3503 txn->req.msg_state = HTTP_MSG_CLOSING;
3504 goto http_msg_closing;
3505 }
3506 else {
3507 txn->req.msg_state = HTTP_MSG_CLOSED;
3508 goto http_msg_closed;
3509 }
3510 }
3511 else {
3512 /* other modes are used as a tunnel right now */
3513 buf->flags &= ~BF_DONT_READ;
3514 txn->req.msg_state = HTTP_MSG_TUNNEL;
3515 goto wait_other_side;
3516 }
3517 }
3518
3519 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3520 http_msg_closing:
3521 /* nothing else to forward, just waiting for the output buffer
3522 * to be empty and for the shutw_now to take effect.
3523 */
3524 if (buf->flags & BF_OUT_EMPTY) {
3525 txn->req.msg_state = HTTP_MSG_CLOSED;
3526 goto http_msg_closed;
3527 }
3528 else if (buf->flags & BF_SHUTW) {
3529 txn->req.msg_state = HTTP_MSG_ERROR;
3530 goto wait_other_side;
3531 }
3532 }
3533
3534 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3535 http_msg_closed:
3536 goto wait_other_side;
3537 }
3538
3539 wait_other_side:
3540 http_silent_debug(__LINE__, s);
3541 return txn->req.msg_state != old_state || buf->flags != old_flags;
3542}
3543
3544
3545/* This function updates the response state machine according to the request
3546 * state machine and buffer flags. It returns 1 if it changes anything (flag
3547 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3548 * it is only used to find when a request/response couple is complete. Both
3549 * this function and its equivalent should loop until both return zero. It
3550 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3551 */
3552int http_sync_res_state(struct session *s)
3553{
3554 struct buffer *buf = s->rep;
3555 struct http_txn *txn = &s->txn;
3556 unsigned int old_flags = buf->flags;
3557 unsigned int old_state = txn->rsp.msg_state;
3558
3559 http_silent_debug(__LINE__, s);
3560 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3561 return 0;
3562
3563 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3564 /* In theory, we don't need to read anymore, but we must
3565 * still monitor the server connection for a possible close,
3566 * so we don't set the BF_DONT_READ flag here.
3567 */
3568 /* buf->flags |= BF_DONT_READ; */
3569
3570 if (txn->req.msg_state == HTTP_MSG_ERROR)
3571 goto wait_other_side;
3572
3573 if (txn->req.msg_state < HTTP_MSG_DONE) {
3574 /* The client seems to still be sending data, probably
3575 * because we got an error response during an upload.
3576 * We have the choice of either breaking the connection
3577 * or letting it pass through. Let's do the later.
3578 */
3579 goto wait_other_side;
3580 }
3581
3582 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3583 /* if any side switches to tunnel mode, the other one does too */
3584 buf->flags &= ~BF_DONT_READ;
3585 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3586 goto wait_other_side;
3587 }
3588
3589 /* When we get here, it means that both the request and the
3590 * response have finished receiving. Depending on the connection
3591 * mode, we'll have to wait for the last bytes to leave in either
3592 * direction, and sometimes for a close to be effective.
3593 */
3594
3595 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3596 /* Server-close mode : shut read and wait for the request
3597 * side to close its output buffer. The caller will detect
3598 * when we're in DONE and the other is in CLOSED and will
3599 * catch that for the final cleanup.
3600 */
3601 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3602 buffer_shutr_now(buf);
3603 goto wait_other_side;
3604 }
3605 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3606 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3607 /* Option forceclose is set, let's enforce it now
3608 * that we're not expecting any new data to come.
3609 * The caller knows the session is complete once
3610 * both states are CLOSED.
3611 */
3612 buffer_shutr_now(buf);
3613 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003614 }
3615 else {
3616 /* other modes include httpclose (no action) and keepalive
3617 * (not implemented). These modes are used as a tunnel right
3618 * now.
3619 */
3620 buf->flags &= ~BF_DONT_READ;
3621 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3622 goto wait_other_side;
3623 }
3624
3625 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3626 /* if we've just closed an output, let's switch */
3627 if (!(buf->flags & BF_OUT_EMPTY)) {
3628 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3629 goto http_msg_closing;
3630 }
3631 else {
3632 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3633 goto http_msg_closed;
3634 }
3635 }
3636 goto wait_other_side;
3637 }
3638
3639 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3640 http_msg_closing:
3641 /* nothing else to forward, just waiting for the output buffer
3642 * to be empty and for the shutw_now to take effect.
3643 */
3644 if (buf->flags & BF_OUT_EMPTY) {
3645 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3646 goto http_msg_closed;
3647 }
3648 else if (buf->flags & BF_SHUTW) {
3649 txn->rsp.msg_state = HTTP_MSG_ERROR;
3650 goto wait_other_side;
3651 }
3652 }
3653
3654 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3655 http_msg_closed:
3656 /* drop any pending data */
3657 buffer_ignore(buf, buf->l - buf->send_max);
3658 buffer_auto_close(buf);
3659 goto wait_other_side;
3660 }
3661
3662 wait_other_side:
3663 http_silent_debug(__LINE__, s);
3664 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3665}
3666
3667
3668/* Resync the request and response state machines. Return 1 if either state
3669 * changes.
3670 */
3671int http_resync_states(struct session *s)
3672{
3673 struct http_txn *txn = &s->txn;
3674 int old_req_state = txn->req.msg_state;
3675 int old_res_state = txn->rsp.msg_state;
3676
3677 http_silent_debug(__LINE__, s);
3678 http_sync_req_state(s);
3679 while (1) {
3680 http_silent_debug(__LINE__, s);
3681 if (!http_sync_res_state(s))
3682 break;
3683 http_silent_debug(__LINE__, s);
3684 if (!http_sync_req_state(s))
3685 break;
3686 }
3687 http_silent_debug(__LINE__, s);
3688 /* OK, both state machines agree on a compatible state.
3689 * There are a few cases we're interested in :
3690 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3691 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3692 * directions, so let's simply disable both analysers.
3693 * - HTTP_MSG_CLOSED on the response only means we must abort the
3694 * request.
3695 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3696 * with server-close mode means we've completed one request and we
3697 * must re-initialize the server connection.
3698 */
3699
3700 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3701 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3702 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3703 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3704 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003705 s->req->flags &= ~BF_DONT_READ;
3706 buffer_auto_close(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003707 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003708 buffer_auto_close(s->rep);
3709 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003710 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003711 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3712 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3713 (s->rep->flags & BF_SHUTW)) {
3714 s->rep->flags &= ~BF_DONT_READ;
3715 s->req->flags &= ~BF_DONT_READ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003716 buffer_abort(s->req);
3717 buffer_auto_close(s->req);
3718 buffer_ignore(s->req, s->req->l - s->req->send_max);
3719 s->req->analysers = 0;
3720 s->rep->analysers = 0;
3721 }
3722 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3723 txn->rsp.msg_state == HTTP_MSG_DONE &&
3724 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3725 /* server-close: terminate this server connection and
3726 * reinitialize a fresh-new transaction.
3727 */
3728 http_end_txn_clean_session(s);
3729 }
3730
3731 http_silent_debug(__LINE__, s);
3732 return txn->req.msg_state != old_req_state ||
3733 txn->rsp.msg_state != old_res_state;
3734}
3735
Willy Tarreaud98cf932009-12-27 22:54:55 +01003736/* This function is an analyser which forwards request body (including chunk
3737 * sizes if any). It is called as soon as we must forward, even if we forward
3738 * zero byte. The only situation where it must not be called is when we're in
3739 * tunnel mode and we want to forward till the close. It's used both to forward
3740 * remaining data and to resync after end of body. It expects the msg_state to
3741 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3742 * read more data, or 1 once we can go on with next request or end the session.
3743 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3744 * bytes of pending data + the headers if not already done (between som and sov).
3745 * It eventually adjusts som to match sov after the data in between have been sent.
3746 */
3747int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3748{
3749 struct http_txn *txn = &s->txn;
3750 struct http_msg *msg = &s->txn.req;
3751
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003752 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3753 return 0;
3754
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003755 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3756 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3757 /* Output closed while we were sending data. We must abort. */
3758 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003759 req->analysers &= ~an_bit;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003760 req->flags &= ~BF_DONT_READ;
Willy Tarreau082b01c2010-01-02 23:58:04 +01003761 return 1;
3762 }
3763
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003764 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003765
3766 /* Note that we don't have to send 100-continue back because we don't
3767 * need the data to complete our job, and it's up to the server to
3768 * decide whether to return 100, 417 or anything else in return of
3769 * an "Expect: 100-continue" header.
3770 */
3771
3772 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3773 /* we have msg->col and msg->sov which both point to the first
3774 * byte of message body. msg->som still points to the beginning
3775 * of the message. We must save the body in req->lr because it
3776 * survives buffer re-alignments.
3777 */
3778 req->lr = req->data + msg->sov;
3779 if (txn->flags & TX_REQ_TE_CHNK)
3780 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3781 else {
3782 msg->msg_state = HTTP_MSG_DATA;
3783 }
3784 }
3785
Willy Tarreaud98cf932009-12-27 22:54:55 +01003786 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003788 /* we may have some data pending */
3789 if (msg->hdr_content_len || msg->som != msg->sov) {
3790 int bytes = msg->sov - msg->som;
3791 if (bytes < 0) /* sov may have wrapped at the end */
3792 bytes += req->size;
3793 buffer_forward(req, bytes + msg->hdr_content_len);
3794 msg->hdr_content_len = 0; /* don't forward that again */
3795 msg->som = msg->sov;
3796 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003797
Willy Tarreaucaabe412010-01-03 23:08:28 +01003798 if (msg->msg_state == HTTP_MSG_DATA) {
3799 /* must still forward */
3800 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003801 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003802
3803 /* nothing left to forward */
3804 if (txn->flags & TX_REQ_TE_CHNK)
3805 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003806 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003807 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003808 }
3809 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003810 /* read the chunk size and assign it to ->hdr_content_len, then
3811 * set ->sov and ->lr to point to the body and switch to DATA or
3812 * TRAILERS state.
3813 */
3814 int ret = http_parse_chunk_size(req, msg);
3815
3816 if (!ret)
3817 goto missing_data;
3818 else if (ret < 0)
3819 goto return_bad_req;
3820 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003821 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003822 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3823 /* we want the CRLF after the data */
3824 int ret;
3825
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003826 req->lr = req->w + req->send_max;
3827 if (req->lr >= req->data + req->size)
3828 req->lr -= req->size;
3829
Willy Tarreaud98cf932009-12-27 22:54:55 +01003830 ret = http_skip_chunk_crlf(req, msg);
3831
3832 if (ret == 0)
3833 goto missing_data;
3834 else if (ret < 0)
3835 goto return_bad_req;
3836 /* we're in MSG_CHUNK_SIZE now */
3837 }
3838 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3839 int ret = http_forward_trailers(req, msg);
3840
3841 if (ret == 0)
3842 goto missing_data;
3843 else if (ret < 0)
3844 goto return_bad_req;
3845 /* we're in HTTP_MSG_DONE now */
3846 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003847 else {
3848 /* other states, DONE...TUNNEL */
3849 if (http_resync_states(s)) {
3850 /* some state changes occurred, maybe the analyser
3851 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003852 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3854 goto return_bad_req;
3855 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003856 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003857 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003858 }
3859 }
3860
Willy Tarreaud98cf932009-12-27 22:54:55 +01003861 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003862 /* stop waiting for data if the input is closed before the end */
3863 if (req->flags & BF_SHUTR)
3864 goto return_bad_req;
3865
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003866 /* waiting for the last bits to leave the buffer */
3867 if (req->flags & BF_SHUTW)
3868 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003869
3870 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003871 return 0;
3872
Willy Tarreaud98cf932009-12-27 22:54:55 +01003873 return_bad_req: /* let's centralize all bad requests */
3874 txn->req.msg_state = HTTP_MSG_ERROR;
3875 txn->status = 400;
3876 /* Note: we don't send any error if some data were already sent */
3877 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3878
3879 req->analysers = 0;
3880 s->fe->counters.failed_req++;
3881 if (s->listener->counters)
3882 s->listener->counters->failed_req++;
3883
3884 if (!(s->flags & SN_ERR_MASK))
3885 s->flags |= SN_ERR_PRXCOND;
3886 if (!(s->flags & SN_FINST_MASK))
3887 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003889 return 0;
3890}
3891
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003892/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3893 * processing can continue on next analysers, or zero if it either needs more
3894 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3895 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3896 * when it has nothing left to do, and may remove any analyser when it wants to
3897 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003898 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003899int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003900{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003901 struct http_txn *txn = &s->txn;
3902 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003903 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003904 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003905 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003906 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003907
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003908 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 +02003909 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003910 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003911 rep,
3912 rep->rex, rep->wex,
3913 rep->flags,
3914 rep->l,
3915 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003916
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003917 /*
3918 * Now parse the partial (or complete) lines.
3919 * We will check the response syntax, and also join multi-line
3920 * headers. An index of all the lines will be elaborated while
3921 * parsing.
3922 *
3923 * For the parsing, we use a 28 states FSM.
3924 *
3925 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003926 * rep->data + msg->som = beginning of response
3927 * rep->data + msg->eoh = end of processed headers / start of current one
3928 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003929 * rep->lr = first non-visited byte
3930 * rep->r = end of data
3931 */
3932
Willy Tarreau83e3af02009-12-28 17:39:57 +01003933 /* There's a protected area at the end of the buffer for rewriting
3934 * purposes. We don't want to start to parse the request if the
3935 * protected area is affected, because we may have to move processed
3936 * data later, which is much more complicated.
3937 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003938 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3939 if (unlikely((rep->flags & BF_FULL) ||
3940 rep->r < rep->lr ||
3941 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3942 if (rep->send_max) {
3943 /* some data has still not left the buffer, wake us once that's done */
3944 buffer_dont_close(rep);
3945 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3946 return 0;
3947 }
3948 if (rep->l <= rep->size - global.tune.maxrewrite)
3949 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003950 }
3951
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003952 if (likely(rep->lr < rep->r))
3953 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003954 }
3955
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003956 /* 1: we might have to print this header in debug mode */
3957 if (unlikely((global.mode & MODE_DEBUG) &&
3958 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003959 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003960 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003961
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003962 sol = rep->data + msg->som;
3963 eol = sol + msg->sl.rq.l;
3964 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003965
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003966 sol += hdr_idx_first_pos(&txn->hdr_idx);
3967 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003969 while (cur_idx) {
3970 eol = sol + txn->hdr_idx.v[cur_idx].len;
3971 debug_hdr("srvhdr", s, sol, eol);
3972 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3973 cur_idx = txn->hdr_idx.v[cur_idx].next;
3974 }
3975 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003976
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003977 /*
3978 * Now we quickly check if we have found a full valid response.
3979 * If not so, we check the FD and buffer states before leaving.
3980 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003981 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003982 * responses are checked first.
3983 *
3984 * Depending on whether the client is still there or not, we
3985 * may send an error response back or not. Note that normally
3986 * we should only check for HTTP status there, and check I/O
3987 * errors somewhere else.
3988 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003989
Willy Tarreau655dce92009-11-08 13:10:58 +01003990 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003991 /* Invalid response */
3992 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3993 /* we detected a parsing error. We want to archive this response
3994 * in the dedicated proxy area for later troubleshooting.
3995 */
3996 hdr_response_bad:
3997 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3998 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3999
4000 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004001 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004002 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004003 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4004 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004005
4006 rep->analysers = 0;
4007 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004008 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004009 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4010
4011 if (!(s->flags & SN_ERR_MASK))
4012 s->flags |= SN_ERR_PRXCOND;
4013 if (!(s->flags & SN_FINST_MASK))
4014 s->flags |= SN_FINST_H;
4015
4016 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004017 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004018
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004019 /* too large response does not fit in buffer. */
4020 else if (rep->flags & BF_FULL) {
4021 goto hdr_response_bad;
4022 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004023
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004024 /* read error */
4025 else if (rep->flags & BF_READ_ERROR) {
4026 if (msg->err_pos >= 0)
4027 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004028
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004029 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004030 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004031 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004032 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4033 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004034
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004035 rep->analysers = 0;
4036 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004037 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004038 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004039
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004040 if (!(s->flags & SN_ERR_MASK))
4041 s->flags |= SN_ERR_SRVCL;
4042 if (!(s->flags & SN_FINST_MASK))
4043 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004044 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004045 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004046
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004047 /* read timeout : return a 504 to the client. */
4048 else if (rep->flags & BF_READ_TIMEOUT) {
4049 if (msg->err_pos >= 0)
4050 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004051
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004052 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004053 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004054 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004055 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4056 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004057
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004058 rep->analysers = 0;
4059 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004060 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004061 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004062
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004063 if (!(s->flags & SN_ERR_MASK))
4064 s->flags |= SN_ERR_SRVTO;
4065 if (!(s->flags & SN_FINST_MASK))
4066 s->flags |= SN_FINST_H;
4067 return 0;
4068 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004069
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004070 /* close from server */
4071 else if (rep->flags & BF_SHUTR) {
4072 if (msg->err_pos >= 0)
4073 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004074
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004075 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004076 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004077 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004078 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4079 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004080
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004081 rep->analysers = 0;
4082 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004083 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004084 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004085
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004086 if (!(s->flags & SN_ERR_MASK))
4087 s->flags |= SN_ERR_SRVCL;
4088 if (!(s->flags & SN_FINST_MASK))
4089 s->flags |= SN_FINST_H;
4090 return 0;
4091 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004092
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004093 /* write error to client (we don't send any message then) */
4094 else if (rep->flags & BF_WRITE_ERROR) {
4095 if (msg->err_pos >= 0)
4096 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004097
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004098 s->be->counters.failed_resp++;
4099 rep->analysers = 0;
4100
4101 if (!(s->flags & SN_ERR_MASK))
4102 s->flags |= SN_ERR_CLICL;
4103 if (!(s->flags & SN_FINST_MASK))
4104 s->flags |= SN_FINST_H;
4105
4106 /* process_session() will take care of the error */
4107 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004108 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004109
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004110 buffer_dont_close(rep);
4111 return 0;
4112 }
4113
4114 /* More interesting part now : we know that we have a complete
4115 * response which at least looks like HTTP. We have an indicator
4116 * of each header's length, so we can parse them quickly.
4117 */
4118
4119 if (unlikely(msg->err_pos >= 0))
4120 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4121
4122 /* ensure we keep this pointer to the beginning of the message */
4123 msg->sol = rep->data + msg->som;
4124
4125 /*
4126 * 1: get the status code
4127 */
4128 n = rep->data[msg->sl.st.c] - '0';
4129 if (n < 1 || n > 5)
4130 n = 0;
4131 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004132
Willy Tarreau5b154472009-12-21 20:11:07 +01004133 /* check if the response is HTTP/1.1 or above */
4134 if ((msg->sl.st.v_l == 8) &&
4135 ((rep->data[msg->som + 5] > '1') ||
4136 ((rep->data[msg->som + 5] == '1') &&
4137 (rep->data[msg->som + 7] >= '1'))))
4138 txn->flags |= TX_RES_VER_11;
4139
4140 /* "connection" has not been parsed yet */
4141 txn->flags &= ~TX_CON_HDR_PARS;
4142
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004143 /* transfer length unknown*/
4144 txn->flags &= ~TX_RES_XFER_LEN;
4145
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004146 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
4147
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004148 if (txn->status >= 100 && txn->status < 500)
4149 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4150 else
4151 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004153 /*
4154 * 2: check for cacheability.
4155 */
4156
4157 switch (txn->status) {
4158 case 200:
4159 case 203:
4160 case 206:
4161 case 300:
4162 case 301:
4163 case 410:
4164 /* RFC2616 @13.4:
4165 * "A response received with a status code of
4166 * 200, 203, 206, 300, 301 or 410 MAY be stored
4167 * by a cache (...) unless a cache-control
4168 * directive prohibits caching."
4169 *
4170 * RFC2616 @9.5: POST method :
4171 * "Responses to this method are not cacheable,
4172 * unless the response includes appropriate
4173 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004174 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004175 if (likely(txn->meth != HTTP_METH_POST) &&
4176 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4177 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4178 break;
4179 default:
4180 break;
4181 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004182
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004183 /*
4184 * 3: we may need to capture headers
4185 */
4186 s->logs.logwait &= ~LW_RESP;
4187 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
4188 capture_headers(rep->data + msg->som, &txn->hdr_idx,
4189 txn->rsp.cap, s->fe->rsp_cap);
4190
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004191 /* 4: determine the transfer-length.
4192 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4193 * the presence of a message-body in a RESPONSE and its transfer length
4194 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004195 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004196 * All responses to the HEAD request method MUST NOT include a
4197 * message-body, even though the presence of entity-header fields
4198 * might lead one to believe they do. All 1xx (informational), 204
4199 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4200 * message-body. All other responses do include a message-body,
4201 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004202 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004203 * 1. Any response which "MUST NOT" include a message-body (such as the
4204 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4205 * always terminated by the first empty line after the header fields,
4206 * regardless of the entity-header fields present in the message.
4207 *
4208 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4209 * the "chunked" transfer-coding (Section 6.2) is used, the
4210 * transfer-length is defined by the use of this transfer-coding.
4211 * If a Transfer-Encoding header field is present and the "chunked"
4212 * transfer-coding is not present, the transfer-length is defined by
4213 * the sender closing the connection.
4214 *
4215 * 3. If a Content-Length header field is present, its decimal value in
4216 * OCTETs represents both the entity-length and the transfer-length.
4217 * If a message is received with both a Transfer-Encoding header
4218 * field and a Content-Length header field, the latter MUST be ignored.
4219 *
4220 * 4. If the message uses the media type "multipart/byteranges", and
4221 * the transfer-length is not otherwise specified, then this self-
4222 * delimiting media type defines the transfer-length. This media
4223 * type MUST NOT be used unless the sender knows that the recipient
4224 * can parse it; the presence in a request of a Range header with
4225 * multiple byte-range specifiers from a 1.1 client implies that the
4226 * client can parse multipart/byteranges responses.
4227 *
4228 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004229 */
4230
4231 /* Skip parsing if no content length is possible. The response flags
4232 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004233 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004234 * FIXME: should we parse anyway and return an error on chunked encoding ?
4235 */
4236 if (txn->meth == HTTP_METH_HEAD ||
4237 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004238 txn->status == 204 || txn->status == 304) {
4239 txn->flags |= TX_RES_XFER_LEN;
4240 goto skip_content_length;
4241 }
4242
4243 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004244 goto skip_content_length;
4245
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004246 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004247 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004248 while ((txn->flags & TX_RES_VER_11) &&
4249 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004250 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4251 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4252 else if (txn->flags & TX_RES_TE_CHNK) {
4253 /* bad transfer-encoding (chunked followed by something else) */
4254 use_close_only = 1;
4255 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4256 break;
4257 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004258 }
4259
4260 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4261 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004262 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004263 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4264 signed long long cl;
4265
4266 if (!ctx.vlen)
4267 goto hdr_response_bad;
4268
4269 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4270 goto hdr_response_bad; /* parse failure */
4271
4272 if (cl < 0)
4273 goto hdr_response_bad;
4274
4275 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4276 goto hdr_response_bad; /* already specified, was different */
4277
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004278 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004279 msg->hdr_content_len = cl;
4280 }
4281
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004282 /* FIXME: we should also implement the multipart/byterange method.
4283 * For now on, we resort to close mode in this case (unknown length).
4284 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004285skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004286
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004287 /* end of job, return OK */
4288 rep->analysers &= ~an_bit;
4289 rep->analyse_exp = TICK_ETERNITY;
4290 return 1;
4291}
4292
4293/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004294 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4295 * and updates t->rep->analysers. It might make sense to explode it into several
4296 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004297 */
4298int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4299{
4300 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004301 struct http_msg *msg = &txn->rsp;
4302 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004303 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004304 int conn_ka = 0, conn_cl = 0;
4305 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004306 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004307
4308 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4309 now_ms, __FUNCTION__,
4310 t,
4311 rep,
4312 rep->rex, rep->wex,
4313 rep->flags,
4314 rep->l,
4315 rep->analysers);
4316
Willy Tarreau655dce92009-11-08 13:10:58 +01004317 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004318 return 0;
4319
4320 rep->analysers &= ~an_bit;
4321 rep->analyse_exp = TICK_ETERNITY;
4322
Willy Tarreau5b154472009-12-21 20:11:07 +01004323 /* Now we have to check if we need to modify the Connection header.
4324 * This is more difficult on the response than it is on the request,
4325 * because we can have two different HTTP versions and we don't know
4326 * how the client will interprete a response. For instance, let's say
4327 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4328 * HTTP/1.1 response without any header. Maybe it will bound itself to
4329 * HTTP/1.0 because it only knows about it, and will consider the lack
4330 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4331 * the lack of header as a keep-alive. Thus we will use two flags
4332 * indicating how a request MAY be understood by the client. In case
4333 * of multiple possibilities, we'll fix the header to be explicit. If
4334 * ambiguous cases such as both close and keepalive are seen, then we
4335 * will fall back to explicit close. Note that we won't take risks with
4336 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4337 */
4338
4339 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004340 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4341 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4342 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004343 int may_keep = 0, may_close = 0; /* how it may be understood */
4344 struct hdr_ctx ctx;
4345
4346 ctx.idx = 0;
4347 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4348 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4349 conn_cl = 1;
4350 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4351 conn_ka = 1;
4352 }
4353
4354 if (conn_cl) {
4355 /* close header present */
4356 may_close = 1;
4357 if (conn_ka) /* we have both close and keep-alive */
4358 may_keep = 1;
4359 }
4360 else if (conn_ka) {
4361 /* keep-alive alone */
4362 may_keep = 1;
4363 }
4364 else {
4365 /* no close nor keep-alive header */
4366 if (txn->flags & TX_RES_VER_11)
4367 may_keep = 1;
4368 else
4369 may_close = 1;
4370
4371 if (txn->flags & TX_REQ_VER_11)
4372 may_keep = 1;
4373 else
4374 may_close = 1;
4375 }
4376
4377 /* let's update the transaction status to reflect any close.
4378 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004379 * handled. We also explicitly state that we will close in
4380 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004381 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004382 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004383 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4384 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004385 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4386
4387 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004388 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004389 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4390 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004391 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004392 if (may_keep &&
4393 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4394 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004395 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004396 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4397 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4398 must_del_close = 1;
4399 if (!(txn->flags & TX_REQ_VER_11))
4400 must_keep = 1;
4401 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004402
4403 txn->flags |= TX_CON_HDR_PARS;
4404 }
4405
4406 /* We might have to check for "Connection:" if the server
4407 * returns a connection status that is not compatible with
4408 * the client's or with the config.
4409 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004410 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004411 char *cur_ptr, *cur_end, *cur_next;
4412 int cur_idx, old_idx, delta, val;
4413 int must_delete;
4414 struct hdr_idx_elem *cur_hdr;
4415
4416 /* we just have to remove the headers if both sides are 1.0 */
4417 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004418
4419 /* same if we want to re-enable keep-alive on 1.1 */
4420 must_delete |= must_del_close;
4421
Willy Tarreau5b154472009-12-21 20:11:07 +01004422 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4423
4424 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4425 cur_hdr = &txn->hdr_idx.v[cur_idx];
4426 cur_ptr = cur_next;
4427 cur_end = cur_ptr + cur_hdr->len;
4428 cur_next = cur_end + cur_hdr->cr + 1;
4429
4430 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4431 if (!val)
4432 continue;
4433
4434 /* 3 possibilities :
4435 * - we have already set "Connection: close" or we're in
4436 * HTTP/1.0, so we remove this line.
4437 * - we have not yet set "Connection: close", but this line
4438 * indicates close. We leave it untouched and set the flag.
4439 * - we have not yet set "Connection: close", and this line
4440 * indicates non-close. We replace it and set the flag.
4441 */
4442 if (must_delete) {
4443 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4444 http_msg_move_end(&txn->rsp, delta);
4445 cur_next += delta;
4446 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4447 txn->hdr_idx.used--;
4448 cur_hdr->len = 0;
4449 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004450 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004451 } else {
4452 if (cur_end - cur_ptr - val != 5 ||
4453 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4454 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4455 "close", 5);
4456 cur_next += delta;
4457 cur_hdr->len += delta;
4458 http_msg_move_end(&txn->rsp, delta);
4459 }
4460 must_delete = 1;
4461 must_close = 0;
4462 }
4463 } /* for loop */
4464 } /* if must close keep-alive */
4465
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004466 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004467 /*
4468 * 3: we will have to evaluate the filters.
4469 * As opposed to version 1.2, now they will be evaluated in the
4470 * filters order and not in the header order. This means that
4471 * each filter has to be validated among all headers.
4472 *
4473 * Filters are tried with ->be first, then with ->fe if it is
4474 * different from ->be.
4475 */
4476
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004477 cur_proxy = t->be;
4478 while (1) {
4479 struct proxy *rule_set = cur_proxy;
4480
4481 /* try headers filters */
4482 if (rule_set->rsp_exp != NULL) {
4483 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4484 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004485 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004486 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004487 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4488 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004489 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004490 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004491 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004492 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004493 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004494 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004495 if (!(t->flags & SN_ERR_MASK))
4496 t->flags |= SN_ERR_PRXCOND;
4497 if (!(t->flags & SN_FINST_MASK))
4498 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004499 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004500 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004501 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004502
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004503 /* has the response been denied ? */
4504 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004505 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004506 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004507
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004508 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004509 if (t->listener->counters)
4510 t->listener->counters->denied_resp++;
4511
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004512 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004513 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004514
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004515 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004516 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004517 if (txn->status < 200)
4518 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004519 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004520 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004521 }
4522
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004523 /* check whether we're already working on the frontend */
4524 if (cur_proxy == t->fe)
4525 break;
4526 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004527 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004528
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004529 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004530 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4531 * in which case this is not the right response, and we're waiting for the
4532 * next one. Let's allow this response to go to the client and wait for the
4533 * next one.
4534 */
4535 if (txn->status < 200) {
4536 hdr_idx_init(&txn->hdr_idx);
4537 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4538 msg->msg_state = HTTP_MSG_RPBEFORE;
4539 txn->status = 0;
4540 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4541 return 1;
4542 }
4543
4544 /* we don't have any 1xx status code now */
4545
4546 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004547 * 4: check for server cookie.
4548 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004549 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4550 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004551 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004552
Willy Tarreaubaaee002006-06-26 02:48:02 +02004553
Willy Tarreaua15645d2007-03-18 16:22:39 +01004554 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004555 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004556 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004557 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004558 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004559
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004560 /*
4561 * 6: add server cookie in the response if needed
4562 */
4563 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004564 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004565 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004566
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004567 /* the server is known, it's not the one the client requested, we have to
4568 * insert a set-cookie here, except if we want to insert only on POST
4569 * requests and this one isn't. Note that servers which don't have cookies
4570 * (eg: some backup servers) will return a full cookie removal request.
4571 */
4572 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4573 t->be->cookie_name,
4574 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004575
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576 if (t->be->cookie_domain)
4577 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004578
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004579 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004580 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004581 goto return_bad_resp;
4582 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004583
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584 /* Here, we will tell an eventual cache on the client side that we don't
4585 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4586 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4587 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4588 */
4589 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004590
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004591 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4592
4593 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004594 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004595 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004596 }
4597 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004598
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004599 /*
4600 * 7: check if result will be cacheable with a cookie.
4601 * We'll block the response if security checks have caught
4602 * nasty things such as a cacheable cookie.
4603 */
4604 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4605 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004606 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004607
4608 /* we're in presence of a cacheable response containing
4609 * a set-cookie header. We'll block it as requested by
4610 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004611 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004612 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004613 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004614
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004615 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004616 if (t->listener->counters)
4617 t->listener->counters->denied_resp++;
4618
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004619 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4620 t->be->id, t->srv?t->srv->id:"<dispatch>");
4621 send_log(t->be, LOG_ALERT,
4622 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4623 t->be->id, t->srv?t->srv->id:"<dispatch>");
4624 goto return_srv_prx_502;
4625 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004626
4627 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004628 * 8: add "Connection: close" if needed and not yet set. This is
4629 * only needed for 1.1 responses since we know there is no other
4630 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004631 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004632 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004633 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004634 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004635 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004636 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004637 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004638 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4639 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4640 "Connection: keep-alive", 22) < 0))
4641 goto return_bad_resp;
4642 must_keep = 0;
4643 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004644
Willy Tarreaud98cf932009-12-27 22:54:55 +01004645 if (txn->flags & TX_RES_XFER_LEN)
4646 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004647
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004648 /*************************************************************
4649 * OK, that's finished for the headers. We have done what we *
4650 * could. Let's switch to the DATA state. *
4651 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004652
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004653 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004654
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004655 /* if the user wants to log as soon as possible, without counting
4656 * bytes from the server, then this is the right moment. We have
4657 * to temporarily assign bytes_out to log what we currently have.
4658 */
4659 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4660 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4661 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004662 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004663 t->logs.bytes_out = 0;
4664 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004665
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004666 /* Note: we must not try to cheat by jumping directly to DATA,
4667 * otherwise we would not let the client side wake up.
4668 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004669
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004670 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004671 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004672 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004673}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004674
Willy Tarreaud98cf932009-12-27 22:54:55 +01004675/* This function is an analyser which forwards response body (including chunk
4676 * sizes if any). It is called as soon as we must forward, even if we forward
4677 * zero byte. The only situation where it must not be called is when we're in
4678 * tunnel mode and we want to forward till the close. It's used both to forward
4679 * remaining data and to resync after end of body. It expects the msg_state to
4680 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4681 * read more data, or 1 once we can go on with next request or end the session.
4682 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4683 * bytes of pending data + the headers if not already done (between som and sov).
4684 * It eventually adjusts som to match sov after the data in between have been sent.
4685 */
4686int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4687{
4688 struct http_txn *txn = &s->txn;
4689 struct http_msg *msg = &s->txn.rsp;
4690
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004691 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4692 return 0;
4693
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004694 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004695 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004696 !s->req->analysers) {
4697 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4698 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004699 res->analysers &= ~an_bit;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004700 res->flags &= ~BF_DONT_READ;
Willy Tarreau082b01c2010-01-02 23:58:04 +01004701 return 1;
4702 }
4703
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004704 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004705
Willy Tarreaud98cf932009-12-27 22:54:55 +01004706 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4707 /* we have msg->col and msg->sov which both point to the first
4708 * byte of message body. msg->som still points to the beginning
4709 * of the message. We must save the body in req->lr because it
4710 * survives buffer re-alignments.
4711 */
4712 res->lr = res->data + msg->sov;
4713 if (txn->flags & TX_RES_TE_CHNK)
4714 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4715 else {
4716 msg->msg_state = HTTP_MSG_DATA;
4717 }
4718 }
4719
Willy Tarreaud98cf932009-12-27 22:54:55 +01004720 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004721 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004722 /* we may have some data pending */
4723 if (msg->hdr_content_len || msg->som != msg->sov) {
4724 int bytes = msg->sov - msg->som;
4725 if (bytes < 0) /* sov may have wrapped at the end */
4726 bytes += res->size;
4727 buffer_forward(res, bytes + msg->hdr_content_len);
4728 msg->hdr_content_len = 0; /* don't forward that again */
4729 msg->som = msg->sov;
4730 }
4731
Willy Tarreaucaabe412010-01-03 23:08:28 +01004732 if (msg->msg_state == HTTP_MSG_DATA) {
4733 /* must still forward */
4734 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004735 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004736
4737 /* nothing left to forward */
4738 if (txn->flags & TX_RES_TE_CHNK)
4739 msg->msg_state = HTTP_MSG_DATA_CRLF;
4740 else
4741 msg->msg_state = HTTP_MSG_DONE;
4742 }
4743 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004744 /* read the chunk size and assign it to ->hdr_content_len, then
4745 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4746 */
4747 int ret = http_parse_chunk_size(res, msg);
4748
4749 if (!ret)
4750 goto missing_data;
4751 else if (ret < 0)
4752 goto return_bad_res;
4753 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004754 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004755 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4756 /* we want the CRLF after the data */
4757 int ret;
4758
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004759 res->lr = res->w + res->send_max;
4760 if (res->lr >= res->data + res->size)
4761 res->lr -= res->size;
4762
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 ret = http_skip_chunk_crlf(res, msg);
4764
4765 if (!ret)
4766 goto missing_data;
4767 else if (ret < 0)
4768 goto return_bad_res;
4769 /* we're in MSG_CHUNK_SIZE now */
4770 }
4771 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4772 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004773
Willy Tarreaud98cf932009-12-27 22:54:55 +01004774 if (ret == 0)
4775 goto missing_data;
4776 else if (ret < 0)
4777 goto return_bad_res;
4778 /* we're in HTTP_MSG_DONE now */
4779 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004780 else {
4781 /* other states, DONE...TUNNEL */
4782 if (http_resync_states(s)) {
4783 http_silent_debug(__LINE__, s);
4784 /* some state changes occurred, maybe the analyser
4785 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004786 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4788 goto return_bad_res;
4789 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004790 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004791 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004792 }
4793 }
4794
Willy Tarreaud98cf932009-12-27 22:54:55 +01004795 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004796 /* stop waiting for data if the input is closed before the end */
4797 if (res->flags & BF_SHUTR)
4798 goto return_bad_res;
4799
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800 if (!s->req->analysers)
4801 goto return_bad_res;
4802
Willy Tarreaud98cf932009-12-27 22:54:55 +01004803 /* forward the chunk size as well as any pending data */
4804 if (msg->hdr_content_len || msg->som != msg->sov) {
4805 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4806 msg->hdr_content_len = 0; /* don't forward that again */
4807 msg->som = msg->sov;
4808 }
4809
Willy Tarreaud98cf932009-12-27 22:54:55 +01004810 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004811 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812 return 0;
4813
4814 return_bad_res: /* let's centralize all bad resuests */
4815 txn->rsp.msg_state = HTTP_MSG_ERROR;
4816 txn->status = 502;
4817 stream_int_cond_close(res->cons, NULL);
4818
4819 res->analysers = 0;
4820 s->be->counters.failed_resp++;
4821 if (s->srv) {
4822 s->srv->counters.failed_resp++;
4823 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4824 }
4825
4826 if (!(s->flags & SN_ERR_MASK))
4827 s->flags |= SN_ERR_PRXCOND;
4828 if (!(s->flags & SN_FINST_MASK))
4829 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004830 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004831 return 0;
4832}
4833
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004834/* Iterate the same filter through all request headers.
4835 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004836 * Since it can manage the switch to another backend, it updates the per-proxy
4837 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004838 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004839int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004840{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004841 char term;
4842 char *cur_ptr, *cur_end, *cur_next;
4843 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004844 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004845 struct hdr_idx_elem *cur_hdr;
4846 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004847
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004848 last_hdr = 0;
4849
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004850 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004851 old_idx = 0;
4852
4853 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004854 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004855 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004856 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004857 (exp->action == ACT_ALLOW ||
4858 exp->action == ACT_DENY ||
4859 exp->action == ACT_TARPIT))
4860 return 0;
4861
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004862 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004863 if (!cur_idx)
4864 break;
4865
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004866 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004867 cur_ptr = cur_next;
4868 cur_end = cur_ptr + cur_hdr->len;
4869 cur_next = cur_end + cur_hdr->cr + 1;
4870
4871 /* Now we have one header between cur_ptr and cur_end,
4872 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004873 */
4874
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004875 /* The annoying part is that pattern matching needs
4876 * that we modify the contents to null-terminate all
4877 * strings before testing them.
4878 */
4879
4880 term = *cur_end;
4881 *cur_end = '\0';
4882
4883 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4884 switch (exp->action) {
4885 case ACT_SETBE:
4886 /* It is not possible to jump a second time.
4887 * FIXME: should we return an HTTP/500 here so that
4888 * the admin knows there's a problem ?
4889 */
4890 if (t->be != t->fe)
4891 break;
4892
4893 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004894 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 last_hdr = 1;
4896 break;
4897
4898 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004899 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004900 last_hdr = 1;
4901 break;
4902
4903 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004904 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004905 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004906
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004907 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004908 if (t->listener->counters)
4909 t->listener->counters->denied_resp++;
4910
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004911 break;
4912
4913 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004914 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004915 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004916
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004917 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004918 if (t->listener->counters)
4919 t->listener->counters->denied_resp++;
4920
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004921 break;
4922
4923 case ACT_REPLACE:
4924 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4925 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4926 /* FIXME: if the user adds a newline in the replacement, the
4927 * index will not be recalculated for now, and the new line
4928 * will not be counted as a new header.
4929 */
4930
4931 cur_end += delta;
4932 cur_next += delta;
4933 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004934 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004935 break;
4936
4937 case ACT_REMOVE:
4938 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4939 cur_next += delta;
4940
Willy Tarreaufa355d42009-11-29 18:12:29 +01004941 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004942 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4943 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004944 cur_hdr->len = 0;
4945 cur_end = NULL; /* null-term has been rewritten */
4946 break;
4947
4948 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004949 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004950 if (cur_end)
4951 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004952
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004953 /* keep the link from this header to next one in case of later
4954 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004955 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004956 old_idx = cur_idx;
4957 }
4958 return 0;
4959}
4960
4961
4962/* Apply the filter to the request line.
4963 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4964 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004965 * Since it can manage the switch to another backend, it updates the per-proxy
4966 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004967 */
4968int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4969{
4970 char term;
4971 char *cur_ptr, *cur_end;
4972 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004973 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004974 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004975
Willy Tarreau58f10d72006-12-04 02:26:12 +01004976
Willy Tarreau3d300592007-03-18 18:34:41 +01004977 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004978 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004979 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004980 (exp->action == ACT_ALLOW ||
4981 exp->action == ACT_DENY ||
4982 exp->action == ACT_TARPIT))
4983 return 0;
4984 else if (exp->action == ACT_REMOVE)
4985 return 0;
4986
4987 done = 0;
4988
Willy Tarreau9cdde232007-05-02 20:58:19 +02004989 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004990 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004991
4992 /* Now we have the request line between cur_ptr and cur_end */
4993
4994 /* The annoying part is that pattern matching needs
4995 * that we modify the contents to null-terminate all
4996 * strings before testing them.
4997 */
4998
4999 term = *cur_end;
5000 *cur_end = '\0';
5001
5002 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5003 switch (exp->action) {
5004 case ACT_SETBE:
5005 /* It is not possible to jump a second time.
5006 * FIXME: should we return an HTTP/500 here so that
5007 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005008 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005009 if (t->be != t->fe)
5010 break;
5011
5012 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005013 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 done = 1;
5015 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005017 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005018 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005019 done = 1;
5020 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005022 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005023 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005024
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005025 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005026 if (t->listener->counters)
5027 t->listener->counters->denied_resp++;
5028
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005029 done = 1;
5030 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005032 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005033 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005034
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005035 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005036 if (t->listener->counters)
5037 t->listener->counters->denied_resp++;
5038
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005039 done = 1;
5040 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005042 case ACT_REPLACE:
5043 *cur_end = term; /* restore the string terminator */
5044 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5045 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5046 /* FIXME: if the user adds a newline in the replacement, the
5047 * index will not be recalculated for now, and the new line
5048 * will not be counted as a new header.
5049 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005050
Willy Tarreaufa355d42009-11-29 18:12:29 +01005051 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005052 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005053
Willy Tarreau9cdde232007-05-02 20:58:19 +02005054 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005055 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005056 HTTP_MSG_RQMETH,
5057 cur_ptr, cur_end + 1,
5058 NULL, NULL);
5059 if (unlikely(!cur_end))
5060 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005062 /* we have a full request and we know that we have either a CR
5063 * or an LF at <ptr>.
5064 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005065 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5066 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005067 /* there is no point trying this regex on headers */
5068 return 1;
5069 }
5070 }
5071 *cur_end = term; /* restore the string terminator */
5072 return done;
5073}
Willy Tarreau97de6242006-12-27 17:18:38 +01005074
Willy Tarreau58f10d72006-12-04 02:26:12 +01005075
Willy Tarreau58f10d72006-12-04 02:26:12 +01005076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005077/*
5078 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5079 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005080 * unparsable request. Since it can manage the switch to another backend, it
5081 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005082 */
5083int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5084{
Willy Tarreau3d300592007-03-18 18:34:41 +01005085 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005086 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005087 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005088 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005089
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005090 /*
5091 * The interleaving of transformations and verdicts
5092 * makes it difficult to decide to continue or stop
5093 * the evaluation.
5094 */
5095
Willy Tarreau3d300592007-03-18 18:34:41 +01005096 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005097 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5098 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5099 exp = exp->next;
5100 continue;
5101 }
5102
5103 /* Apply the filter to the request line. */
5104 ret = apply_filter_to_req_line(t, req, exp);
5105 if (unlikely(ret < 0))
5106 return -1;
5107
5108 if (likely(ret == 0)) {
5109 /* The filter did not match the request, it can be
5110 * iterated through all headers.
5111 */
5112 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005113 }
5114 exp = exp->next;
5115 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005116 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005117}
5118
5119
Willy Tarreaua15645d2007-03-18 16:22:39 +01005120
Willy Tarreau58f10d72006-12-04 02:26:12 +01005121/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005122 * Try to retrieve the server associated to the appsession.
5123 * If the server is found, it's assigned to the session.
5124 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005125void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005126 struct http_txn *txn = &t->txn;
5127 appsess *asession = NULL;
5128 char *sessid_temp = NULL;
5129
Cyril Bontéb21570a2009-11-29 20:04:48 +01005130 if (len > t->be->appsession_len) {
5131 len = t->be->appsession_len;
5132 }
5133
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005134 if (t->be->options2 & PR_O2_AS_REQL) {
5135 /* request-learn option is enabled : store the sessid in the session for future use */
5136 if (t->sessid != NULL) {
5137 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5138 pool_free2(apools.sessid, t->sessid);
5139 }
5140
5141 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5142 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5143 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5144 return;
5145 }
5146
Cyril Bontéb21570a2009-11-29 20:04:48 +01005147 memcpy(t->sessid, buf, len);
5148 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005149 }
5150
5151 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5152 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5153 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5154 return;
5155 }
5156
Cyril Bontéb21570a2009-11-29 20:04:48 +01005157 memcpy(sessid_temp, buf, len);
5158 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005159
5160 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5161 /* free previously allocated memory */
5162 pool_free2(apools.sessid, sessid_temp);
5163
5164 if (asession != NULL) {
5165 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5166 if (!(t->be->options2 & PR_O2_AS_REQL))
5167 asession->request_count++;
5168
5169 if (asession->serverid != NULL) {
5170 struct server *srv = t->be->srv;
5171 while (srv) {
5172 if (strcmp(srv->id, asession->serverid) == 0) {
5173 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5174 /* we found the server and it's usable */
5175 txn->flags &= ~TX_CK_MASK;
5176 txn->flags |= TX_CK_VALID;
5177 t->flags |= SN_DIRECT | SN_ASSIGNED;
5178 t->srv = srv;
5179 break;
5180 } else {
5181 txn->flags &= ~TX_CK_MASK;
5182 txn->flags |= TX_CK_DOWN;
5183 }
5184 }
5185 srv = srv->next;
5186 }
5187 }
5188 }
5189}
5190
5191/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005192 * Manage client-side cookie. It can impact performance by about 2% so it is
5193 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005194 */
5195void manage_client_side_cookies(struct session *t, struct buffer *req)
5196{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005197 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005198 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005199 char *del_colon, *del_cookie, *colon;
5200 int app_cookies;
5201
Willy Tarreau58f10d72006-12-04 02:26:12 +01005202 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005203 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005204
Willy Tarreau2a324282006-12-05 00:05:46 +01005205 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005206 * we start with the start line.
5207 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005208 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005209 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005210
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005211 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005212 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005213 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005215 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005216 cur_ptr = cur_next;
5217 cur_end = cur_ptr + cur_hdr->len;
5218 cur_next = cur_end + cur_hdr->cr + 1;
5219
5220 /* We have one full header between cur_ptr and cur_end, and the
5221 * next header starts at cur_next. We're only interested in
5222 * "Cookie:" headers.
5223 */
5224
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005225 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5226 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005227 old_idx = cur_idx;
5228 continue;
5229 }
5230
5231 /* Now look for cookies. Conforming to RFC2109, we have to support
5232 * attributes whose name begin with a '$', and associate them with
5233 * the right cookie, if we want to delete this cookie.
5234 * So there are 3 cases for each cookie read :
5235 * 1) it's a special attribute, beginning with a '$' : ignore it.
5236 * 2) it's a server id cookie that we *MAY* want to delete : save
5237 * some pointers on it (last semi-colon, beginning of cookie...)
5238 * 3) it's an application cookie : we *MAY* have to delete a previous
5239 * "special" cookie.
5240 * At the end of loop, if a "special" cookie remains, we may have to
5241 * remove it. If no application cookie persists in the header, we
5242 * *MUST* delete it
5243 */
5244
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005245 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246
Willy Tarreau58f10d72006-12-04 02:26:12 +01005247 /* del_cookie == NULL => nothing to be deleted */
5248 del_colon = del_cookie = NULL;
5249 app_cookies = 0;
5250
5251 while (p1 < cur_end) {
5252 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005253 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005254 while (p1 < cur_end) {
5255 if (*p1 == ';' || *p1 == ',')
5256 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005257 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258 break;
5259 p1++;
5260 }
5261
5262 if (p1 == cur_end)
5263 break;
5264
5265 /* p1 is at the beginning of the cookie name */
5266 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005267 while (p2 < cur_end && *p2 != '=') {
5268 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5269 /* oops, the cookie name was truncated, resync */
5270 p1 = p2;
5271 goto resync_name;
5272 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005273 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005274 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005275
5276 if (p2 == cur_end)
5277 break;
5278
5279 p3 = p2 + 1; /* skips the '=' sign */
5280 if (p3 == cur_end)
5281 break;
5282
Willy Tarreau305ae852010-01-03 19:45:54 +01005283 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5284 p5 = p4 = p3;
5285 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5286 if (!isspace((unsigned char)*p5))
5287 p4 = p5 + 1;
5288 p5++;
5289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005290
5291 /* here, we have the cookie name between p1 and p2,
5292 * and its value between p3 and p4.
5293 * we can process it :
5294 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005295 * Cookie: NAME=VALUE ;
5296 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005297 * | || || +--> p4
5298 * | || |+-------> p3
5299 * | || +--------> p2
5300 * | |+------------> p1
5301 * | +-------------> colon
5302 * +--------------------> cur_ptr
5303 */
5304
5305 if (*p1 == '$') {
5306 /* skip this one */
5307 }
5308 else {
5309 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005310 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005311 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005312 (p4 - p1 >= t->fe->capture_namelen) &&
5313 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005314 int log_len = p4 - p1;
5315
Willy Tarreau086b3b42007-05-13 21:45:51 +02005316 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005317 Alert("HTTP logging : out of memory.\n");
5318 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005319 if (log_len > t->fe->capture_len)
5320 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005321 memcpy(txn->cli_cookie, p1, log_len);
5322 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005323 }
5324 }
5325
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005326 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5327 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005328 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005329 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005330 char *delim;
5331
5332 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5333 * have the server ID betweek p3 and delim, and the original cookie between
5334 * delim+1 and p4. Otherwise, delim==p4 :
5335 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005336 * Cookie: NAME=SRV~VALUE ;
5337 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005338 * | || || | +--> p4
5339 * | || || +--------> delim
5340 * | || |+-----------> p3
5341 * | || +------------> p2
5342 * | |+----------------> p1
5343 * | +-----------------> colon
5344 * +------------------------> cur_ptr
5345 */
5346
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005347 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005348 for (delim = p3; delim < p4; delim++)
5349 if (*delim == COOKIE_DELIM)
5350 break;
5351 }
5352 else
5353 delim = p4;
5354
5355
5356 /* Here, we'll look for the first running server which supports the cookie.
5357 * This allows to share a same cookie between several servers, for example
5358 * to dedicate backup servers to specific servers only.
5359 * However, to prevent clients from sticking to cookie-less backup server
5360 * when they have incidentely learned an empty cookie, we simply ignore
5361 * empty cookies and mark them as invalid.
5362 */
5363 if (delim == p3)
5364 srv = NULL;
5365
5366 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005367 if (srv->cookie && (srv->cklen == delim - p3) &&
5368 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005369 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005370 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005371 txn->flags &= ~TX_CK_MASK;
5372 txn->flags |= TX_CK_VALID;
5373 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005374 t->srv = srv;
5375 break;
5376 } else {
5377 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005378 txn->flags &= ~TX_CK_MASK;
5379 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005380 }
5381 }
5382 srv = srv->next;
5383 }
5384
Willy Tarreau3d300592007-03-18 18:34:41 +01005385 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005386 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005387 txn->flags &= ~TX_CK_MASK;
5388 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005389 }
5390
5391 /* depending on the cookie mode, we may have to either :
5392 * - delete the complete cookie if we're in insert+indirect mode, so that
5393 * the server never sees it ;
5394 * - remove the server id from the cookie value, and tag the cookie as an
5395 * application cookie so that it does not get accidentely removed later,
5396 * if we're in cookie prefix mode
5397 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005398 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005399 int delta; /* negative */
5400
5401 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5402 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005403 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005404 cur_end += delta;
5405 cur_next += delta;
5406 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005407 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005408
5409 del_cookie = del_colon = NULL;
5410 app_cookies++; /* protect the header from deletion */
5411 }
5412 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005413 (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 +01005414 del_cookie = p1;
5415 del_colon = colon;
5416 }
5417 } else {
5418 /* now we know that we must keep this cookie since it's
5419 * not ours. But if we wanted to delete our cookie
5420 * earlier, we cannot remove the complete header, but we
5421 * can remove the previous block itself.
5422 */
5423 app_cookies++;
5424
5425 if (del_cookie != NULL) {
5426 int delta; /* negative */
5427
5428 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5429 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005430 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005431 cur_end += delta;
5432 cur_next += delta;
5433 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005434 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005435 del_cookie = del_colon = NULL;
5436 }
5437 }
5438
Cyril Bontéb21570a2009-11-29 20:04:48 +01005439 if (t->be->appsession_name != NULL) {
5440 int cmp_len, value_len;
5441 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005442
Cyril Bontéb21570a2009-11-29 20:04:48 +01005443 if (t->be->options2 & PR_O2_AS_PFX) {
5444 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5445 value_begin = p1 + t->be->appsession_name_len;
5446 value_len = p4 - p1 - t->be->appsession_name_len;
5447 } else {
5448 cmp_len = p2 - p1;
5449 value_begin = p3;
5450 value_len = p4 - p3;
5451 }
5452
5453 /* let's see if the cookie is our appcookie */
5454 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5455 /* Cool... it's the right one */
5456 manage_client_side_appsession(t, value_begin, value_len);
5457 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005458#if defined(DEBUG_HASH)
5459 Alert("manage_client_side_cookies\n");
5460 appsession_hash_dump(&(t->be->htbl_proxy));
5461#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005462 }/* end if ((t->proxy->appsession_name != NULL) ... */
5463 }
5464
5465 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005466 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005467 } /* while (p1 < cur_end) */
5468
5469 /* There's no more cookie on this line.
5470 * We may have marked the last one(s) for deletion.
5471 * We must do this now in two ways :
5472 * - if there is no app cookie, we simply delete the header ;
5473 * - if there are app cookies, we must delete the end of the
5474 * string properly, including the colon/semi-colon before
5475 * the cookie name.
5476 */
5477 if (del_cookie != NULL) {
5478 int delta;
5479 if (app_cookies) {
5480 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5481 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005482 cur_hdr->len += delta;
5483 } else {
5484 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005485
5486 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005487 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5488 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005489 cur_hdr->len = 0;
5490 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005491 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005492 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005493 }
5494
5495 /* keep the link from this header to next one */
5496 old_idx = cur_idx;
5497 } /* end of cookie processing on this header */
5498}
5499
5500
Willy Tarreaua15645d2007-03-18 16:22:39 +01005501/* Iterate the same filter through all response headers contained in <rtr>.
5502 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5503 */
5504int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5505{
5506 char term;
5507 char *cur_ptr, *cur_end, *cur_next;
5508 int cur_idx, old_idx, last_hdr;
5509 struct http_txn *txn = &t->txn;
5510 struct hdr_idx_elem *cur_hdr;
5511 int len, delta;
5512
5513 last_hdr = 0;
5514
5515 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5516 old_idx = 0;
5517
5518 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005519 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005520 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005521 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005522 (exp->action == ACT_ALLOW ||
5523 exp->action == ACT_DENY))
5524 return 0;
5525
5526 cur_idx = txn->hdr_idx.v[old_idx].next;
5527 if (!cur_idx)
5528 break;
5529
5530 cur_hdr = &txn->hdr_idx.v[cur_idx];
5531 cur_ptr = cur_next;
5532 cur_end = cur_ptr + cur_hdr->len;
5533 cur_next = cur_end + cur_hdr->cr + 1;
5534
5535 /* Now we have one header between cur_ptr and cur_end,
5536 * and the next header starts at cur_next.
5537 */
5538
5539 /* The annoying part is that pattern matching needs
5540 * that we modify the contents to null-terminate all
5541 * strings before testing them.
5542 */
5543
5544 term = *cur_end;
5545 *cur_end = '\0';
5546
5547 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5548 switch (exp->action) {
5549 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005550 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005551 last_hdr = 1;
5552 break;
5553
5554 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005555 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005556 last_hdr = 1;
5557 break;
5558
5559 case ACT_REPLACE:
5560 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5561 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5562 /* FIXME: if the user adds a newline in the replacement, the
5563 * index will not be recalculated for now, and the new line
5564 * will not be counted as a new header.
5565 */
5566
5567 cur_end += delta;
5568 cur_next += delta;
5569 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005570 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005571 break;
5572
5573 case ACT_REMOVE:
5574 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5575 cur_next += delta;
5576
Willy Tarreaufa355d42009-11-29 18:12:29 +01005577 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005578 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5579 txn->hdr_idx.used--;
5580 cur_hdr->len = 0;
5581 cur_end = NULL; /* null-term has been rewritten */
5582 break;
5583
5584 }
5585 }
5586 if (cur_end)
5587 *cur_end = term; /* restore the string terminator */
5588
5589 /* keep the link from this header to next one in case of later
5590 * removal of next header.
5591 */
5592 old_idx = cur_idx;
5593 }
5594 return 0;
5595}
5596
5597
5598/* Apply the filter to the status line in the response buffer <rtr>.
5599 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5600 * or -1 if a replacement resulted in an invalid status line.
5601 */
5602int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5603{
5604 char term;
5605 char *cur_ptr, *cur_end;
5606 int done;
5607 struct http_txn *txn = &t->txn;
5608 int len, delta;
5609
5610
Willy Tarreau3d300592007-03-18 18:34:41 +01005611 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005612 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005613 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005614 (exp->action == ACT_ALLOW ||
5615 exp->action == ACT_DENY))
5616 return 0;
5617 else if (exp->action == ACT_REMOVE)
5618 return 0;
5619
5620 done = 0;
5621
Willy Tarreau9cdde232007-05-02 20:58:19 +02005622 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005623 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5624
5625 /* Now we have the status line between cur_ptr and cur_end */
5626
5627 /* The annoying part is that pattern matching needs
5628 * that we modify the contents to null-terminate all
5629 * strings before testing them.
5630 */
5631
5632 term = *cur_end;
5633 *cur_end = '\0';
5634
5635 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5636 switch (exp->action) {
5637 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005638 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005639 done = 1;
5640 break;
5641
5642 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005643 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005644 done = 1;
5645 break;
5646
5647 case ACT_REPLACE:
5648 *cur_end = term; /* restore the string terminator */
5649 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5650 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5651 /* FIXME: if the user adds a newline in the replacement, the
5652 * index will not be recalculated for now, and the new line
5653 * will not be counted as a new header.
5654 */
5655
Willy Tarreaufa355d42009-11-29 18:12:29 +01005656 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005657 cur_end += delta;
5658
Willy Tarreau9cdde232007-05-02 20:58:19 +02005659 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005660 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005661 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005662 cur_ptr, cur_end + 1,
5663 NULL, NULL);
5664 if (unlikely(!cur_end))
5665 return -1;
5666
5667 /* we have a full respnse and we know that we have either a CR
5668 * or an LF at <ptr>.
5669 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005670 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005671 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5672 /* there is no point trying this regex on headers */
5673 return 1;
5674 }
5675 }
5676 *cur_end = term; /* restore the string terminator */
5677 return done;
5678}
5679
5680
5681
5682/*
5683 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5684 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5685 * unparsable response.
5686 */
5687int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5688{
Willy Tarreau3d300592007-03-18 18:34:41 +01005689 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005690 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005691 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005692 int ret;
5693
5694 /*
5695 * The interleaving of transformations and verdicts
5696 * makes it difficult to decide to continue or stop
5697 * the evaluation.
5698 */
5699
Willy Tarreau3d300592007-03-18 18:34:41 +01005700 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005701 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5702 exp->action == ACT_PASS)) {
5703 exp = exp->next;
5704 continue;
5705 }
5706
5707 /* Apply the filter to the status line. */
5708 ret = apply_filter_to_sts_line(t, rtr, exp);
5709 if (unlikely(ret < 0))
5710 return -1;
5711
5712 if (likely(ret == 0)) {
5713 /* The filter did not match the response, it can be
5714 * iterated through all headers.
5715 */
5716 apply_filter_to_resp_headers(t, rtr, exp);
5717 }
5718 exp = exp->next;
5719 }
5720 return 0;
5721}
5722
5723
5724
5725/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005726 * Manage server-side cookies. It can impact performance by about 2% so it is
5727 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005728 */
5729void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5730{
5731 struct http_txn *txn = &t->txn;
5732 char *p1, *p2, *p3, *p4;
5733
Willy Tarreaua15645d2007-03-18 16:22:39 +01005734 char *cur_ptr, *cur_end, *cur_next;
5735 int cur_idx, old_idx, delta;
5736
Willy Tarreaua15645d2007-03-18 16:22:39 +01005737 /* Iterate through the headers.
5738 * we start with the start line.
5739 */
5740 old_idx = 0;
5741 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5742
5743 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5744 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005745 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005746
5747 cur_hdr = &txn->hdr_idx.v[cur_idx];
5748 cur_ptr = cur_next;
5749 cur_end = cur_ptr + cur_hdr->len;
5750 cur_next = cur_end + cur_hdr->cr + 1;
5751
5752 /* We have one full header between cur_ptr and cur_end, and the
5753 * next header starts at cur_next. We're only interested in
5754 * "Cookie:" headers.
5755 */
5756
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005757 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5758 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005759 old_idx = cur_idx;
5760 continue;
5761 }
5762
5763 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005764 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005765
5766
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005767 /* maybe we only wanted to see if there was a set-cookie. Note that
5768 * the cookie capture is declared in the fronend.
5769 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005770 if (t->be->cookie_name == NULL &&
5771 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005772 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005773 return;
5774
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005775 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005776
5777 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005778 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5779 break;
5780
5781 /* p1 is at the beginning of the cookie name */
5782 p2 = p1;
5783
5784 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5785 p2++;
5786
5787 if (p2 == cur_end || *p2 == ';') /* next cookie */
5788 break;
5789
5790 p3 = p2 + 1; /* skip the '=' sign */
5791 if (p3 == cur_end)
5792 break;
5793
5794 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005795 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005796 p4++;
5797
5798 /* here, we have the cookie name between p1 and p2,
5799 * and its value between p3 and p4.
5800 * we can process it.
5801 */
5802
5803 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005804 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005805 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005806 (p4 - p1 >= t->fe->capture_namelen) &&
5807 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005808 int log_len = p4 - p1;
5809
Willy Tarreau086b3b42007-05-13 21:45:51 +02005810 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005811 Alert("HTTP logging : out of memory.\n");
5812 }
5813
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005814 if (log_len > t->fe->capture_len)
5815 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005816 memcpy(txn->srv_cookie, p1, log_len);
5817 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005818 }
5819
5820 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005821 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5822 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005823 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005824 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005825
5826 /* If the cookie is in insert mode on a known server, we'll delete
5827 * this occurrence because we'll insert another one later.
5828 * We'll delete it too if the "indirect" option is set and we're in
5829 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005830 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5831 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005832 /* this header must be deleted */
5833 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5834 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5835 txn->hdr_idx.used--;
5836 cur_hdr->len = 0;
5837 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005838 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005839
Willy Tarreau3d300592007-03-18 18:34:41 +01005840 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005841 }
5842 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005843 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005844 /* replace bytes p3->p4 with the cookie name associated
5845 * with this server since we know it.
5846 */
5847 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5848 cur_hdr->len += delta;
5849 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005850 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005851
Willy Tarreau3d300592007-03-18 18:34:41 +01005852 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005853 }
5854 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005855 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005856 /* insert the cookie name associated with this server
5857 * before existing cookie, and insert a delimitor between them..
5858 */
5859 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5860 cur_hdr->len += delta;
5861 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005862 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005863
5864 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005865 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005866 }
5867 }
5868 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005869 else if (t->be->appsession_name != NULL) {
5870 int cmp_len, value_len;
5871 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005872
Cyril Bontéb21570a2009-11-29 20:04:48 +01005873 if (t->be->options2 & PR_O2_AS_PFX) {
5874 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5875 value_begin = p1 + t->be->appsession_name_len;
5876 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5877 } else {
5878 cmp_len = p2 - p1;
5879 value_begin = p3;
5880 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005881 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005882
5883 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5884 /* Cool... it's the right one */
5885 if (t->sessid != NULL) {
5886 /* free previously allocated memory as we don't need it anymore */
5887 pool_free2(apools.sessid, t->sessid);
5888 }
5889 /* Store the sessid in the session for future use */
5890 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5891 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5892 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5893 return;
5894 }
5895 memcpy(t->sessid, value_begin, value_len);
5896 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005897 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005898 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005899 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5900 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005901 /* keep the link from this header to next one */
5902 old_idx = cur_idx;
5903 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005904
5905 if (t->sessid != NULL) {
5906 appsess *asession = NULL;
5907 /* only do insert, if lookup fails */
5908 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5909 if (asession == NULL) {
5910 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5911 Alert("Not enough Memory process_srv():asession:calloc().\n");
5912 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5913 return;
5914 }
5915 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5916 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5917 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5918 return;
5919 }
5920 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5921 asession->sessid[t->be->appsession_len] = 0;
5922
5923 size_t server_id_len = strlen(t->srv->id) + 1;
5924 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5925 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5926 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5927 return;
5928 }
5929 asession->serverid[0] = '\0';
5930 memcpy(asession->serverid, t->srv->id, server_id_len);
5931
5932 asession->request_count = 0;
5933 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5934 }
5935
5936 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5937 asession->request_count++;
5938 }
5939
5940#if defined(DEBUG_HASH)
5941 Alert("manage_server_side_cookies\n");
5942 appsession_hash_dump(&(t->be->htbl_proxy));
5943#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944}
5945
5946
5947
5948/*
5949 * Check if response is cacheable or not. Updates t->flags.
5950 */
5951void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5952{
5953 struct http_txn *txn = &t->txn;
5954 char *p1, *p2;
5955
5956 char *cur_ptr, *cur_end, *cur_next;
5957 int cur_idx;
5958
Willy Tarreau5df51872007-11-25 16:20:08 +01005959 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005960 return;
5961
5962 /* Iterate through the headers.
5963 * we start with the start line.
5964 */
5965 cur_idx = 0;
5966 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5967
5968 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5969 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005970 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971
5972 cur_hdr = &txn->hdr_idx.v[cur_idx];
5973 cur_ptr = cur_next;
5974 cur_end = cur_ptr + cur_hdr->len;
5975 cur_next = cur_end + cur_hdr->cr + 1;
5976
5977 /* We have one full header between cur_ptr and cur_end, and the
5978 * next header starts at cur_next. We're only interested in
5979 * "Cookie:" headers.
5980 */
5981
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005982 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5983 if (val) {
5984 if ((cur_end - (cur_ptr + val) >= 8) &&
5985 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5986 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5987 return;
5988 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005989 }
5990
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005991 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5992 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005993 continue;
5994
5995 /* OK, right now we know we have a cache-control header at cur_ptr */
5996
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005997 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005998
5999 if (p1 >= cur_end) /* no more info */
6000 continue;
6001
6002 /* p1 is at the beginning of the value */
6003 p2 = p1;
6004
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006005 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006006 p2++;
6007
6008 /* we have a complete value between p1 and p2 */
6009 if (p2 < cur_end && *p2 == '=') {
6010 /* we have something of the form no-cache="set-cookie" */
6011 if ((cur_end - p1 >= 21) &&
6012 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6013 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006014 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006015 continue;
6016 }
6017
6018 /* OK, so we know that either p2 points to the end of string or to a comma */
6019 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6020 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6021 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6022 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006023 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006024 return;
6025 }
6026
6027 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006028 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006029 continue;
6030 }
6031 }
6032}
6033
6034
Willy Tarreau58f10d72006-12-04 02:26:12 +01006035/*
6036 * Try to retrieve a known appsession in the URI, then the associated server.
6037 * If the server is found, it's assigned to the session.
6038 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006039void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006041 char *end_params, *first_param, *cur_param, *next_param;
6042 char separator;
6043 int value_len;
6044
6045 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006046
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006047 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006048 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006049 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006050 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051
Cyril Bontéb21570a2009-11-29 20:04:48 +01006052 first_param = NULL;
6053 switch (mode) {
6054 case PR_O2_AS_M_PP:
6055 first_param = memchr(begin, ';', len);
6056 break;
6057 case PR_O2_AS_M_QS:
6058 first_param = memchr(begin, '?', len);
6059 break;
6060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006061
Cyril Bontéb21570a2009-11-29 20:04:48 +01006062 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006064 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006065
Cyril Bontéb21570a2009-11-29 20:04:48 +01006066 switch (mode) {
6067 case PR_O2_AS_M_PP:
6068 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6069 end_params = (char *) begin + len;
6070 }
6071 separator = ';';
6072 break;
6073 case PR_O2_AS_M_QS:
6074 end_params = (char *) begin + len;
6075 separator = '&';
6076 break;
6077 default:
6078 /* unknown mode, shouldn't happen */
6079 return;
6080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006081
Cyril Bontéb21570a2009-11-29 20:04:48 +01006082 cur_param = next_param = end_params;
6083 while (cur_param > first_param) {
6084 cur_param--;
6085 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6086 /* let's see if this is the appsession parameter */
6087 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6088 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6089 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6090 /* Cool... it's the right one */
6091 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6092 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6093 if (value_len > 0) {
6094 manage_client_side_appsession(t, cur_param, value_len);
6095 }
6096 break;
6097 }
6098 next_param = cur_param;
6099 }
6100 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006101#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006102 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006103 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006104#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006105}
6106
6107
Willy Tarreaub2513902006-12-17 14:52:38 +01006108/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006109 * In a GET or HEAD request, check if the requested URI matches the stats uri
6110 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006111 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006112 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006113 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006114 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006115 *
6116 * Returns 1 if the session's state changes, otherwise 0.
6117 */
6118int stats_check_uri_auth(struct session *t, struct proxy *backend)
6119{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006120 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006121 struct uri_auth *uri_auth = backend->uri_auth;
6122 struct user_auth *user;
6123 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006125
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006126 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6127
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006128 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006129 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006130 return 0;
6131
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006132 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006133
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006134 /* the URI is in h */
6135 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006136 return 0;
6137
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006138 h += uri_auth->uri_len;
6139 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6140 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006141 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006142 break;
6143 }
6144 h++;
6145 }
6146
6147 if (uri_auth->refresh) {
6148 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6149 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6150 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006151 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006152 break;
6153 }
6154 h++;
6155 }
6156 }
6157
Willy Tarreau55bb8452007-10-17 18:44:57 +02006158 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6159 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6160 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006161 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006162 break;
6163 }
6164 h++;
6165 }
6166
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006167 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6168
Willy Tarreaub2513902006-12-17 14:52:38 +01006169 /* we are in front of a interceptable URI. Let's check
6170 * if there's an authentication and if it's valid.
6171 */
6172 user = uri_auth->users;
6173 if (!user) {
6174 /* no user auth required, it's OK */
6175 authenticated = 1;
6176 } else {
6177 authenticated = 0;
6178
6179 /* a user list is defined, we have to check.
6180 * skip 21 chars for "Authorization: Basic ".
6181 */
6182
6183 /* FIXME: this should move to an earlier place */
6184 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006185 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6186 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6187 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006188 if (len > 14 &&
6189 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006190 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006191 break;
6192 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006193 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006194 }
6195
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006196 if (txn->auth_hdr.len < 21 ||
6197 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006198 user = NULL;
6199
6200 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006201 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6202 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006203 user->user_pwd, user->user_len)) {
6204 authenticated = 1;
6205 break;
6206 }
6207 user = user->next;
6208 }
6209 }
6210
6211 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006212 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006213
6214 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006215 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6216 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006217 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006218 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006219 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006220 if (!(t->flags & SN_ERR_MASK))
6221 t->flags |= SN_ERR_PRXCOND;
6222 if (!(t->flags & SN_FINST_MASK))
6223 t->flags |= SN_FINST_R;
6224 return 1;
6225 }
6226
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006227 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006228 * data.
6229 */
Willy Tarreau70089872008-06-13 21:12:51 +02006230 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006231 t->data_source = DATA_SRC_STATS;
6232 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006233 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006234 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6235 t->rep->prod->private = t;
6236 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006237 return 1;
6238}
6239
Willy Tarreau4076a152009-04-02 15:18:36 +02006240/*
6241 * Capture a bad request or response and archive it in the proxy's structure.
6242 */
6243void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6244 struct buffer *buf, struct http_msg *msg,
6245 struct proxy *other_end)
6246{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006247 es->len = buf->r - (buf->data + msg->som);
6248 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006249 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006250 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006251 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006252 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006253 es->when = date; // user-visible date
6254 es->sid = s->uniq_id;
6255 es->srv = s->srv;
6256 es->oe = other_end;
6257 es->src = s->cli_addr;
6258}
Willy Tarreaub2513902006-12-17 14:52:38 +01006259
Willy Tarreaubaaee002006-06-26 02:48:02 +02006260/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006261 * Print a debug line with a header
6262 */
6263void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6264{
6265 int len, max;
6266 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006267 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006268 max = end - start;
6269 UBOUND(max, sizeof(trash) - len - 1);
6270 len += strlcpy2(trash + len, start, max + 1);
6271 trash[len++] = '\n';
6272 write(1, trash, len);
6273}
6274
Willy Tarreau0937bc42009-12-22 15:03:09 +01006275/*
6276 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6277 * the required fields are properly allocated and that we only need to (re)init
6278 * them. This should be used before processing any new request.
6279 */
6280void http_init_txn(struct session *s)
6281{
6282 struct http_txn *txn = &s->txn;
6283 struct proxy *fe = s->fe;
6284
6285 txn->flags = 0;
6286 txn->status = -1;
6287
6288 txn->req.sol = txn->req.eol = NULL;
6289 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6290 txn->rsp.sol = txn->rsp.eol = NULL;
6291 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6292 txn->req.hdr_content_len = 0LL;
6293 txn->rsp.hdr_content_len = 0LL;
6294 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6295 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6296 chunk_reset(&txn->auth_hdr);
6297
6298 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6299 if (fe->options2 & PR_O2_REQBUG_OK)
6300 txn->req.err_pos = -1; /* let buggy requests pass */
6301
6302 if (txn->req.cap)
6303 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6304
6305 if (txn->rsp.cap)
6306 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6307
6308 if (txn->hdr_idx.v)
6309 hdr_idx_init(&txn->hdr_idx);
6310}
6311
6312/* to be used at the end of a transaction */
6313void http_end_txn(struct session *s)
6314{
6315 struct http_txn *txn = &s->txn;
6316
6317 /* these ones will have been dynamically allocated */
6318 pool_free2(pool2_requri, txn->uri);
6319 pool_free2(pool2_capture, txn->cli_cookie);
6320 pool_free2(pool2_capture, txn->srv_cookie);
6321 txn->uri = NULL;
6322 txn->srv_cookie = NULL;
6323 txn->cli_cookie = NULL;
6324}
6325
6326/* to be used at the end of a transaction to prepare a new one */
6327void http_reset_txn(struct session *s)
6328{
6329 http_end_txn(s);
6330 http_init_txn(s);
6331
6332 s->be = s->fe;
6333 s->req->analysers = s->listener->analysers;
6334 s->logs.logwait = s->fe->to_log;
6335 s->srv = s->prev_srv = s->srv_conn = NULL;
6336 s->pend_pos = NULL;
6337 s->conn_retries = s->be->conn_retries;
6338
6339 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6340
6341 s->req->rto = s->fe->timeout.client;
6342 s->req->wto = s->be->timeout.server;
6343 s->req->cto = s->be->timeout.connect;
6344
6345 s->rep->rto = s->be->timeout.server;
6346 s->rep->wto = s->fe->timeout.client;
6347 s->rep->cto = TICK_ETERNITY;
6348
6349 s->req->rex = TICK_ETERNITY;
6350 s->req->wex = TICK_ETERNITY;
6351 s->req->analyse_exp = TICK_ETERNITY;
6352 s->rep->rex = TICK_ETERNITY;
6353 s->rep->wex = TICK_ETERNITY;
6354 s->rep->analyse_exp = TICK_ETERNITY;
6355}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006356
Willy Tarreau8797c062007-05-07 00:55:35 +02006357/************************************************************************/
6358/* The code below is dedicated to ACL parsing and matching */
6359/************************************************************************/
6360
6361
6362
6363
6364/* 1. Check on METHOD
6365 * We use the pre-parsed method if it is known, and store its number as an
6366 * integer. If it is unknown, we use the pointer and the length.
6367 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006368static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006369{
6370 int len, meth;
6371
Willy Tarreauae8b7962007-06-09 23:10:04 +02006372 len = strlen(*text);
6373 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006374
6375 pattern->val.i = meth;
6376 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006377 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006378 if (!pattern->ptr.str)
6379 return 0;
6380 pattern->len = len;
6381 }
6382 return 1;
6383}
6384
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006385static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006386acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6387 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006388{
6389 int meth;
6390 struct http_txn *txn = l7;
6391
Willy Tarreaub6866442008-07-14 23:54:42 +02006392 if (!txn)
6393 return 0;
6394
Willy Tarreau655dce92009-11-08 13:10:58 +01006395 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006396 return 0;
6397
Willy Tarreau8797c062007-05-07 00:55:35 +02006398 meth = txn->meth;
6399 test->i = meth;
6400 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006401 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6402 /* ensure the indexes are not affected */
6403 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006404 test->len = txn->req.sl.rq.m_l;
6405 test->ptr = txn->req.sol;
6406 }
6407 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6408 return 1;
6409}
6410
6411static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6412{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006413 int icase;
6414
Willy Tarreau8797c062007-05-07 00:55:35 +02006415 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006416 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006417
6418 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006419 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006420
6421 /* Other method, we must compare the strings */
6422 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006423 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006424
6425 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6426 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6427 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006428 return ACL_PAT_FAIL;
6429 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006430}
6431
6432/* 2. Check on Request/Status Version
6433 * We simply compare strings here.
6434 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006435static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006436{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006437 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006438 if (!pattern->ptr.str)
6439 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006440 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006441 return 1;
6442}
6443
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006444static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006445acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6446 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006447{
6448 struct http_txn *txn = l7;
6449 char *ptr;
6450 int len;
6451
Willy Tarreaub6866442008-07-14 23:54:42 +02006452 if (!txn)
6453 return 0;
6454
Willy Tarreau655dce92009-11-08 13:10:58 +01006455 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006456 return 0;
6457
Willy Tarreau8797c062007-05-07 00:55:35 +02006458 len = txn->req.sl.rq.v_l;
6459 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6460
6461 while ((len-- > 0) && (*ptr++ != '/'));
6462 if (len <= 0)
6463 return 0;
6464
6465 test->ptr = ptr;
6466 test->len = len;
6467
6468 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6469 return 1;
6470}
6471
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006472static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006473acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6474 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006475{
6476 struct http_txn *txn = l7;
6477 char *ptr;
6478 int len;
6479
Willy Tarreaub6866442008-07-14 23:54:42 +02006480 if (!txn)
6481 return 0;
6482
Willy Tarreau655dce92009-11-08 13:10:58 +01006483 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006484 return 0;
6485
Willy Tarreau8797c062007-05-07 00:55:35 +02006486 len = txn->rsp.sl.st.v_l;
6487 ptr = txn->rsp.sol;
6488
6489 while ((len-- > 0) && (*ptr++ != '/'));
6490 if (len <= 0)
6491 return 0;
6492
6493 test->ptr = ptr;
6494 test->len = len;
6495
6496 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6497 return 1;
6498}
6499
6500/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006501static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006502acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6503 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006504{
6505 struct http_txn *txn = l7;
6506 char *ptr;
6507 int len;
6508
Willy Tarreaub6866442008-07-14 23:54:42 +02006509 if (!txn)
6510 return 0;
6511
Willy Tarreau655dce92009-11-08 13:10:58 +01006512 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006513 return 0;
6514
Willy Tarreau8797c062007-05-07 00:55:35 +02006515 len = txn->rsp.sl.st.c_l;
6516 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6517
6518 test->i = __strl2ui(ptr, len);
6519 test->flags = ACL_TEST_F_VOL_1ST;
6520 return 1;
6521}
6522
6523/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006524static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006525acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6526 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006527{
6528 struct http_txn *txn = l7;
6529
Willy Tarreaub6866442008-07-14 23:54:42 +02006530 if (!txn)
6531 return 0;
6532
Willy Tarreau655dce92009-11-08 13:10:58 +01006533 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006534 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006535
Willy Tarreauc11416f2007-06-17 16:58:38 +02006536 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6537 /* ensure the indexes are not affected */
6538 return 0;
6539
Willy Tarreau8797c062007-05-07 00:55:35 +02006540 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006541 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006542
Willy Tarreauf3d25982007-05-08 22:45:09 +02006543 /* we do not need to set READ_ONLY because the data is in a buffer */
6544 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006545 return 1;
6546}
6547
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006548static int
6549acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6550 struct acl_expr *expr, struct acl_test *test)
6551{
6552 struct http_txn *txn = l7;
6553
Willy Tarreaub6866442008-07-14 23:54:42 +02006554 if (!txn)
6555 return 0;
6556
Willy Tarreau655dce92009-11-08 13:10:58 +01006557 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006558 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006559
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006560 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6561 /* ensure the indexes are not affected */
6562 return 0;
6563
6564 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006565 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006566 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6567 test->i = AF_INET;
6568
6569 /*
6570 * If we are parsing url in frontend space, we prepare backend stage
6571 * to not parse again the same url ! optimization lazyness...
6572 */
6573 if (px->options & PR_O_HTTP_PROXY)
6574 l4->flags |= SN_ADDR_SET;
6575
6576 test->flags = ACL_TEST_F_READ_ONLY;
6577 return 1;
6578}
6579
6580static int
6581acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6582 struct acl_expr *expr, struct acl_test *test)
6583{
6584 struct http_txn *txn = l7;
6585
Willy Tarreaub6866442008-07-14 23:54:42 +02006586 if (!txn)
6587 return 0;
6588
Willy Tarreau655dce92009-11-08 13:10:58 +01006589 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006590 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006591
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006592 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6593 /* ensure the indexes are not affected */
6594 return 0;
6595
6596 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006597 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006598 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6599
6600 if (px->options & PR_O_HTTP_PROXY)
6601 l4->flags |= SN_ADDR_SET;
6602
6603 test->flags = ACL_TEST_F_READ_ONLY;
6604 return 1;
6605}
6606
Willy Tarreauc11416f2007-06-17 16:58:38 +02006607/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6608 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6609 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006610static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006611acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006612 struct acl_expr *expr, struct acl_test *test)
6613{
6614 struct http_txn *txn = l7;
6615 struct hdr_idx *idx = &txn->hdr_idx;
6616 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006617
Willy Tarreaub6866442008-07-14 23:54:42 +02006618 if (!txn)
6619 return 0;
6620
Willy Tarreau33a7e692007-06-10 19:45:56 +02006621 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6622 /* search for header from the beginning */
6623 ctx->idx = 0;
6624
Willy Tarreau33a7e692007-06-10 19:45:56 +02006625 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6626 test->flags |= ACL_TEST_F_FETCH_MORE;
6627 test->flags |= ACL_TEST_F_VOL_HDR;
6628 test->len = ctx->vlen;
6629 test->ptr = (char *)ctx->line + ctx->val;
6630 return 1;
6631 }
6632
6633 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6634 test->flags |= ACL_TEST_F_VOL_HDR;
6635 return 0;
6636}
6637
Willy Tarreau33a7e692007-06-10 19:45:56 +02006638static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006639acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6640 struct acl_expr *expr, struct acl_test *test)
6641{
6642 struct http_txn *txn = l7;
6643
Willy Tarreaub6866442008-07-14 23:54:42 +02006644 if (!txn)
6645 return 0;
6646
Willy Tarreau655dce92009-11-08 13:10:58 +01006647 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006648 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006649
Willy Tarreauc11416f2007-06-17 16:58:38 +02006650 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6651 /* ensure the indexes are not affected */
6652 return 0;
6653
6654 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6655}
6656
6657static int
6658acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6659 struct acl_expr *expr, struct acl_test *test)
6660{
6661 struct http_txn *txn = l7;
6662
Willy Tarreaub6866442008-07-14 23:54:42 +02006663 if (!txn)
6664 return 0;
6665
Willy Tarreau655dce92009-11-08 13:10:58 +01006666 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006667 return 0;
6668
6669 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6670}
6671
6672/* 6. Check on HTTP header count. The number of occurrences is returned.
6673 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6674 */
6675static int
6676acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006677 struct acl_expr *expr, struct acl_test *test)
6678{
6679 struct http_txn *txn = l7;
6680 struct hdr_idx *idx = &txn->hdr_idx;
6681 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006682 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006683
Willy Tarreaub6866442008-07-14 23:54:42 +02006684 if (!txn)
6685 return 0;
6686
Willy Tarreau33a7e692007-06-10 19:45:56 +02006687 ctx.idx = 0;
6688 cnt = 0;
6689 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6690 cnt++;
6691
6692 test->i = cnt;
6693 test->flags = ACL_TEST_F_VOL_HDR;
6694 return 1;
6695}
6696
Willy Tarreauc11416f2007-06-17 16:58:38 +02006697static int
6698acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6699 struct acl_expr *expr, struct acl_test *test)
6700{
6701 struct http_txn *txn = l7;
6702
Willy Tarreaub6866442008-07-14 23:54:42 +02006703 if (!txn)
6704 return 0;
6705
Willy Tarreau655dce92009-11-08 13:10:58 +01006706 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006707 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006708
Willy Tarreauc11416f2007-06-17 16:58:38 +02006709 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6710 /* ensure the indexes are not affected */
6711 return 0;
6712
6713 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6714}
6715
6716static int
6717acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6718 struct acl_expr *expr, struct acl_test *test)
6719{
6720 struct http_txn *txn = l7;
6721
Willy Tarreaub6866442008-07-14 23:54:42 +02006722 if (!txn)
6723 return 0;
6724
Willy Tarreau655dce92009-11-08 13:10:58 +01006725 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006726 return 0;
6727
6728 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6729}
6730
Willy Tarreau33a7e692007-06-10 19:45:56 +02006731/* 7. Check on HTTP header's integer value. The integer value is returned.
6732 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006733 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006734 */
6735static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006736acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006737 struct acl_expr *expr, struct acl_test *test)
6738{
6739 struct http_txn *txn = l7;
6740 struct hdr_idx *idx = &txn->hdr_idx;
6741 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006742
Willy Tarreaub6866442008-07-14 23:54:42 +02006743 if (!txn)
6744 return 0;
6745
Willy Tarreau33a7e692007-06-10 19:45:56 +02006746 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6747 /* search for header from the beginning */
6748 ctx->idx = 0;
6749
Willy Tarreau33a7e692007-06-10 19:45:56 +02006750 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6751 test->flags |= ACL_TEST_F_FETCH_MORE;
6752 test->flags |= ACL_TEST_F_VOL_HDR;
6753 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6754 return 1;
6755 }
6756
6757 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6758 test->flags |= ACL_TEST_F_VOL_HDR;
6759 return 0;
6760}
6761
Willy Tarreauc11416f2007-06-17 16:58:38 +02006762static int
6763acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6764 struct acl_expr *expr, struct acl_test *test)
6765{
6766 struct http_txn *txn = l7;
6767
Willy Tarreaub6866442008-07-14 23:54:42 +02006768 if (!txn)
6769 return 0;
6770
Willy Tarreau655dce92009-11-08 13:10:58 +01006771 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006772 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006773
Willy Tarreauc11416f2007-06-17 16:58:38 +02006774 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6775 /* ensure the indexes are not affected */
6776 return 0;
6777
6778 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6779}
6780
6781static int
6782acl_fetch_shdr_val(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->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006791 return 0;
6792
6793 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6794}
6795
Willy Tarreau106f9792009-09-19 07:54:16 +02006796/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6797 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6798 */
6799static int
6800acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6801 struct acl_expr *expr, struct acl_test *test)
6802{
6803 struct http_txn *txn = l7;
6804 struct hdr_idx *idx = &txn->hdr_idx;
6805 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6806
6807 if (!txn)
6808 return 0;
6809
6810 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6811 /* search for header from the beginning */
6812 ctx->idx = 0;
6813
6814 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6815 test->flags |= ACL_TEST_F_FETCH_MORE;
6816 test->flags |= ACL_TEST_F_VOL_HDR;
6817 /* Same optimization as url_ip */
6818 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6819 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6820 test->ptr = (void *)&l4->srv_addr.sin_addr;
6821 test->i = AF_INET;
6822 return 1;
6823 }
6824
6825 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6826 test->flags |= ACL_TEST_F_VOL_HDR;
6827 return 0;
6828}
6829
6830static int
6831acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6832 struct acl_expr *expr, struct acl_test *test)
6833{
6834 struct http_txn *txn = l7;
6835
6836 if (!txn)
6837 return 0;
6838
Willy Tarreau655dce92009-11-08 13:10:58 +01006839 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006840 return 0;
6841
6842 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6843 /* ensure the indexes are not affected */
6844 return 0;
6845
6846 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6847}
6848
6849static int
6850acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6851 struct acl_expr *expr, struct acl_test *test)
6852{
6853 struct http_txn *txn = l7;
6854
6855 if (!txn)
6856 return 0;
6857
Willy Tarreau655dce92009-11-08 13:10:58 +01006858 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006859 return 0;
6860
6861 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6862}
6863
Willy Tarreau737b0c12007-06-10 21:28:46 +02006864/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6865 * the first '/' after the possible hostname, and ends before the possible '?'.
6866 */
6867static int
6868acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6869 struct acl_expr *expr, struct acl_test *test)
6870{
6871 struct http_txn *txn = l7;
6872 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006873
Willy Tarreaub6866442008-07-14 23:54:42 +02006874 if (!txn)
6875 return 0;
6876
Willy Tarreau655dce92009-11-08 13:10:58 +01006877 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006878 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006879
Willy Tarreauc11416f2007-06-17 16:58:38 +02006880 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6881 /* ensure the indexes are not affected */
6882 return 0;
6883
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006884 end = txn->req.sol - txn->req.som + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006885 ptr = http_get_path(txn);
6886 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006887 return 0;
6888
6889 /* OK, we got the '/' ! */
6890 test->ptr = ptr;
6891
6892 while (ptr < end && *ptr != '?')
6893 ptr++;
6894
6895 test->len = ptr - test->ptr;
6896
6897 /* we do not need to set READ_ONLY because the data is in a buffer */
6898 test->flags = ACL_TEST_F_VOL_1ST;
6899 return 1;
6900}
6901
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006902static int
6903acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6904 struct acl_expr *expr, struct acl_test *test)
6905{
6906 struct buffer *req = s->req;
6907 struct http_txn *txn = &s->txn;
6908 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006909
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006910 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6911 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6912 */
6913
6914 if (!s || !req)
6915 return 0;
6916
Willy Tarreau655dce92009-11-08 13:10:58 +01006917 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006918 /* Already decoded as OK */
6919 test->flags |= ACL_TEST_F_SET_RES_PASS;
6920 return 1;
6921 }
6922
6923 /* Try to decode HTTP request */
6924 if (likely(req->lr < req->r))
6925 http_msg_analyzer(req, msg, &txn->hdr_idx);
6926
Willy Tarreau655dce92009-11-08 13:10:58 +01006927 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006928 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6929 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6930 return 1;
6931 }
6932 /* wait for final state */
6933 test->flags |= ACL_TEST_F_MAY_CHANGE;
6934 return 0;
6935 }
6936
6937 /* OK we got a valid HTTP request. We have some minor preparation to
6938 * perform so that further checks can rely on HTTP tests.
6939 */
6940 msg->sol = req->data + msg->som;
6941 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6942 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6943 s->flags |= SN_REDIRECTABLE;
6944
6945 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6946 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6947 return 1;
6948 }
6949
6950 test->flags |= ACL_TEST_F_SET_RES_PASS;
6951 return 1;
6952}
6953
Willy Tarreau8797c062007-05-07 00:55:35 +02006954
6955/************************************************************************/
6956/* All supported keywords must be declared here. */
6957/************************************************************************/
6958
6959/* Note: must not be declared <const> as its list will be overwritten */
6960static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006961 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6962
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006963 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6964 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6965 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6966 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006967
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006968 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6969 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6970 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6971 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6972 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6973 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6974 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6975 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6976 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006977
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006978 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6979 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6980 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6981 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6982 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6983 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6984 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6985 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6986 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6987 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006988 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006989
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006990 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6991 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6992 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6993 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6994 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6995 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6996 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6997 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6998 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006999 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007000
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007001 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7002 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7003 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7004 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7005 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7006 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7007 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007008
Willy Tarreauf3d25982007-05-08 22:45:09 +02007009 { NULL, NULL, NULL, NULL },
7010
7011#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007012 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7013 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7014 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7015 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7016 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7017 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7018 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7019
Willy Tarreau8797c062007-05-07 00:55:35 +02007020 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7021 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7022 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7023 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7024 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7025 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7026 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7027 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7028
7029 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7030 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7031 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7032 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7033 { NULL, NULL, NULL, NULL },
7034#endif
7035}};
7036
7037
7038__attribute__((constructor))
7039static void __http_protocol_init(void)
7040{
7041 acl_register_keywords(&acl_kws);
7042}
7043
7044
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007046 * Local variables:
7047 * c-indent-level: 8
7048 * c-basic-offset: 8
7049 * End:
7050 */