blob: 03e730a2f400bf4138cb1a510e417a686ee51a0f [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
1811 if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
1812 if (conn_cl) {
1813 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1814 if (!conn_ka)
1815 txn->flags |= TX_REQ_CONN_CLO;
1816 }
1817 } else { /* HTTP/1.0 */
1818 if (!conn_ka)
1819 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO;
1820 else if (conn_cl)
1821 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1822 }
1823 txn->flags |= TX_CON_HDR_PARS;
1824}
1825
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1827 * first byte of body, and increments msg->sov by the number of bytes parsed,
1828 * so that we know we can forward between ->som and ->sov. Note that due to
1829 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1830 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001831 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001833 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001835{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 char *ptr = buf->lr;
1837 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 unsigned int chunk = 0;
1839
1840 /* The chunk size is in the following form, though we are only
1841 * interested in the size and CRLF :
1842 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1843 */
1844 while (1) {
1845 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001846 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001849 if (c < 0) /* not a hex digit anymore */
1850 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001851 if (++ptr >= end)
1852 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 if (chunk & 0xF000000) /* overflow will occur */
1854 return -1;
1855 chunk = (chunk << 4) + c;
1856 }
1857
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 /* empty size not allowed */
1859 if (ptr == buf->lr)
1860 return -1;
1861
1862 while (http_is_spht[(unsigned char)*ptr]) {
1863 if (++ptr >= end)
1864 ptr = buf->data;
1865 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001866 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 }
1868
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 /* Up to there, we know that at least one byte is present at *ptr. Check
1870 * for the end of chunk size.
1871 */
1872 while (1) {
1873 if (likely(HTTP_IS_CRLF(*ptr))) {
1874 /* we now have a CR or an LF at ptr */
1875 if (likely(*ptr == '\r')) {
1876 if (++ptr >= end)
1877 ptr = buf->data;
1878 if (ptr == buf->r)
1879 return 0;
1880 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001881
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 if (*ptr != '\n')
1883 return -1;
1884 if (++ptr >= end)
1885 ptr = buf->data;
1886 /* done */
1887 break;
1888 }
1889 else if (*ptr == ';') {
1890 /* chunk extension, ends at next CRLF */
1891 if (++ptr >= end)
1892 ptr = buf->data;
1893 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001894 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
1896 while (!HTTP_IS_CRLF(*ptr)) {
1897 if (++ptr >= end)
1898 ptr = buf->data;
1899 if (ptr == buf->r)
1900 return 0;
1901 }
1902 /* we have a CRLF now, loop above */
1903 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001904 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001906 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 }
1908
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 /* OK we found our CRLF and now <ptr> points to the next byte,
1910 * which may or may not be present. We save that into ->lr and
1911 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 msg->sov += ptr - buf->lr;
1914 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001915 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001916 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001917 return 1;
1918}
1919
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920/* This function skips trailers in the buffer <buf> associated with HTTP
1921 * message <msg>. The first visited position is buf->lr. If the end of
1922 * the trailers is found, it is automatically scheduled to be forwarded,
1923 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1924 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001925 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1926 * zero. If a parse error is encountered, the function returns < 0 and does not
1927 * change anything except maybe buf->lr and msg->sov. Note that the message
1928 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1929 * which implies that all non-trailers data have already been scheduled for
1930 * forwarding, and that the difference between msg->som and msg->sov exactly
1931 * matches the length of trailers already parsed and not forwarded. It is also
1932 * important to note that this function is designed to be able to parse wrapped
1933 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 */
1935int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1936{
1937 /* we have buf->lr which points to next line. Look for CRLF. */
1938 while (1) {
1939 char *p1 = NULL, *p2 = NULL;
1940 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001941 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942
1943 /* scan current line and stop at LF or CRLF */
1944 while (1) {
1945 if (ptr == buf->r)
1946 return 0;
1947
1948 if (*ptr == '\n') {
1949 if (!p1)
1950 p1 = ptr;
1951 p2 = ptr;
1952 break;
1953 }
1954
1955 if (*ptr == '\r') {
1956 if (p1)
1957 return -1;
1958 p1 = ptr;
1959 }
1960
1961 ptr++;
1962 if (ptr >= buf->data + buf->size)
1963 ptr = buf->data;
1964 }
1965
1966 /* after LF; point to beginning of next line */
1967 p2++;
1968 if (p2 >= buf->data + buf->size)
1969 p2 = buf->data;
1970
Willy Tarreau638cd022010-01-03 07:42:04 +01001971 bytes = p2 - buf->lr;
1972 if (bytes < 0)
1973 bytes += buf->size;
1974
1975 /* schedule this line for forwarding */
1976 msg->sov += bytes;
1977 if (msg->sov >= buf->size)
1978 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979
Willy Tarreau638cd022010-01-03 07:42:04 +01001980 if (p1 == buf->lr) {
1981 /* LF/CRLF at beginning of line => end of trailers at p2.
1982 * Everything was scheduled for forwarding, there's nothing
1983 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001984 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001985 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001986 msg->msg_state = HTTP_MSG_DONE;
1987 return 1;
1988 }
1989 /* OK, next line then */
1990 buf->lr = p2;
1991 }
1992}
1993
1994/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1995 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1996 * ->som, buf->lr in order to include this part into the next forwarding phase.
1997 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1998 * not enough data are available, the function does not change anything and
1999 * returns zero. If a parse error is encountered, the function returns < 0 and
2000 * does not change anything. Note: this function is designed to parse wrapped
2001 * CRLF at the end of the buffer.
2002 */
2003int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2004{
2005 char *ptr;
2006 int bytes;
2007
2008 /* NB: we'll check data availabilty at the end. It's not a
2009 * problem because whatever we match first will be checked
2010 * against the correct length.
2011 */
2012 bytes = 1;
2013 ptr = buf->lr;
2014 if (*ptr == '\r') {
2015 bytes++;
2016 ptr++;
2017 if (ptr >= buf->data + buf->size)
2018 ptr = buf->data;
2019 }
2020
2021 if (buf->l < bytes)
2022 return 0;
2023
2024 if (*ptr != '\n')
2025 return -1;
2026
2027 ptr++;
2028 if (ptr >= buf->data + buf->size)
2029 ptr = buf->data;
2030 buf->lr = ptr;
2031 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2032 msg->sov = ptr - buf->data;
2033 msg->som = msg->sov - bytes;
2034 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2035 return 1;
2036}
Willy Tarreau5b154472009-12-21 20:11:07 +01002037
Willy Tarreau83e3af02009-12-28 17:39:57 +01002038void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2039{
2040 char *end = buf->data + buf->size;
2041 int off = buf->data + buf->size - buf->w;
2042
2043 /* two possible cases :
2044 * - the buffer is in one contiguous block, we move it in-place
2045 * - the buffer is in two blocks, we move it via the trash
2046 */
2047 if (buf->l) {
2048 int block1 = buf->l;
2049 int block2 = 0;
2050 if (buf->r <= buf->w) {
2051 /* non-contiguous block */
2052 block1 = buf->data + buf->size - buf->w;
2053 block2 = buf->r - buf->data;
2054 }
2055 if (block2)
2056 memcpy(trash, buf->data, block2);
2057 memmove(buf->data, buf->w, block1);
2058 if (block2)
2059 memcpy(buf->data + block1, trash, block2);
2060 }
2061
2062 /* adjust all known pointers */
2063 buf->w = buf->data;
2064 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2065 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2066 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2067 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2068
2069 /* adjust relative pointers */
2070 msg->som = 0;
2071 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2072 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2073 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2074
2075 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2076 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2077
2078 if (msg->err_pos >= 0) {
2079 msg->err_pos += off;
2080 if (msg->err_pos >= buf->size)
2081 msg->err_pos -= buf->size;
2082 }
2083
2084 buf->flags &= ~BF_FULL;
2085 if (buf->l >= buffer_max_len(buf))
2086 buf->flags |= BF_FULL;
2087}
2088
Willy Tarreaud787e662009-07-07 10:14:51 +02002089/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2090 * processing can continue on next analysers, or zero if it either needs more
2091 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2092 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2093 * when it has nothing left to do, and may remove any analyser when it wants to
2094 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002095 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002096int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002097{
Willy Tarreau59234e92008-11-30 23:51:27 +01002098 /*
2099 * We will parse the partial (or complete) lines.
2100 * We will check the request syntax, and also join multi-line
2101 * headers. An index of all the lines will be elaborated while
2102 * parsing.
2103 *
2104 * For the parsing, we use a 28 states FSM.
2105 *
2106 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002107 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002108 * req->data + msg->eoh = end of processed headers / start of current one
2109 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 * req->lr = first non-visited byte
2111 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002112 *
2113 * At end of parsing, we may perform a capture of the error (if any), and
2114 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2115 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2116 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002118
Willy Tarreau59234e92008-11-30 23:51:27 +01002119 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002120 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002121 struct http_txn *txn = &s->txn;
2122 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002123 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002124
Willy Tarreau6bf17362009-02-24 10:48:35 +01002125 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2126 now_ms, __FUNCTION__,
2127 s,
2128 req,
2129 req->rex, req->wex,
2130 req->flags,
2131 req->l,
2132 req->analysers);
2133
Willy Tarreau52a0c602009-08-16 22:45:38 +02002134 /* we're speaking HTTP here, so let's speak HTTP to the client */
2135 s->srv_error = http_return_srv_error;
2136
Willy Tarreau83e3af02009-12-28 17:39:57 +01002137 /* There's a protected area at the end of the buffer for rewriting
2138 * purposes. We don't want to start to parse the request if the
2139 * protected area is affected, because we may have to move processed
2140 * data later, which is much more complicated.
2141 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002142 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
2143 if (unlikely((req->flags & BF_FULL) ||
2144 req->r < req->lr ||
2145 req->r > req->data + req->size - global.tune.maxrewrite)) {
2146 if (req->send_max) {
2147 /* some data has still not left the buffer, wake us once that's done */
2148 buffer_dont_connect(req);
2149 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2150 return 0;
2151 }
2152 if (req->l <= req->size - global.tune.maxrewrite)
2153 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002154 }
2155
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002156 if (likely(req->lr < req->r))
2157 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002158 }
2159
Willy Tarreau59234e92008-11-30 23:51:27 +01002160 /* 1: we might have to print this header in debug mode */
2161 if (unlikely((global.mode & MODE_DEBUG) &&
2162 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002163 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002164 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 sol = req->data + msg->som;
2167 eol = sol + msg->sl.rq.l;
2168 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002169
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 sol += hdr_idx_first_pos(&txn->hdr_idx);
2171 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002172
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 while (cur_idx) {
2174 eol = sol + txn->hdr_idx.v[cur_idx].len;
2175 debug_hdr("clihdr", s, sol, eol);
2176 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2177 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002178 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002179 }
2180
Willy Tarreau58f10d72006-12-04 02:26:12 +01002181
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 /*
2183 * Now we quickly check if we have found a full valid request.
2184 * If not so, we check the FD and buffer states before leaving.
2185 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002186 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 * requests are checked first.
2188 *
2189 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002190
Willy Tarreau655dce92009-11-08 13:10:58 +01002191 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002192 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002193 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002194 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2196 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 /* 1: Since we are in header mode, if there's no space
2199 * left for headers, we won't be able to free more
2200 * later, so the session will never terminate. We
2201 * must terminate it now.
2202 */
2203 if (unlikely(req->flags & BF_FULL)) {
2204 /* FIXME: check if URI is set and return Status
2205 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002206 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002207 goto return_bad_req;
2208 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002209
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 /* 2: have we encountered a read error ? */
2211 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002212 if (txn->flags & TX_NOT_FIRST)
2213 goto failed_keep_alive;
2214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002216 if (msg->err_pos >= 0)
2217 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002218 msg->msg_state = HTTP_MSG_ERROR;
2219 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002220
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002221 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002222 if (s->listener->counters)
2223 s->listener->counters->failed_req++;
2224
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 if (!(s->flags & SN_ERR_MASK))
2226 s->flags |= SN_ERR_CLICL;
2227 if (!(s->flags & SN_FINST_MASK))
2228 s->flags |= SN_FINST_R;
2229 return 0;
2230 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002231
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 /* 3: has the read timeout expired ? */
2233 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002234 if (txn->flags & TX_NOT_FIRST)
2235 goto failed_keep_alive;
2236
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002238 if (msg->err_pos >= 0)
2239 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002240 txn->status = 408;
2241 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2242 msg->msg_state = HTTP_MSG_ERROR;
2243 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002244
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002245 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002246 if (s->listener->counters)
2247 s->listener->counters->failed_req++;
2248
Willy Tarreau59234e92008-11-30 23:51:27 +01002249 if (!(s->flags & SN_ERR_MASK))
2250 s->flags |= SN_ERR_CLITO;
2251 if (!(s->flags & SN_FINST_MASK))
2252 s->flags |= SN_FINST_R;
2253 return 0;
2254 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002255
Willy Tarreau59234e92008-11-30 23:51:27 +01002256 /* 4: have we encountered a close ? */
2257 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002258 if (txn->flags & TX_NOT_FIRST)
2259 goto failed_keep_alive;
2260
Willy Tarreau4076a152009-04-02 15:18:36 +02002261 if (msg->err_pos >= 0)
2262 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 txn->status = 400;
2264 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2265 msg->msg_state = HTTP_MSG_ERROR;
2266 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002267
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002268 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002269 if (s->listener->counters)
2270 s->listener->counters->failed_req++;
2271
Willy Tarreau59234e92008-11-30 23:51:27 +01002272 if (!(s->flags & SN_ERR_MASK))
2273 s->flags |= SN_ERR_CLICL;
2274 if (!(s->flags & SN_FINST_MASK))
2275 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002276 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002277 }
2278
Willy Tarreau520d95e2009-09-19 21:04:57 +02002279 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002280 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002281 req->flags &= ~BF_DONT_READ;
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002282
Willy Tarreau59234e92008-11-30 23:51:27 +01002283 /* just set the request timeout once at the beginning of the request */
2284 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002285 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002286
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 /* we're not ready yet */
2288 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002289
2290 failed_keep_alive:
2291 /* Here we process low-level errors for keep-alive requests. In
2292 * short, if the request is not the first one and it experiences
2293 * a timeout, read error or shutdown, we just silently close so
2294 * that the client can try again.
2295 */
2296 txn->status = 0;
2297 msg->msg_state = HTTP_MSG_RQBEFORE;
2298 req->analysers = 0;
2299 s->logs.logwait = 0;
2300 stream_int_cond_close(req->prod, NULL);
2301 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002302 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002303
Willy Tarreaud787e662009-07-07 10:14:51 +02002304 /* OK now we have a complete HTTP request with indexed headers. Let's
2305 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002306 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2307 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2308 * points to the CRLF of the request line. req->lr points to the first
2309 * byte after the last LF. msg->col and msg->sov point to the first
2310 * byte of data. msg->eol cannot be trusted because it may have been
2311 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002312 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002313
Willy Tarreaud787e662009-07-07 10:14:51 +02002314 /* Maybe we found in invalid header name while we were configured not
2315 * to block on that, so we have to capture it now.
2316 */
2317 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002318 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2319
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 /* ensure we keep this pointer to the beginning of the message */
2321 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 /*
2324 * 1: identify the method
2325 */
2326 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2327
2328 /* we can make use of server redirect on GET and HEAD */
2329 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2330 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /*
2333 * 2: check if the URI matches the monitor_uri.
2334 * We have to do this for every request which gets in, because
2335 * the monitor-uri is defined by the frontend.
2336 */
2337 if (unlikely((s->fe->monitor_uri_len != 0) &&
2338 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2339 !memcmp(&req->data[msg->sl.rq.u],
2340 s->fe->monitor_uri,
2341 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002344 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002348
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002350 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2351 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 ret = acl_pass(ret);
2354 if (cond->pol == ACL_COND_UNLESS)
2355 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 if (ret) {
2358 /* we fail this request, let's return 503 service unavail */
2359 txn->status = 503;
2360 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2361 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002362 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002363 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 /* nothing to fail, let's reply normaly */
2366 txn->status = 200;
2367 stream_int_retnclose(req->prod, &http_200_chunk);
2368 goto return_prx_cond;
2369 }
2370
2371 /*
2372 * 3: Maybe we have to copy the original REQURI for the logs ?
2373 * Note: we cannot log anymore if the request has been
2374 * classified as invalid.
2375 */
2376 if (unlikely(s->logs.logwait & LW_REQ)) {
2377 /* we have a complete HTTP request that we must log */
2378 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2379 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (urilen >= REQURI_LEN)
2382 urilen = REQURI_LEN - 1;
2383 memcpy(txn->uri, &req->data[msg->som], urilen);
2384 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002385
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 if (!(s->logs.logwait &= ~LW_REQ))
2387 s->do_log(s);
2388 } else {
2389 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002390 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 }
Willy Tarreau06619262006-12-17 08:37:22 +01002392
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002394 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2395 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002396
Willy Tarreau5b154472009-12-21 20:11:07 +01002397 /* ... and check if the request is HTTP/1.1 or above */
2398 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002399 ((req->data[msg->sl.rq.v + 5] > '1') ||
2400 ((req->data[msg->sl.rq.v + 5] == '1') &&
2401 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002402 txn->flags |= TX_REQ_VER_11;
2403
2404 /* "connection" has not been parsed yet */
2405 txn->flags &= ~TX_CON_HDR_PARS;
2406
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002407 /* transfer length unknown*/
2408 txn->flags &= ~TX_REQ_XFER_LEN;
2409
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 /* 5: we may need to capture headers */
2411 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2412 capture_headers(req->data + msg->som, &txn->hdr_idx,
2413 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002414
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002415 /* 6: determine the transfer-length.
2416 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2417 * the presence of a message-body in a REQUEST and its transfer length
2418 * must be determined that way (in order of precedence) :
2419 * 1. The presence of a message-body in a request is signaled by the
2420 * inclusion of a Content-Length or Transfer-Encoding header field
2421 * in the request's header fields. When a request message contains
2422 * both a message-body of non-zero length and a method that does
2423 * not define any semantics for that request message-body, then an
2424 * origin server SHOULD either ignore the message-body or respond
2425 * with an appropriate error message (e.g., 413). A proxy or
2426 * gateway, when presented the same request, SHOULD either forward
2427 * the request inbound with the message- body or ignore the
2428 * message-body when determining a response.
2429 *
2430 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2431 * and the "chunked" transfer-coding (Section 6.2) is used, the
2432 * transfer-length is defined by the use of this transfer-coding.
2433 * If a Transfer-Encoding header field is present and the "chunked"
2434 * transfer-coding is not present, the transfer-length is defined
2435 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002436 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002437 * 3. If a Content-Length header field is present, its decimal value in
2438 * OCTETs represents both the entity-length and the transfer-length.
2439 * If a message is received with both a Transfer-Encoding header
2440 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002441 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 * 4. By the server closing the connection. (Closing the connection
2443 * cannot be used to indicate the end of a request body, since that
2444 * would leave no possibility for the server to send back a response.)
2445 *
2446 * Whenever a transfer-coding is applied to a message-body, the set of
2447 * transfer-codings MUST include "chunked", unless the message indicates
2448 * it is terminated by closing the connection. When the "chunked"
2449 * transfer-coding is used, it MUST be the last transfer-coding applied
2450 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002451 */
2452
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002453 /* CONNECT sets a tunnel and ignores everything else */
2454 if (txn->meth == HTTP_METH_CONNECT)
2455 goto skip_xfer_len;
2456
2457 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002458 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002459 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002460 while ((txn->flags & TX_REQ_VER_11) &&
2461 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002462 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2463 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2464 else if (txn->flags & TX_REQ_TE_CHNK) {
2465 /* bad transfer-encoding (chunked followed by something else) */
2466 use_close_only = 1;
2467 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2468 break;
2469 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002470 }
2471
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002473 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2475 signed long long cl;
2476
2477 if (!ctx.vlen)
2478 goto return_bad_req;
2479
2480 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2481 goto return_bad_req; /* parse failure */
2482
2483 if (cl < 0)
2484 goto return_bad_req;
2485
2486 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2487 goto return_bad_req; /* already specified, was different */
2488
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002489 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002490 msg->hdr_content_len = cl;
2491 }
2492
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002493 /* bodyless requests have a known length */
2494 if (!use_close_only)
2495 txn->flags |= TX_REQ_XFER_LEN;
2496
2497 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002498 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002499 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002500 req->analyse_exp = TICK_ETERNITY;
2501 return 1;
2502
2503 return_bad_req:
2504 /* We centralize bad requests processing here */
2505 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2506 /* we detected a parsing error. We want to archive this request
2507 * in the dedicated proxy area for later troubleshooting.
2508 */
2509 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2510 }
2511
2512 txn->req.msg_state = HTTP_MSG_ERROR;
2513 txn->status = 400;
2514 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002515
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002516 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002517 if (s->listener->counters)
2518 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002519
2520 return_prx_cond:
2521 if (!(s->flags & SN_ERR_MASK))
2522 s->flags |= SN_ERR_PRXCOND;
2523 if (!(s->flags & SN_FINST_MASK))
2524 s->flags |= SN_FINST_R;
2525
2526 req->analysers = 0;
2527 req->analyse_exp = TICK_ETERNITY;
2528 return 0;
2529}
2530
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002531/* This stream analyser runs all HTTP request processing which is common to
2532 * frontends and backends, which means blocking ACLs, filters, connection-close,
2533 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002534 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002535 * either needs more data or wants to immediately abort the request (eg: deny,
2536 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002538int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002539{
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 struct http_txn *txn = &s->txn;
2541 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002542 struct acl_cond *cond;
2543 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002544 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002545 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002546
Willy Tarreau655dce92009-11-08 13:10:58 +01002547 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002548 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002549 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002550 return 0;
2551 }
2552
Willy Tarreau3a816292009-07-07 10:55:49 +02002553 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002554 req->analyse_exp = TICK_ETERNITY;
2555
2556 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2557 now_ms, __FUNCTION__,
2558 s,
2559 req,
2560 req->rex, req->wex,
2561 req->flags,
2562 req->l,
2563 req->analysers);
2564
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002565 /* first check whether we have some ACLs set to block this request */
2566 list_for_each_entry(cond, &px->block_cond, list) {
2567 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002568
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002569 ret = acl_pass(ret);
2570 if (cond->pol == ACL_COND_UNLESS)
2571 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002572
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002573 if (ret) {
2574 txn->status = 403;
2575 /* let's log the request time */
2576 s->logs.tv_request = now;
2577 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2578 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002580 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002581
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002582 /* try headers filters */
2583 if (px->req_exp != NULL) {
2584 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2585 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002586
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 /* has the request been denied ? */
2588 if (txn->flags & TX_CLDENY) {
2589 /* no need to go further */
2590 txn->status = 403;
2591 /* let's log the request time */
2592 s->logs.tv_request = now;
2593 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2594 goto return_prx_cond;
2595 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002596
2597 /* When a connection is tarpitted, we use the tarpit timeout,
2598 * which may be the same as the connect timeout if unspecified.
2599 * If unset, then set it to zero because we really want it to
2600 * eventually expire. We build the tarpit as an analyser.
2601 */
2602 if (txn->flags & TX_CLTARPIT) {
2603 buffer_erase(s->req);
2604 /* wipe the request out so that we can drop the connection early
2605 * if the client closes first.
2606 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002607 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002608 req->analysers = 0; /* remove switching rules etc... */
2609 req->analysers |= AN_REQ_HTTP_TARPIT;
2610 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2611 if (!req->analyse_exp)
2612 req->analyse_exp = tick_add(now_ms, 0);
2613 return 1;
2614 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002615 }
Willy Tarreau06619262006-12-17 08:37:22 +01002616
Willy Tarreau5b154472009-12-21 20:11:07 +01002617 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2618 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002619 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002620
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002621 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622 ((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 +01002623 int tmp = TX_CON_WANT_TUN;
2624 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2625 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002626 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2627 tmp = TX_CON_WANT_SCL;
Willy Tarreau5b154472009-12-21 20:11:07 +01002628 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2629 tmp = TX_CON_WANT_CLO;
2630
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002631 if (!(txn->flags & TX_REQ_XFER_LEN))
2632 tmp = TX_CON_WANT_CLO;
2633
Willy Tarreau5b154472009-12-21 20:11:07 +01002634 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002635 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002636
2637 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2638 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2639 }
2640
2641 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2642 * once we know the backend, because the tunnel mode can be implied by the
2643 * lack of any close/keepalive options in both the FE and the BE. Since
2644 * this information can evolve with time, we proceed by trying to make the
2645 * header status match the desired status. For this, we'll have to adjust
2646 * the "Connection" header. The test for persistent connections has already
2647 * been performed, so we only enter here if there is a risk the connection
2648 * is considered as persistent and we want it to be closed on the server
2649 * side. It would be nice if we could enter this place only when a
2650 * Connection header exists. Note that a CONNECT method will not enter
2651 * here.
2652 */
2653 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002654 char *cur_ptr, *cur_end, *cur_next;
2655 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002656 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002657 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002658
Willy Tarreau5b154472009-12-21 20:11:07 +01002659 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002660 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002661
Willy Tarreau5b154472009-12-21 20:11:07 +01002662 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002663 cur_hdr = &txn->hdr_idx.v[cur_idx];
2664 cur_ptr = cur_next;
2665 cur_end = cur_ptr + cur_hdr->len;
2666 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002667
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002668 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002669 if (!val)
2670 continue;
2671
2672 /* 3 possibilities :
2673 * - we have already set "Connection: close" or we're in
2674 * HTTP/1.0, so we remove this line.
2675 * - we have not yet set "Connection: close", but this line
2676 * indicates close. We leave it untouched and set the flag.
2677 * - we have not yet set "Connection: close", and this line
2678 * indicates non-close. We replace it and set the flag.
2679 */
2680 if (must_delete) {
2681 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2682 http_msg_move_end(&txn->req, delta);
2683 cur_next += delta;
2684 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2685 txn->hdr_idx.used--;
2686 cur_hdr->len = 0;
2687 txn->flags |= TX_REQ_CONN_CLO;
2688 } else {
2689 if (cur_end - cur_ptr - val != 5 ||
2690 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2691 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2692 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002693 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002694 cur_hdr->len += delta;
2695 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002696 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002697 txn->flags |= TX_REQ_CONN_CLO;
2698 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002699 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002700 } /* for loop */
2701 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002702
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002703 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002704 list_for_each_entry(wl, &px->req_add, list) {
2705 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706 goto return_bad_req;
2707 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002708
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002709 /* check if stats URI was requested, and if an auth is needed */
2710 if (px->uri_auth != NULL &&
2711 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2712 /* we have to check the URI and auth for this request.
2713 * FIXME!!! that one is rather dangerous, we want to
2714 * make it follow standard rules (eg: clear req->analysers).
2715 */
2716 if (stats_check_uri_auth(s, px)) {
2717 req->analysers = 0;
2718 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002719 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002720 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002721
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722 /* check whether we have some ACLs set to redirect this request */
2723 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002724 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002725
Willy Tarreauf285f542010-01-03 20:03:03 +01002726 if (rule->cond) {
2727 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2728 ret = acl_pass(ret);
2729 if (rule->cond->pol == ACL_COND_UNLESS)
2730 ret = !ret;
2731 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002732
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002734 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002735 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002736
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002737 /* build redirect message */
2738 switch(rule->code) {
2739 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002740 msg_fmt = HTTP_303;
2741 break;
2742 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002743 msg_fmt = HTTP_301;
2744 break;
2745 case 302:
2746 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 msg_fmt = HTTP_302;
2748 break;
2749 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002750
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002751 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002753
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 switch(rule->type) {
2755 case REDIRECT_TYPE_PREFIX: {
2756 const char *path;
2757 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002758
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002759 path = http_get_path(txn);
2760 /* build message using path */
2761 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002762 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 +02002763 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2764 int qs = 0;
2765 while (qs < pathlen) {
2766 if (path[qs] == '?') {
2767 pathlen = qs;
2768 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002769 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002771 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002772 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 } else {
2774 path = "/";
2775 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002776 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002777
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002778 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002779 goto return_bad_req;
2780
2781 /* add prefix. Note that if prefix == "/", we don't want to
2782 * add anything, otherwise it makes it hard for the user to
2783 * configure a self-redirection.
2784 */
2785 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002786 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2787 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002788 }
2789
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002790 /* add path */
2791 memcpy(rdr.str + rdr.len, path, pathlen);
2792 rdr.len += pathlen;
2793 break;
2794 }
2795 case REDIRECT_TYPE_LOCATION:
2796 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002797 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002799
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002800 /* add location */
2801 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2802 rdr.len += rule->rdr_len;
2803 break;
2804 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002805
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002806 if (rule->cookie_len) {
2807 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2808 rdr.len += 14;
2809 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2810 rdr.len += rule->cookie_len;
2811 memcpy(rdr.str + rdr.len, "\r\n", 2);
2812 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002813 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002814
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002815 /* add end of headers and the keep-alive/close status.
2816 * We may choose to set keep-alive if the Location begins
2817 * with a slash, because the client will come back to the
2818 * same server.
2819 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820 txn->status = rule->code;
2821 /* let's log the request time */
2822 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002823
2824 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2825 (txn->flags & TX_REQ_XFER_LEN) &&
2826 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2827 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2828 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2829 /* keep-alive possible */
2830 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2831 rdr.len += 28;
2832 buffer_write(req->prod->ob, rdr.str, rdr.len);
2833 /* "eat" the request */
2834 buffer_ignore(req, msg->sov - msg->som);
2835 msg->som = msg->sov;
2836 req->analysers = AN_REQ_HTTP_XFER_BODY;
2837 txn->req.msg_state = HTTP_MSG_DONE;
2838 txn->rsp.msg_state = HTTP_MSG_CLOSED;
2839 break;
2840 } else {
2841 /* keep-alive not possible */
2842 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2843 rdr.len += 23;
2844 stream_int_cond_close(req->prod, &rdr);
2845 goto return_prx_cond;
2846 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002847 }
2848 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002849
Willy Tarreaud98cf932009-12-27 22:54:55 +01002850 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2851 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002852 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002853 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002854 req->flags |= BF_DONT_READ;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002855 else
2856 req->flags &= ~BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002857
Willy Tarreau2be39392010-01-03 17:24:51 +01002858 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2859 * If this happens, then the data will not come immediately, so we must
2860 * send all what we have without waiting. Note that due to the small gain
2861 * in waiting for the body of the request, it's easier to simply put the
2862 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2863 * itself once used.
2864 */
2865 req->flags |= BF_SEND_DONTWAIT;
2866
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002867 /* that's OK for us now, let's move on to next analysers */
2868 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002869
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002870 return_bad_req:
2871 /* We centralize bad requests processing here */
2872 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2873 /* we detected a parsing error. We want to archive this request
2874 * in the dedicated proxy area for later troubleshooting.
2875 */
2876 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2877 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002878
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002879 txn->req.msg_state = HTTP_MSG_ERROR;
2880 txn->status = 400;
2881 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002882
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002883 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002884 if (s->listener->counters)
2885 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002886
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002887 return_prx_cond:
2888 if (!(s->flags & SN_ERR_MASK))
2889 s->flags |= SN_ERR_PRXCOND;
2890 if (!(s->flags & SN_FINST_MASK))
2891 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002892
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 req->analysers = 0;
2894 req->analyse_exp = TICK_ETERNITY;
2895 return 0;
2896}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002897
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002898/* This function performs all the processing enabled for the current request.
2899 * It returns 1 if the processing can continue on next analysers, or zero if it
2900 * needs more data, encounters an error, or wants to immediately abort the
2901 * request. It relies on buffers flags, and updates s->req->analysers.
2902 */
2903int http_process_request(struct session *s, struct buffer *req, int an_bit)
2904{
2905 struct http_txn *txn = &s->txn;
2906 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002907
Willy Tarreau655dce92009-11-08 13:10:58 +01002908 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002909 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002910 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002911 return 0;
2912 }
2913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002914 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2915 now_ms, __FUNCTION__,
2916 s,
2917 req,
2918 req->rex, req->wex,
2919 req->flags,
2920 req->l,
2921 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002922
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 /*
2924 * Right now, we know that we have processed the entire headers
2925 * and that unwanted requests have been filtered out. We can do
2926 * whatever we want with the remaining request. Also, now we
2927 * may have separate values for ->fe, ->be.
2928 */
Willy Tarreau06619262006-12-17 08:37:22 +01002929
Willy Tarreau59234e92008-11-30 23:51:27 +01002930 /*
2931 * If HTTP PROXY is set we simply get remote server address
2932 * parsing incoming request.
2933 */
2934 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2935 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2936 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002937
Willy Tarreau59234e92008-11-30 23:51:27 +01002938 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002939 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002940 * Note that doing so might move headers in the request, but
2941 * the fields will stay coherent and the URI will not move.
2942 * This should only be performed in the backend.
2943 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002944 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2946 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002947
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002949 * 8: the appsession cookie was looked up very early in 1.2,
2950 * so let's do the same now.
2951 */
2952
2953 /* It needs to look into the URI */
2954 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002955 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002956 }
2957
2958 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 * 9: add X-Forwarded-For if either the frontend or the backend
2960 * asks for it.
2961 */
2962 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2963 if (s->cli_addr.ss_family == AF_INET) {
2964 /* Add an X-Forwarded-For header unless the source IP is
2965 * in the 'except' network range.
2966 */
2967 if ((!s->fe->except_mask.s_addr ||
2968 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2969 != s->fe->except_net.s_addr) &&
2970 (!s->be->except_mask.s_addr ||
2971 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2972 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002973 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002974 unsigned char *pn;
2975 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002976
2977 /* Note: we rely on the backend to get the header name to be used for
2978 * x-forwarded-for, because the header is really meant for the backends.
2979 * However, if the backend did not specify any option, we have to rely
2980 * on the frontend's header name.
2981 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002982 if (s->be->fwdfor_hdr_len) {
2983 len = s->be->fwdfor_hdr_len;
2984 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002985 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002986 len = s->fe->fwdfor_hdr_len;
2987 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002988 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002990
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002991 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002992 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002993 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002994 }
2995 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 else if (s->cli_addr.ss_family == AF_INET6) {
2997 /* FIXME: for the sake of completeness, we should also support
2998 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002999 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003000 int len;
3001 char pn[INET6_ADDRSTRLEN];
3002 inet_ntop(AF_INET6,
3003 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3004 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003005
Willy Tarreau59234e92008-11-30 23:51:27 +01003006 /* Note: we rely on the backend to get the header name to be used for
3007 * x-forwarded-for, because the header is really meant for the backends.
3008 * However, if the backend did not specify any option, we have to rely
3009 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003010 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003011 if (s->be->fwdfor_hdr_len) {
3012 len = s->be->fwdfor_hdr_len;
3013 memcpy(trash, s->be->fwdfor_hdr_name, len);
3014 } else {
3015 len = s->fe->fwdfor_hdr_len;
3016 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003017 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003018 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003019
Willy Tarreau59234e92008-11-30 23:51:27 +01003020 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003021 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003022 goto return_bad_req;
3023 }
3024 }
3025
3026 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003027 * 10: add X-Original-To if either the frontend or the backend
3028 * asks for it.
3029 */
3030 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3031
3032 /* FIXME: don't know if IPv6 can handle that case too. */
3033 if (s->cli_addr.ss_family == AF_INET) {
3034 /* Add an X-Original-To header unless the destination IP is
3035 * in the 'except' network range.
3036 */
3037 if (!(s->flags & SN_FRT_ADDR_SET))
3038 get_frt_addr(s);
3039
3040 if ((!s->fe->except_mask_to.s_addr ||
3041 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3042 != s->fe->except_to.s_addr) &&
3043 (!s->be->except_mask_to.s_addr ||
3044 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3045 != s->be->except_to.s_addr)) {
3046 int len;
3047 unsigned char *pn;
3048 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3049
3050 /* Note: we rely on the backend to get the header name to be used for
3051 * x-original-to, because the header is really meant for the backends.
3052 * However, if the backend did not specify any option, we have to rely
3053 * on the frontend's header name.
3054 */
3055 if (s->be->orgto_hdr_len) {
3056 len = s->be->orgto_hdr_len;
3057 memcpy(trash, s->be->orgto_hdr_name, len);
3058 } else {
3059 len = s->fe->orgto_hdr_len;
3060 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003061 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003062 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3063
3064 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003065 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003066 goto return_bad_req;
3067 }
3068 }
3069 }
3070
Willy Tarreau78599912009-10-17 20:12:21 +02003071 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01003072 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02003073 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003074 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003075 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003076 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003077 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003078
3079 /* If we have no server assigned yet and we're balancing on url_param
3080 * with a POST request, we may be interested in checking the body for
3081 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003082 */
3083 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3084 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003085 s->be->url_param_post_limit != 0 &&
3086 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003087 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003088 buffer_dont_connect(req);
3089 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003090 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003091
Willy Tarreaud98cf932009-12-27 22:54:55 +01003092 if (txn->flags & TX_REQ_XFER_LEN)
3093 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003094
Willy Tarreau59234e92008-11-30 23:51:27 +01003095 /*************************************************************
3096 * OK, that's finished for the headers. We have done what we *
3097 * could. Let's switch to the DATA state. *
3098 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003099 req->analyse_exp = TICK_ETERNITY;
3100 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003101
Willy Tarreau59234e92008-11-30 23:51:27 +01003102 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003103 /* OK let's go on with the BODY now */
3104 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003105
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003107 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003108 /* we detected a parsing error. We want to archive this request
3109 * in the dedicated proxy area for later troubleshooting.
3110 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003111 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003112 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003113
Willy Tarreau59234e92008-11-30 23:51:27 +01003114 txn->req.msg_state = HTTP_MSG_ERROR;
3115 txn->status = 400;
3116 req->analysers = 0;
3117 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003118
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003119 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003120 if (s->listener->counters)
3121 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003122
Willy Tarreau59234e92008-11-30 23:51:27 +01003123 if (!(s->flags & SN_ERR_MASK))
3124 s->flags |= SN_ERR_PRXCOND;
3125 if (!(s->flags & SN_FINST_MASK))
3126 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003127 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003128}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003129
Willy Tarreau60b85b02008-11-30 23:28:40 +01003130/* This function is an analyser which processes the HTTP tarpit. It always
3131 * returns zero, at the beginning because it prevents any other processing
3132 * from occurring, and at the end because it terminates the request.
3133 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003134int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003135{
3136 struct http_txn *txn = &s->txn;
3137
3138 /* This connection is being tarpitted. The CLIENT side has
3139 * already set the connect expiration date to the right
3140 * timeout. We just have to check that the client is still
3141 * there and that the timeout has not expired.
3142 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003143 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003144 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3145 !tick_is_expired(req->analyse_exp, now_ms))
3146 return 0;
3147
3148 /* We will set the queue timer to the time spent, just for
3149 * logging purposes. We fake a 500 server error, so that the
3150 * attacker will not suspect his connection has been tarpitted.
3151 * It will not cause trouble to the logs because we can exclude
3152 * the tarpitted connections by filtering on the 'PT' status flags.
3153 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003154 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3155
3156 txn->status = 500;
3157 if (req->flags != BF_READ_ERROR)
3158 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3159
3160 req->analysers = 0;
3161 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003162
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003163 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003164 if (s->listener->counters)
3165 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003166
Willy Tarreau60b85b02008-11-30 23:28:40 +01003167 if (!(s->flags & SN_ERR_MASK))
3168 s->flags |= SN_ERR_PRXCOND;
3169 if (!(s->flags & SN_FINST_MASK))
3170 s->flags |= SN_FINST_T;
3171 return 0;
3172}
3173
Willy Tarreaud34af782008-11-30 23:36:37 +01003174/* This function is an analyser which processes the HTTP request body. It looks
3175 * for parameters to be used for the load balancing algorithm (url_param). It
3176 * must only be called after the standard HTTP request processing has occurred,
3177 * because it expects the request to be parsed. It returns zero if it needs to
3178 * read more data, or 1 once it has completed its analysis.
3179 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003180int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003181{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003182 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003183 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003184 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003185
3186 /* We have to parse the HTTP request body to find any required data.
3187 * "balance url_param check_post" should have been the only way to get
3188 * into this. We were brought here after HTTP header analysis, so all
3189 * related structures are ready.
3190 */
3191
Willy Tarreau522d6c02009-12-06 18:49:18 +01003192 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3193 goto missing_data;
3194
3195 if (msg->msg_state < HTTP_MSG_100_SENT) {
3196 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3197 * send an HTTP/1.1 100 Continue intermediate response.
3198 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003199 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003200 struct hdr_ctx ctx;
3201 ctx.idx = 0;
3202 /* Expect is allowed in 1.1, look for it */
3203 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3204 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3205 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3206 }
3207 }
3208 msg->msg_state = HTTP_MSG_100_SENT;
3209 }
3210
3211 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003212 /* we have msg->col and msg->sov which both point to the first
3213 * byte of message body. msg->som still points to the beginning
3214 * of the message. We must save the body in req->lr because it
3215 * survives buffer re-alignments.
3216 */
3217 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003218 if (txn->flags & TX_REQ_TE_CHNK)
3219 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3220 else
3221 msg->msg_state = HTTP_MSG_DATA;
3222 }
3223
3224 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003225 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003226 * set ->sov and ->lr to point to the body and switch to DATA or
3227 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003228 */
3229 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003230
Willy Tarreau115acb92009-12-26 13:56:06 +01003231 if (!ret)
3232 goto missing_data;
3233 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003234 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003235 }
3236
Willy Tarreaud98cf932009-12-27 22:54:55 +01003237 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003238 * We have the first non-header byte in msg->col, which is either the
3239 * beginning of the chunk size or of the data. The first data byte is in
3240 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3241 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003242 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003243
3244 if (msg->hdr_content_len < limit)
3245 limit = msg->hdr_content_len;
3246
Willy Tarreau7c96f672009-12-27 22:47:25 +01003247 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003248 goto http_end;
3249
3250 missing_data:
3251 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003252 if (req->flags & BF_FULL)
3253 goto return_bad_req;
3254
Willy Tarreau522d6c02009-12-06 18:49:18 +01003255 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3256 txn->status = 408;
3257 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3258 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003259 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003260
3261 /* we get here if we need to wait for more data */
3262 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003263 /* Not enough data. We'll re-use the http-request
3264 * timeout here. Ideally, we should set the timeout
3265 * relative to the accept() date. We just set the
3266 * request timeout once at the beginning of the
3267 * request.
3268 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003269 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003270 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003271 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003272 return 0;
3273 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003274
3275 http_end:
3276 /* The situation will not evolve, so let's give up on the analysis. */
3277 s->logs.tv_request = now; /* update the request timer to reflect full request */
3278 req->analysers &= ~an_bit;
3279 req->analyse_exp = TICK_ETERNITY;
3280 return 1;
3281
3282 return_bad_req: /* let's centralize all bad requests */
3283 txn->req.msg_state = HTTP_MSG_ERROR;
3284 txn->status = 400;
3285 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3286
3287 return_err_msg:
3288 req->analysers = 0;
3289 s->fe->counters.failed_req++;
3290 if (s->listener->counters)
3291 s->listener->counters->failed_req++;
3292
3293 if (!(s->flags & SN_ERR_MASK))
3294 s->flags |= SN_ERR_PRXCOND;
3295 if (!(s->flags & SN_FINST_MASK))
3296 s->flags |= SN_FINST_R;
3297 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003298}
3299
Willy Tarreau610ecce2010-01-04 21:15:02 +01003300/* Terminate current transaction and prepare a new one. This is very tricky
3301 * right now but it works.
3302 */
3303void http_end_txn_clean_session(struct session *s)
3304{
3305 /* FIXME: We need a more portable way of releasing a backend's and a
3306 * server's connections. We need a safer way to reinitialize buffer
3307 * flags. We also need a more accurate method for computing per-request
3308 * data.
3309 */
3310 http_silent_debug(__LINE__, s);
3311
3312 s->req->cons->flags |= SI_FL_NOLINGER;
3313 s->req->cons->shutr(s->req->cons);
3314 s->req->cons->shutw(s->req->cons);
3315
3316 http_silent_debug(__LINE__, s);
3317
3318 if (s->flags & SN_BE_ASSIGNED)
3319 s->be->beconn--;
3320
3321 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3322 session_process_counters(s);
3323
3324 if (s->txn.status) {
3325 int n;
3326
3327 n = s->txn.status / 100;
3328 if (n < 1 || n > 5)
3329 n = 0;
3330
3331 if (s->fe->mode == PR_MODE_HTTP)
3332 s->fe->counters.p.http.rsp[n]++;
3333
3334 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3335 (s->be->mode == PR_MODE_HTTP))
3336 s->be->counters.p.http.rsp[n]++;
3337 }
3338
3339 /* don't count other requests' data */
3340 s->logs.bytes_in -= s->req->l - s->req->send_max;
3341 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3342
3343 /* let's do a final log if we need it */
3344 if (s->logs.logwait &&
3345 !(s->flags & SN_MONITOR) &&
3346 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3347 s->do_log(s);
3348 }
3349
3350 s->logs.accept_date = date; /* user-visible date for logging */
3351 s->logs.tv_accept = now; /* corrected date for internal use */
3352 tv_zero(&s->logs.tv_request);
3353 s->logs.t_queue = -1;
3354 s->logs.t_connect = -1;
3355 s->logs.t_data = -1;
3356 s->logs.t_close = 0;
3357 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3358 s->logs.srv_queue_size = 0; /* we will get this number soon */
3359
3360 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3361 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3362
3363 if (s->pend_pos)
3364 pendconn_free(s->pend_pos);
3365
3366 if (s->srv) {
3367 if (s->flags & SN_CURR_SESS) {
3368 s->flags &= ~SN_CURR_SESS;
3369 s->srv->cur_sess--;
3370 }
3371 if (may_dequeue_tasks(s->srv, s->be))
3372 process_srv_queue(s->srv);
3373 }
3374
3375 if (unlikely(s->srv_conn))
3376 sess_change_server(s, NULL);
3377 s->srv = NULL;
3378
3379 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3380 s->req->cons->fd = -1; /* just to help with debugging */
3381 s->req->cons->err_type = SI_ET_NONE;
3382 s->req->cons->err_loc = NULL;
3383 s->req->cons->exp = TICK_ETERNITY;
3384 s->req->cons->flags = SI_FL_NONE;
3385 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE);
3386 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);
3387 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3388 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3389 s->txn.meth = 0;
3390 http_reset_txn(s);
3391 s->txn.flags |= TX_NOT_FIRST;
3392 if (s->be->options2 & PR_O2_INDEPSTR)
3393 s->req->cons->flags |= SI_FL_INDEP_STR;
3394
3395 /* if the request buffer is not empty, it means we're
3396 * about to process another request, so send pending
3397 * data with MSG_MORE to merge TCP packets when possible.
3398 * Also, let's not start reading a small request packet,
3399 * we may prefer to read a larger one later.
3400 */
3401 s->req->flags &= ~BF_DONT_READ;
3402 if (s->req->l > s->req->send_max) {
3403 s->rep->flags |= BF_EXPECT_MORE;
3404 s->req->flags |= BF_DONT_READ;
3405 }
3406
3407 /* make ->lr point to the first non-forwarded byte */
3408 s->req->lr = s->req->w + s->req->send_max;
3409 if (s->req->lr >= s->req->data + s->req->size)
3410 s->req->lr -= s->req->size;
3411 s->rep->lr = s->rep->w + s->rep->send_max;
3412 if (s->rep->lr >= s->rep->data + s->rep->size)
3413 s->rep->lr -= s->req->size;
3414
3415 s->req->analysers |= s->fe->fe_req_ana;
3416 s->rep->analysers = 0;
3417
3418 http_silent_debug(__LINE__, s);
3419}
3420
3421
3422/* This function updates the request state machine according to the response
3423 * state machine and buffer flags. It returns 1 if it changes anything (flag
3424 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3425 * it is only used to find when a request/response couple is complete. Both
3426 * this function and its equivalent should loop until both return zero. It
3427 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3428 */
3429int http_sync_req_state(struct session *s)
3430{
3431 struct buffer *buf = s->req;
3432 struct http_txn *txn = &s->txn;
3433 unsigned int old_flags = buf->flags;
3434 unsigned int old_state = txn->req.msg_state;
3435
3436 http_silent_debug(__LINE__, s);
3437 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3438 return 0;
3439
3440 if (txn->req.msg_state == HTTP_MSG_DONE) {
3441 /* No need to read anymore, the request was completely parsed */
3442 buf->flags |= BF_DONT_READ;
3443
3444 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3445 goto wait_other_side;
3446
3447 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3448 /* The server has not finished to respond, so we
3449 * don't want to move in order not to upset it.
3450 */
3451 goto wait_other_side;
3452 }
3453
3454 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3455 /* if any side switches to tunnel mode, the other one does too */
3456 buf->flags &= ~BF_DONT_READ;
3457 txn->req.msg_state = HTTP_MSG_TUNNEL;
3458 goto wait_other_side;
3459 }
3460
3461 /* When we get here, it means that both the request and the
3462 * response have finished receiving. Depending on the connection
3463 * mode, we'll have to wait for the last bytes to leave in either
3464 * direction, and sometimes for a close to be effective.
3465 */
3466
3467 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3468 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3469 /* Server-close mode : queue a connection close to the server */
3470 buffer_shutw_now(buf);
3471 buf->cons->flags |= SI_FL_NOLINGER;
3472 }
3473 else if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
3474 /* Option forceclose is set, let's enforce it now
3475 * that we're not expecting any new data to come.
3476 */
3477 buffer_shutr_now(buf);
3478 buffer_shutw_now(buf);
3479 buf->cons->flags |= SI_FL_NOLINGER;
3480 }
3481 /* other modes include httpclose (no action) and keepalive (not implemented) */
3482 }
3483
3484 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3485 /* if we've just closed an output, let's switch */
3486 if (!(buf->flags & BF_OUT_EMPTY)) {
3487 txn->req.msg_state = HTTP_MSG_CLOSING;
3488 goto http_msg_closing;
3489 }
3490 else {
3491 txn->req.msg_state = HTTP_MSG_CLOSED;
3492 goto http_msg_closed;
3493 }
3494 }
3495 else {
3496 /* other modes are used as a tunnel right now */
3497 buf->flags &= ~BF_DONT_READ;
3498 txn->req.msg_state = HTTP_MSG_TUNNEL;
3499 goto wait_other_side;
3500 }
3501 }
3502
3503 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3504 http_msg_closing:
3505 /* nothing else to forward, just waiting for the output buffer
3506 * to be empty and for the shutw_now to take effect.
3507 */
3508 if (buf->flags & BF_OUT_EMPTY) {
3509 txn->req.msg_state = HTTP_MSG_CLOSED;
3510 goto http_msg_closed;
3511 }
3512 else if (buf->flags & BF_SHUTW) {
3513 txn->req.msg_state = HTTP_MSG_ERROR;
3514 goto wait_other_side;
3515 }
3516 }
3517
3518 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3519 http_msg_closed:
3520 goto wait_other_side;
3521 }
3522
3523 wait_other_side:
3524 http_silent_debug(__LINE__, s);
3525 return txn->req.msg_state != old_state || buf->flags != old_flags;
3526}
3527
3528
3529/* This function updates the response state machine according to the request
3530 * state machine and buffer flags. It returns 1 if it changes anything (flag
3531 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3532 * it is only used to find when a request/response couple is complete. Both
3533 * this function and its equivalent should loop until both return zero. It
3534 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3535 */
3536int http_sync_res_state(struct session *s)
3537{
3538 struct buffer *buf = s->rep;
3539 struct http_txn *txn = &s->txn;
3540 unsigned int old_flags = buf->flags;
3541 unsigned int old_state = txn->rsp.msg_state;
3542
3543 http_silent_debug(__LINE__, s);
3544 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3545 return 0;
3546
3547 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3548 /* In theory, we don't need to read anymore, but we must
3549 * still monitor the server connection for a possible close,
3550 * so we don't set the BF_DONT_READ flag here.
3551 */
3552 /* buf->flags |= BF_DONT_READ; */
3553
3554 if (txn->req.msg_state == HTTP_MSG_ERROR)
3555 goto wait_other_side;
3556
3557 if (txn->req.msg_state < HTTP_MSG_DONE) {
3558 /* The client seems to still be sending data, probably
3559 * because we got an error response during an upload.
3560 * We have the choice of either breaking the connection
3561 * or letting it pass through. Let's do the later.
3562 */
3563 goto wait_other_side;
3564 }
3565
3566 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3567 /* if any side switches to tunnel mode, the other one does too */
3568 buf->flags &= ~BF_DONT_READ;
3569 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3570 goto wait_other_side;
3571 }
3572
3573 /* When we get here, it means that both the request and the
3574 * response have finished receiving. Depending on the connection
3575 * mode, we'll have to wait for the last bytes to leave in either
3576 * direction, and sometimes for a close to be effective.
3577 */
3578
3579 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3580 /* Server-close mode : shut read and wait for the request
3581 * side to close its output buffer. The caller will detect
3582 * when we're in DONE and the other is in CLOSED and will
3583 * catch that for the final cleanup.
3584 */
3585 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3586 buffer_shutr_now(buf);
3587 goto wait_other_side;
3588 }
3589 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3590 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3591 /* Option forceclose is set, let's enforce it now
3592 * that we're not expecting any new data to come.
3593 * The caller knows the session is complete once
3594 * both states are CLOSED.
3595 */
3596 buffer_shutr_now(buf);
3597 buffer_shutw_now(buf);
3598 buf->cons->flags |= SI_FL_NOLINGER;
3599 }
3600 else {
3601 /* other modes include httpclose (no action) and keepalive
3602 * (not implemented). These modes are used as a tunnel right
3603 * now.
3604 */
3605 buf->flags &= ~BF_DONT_READ;
3606 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3607 goto wait_other_side;
3608 }
3609
3610 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3611 /* if we've just closed an output, let's switch */
3612 if (!(buf->flags & BF_OUT_EMPTY)) {
3613 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3614 goto http_msg_closing;
3615 }
3616 else {
3617 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3618 goto http_msg_closed;
3619 }
3620 }
3621 goto wait_other_side;
3622 }
3623
3624 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3625 http_msg_closing:
3626 /* nothing else to forward, just waiting for the output buffer
3627 * to be empty and for the shutw_now to take effect.
3628 */
3629 if (buf->flags & BF_OUT_EMPTY) {
3630 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3631 goto http_msg_closed;
3632 }
3633 else if (buf->flags & BF_SHUTW) {
3634 txn->rsp.msg_state = HTTP_MSG_ERROR;
3635 goto wait_other_side;
3636 }
3637 }
3638
3639 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3640 http_msg_closed:
3641 /* drop any pending data */
3642 buffer_ignore(buf, buf->l - buf->send_max);
3643 buffer_auto_close(buf);
3644 goto wait_other_side;
3645 }
3646
3647 wait_other_side:
3648 http_silent_debug(__LINE__, s);
3649 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3650}
3651
3652
3653/* Resync the request and response state machines. Return 1 if either state
3654 * changes.
3655 */
3656int http_resync_states(struct session *s)
3657{
3658 struct http_txn *txn = &s->txn;
3659 int old_req_state = txn->req.msg_state;
3660 int old_res_state = txn->rsp.msg_state;
3661
3662 http_silent_debug(__LINE__, s);
3663 http_sync_req_state(s);
3664 while (1) {
3665 http_silent_debug(__LINE__, s);
3666 if (!http_sync_res_state(s))
3667 break;
3668 http_silent_debug(__LINE__, s);
3669 if (!http_sync_req_state(s))
3670 break;
3671 }
3672 http_silent_debug(__LINE__, s);
3673 /* OK, both state machines agree on a compatible state.
3674 * There are a few cases we're interested in :
3675 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3676 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3677 * directions, so let's simply disable both analysers.
3678 * - HTTP_MSG_CLOSED on the response only means we must abort the
3679 * request.
3680 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3681 * with server-close mode means we've completed one request and we
3682 * must re-initialize the server connection.
3683 */
3684
3685 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3686 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3687 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3688 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3689 s->req->analysers = 0;
3690 s->rep->analysers = 0;
3691 }
3692 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3693 buffer_abort(s->req);
3694 buffer_auto_close(s->req);
3695 buffer_ignore(s->req, s->req->l - s->req->send_max);
3696 s->req->analysers = 0;
3697 s->rep->analysers = 0;
3698 }
3699 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3700 txn->rsp.msg_state == HTTP_MSG_DONE &&
3701 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3702 /* server-close: terminate this server connection and
3703 * reinitialize a fresh-new transaction.
3704 */
3705 http_end_txn_clean_session(s);
3706 }
3707
3708 http_silent_debug(__LINE__, s);
3709 return txn->req.msg_state != old_req_state ||
3710 txn->rsp.msg_state != old_res_state;
3711}
3712
Willy Tarreaud98cf932009-12-27 22:54:55 +01003713/* This function is an analyser which forwards request body (including chunk
3714 * sizes if any). It is called as soon as we must forward, even if we forward
3715 * zero byte. The only situation where it must not be called is when we're in
3716 * tunnel mode and we want to forward till the close. It's used both to forward
3717 * remaining data and to resync after end of body. It expects the msg_state to
3718 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3719 * read more data, or 1 once we can go on with next request or end the session.
3720 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3721 * bytes of pending data + the headers if not already done (between som and sov).
3722 * It eventually adjusts som to match sov after the data in between have been sent.
3723 */
3724int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3725{
3726 struct http_txn *txn = &s->txn;
3727 struct http_msg *msg = &s->txn.req;
3728
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003729 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3730 return 0;
3731
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003732 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3733 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3734 /* Output closed while we were sending data. We must abort. */
3735 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003736 req->analysers &= ~an_bit;
3737 return 1;
3738 }
3739
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003740 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003741
3742 /* Note that we don't have to send 100-continue back because we don't
3743 * need the data to complete our job, and it's up to the server to
3744 * decide whether to return 100, 417 or anything else in return of
3745 * an "Expect: 100-continue" header.
3746 */
3747
3748 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3749 /* we have msg->col and msg->sov which both point to the first
3750 * byte of message body. msg->som still points to the beginning
3751 * of the message. We must save the body in req->lr because it
3752 * survives buffer re-alignments.
3753 */
3754 req->lr = req->data + msg->sov;
3755 if (txn->flags & TX_REQ_TE_CHNK)
3756 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3757 else {
3758 msg->msg_state = HTTP_MSG_DATA;
3759 }
3760 }
3761
Willy Tarreaud98cf932009-12-27 22:54:55 +01003762 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003763 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003764 /* we may have some data pending */
3765 if (msg->hdr_content_len || msg->som != msg->sov) {
3766 int bytes = msg->sov - msg->som;
3767 if (bytes < 0) /* sov may have wrapped at the end */
3768 bytes += req->size;
3769 buffer_forward(req, bytes + msg->hdr_content_len);
3770 msg->hdr_content_len = 0; /* don't forward that again */
3771 msg->som = msg->sov;
3772 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003773
Willy Tarreaucaabe412010-01-03 23:08:28 +01003774 if (msg->msg_state == HTTP_MSG_DATA) {
3775 /* must still forward */
3776 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003777 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003778
3779 /* nothing left to forward */
3780 if (txn->flags & TX_REQ_TE_CHNK)
3781 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003783 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003784 }
3785 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003786 /* read the chunk size and assign it to ->hdr_content_len, then
3787 * set ->sov and ->lr to point to the body and switch to DATA or
3788 * TRAILERS state.
3789 */
3790 int ret = http_parse_chunk_size(req, msg);
3791
3792 if (!ret)
3793 goto missing_data;
3794 else if (ret < 0)
3795 goto return_bad_req;
3796 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003797 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003798 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3799 /* we want the CRLF after the data */
3800 int ret;
3801
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003802 req->lr = req->w + req->send_max;
3803 if (req->lr >= req->data + req->size)
3804 req->lr -= req->size;
3805
Willy Tarreaud98cf932009-12-27 22:54:55 +01003806 ret = http_skip_chunk_crlf(req, msg);
3807
3808 if (ret == 0)
3809 goto missing_data;
3810 else if (ret < 0)
3811 goto return_bad_req;
3812 /* we're in MSG_CHUNK_SIZE now */
3813 }
3814 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3815 int ret = http_forward_trailers(req, msg);
3816
3817 if (ret == 0)
3818 goto missing_data;
3819 else if (ret < 0)
3820 goto return_bad_req;
3821 /* we're in HTTP_MSG_DONE now */
3822 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003823 else {
3824 /* other states, DONE...TUNNEL */
3825 if (http_resync_states(s)) {
3826 /* some state changes occurred, maybe the analyser
3827 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003828 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3830 goto return_bad_req;
3831 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003832 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003833 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003834 }
3835 }
3836
Willy Tarreaud98cf932009-12-27 22:54:55 +01003837 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003838 /* stop waiting for data if the input is closed before the end */
3839 if (req->flags & BF_SHUTR)
3840 goto return_bad_req;
3841
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003842 /* waiting for the last bits to leave the buffer */
3843 if (req->flags & BF_SHUTW)
3844 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003845
3846 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003847 return 0;
3848
Willy Tarreaud98cf932009-12-27 22:54:55 +01003849 return_bad_req: /* let's centralize all bad requests */
3850 txn->req.msg_state = HTTP_MSG_ERROR;
3851 txn->status = 400;
3852 /* Note: we don't send any error if some data were already sent */
3853 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3854
3855 req->analysers = 0;
3856 s->fe->counters.failed_req++;
3857 if (s->listener->counters)
3858 s->listener->counters->failed_req++;
3859
3860 if (!(s->flags & SN_ERR_MASK))
3861 s->flags |= SN_ERR_PRXCOND;
3862 if (!(s->flags & SN_FINST_MASK))
3863 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003865 return 0;
3866}
3867
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003868/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3869 * processing can continue on next analysers, or zero if it either needs more
3870 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3871 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3872 * when it has nothing left to do, and may remove any analyser when it wants to
3873 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003874 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003875int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003876{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003877 struct http_txn *txn = &s->txn;
3878 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003879 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003880 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003881 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003882 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003883
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003884 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 +02003885 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003886 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003887 rep,
3888 rep->rex, rep->wex,
3889 rep->flags,
3890 rep->l,
3891 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003892
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003893 /*
3894 * Now parse the partial (or complete) lines.
3895 * We will check the response syntax, and also join multi-line
3896 * headers. An index of all the lines will be elaborated while
3897 * parsing.
3898 *
3899 * For the parsing, we use a 28 states FSM.
3900 *
3901 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003902 * rep->data + msg->som = beginning of response
3903 * rep->data + msg->eoh = end of processed headers / start of current one
3904 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003905 * rep->lr = first non-visited byte
3906 * rep->r = end of data
3907 */
3908
Willy Tarreau83e3af02009-12-28 17:39:57 +01003909 /* There's a protected area at the end of the buffer for rewriting
3910 * purposes. We don't want to start to parse the request if the
3911 * protected area is affected, because we may have to move processed
3912 * data later, which is much more complicated.
3913 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003914 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3915 if (unlikely((rep->flags & BF_FULL) ||
3916 rep->r < rep->lr ||
3917 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3918 if (rep->send_max) {
3919 /* some data has still not left the buffer, wake us once that's done */
3920 buffer_dont_close(rep);
3921 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3922 return 0;
3923 }
3924 if (rep->l <= rep->size - global.tune.maxrewrite)
3925 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003926 }
3927
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003928 if (likely(rep->lr < rep->r))
3929 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003930 }
3931
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003932 /* 1: we might have to print this header in debug mode */
3933 if (unlikely((global.mode & MODE_DEBUG) &&
3934 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003935 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003936 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003937
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003938 sol = rep->data + msg->som;
3939 eol = sol + msg->sl.rq.l;
3940 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003941
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003942 sol += hdr_idx_first_pos(&txn->hdr_idx);
3943 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003944
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003945 while (cur_idx) {
3946 eol = sol + txn->hdr_idx.v[cur_idx].len;
3947 debug_hdr("srvhdr", s, sol, eol);
3948 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3949 cur_idx = txn->hdr_idx.v[cur_idx].next;
3950 }
3951 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003952
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003953 /*
3954 * Now we quickly check if we have found a full valid response.
3955 * If not so, we check the FD and buffer states before leaving.
3956 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003957 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003958 * responses are checked first.
3959 *
3960 * Depending on whether the client is still there or not, we
3961 * may send an error response back or not. Note that normally
3962 * we should only check for HTTP status there, and check I/O
3963 * errors somewhere else.
3964 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003965
Willy Tarreau655dce92009-11-08 13:10:58 +01003966 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003967 /* Invalid response */
3968 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3969 /* we detected a parsing error. We want to archive this response
3970 * in the dedicated proxy area for later troubleshooting.
3971 */
3972 hdr_response_bad:
3973 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3974 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3975
3976 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003977 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003978 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003979 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3980 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003981
3982 rep->analysers = 0;
3983 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003984 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003985 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3986
3987 if (!(s->flags & SN_ERR_MASK))
3988 s->flags |= SN_ERR_PRXCOND;
3989 if (!(s->flags & SN_FINST_MASK))
3990 s->flags |= SN_FINST_H;
3991
3992 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003993 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003995 /* too large response does not fit in buffer. */
3996 else if (rep->flags & BF_FULL) {
3997 goto hdr_response_bad;
3998 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003999
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004000 /* read error */
4001 else if (rep->flags & BF_READ_ERROR) {
4002 if (msg->err_pos >= 0)
4003 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004004
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004005 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004006 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004007 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004008 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4009 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004010
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004011 rep->analysers = 0;
4012 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004013 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004014 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004015
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004016 if (!(s->flags & SN_ERR_MASK))
4017 s->flags |= SN_ERR_SRVCL;
4018 if (!(s->flags & SN_FINST_MASK))
4019 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004020 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004021 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004022
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004023 /* read timeout : return a 504 to the client. */
4024 else if (rep->flags & BF_READ_TIMEOUT) {
4025 if (msg->err_pos >= 0)
4026 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004027
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004028 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004029 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004030 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004031 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4032 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004033
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004034 rep->analysers = 0;
4035 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004036 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004037 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004039 if (!(s->flags & SN_ERR_MASK))
4040 s->flags |= SN_ERR_SRVTO;
4041 if (!(s->flags & SN_FINST_MASK))
4042 s->flags |= SN_FINST_H;
4043 return 0;
4044 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004046 /* close from server */
4047 else if (rep->flags & BF_SHUTR) {
4048 if (msg->err_pos >= 0)
4049 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004050
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004051 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004052 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004053 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004054 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4055 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004056
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004057 rep->analysers = 0;
4058 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004059 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004060 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004061
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004062 if (!(s->flags & SN_ERR_MASK))
4063 s->flags |= SN_ERR_SRVCL;
4064 if (!(s->flags & SN_FINST_MASK))
4065 s->flags |= SN_FINST_H;
4066 return 0;
4067 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004068
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004069 /* write error to client (we don't send any message then) */
4070 else if (rep->flags & BF_WRITE_ERROR) {
4071 if (msg->err_pos >= 0)
4072 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004073
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004074 s->be->counters.failed_resp++;
4075 rep->analysers = 0;
4076
4077 if (!(s->flags & SN_ERR_MASK))
4078 s->flags |= SN_ERR_CLICL;
4079 if (!(s->flags & SN_FINST_MASK))
4080 s->flags |= SN_FINST_H;
4081
4082 /* process_session() will take care of the error */
4083 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004084 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004085
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004086 buffer_dont_close(rep);
4087 return 0;
4088 }
4089
4090 /* More interesting part now : we know that we have a complete
4091 * response which at least looks like HTTP. We have an indicator
4092 * of each header's length, so we can parse them quickly.
4093 */
4094
4095 if (unlikely(msg->err_pos >= 0))
4096 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4097
4098 /* ensure we keep this pointer to the beginning of the message */
4099 msg->sol = rep->data + msg->som;
4100
4101 /*
4102 * 1: get the status code
4103 */
4104 n = rep->data[msg->sl.st.c] - '0';
4105 if (n < 1 || n > 5)
4106 n = 0;
4107 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004108
Willy Tarreau5b154472009-12-21 20:11:07 +01004109 /* check if the response is HTTP/1.1 or above */
4110 if ((msg->sl.st.v_l == 8) &&
4111 ((rep->data[msg->som + 5] > '1') ||
4112 ((rep->data[msg->som + 5] == '1') &&
4113 (rep->data[msg->som + 7] >= '1'))))
4114 txn->flags |= TX_RES_VER_11;
4115
4116 /* "connection" has not been parsed yet */
4117 txn->flags &= ~TX_CON_HDR_PARS;
4118
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004119 /* transfer length unknown*/
4120 txn->flags &= ~TX_RES_XFER_LEN;
4121
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004122 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
4123
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004124 if (txn->status >= 100 && txn->status < 500)
4125 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4126 else
4127 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4128
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004129 /*
4130 * 2: check for cacheability.
4131 */
4132
4133 switch (txn->status) {
4134 case 200:
4135 case 203:
4136 case 206:
4137 case 300:
4138 case 301:
4139 case 410:
4140 /* RFC2616 @13.4:
4141 * "A response received with a status code of
4142 * 200, 203, 206, 300, 301 or 410 MAY be stored
4143 * by a cache (...) unless a cache-control
4144 * directive prohibits caching."
4145 *
4146 * RFC2616 @9.5: POST method :
4147 * "Responses to this method are not cacheable,
4148 * unless the response includes appropriate
4149 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004150 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004151 if (likely(txn->meth != HTTP_METH_POST) &&
4152 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4153 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4154 break;
4155 default:
4156 break;
4157 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004158
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004159 /*
4160 * 3: we may need to capture headers
4161 */
4162 s->logs.logwait &= ~LW_RESP;
4163 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
4164 capture_headers(rep->data + msg->som, &txn->hdr_idx,
4165 txn->rsp.cap, s->fe->rsp_cap);
4166
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004167 /* 4: determine the transfer-length.
4168 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4169 * the presence of a message-body in a RESPONSE and its transfer length
4170 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004171 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004172 * All responses to the HEAD request method MUST NOT include a
4173 * message-body, even though the presence of entity-header fields
4174 * might lead one to believe they do. All 1xx (informational), 204
4175 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4176 * message-body. All other responses do include a message-body,
4177 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004178 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004179 * 1. Any response which "MUST NOT" include a message-body (such as the
4180 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4181 * always terminated by the first empty line after the header fields,
4182 * regardless of the entity-header fields present in the message.
4183 *
4184 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4185 * the "chunked" transfer-coding (Section 6.2) is used, the
4186 * transfer-length is defined by the use of this transfer-coding.
4187 * If a Transfer-Encoding header field is present and the "chunked"
4188 * transfer-coding is not present, the transfer-length is defined by
4189 * the sender closing the connection.
4190 *
4191 * 3. If a Content-Length header field is present, its decimal value in
4192 * OCTETs represents both the entity-length and the transfer-length.
4193 * If a message is received with both a Transfer-Encoding header
4194 * field and a Content-Length header field, the latter MUST be ignored.
4195 *
4196 * 4. If the message uses the media type "multipart/byteranges", and
4197 * the transfer-length is not otherwise specified, then this self-
4198 * delimiting media type defines the transfer-length. This media
4199 * type MUST NOT be used unless the sender knows that the recipient
4200 * can parse it; the presence in a request of a Range header with
4201 * multiple byte-range specifiers from a 1.1 client implies that the
4202 * client can parse multipart/byteranges responses.
4203 *
4204 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004205 */
4206
4207 /* Skip parsing if no content length is possible. The response flags
4208 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004209 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004210 * FIXME: should we parse anyway and return an error on chunked encoding ?
4211 */
4212 if (txn->meth == HTTP_METH_HEAD ||
4213 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004214 txn->status == 204 || txn->status == 304) {
4215 txn->flags |= TX_RES_XFER_LEN;
4216 goto skip_content_length;
4217 }
4218
4219 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004220 goto skip_content_length;
4221
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004222 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004223 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004224 while ((txn->flags & TX_RES_VER_11) &&
4225 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004226 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4227 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4228 else if (txn->flags & TX_RES_TE_CHNK) {
4229 /* bad transfer-encoding (chunked followed by something else) */
4230 use_close_only = 1;
4231 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4232 break;
4233 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004234 }
4235
4236 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4237 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004238 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004239 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4240 signed long long cl;
4241
4242 if (!ctx.vlen)
4243 goto hdr_response_bad;
4244
4245 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4246 goto hdr_response_bad; /* parse failure */
4247
4248 if (cl < 0)
4249 goto hdr_response_bad;
4250
4251 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4252 goto hdr_response_bad; /* already specified, was different */
4253
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004254 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004255 msg->hdr_content_len = cl;
4256 }
4257
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004258 /* FIXME: we should also implement the multipart/byterange method.
4259 * For now on, we resort to close mode in this case (unknown length).
4260 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004261skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004262
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004263 /* end of job, return OK */
4264 rep->analysers &= ~an_bit;
4265 rep->analyse_exp = TICK_ETERNITY;
4266 return 1;
4267}
4268
4269/* This function performs all the processing enabled for the current response.
4270 * It normally returns zero, but may return 1 if it absolutely needs to be
4271 * called again after other functions. It relies on buffers flags, and updates
4272 * t->rep->analysers. It might make sense to explode it into several other
4273 * functions. It works like process_request (see indications above).
4274 */
4275int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4276{
4277 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004278 struct http_msg *msg = &txn->rsp;
4279 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004280 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004281 int conn_ka = 0, conn_cl = 0;
4282 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004283 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004284
4285 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4286 now_ms, __FUNCTION__,
4287 t,
4288 rep,
4289 rep->rex, rep->wex,
4290 rep->flags,
4291 rep->l,
4292 rep->analysers);
4293
Willy Tarreau655dce92009-11-08 13:10:58 +01004294 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004295 return 0;
4296
4297 rep->analysers &= ~an_bit;
4298 rep->analyse_exp = TICK_ETERNITY;
4299
Willy Tarreau5b154472009-12-21 20:11:07 +01004300 /* Now we have to check if we need to modify the Connection header.
4301 * This is more difficult on the response than it is on the request,
4302 * because we can have two different HTTP versions and we don't know
4303 * how the client will interprete a response. For instance, let's say
4304 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4305 * HTTP/1.1 response without any header. Maybe it will bound itself to
4306 * HTTP/1.0 because it only knows about it, and will consider the lack
4307 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4308 * the lack of header as a keep-alive. Thus we will use two flags
4309 * indicating how a request MAY be understood by the client. In case
4310 * of multiple possibilities, we'll fix the header to be explicit. If
4311 * ambiguous cases such as both close and keepalive are seen, then we
4312 * will fall back to explicit close. Note that we won't take risks with
4313 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4314 */
4315
4316 if ((txn->meth != HTTP_METH_CONNECT) &&
4317 (txn->status >= 200) &&
4318 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
4319 !(txn->flags & TX_CON_HDR_PARS)) {
4320 int may_keep = 0, may_close = 0; /* how it may be understood */
4321 struct hdr_ctx ctx;
4322
4323 ctx.idx = 0;
4324 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4325 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4326 conn_cl = 1;
4327 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4328 conn_ka = 1;
4329 }
4330
4331 if (conn_cl) {
4332 /* close header present */
4333 may_close = 1;
4334 if (conn_ka) /* we have both close and keep-alive */
4335 may_keep = 1;
4336 }
4337 else if (conn_ka) {
4338 /* keep-alive alone */
4339 may_keep = 1;
4340 }
4341 else {
4342 /* no close nor keep-alive header */
4343 if (txn->flags & TX_RES_VER_11)
4344 may_keep = 1;
4345 else
4346 may_close = 1;
4347
4348 if (txn->flags & TX_REQ_VER_11)
4349 may_keep = 1;
4350 else
4351 may_close = 1;
4352 }
4353
4354 /* let's update the transaction status to reflect any close.
4355 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004356 * handled. We also explicitly state that we will close in
4357 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004358 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004359 if ((may_close &&
4360 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4361 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004362 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4363
4364 /* Now we must adjust the response header :
4365 * - set "close" if may_keep and WANT_CLO
4366 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4367 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
4368 *
4369 * Until we support the server-close mode, we'll only support the set "close".
4370 */
4371 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
4372 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004373 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4374 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4375 must_del_close = 1;
4376 if (!(txn->flags & TX_REQ_VER_11))
4377 must_keep = 1;
4378 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004379
4380 txn->flags |= TX_CON_HDR_PARS;
4381 }
4382
4383 /* We might have to check for "Connection:" if the server
4384 * returns a connection status that is not compatible with
4385 * the client's or with the config.
4386 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004387 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004388 char *cur_ptr, *cur_end, *cur_next;
4389 int cur_idx, old_idx, delta, val;
4390 int must_delete;
4391 struct hdr_idx_elem *cur_hdr;
4392
4393 /* we just have to remove the headers if both sides are 1.0 */
4394 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004395
4396 /* same if we want to re-enable keep-alive on 1.1 */
4397 must_delete |= must_del_close;
4398
Willy Tarreau5b154472009-12-21 20:11:07 +01004399 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4400
4401 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4402 cur_hdr = &txn->hdr_idx.v[cur_idx];
4403 cur_ptr = cur_next;
4404 cur_end = cur_ptr + cur_hdr->len;
4405 cur_next = cur_end + cur_hdr->cr + 1;
4406
4407 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4408 if (!val)
4409 continue;
4410
4411 /* 3 possibilities :
4412 * - we have already set "Connection: close" or we're in
4413 * HTTP/1.0, so we remove this line.
4414 * - we have not yet set "Connection: close", but this line
4415 * indicates close. We leave it untouched and set the flag.
4416 * - we have not yet set "Connection: close", and this line
4417 * indicates non-close. We replace it and set the flag.
4418 */
4419 if (must_delete) {
4420 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4421 http_msg_move_end(&txn->rsp, delta);
4422 cur_next += delta;
4423 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4424 txn->hdr_idx.used--;
4425 cur_hdr->len = 0;
4426 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004427 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004428 } else {
4429 if (cur_end - cur_ptr - val != 5 ||
4430 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4431 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4432 "close", 5);
4433 cur_next += delta;
4434 cur_hdr->len += delta;
4435 http_msg_move_end(&txn->rsp, delta);
4436 }
4437 must_delete = 1;
4438 must_close = 0;
4439 }
4440 } /* for loop */
4441 } /* if must close keep-alive */
4442
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004443 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004444 /*
4445 * 3: we will have to evaluate the filters.
4446 * As opposed to version 1.2, now they will be evaluated in the
4447 * filters order and not in the header order. This means that
4448 * each filter has to be validated among all headers.
4449 *
4450 * Filters are tried with ->be first, then with ->fe if it is
4451 * different from ->be.
4452 */
4453
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004454 cur_proxy = t->be;
4455 while (1) {
4456 struct proxy *rule_set = cur_proxy;
4457
4458 /* try headers filters */
4459 if (rule_set->rsp_exp != NULL) {
4460 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4461 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004462 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004463 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004464 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4465 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004466 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004467 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004468 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004469 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004470 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004471 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004472 if (!(t->flags & SN_ERR_MASK))
4473 t->flags |= SN_ERR_PRXCOND;
4474 if (!(t->flags & SN_FINST_MASK))
4475 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004476 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004477 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004478 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004479
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004480 /* has the response been denied ? */
4481 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004482 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004483 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004484
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004485 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004486 if (t->listener->counters)
4487 t->listener->counters->denied_resp++;
4488
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004489 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004490 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004491
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004492 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004493 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004494 if (txn->status < 200)
4495 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004496 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004497 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004498 }
4499
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004500 /* check whether we're already working on the frontend */
4501 if (cur_proxy == t->fe)
4502 break;
4503 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004504 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004505
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004506 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004507 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4508 * in which case this is not the right response, and we're waiting for the
4509 * next one. Let's allow this response to go to the client and wait for the
4510 * next one.
4511 */
4512 if (txn->status < 200) {
4513 hdr_idx_init(&txn->hdr_idx);
4514 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4515 msg->msg_state = HTTP_MSG_RPBEFORE;
4516 txn->status = 0;
4517 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4518 return 1;
4519 }
4520
4521 /* we don't have any 1xx status code now */
4522
4523 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004524 * 4: check for server cookie.
4525 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004526 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4527 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004528 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004529
Willy Tarreaubaaee002006-06-26 02:48:02 +02004530
Willy Tarreaua15645d2007-03-18 16:22:39 +01004531 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004532 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004533 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004534 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004535 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004536
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004537 /*
4538 * 6: add server cookie in the response if needed
4539 */
4540 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004541 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004542 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004543
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004544 /* the server is known, it's not the one the client requested, we have to
4545 * insert a set-cookie here, except if we want to insert only on POST
4546 * requests and this one isn't. Note that servers which don't have cookies
4547 * (eg: some backup servers) will return a full cookie removal request.
4548 */
4549 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4550 t->be->cookie_name,
4551 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004552
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004553 if (t->be->cookie_domain)
4554 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004555
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004556 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004557 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004558 goto return_bad_resp;
4559 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004560
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004561 /* Here, we will tell an eventual cache on the client side that we don't
4562 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4563 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4564 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4565 */
4566 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004567
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004568 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4569
4570 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004571 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004572 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004573 }
4574 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004575
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576 /*
4577 * 7: check if result will be cacheable with a cookie.
4578 * We'll block the response if security checks have caught
4579 * nasty things such as a cacheable cookie.
4580 */
4581 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4582 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004583 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584
4585 /* we're in presence of a cacheable response containing
4586 * a set-cookie header. We'll block it as requested by
4587 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004588 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004589 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004590 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004591
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004592 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004593 if (t->listener->counters)
4594 t->listener->counters->denied_resp++;
4595
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4597 t->be->id, t->srv?t->srv->id:"<dispatch>");
4598 send_log(t->be, LOG_ALERT,
4599 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4600 t->be->id, t->srv?t->srv->id:"<dispatch>");
4601 goto return_srv_prx_502;
4602 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004603
4604 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004605 * 8: add "Connection: close" if needed and not yet set. This is
4606 * only needed for 1.1 responses since we know there is no other
4607 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004608 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004609 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004610 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004611 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004612 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004613 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004614 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004615 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4616 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4617 "Connection: keep-alive", 22) < 0))
4618 goto return_bad_resp;
4619 must_keep = 0;
4620 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004621
Willy Tarreaud98cf932009-12-27 22:54:55 +01004622 if (txn->flags & TX_RES_XFER_LEN)
4623 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004624
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004625 /*************************************************************
4626 * OK, that's finished for the headers. We have done what we *
4627 * could. Let's switch to the DATA state. *
4628 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004629
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004630 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004631
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004632 /* if the user wants to log as soon as possible, without counting
4633 * bytes from the server, then this is the right moment. We have
4634 * to temporarily assign bytes_out to log what we currently have.
4635 */
4636 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4637 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4638 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004639 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004640 t->logs.bytes_out = 0;
4641 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004642
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004643 /* Note: we must not try to cheat by jumping directly to DATA,
4644 * otherwise we would not let the client side wake up.
4645 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004646
Willy Tarreaudafde432008-08-17 01:00:46 +02004647 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004648 }
4649 return 0;
4650}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004651
Willy Tarreaud98cf932009-12-27 22:54:55 +01004652/* This function is an analyser which forwards response body (including chunk
4653 * sizes if any). It is called as soon as we must forward, even if we forward
4654 * zero byte. The only situation where it must not be called is when we're in
4655 * tunnel mode and we want to forward till the close. It's used both to forward
4656 * remaining data and to resync after end of body. It expects the msg_state to
4657 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4658 * read more data, or 1 once we can go on with next request or end the session.
4659 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4660 * bytes of pending data + the headers if not already done (between som and sov).
4661 * It eventually adjusts som to match sov after the data in between have been sent.
4662 */
4663int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4664{
4665 struct http_txn *txn = &s->txn;
4666 struct http_msg *msg = &s->txn.rsp;
4667
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004668 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4669 return 0;
4670
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004671 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4672 !s->req->analysers) {
4673 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4674 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004675 res->analysers &= ~an_bit;
4676 return 1;
4677 }
4678
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004679 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004680
Willy Tarreaud98cf932009-12-27 22:54:55 +01004681 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4682 /* we have msg->col and msg->sov which both point to the first
4683 * byte of message body. msg->som still points to the beginning
4684 * of the message. We must save the body in req->lr because it
4685 * survives buffer re-alignments.
4686 */
4687 res->lr = res->data + msg->sov;
4688 if (txn->flags & TX_RES_TE_CHNK)
4689 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4690 else {
4691 msg->msg_state = HTTP_MSG_DATA;
4692 }
4693 }
4694
Willy Tarreaud98cf932009-12-27 22:54:55 +01004695 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004696 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004697 /* we may have some data pending */
4698 if (msg->hdr_content_len || msg->som != msg->sov) {
4699 int bytes = msg->sov - msg->som;
4700 if (bytes < 0) /* sov may have wrapped at the end */
4701 bytes += res->size;
4702 buffer_forward(res, bytes + msg->hdr_content_len);
4703 msg->hdr_content_len = 0; /* don't forward that again */
4704 msg->som = msg->sov;
4705 }
4706
Willy Tarreaucaabe412010-01-03 23:08:28 +01004707 if (msg->msg_state == HTTP_MSG_DATA) {
4708 /* must still forward */
4709 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004710 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004711
4712 /* nothing left to forward */
4713 if (txn->flags & TX_RES_TE_CHNK)
4714 msg->msg_state = HTTP_MSG_DATA_CRLF;
4715 else
4716 msg->msg_state = HTTP_MSG_DONE;
4717 }
4718 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004719 /* read the chunk size and assign it to ->hdr_content_len, then
4720 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4721 */
4722 int ret = http_parse_chunk_size(res, msg);
4723
4724 if (!ret)
4725 goto missing_data;
4726 else if (ret < 0)
4727 goto return_bad_res;
4728 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004729 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004730 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4731 /* we want the CRLF after the data */
4732 int ret;
4733
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004734 res->lr = res->w + res->send_max;
4735 if (res->lr >= res->data + res->size)
4736 res->lr -= res->size;
4737
Willy Tarreaud98cf932009-12-27 22:54:55 +01004738 ret = http_skip_chunk_crlf(res, msg);
4739
4740 if (!ret)
4741 goto missing_data;
4742 else if (ret < 0)
4743 goto return_bad_res;
4744 /* we're in MSG_CHUNK_SIZE now */
4745 }
4746 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4747 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004748
Willy Tarreaud98cf932009-12-27 22:54:55 +01004749 if (ret == 0)
4750 goto missing_data;
4751 else if (ret < 0)
4752 goto return_bad_res;
4753 /* we're in HTTP_MSG_DONE now */
4754 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004755 else {
4756 /* other states, DONE...TUNNEL */
4757 if (http_resync_states(s)) {
4758 http_silent_debug(__LINE__, s);
4759 /* some state changes occurred, maybe the analyser
4760 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004761 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004762 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4763 goto return_bad_res;
4764 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004765 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004766 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004767 }
4768 }
4769
Willy Tarreaud98cf932009-12-27 22:54:55 +01004770 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004771 /* stop waiting for data if the input is closed before the end */
4772 if (res->flags & BF_SHUTR)
4773 goto return_bad_res;
4774
Willy Tarreau610ecce2010-01-04 21:15:02 +01004775 if (!s->req->analysers)
4776 goto return_bad_res;
4777
Willy Tarreaud98cf932009-12-27 22:54:55 +01004778 /* forward the chunk size as well as any pending data */
4779 if (msg->hdr_content_len || msg->som != msg->sov) {
4780 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4781 msg->hdr_content_len = 0; /* don't forward that again */
4782 msg->som = msg->sov;
4783 }
4784
Willy Tarreaud98cf932009-12-27 22:54:55 +01004785 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004786 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004787 return 0;
4788
4789 return_bad_res: /* let's centralize all bad resuests */
4790 txn->rsp.msg_state = HTTP_MSG_ERROR;
4791 txn->status = 502;
4792 stream_int_cond_close(res->cons, NULL);
4793
4794 res->analysers = 0;
4795 s->be->counters.failed_resp++;
4796 if (s->srv) {
4797 s->srv->counters.failed_resp++;
4798 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4799 }
4800
4801 if (!(s->flags & SN_ERR_MASK))
4802 s->flags |= SN_ERR_PRXCOND;
4803 if (!(s->flags & SN_FINST_MASK))
4804 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004805 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 return 0;
4807}
4808
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004809/* Iterate the same filter through all request headers.
4810 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004811 * Since it can manage the switch to another backend, it updates the per-proxy
4812 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004813 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004814int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004815{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004816 char term;
4817 char *cur_ptr, *cur_end, *cur_next;
4818 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004819 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004820 struct hdr_idx_elem *cur_hdr;
4821 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004823 last_hdr = 0;
4824
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004825 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004826 old_idx = 0;
4827
4828 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004829 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004830 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004831 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004832 (exp->action == ACT_ALLOW ||
4833 exp->action == ACT_DENY ||
4834 exp->action == ACT_TARPIT))
4835 return 0;
4836
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004837 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004838 if (!cur_idx)
4839 break;
4840
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004841 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004842 cur_ptr = cur_next;
4843 cur_end = cur_ptr + cur_hdr->len;
4844 cur_next = cur_end + cur_hdr->cr + 1;
4845
4846 /* Now we have one header between cur_ptr and cur_end,
4847 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004848 */
4849
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004850 /* The annoying part is that pattern matching needs
4851 * that we modify the contents to null-terminate all
4852 * strings before testing them.
4853 */
4854
4855 term = *cur_end;
4856 *cur_end = '\0';
4857
4858 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4859 switch (exp->action) {
4860 case ACT_SETBE:
4861 /* It is not possible to jump a second time.
4862 * FIXME: should we return an HTTP/500 here so that
4863 * the admin knows there's a problem ?
4864 */
4865 if (t->be != t->fe)
4866 break;
4867
4868 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004869 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004870 last_hdr = 1;
4871 break;
4872
4873 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004874 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004875 last_hdr = 1;
4876 break;
4877
4878 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004879 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004880 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004881
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004882 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004883 if (t->listener->counters)
4884 t->listener->counters->denied_resp++;
4885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004886 break;
4887
4888 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004889 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004890 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004891
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004892 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004893 if (t->listener->counters)
4894 t->listener->counters->denied_resp++;
4895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004896 break;
4897
4898 case ACT_REPLACE:
4899 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4900 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4901 /* FIXME: if the user adds a newline in the replacement, the
4902 * index will not be recalculated for now, and the new line
4903 * will not be counted as a new header.
4904 */
4905
4906 cur_end += delta;
4907 cur_next += delta;
4908 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004909 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004910 break;
4911
4912 case ACT_REMOVE:
4913 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4914 cur_next += delta;
4915
Willy Tarreaufa355d42009-11-29 18:12:29 +01004916 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004917 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4918 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004919 cur_hdr->len = 0;
4920 cur_end = NULL; /* null-term has been rewritten */
4921 break;
4922
4923 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004924 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004925 if (cur_end)
4926 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004928 /* keep the link from this header to next one in case of later
4929 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004930 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004931 old_idx = cur_idx;
4932 }
4933 return 0;
4934}
4935
4936
4937/* Apply the filter to the request line.
4938 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4939 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004940 * Since it can manage the switch to another backend, it updates the per-proxy
4941 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004942 */
4943int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4944{
4945 char term;
4946 char *cur_ptr, *cur_end;
4947 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004948 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004949 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004950
Willy Tarreau58f10d72006-12-04 02:26:12 +01004951
Willy Tarreau3d300592007-03-18 18:34:41 +01004952 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004953 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004954 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004955 (exp->action == ACT_ALLOW ||
4956 exp->action == ACT_DENY ||
4957 exp->action == ACT_TARPIT))
4958 return 0;
4959 else if (exp->action == ACT_REMOVE)
4960 return 0;
4961
4962 done = 0;
4963
Willy Tarreau9cdde232007-05-02 20:58:19 +02004964 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004965 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004966
4967 /* Now we have the request line between cur_ptr and cur_end */
4968
4969 /* The annoying part is that pattern matching needs
4970 * that we modify the contents to null-terminate all
4971 * strings before testing them.
4972 */
4973
4974 term = *cur_end;
4975 *cur_end = '\0';
4976
4977 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4978 switch (exp->action) {
4979 case ACT_SETBE:
4980 /* It is not possible to jump a second time.
4981 * FIXME: should we return an HTTP/500 here so that
4982 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004983 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004984 if (t->be != t->fe)
4985 break;
4986
4987 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004988 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004989 done = 1;
4990 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004992 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004993 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004994 done = 1;
4995 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004997 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004998 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004999
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005000 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005001 if (t->listener->counters)
5002 t->listener->counters->denied_resp++;
5003
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005004 done = 1;
5005 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005006
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005007 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005008 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005009
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005010 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005011 if (t->listener->counters)
5012 t->listener->counters->denied_resp++;
5013
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 done = 1;
5015 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005017 case ACT_REPLACE:
5018 *cur_end = term; /* restore the string terminator */
5019 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5020 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5021 /* FIXME: if the user adds a newline in the replacement, the
5022 * index will not be recalculated for now, and the new line
5023 * will not be counted as a new header.
5024 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005025
Willy Tarreaufa355d42009-11-29 18:12:29 +01005026 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005027 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005028
Willy Tarreau9cdde232007-05-02 20:58:19 +02005029 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005030 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005031 HTTP_MSG_RQMETH,
5032 cur_ptr, cur_end + 1,
5033 NULL, NULL);
5034 if (unlikely(!cur_end))
5035 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005036
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005037 /* we have a full request and we know that we have either a CR
5038 * or an LF at <ptr>.
5039 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005040 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5041 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005042 /* there is no point trying this regex on headers */
5043 return 1;
5044 }
5045 }
5046 *cur_end = term; /* restore the string terminator */
5047 return done;
5048}
Willy Tarreau97de6242006-12-27 17:18:38 +01005049
Willy Tarreau58f10d72006-12-04 02:26:12 +01005050
Willy Tarreau58f10d72006-12-04 02:26:12 +01005051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005052/*
5053 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5054 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005055 * unparsable request. Since it can manage the switch to another backend, it
5056 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005057 */
5058int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5059{
Willy Tarreau3d300592007-03-18 18:34:41 +01005060 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005061 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005062 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005063 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005065 /*
5066 * The interleaving of transformations and verdicts
5067 * makes it difficult to decide to continue or stop
5068 * the evaluation.
5069 */
5070
Willy Tarreau3d300592007-03-18 18:34:41 +01005071 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005072 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5073 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5074 exp = exp->next;
5075 continue;
5076 }
5077
5078 /* Apply the filter to the request line. */
5079 ret = apply_filter_to_req_line(t, req, exp);
5080 if (unlikely(ret < 0))
5081 return -1;
5082
5083 if (likely(ret == 0)) {
5084 /* The filter did not match the request, it can be
5085 * iterated through all headers.
5086 */
5087 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088 }
5089 exp = exp->next;
5090 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005091 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005092}
5093
5094
Willy Tarreaua15645d2007-03-18 16:22:39 +01005095
Willy Tarreau58f10d72006-12-04 02:26:12 +01005096/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005097 * Try to retrieve the server associated to the appsession.
5098 * If the server is found, it's assigned to the session.
5099 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005100void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005101 struct http_txn *txn = &t->txn;
5102 appsess *asession = NULL;
5103 char *sessid_temp = NULL;
5104
Cyril Bontéb21570a2009-11-29 20:04:48 +01005105 if (len > t->be->appsession_len) {
5106 len = t->be->appsession_len;
5107 }
5108
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005109 if (t->be->options2 & PR_O2_AS_REQL) {
5110 /* request-learn option is enabled : store the sessid in the session for future use */
5111 if (t->sessid != NULL) {
5112 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5113 pool_free2(apools.sessid, t->sessid);
5114 }
5115
5116 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5117 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5118 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5119 return;
5120 }
5121
Cyril Bontéb21570a2009-11-29 20:04:48 +01005122 memcpy(t->sessid, buf, len);
5123 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005124 }
5125
5126 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5127 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5128 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5129 return;
5130 }
5131
Cyril Bontéb21570a2009-11-29 20:04:48 +01005132 memcpy(sessid_temp, buf, len);
5133 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005134
5135 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5136 /* free previously allocated memory */
5137 pool_free2(apools.sessid, sessid_temp);
5138
5139 if (asession != NULL) {
5140 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5141 if (!(t->be->options2 & PR_O2_AS_REQL))
5142 asession->request_count++;
5143
5144 if (asession->serverid != NULL) {
5145 struct server *srv = t->be->srv;
5146 while (srv) {
5147 if (strcmp(srv->id, asession->serverid) == 0) {
5148 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5149 /* we found the server and it's usable */
5150 txn->flags &= ~TX_CK_MASK;
5151 txn->flags |= TX_CK_VALID;
5152 t->flags |= SN_DIRECT | SN_ASSIGNED;
5153 t->srv = srv;
5154 break;
5155 } else {
5156 txn->flags &= ~TX_CK_MASK;
5157 txn->flags |= TX_CK_DOWN;
5158 }
5159 }
5160 srv = srv->next;
5161 }
5162 }
5163 }
5164}
5165
5166/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005167 * Manage client-side cookie. It can impact performance by about 2% so it is
5168 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005169 */
5170void manage_client_side_cookies(struct session *t, struct buffer *req)
5171{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005172 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005173 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005174 char *del_colon, *del_cookie, *colon;
5175 int app_cookies;
5176
Willy Tarreau58f10d72006-12-04 02:26:12 +01005177 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005178 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005179
Willy Tarreau2a324282006-12-05 00:05:46 +01005180 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005181 * we start with the start line.
5182 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005183 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005184 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005185
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005186 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005187 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005188 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005189
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005190 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005191 cur_ptr = cur_next;
5192 cur_end = cur_ptr + cur_hdr->len;
5193 cur_next = cur_end + cur_hdr->cr + 1;
5194
5195 /* We have one full header between cur_ptr and cur_end, and the
5196 * next header starts at cur_next. We're only interested in
5197 * "Cookie:" headers.
5198 */
5199
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005200 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5201 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005202 old_idx = cur_idx;
5203 continue;
5204 }
5205
5206 /* Now look for cookies. Conforming to RFC2109, we have to support
5207 * attributes whose name begin with a '$', and associate them with
5208 * the right cookie, if we want to delete this cookie.
5209 * So there are 3 cases for each cookie read :
5210 * 1) it's a special attribute, beginning with a '$' : ignore it.
5211 * 2) it's a server id cookie that we *MAY* want to delete : save
5212 * some pointers on it (last semi-colon, beginning of cookie...)
5213 * 3) it's an application cookie : we *MAY* have to delete a previous
5214 * "special" cookie.
5215 * At the end of loop, if a "special" cookie remains, we may have to
5216 * remove it. If no application cookie persists in the header, we
5217 * *MUST* delete it
5218 */
5219
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005220 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005221
Willy Tarreau58f10d72006-12-04 02:26:12 +01005222 /* del_cookie == NULL => nothing to be deleted */
5223 del_colon = del_cookie = NULL;
5224 app_cookies = 0;
5225
5226 while (p1 < cur_end) {
5227 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005228 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005229 while (p1 < cur_end) {
5230 if (*p1 == ';' || *p1 == ',')
5231 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005232 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005233 break;
5234 p1++;
5235 }
5236
5237 if (p1 == cur_end)
5238 break;
5239
5240 /* p1 is at the beginning of the cookie name */
5241 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005242 while (p2 < cur_end && *p2 != '=') {
5243 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5244 /* oops, the cookie name was truncated, resync */
5245 p1 = p2;
5246 goto resync_name;
5247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005248 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005250
5251 if (p2 == cur_end)
5252 break;
5253
5254 p3 = p2 + 1; /* skips the '=' sign */
5255 if (p3 == cur_end)
5256 break;
5257
Willy Tarreau305ae852010-01-03 19:45:54 +01005258 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5259 p5 = p4 = p3;
5260 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5261 if (!isspace((unsigned char)*p5))
5262 p4 = p5 + 1;
5263 p5++;
5264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005265
5266 /* here, we have the cookie name between p1 and p2,
5267 * and its value between p3 and p4.
5268 * we can process it :
5269 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005270 * Cookie: NAME=VALUE ;
5271 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005272 * | || || +--> p4
5273 * | || |+-------> p3
5274 * | || +--------> p2
5275 * | |+------------> p1
5276 * | +-------------> colon
5277 * +--------------------> cur_ptr
5278 */
5279
5280 if (*p1 == '$') {
5281 /* skip this one */
5282 }
5283 else {
5284 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005285 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005286 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005287 (p4 - p1 >= t->fe->capture_namelen) &&
5288 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005289 int log_len = p4 - p1;
5290
Willy Tarreau086b3b42007-05-13 21:45:51 +02005291 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005292 Alert("HTTP logging : out of memory.\n");
5293 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005294 if (log_len > t->fe->capture_len)
5295 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005296 memcpy(txn->cli_cookie, p1, log_len);
5297 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005298 }
5299 }
5300
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005301 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5302 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005303 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005304 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005305 char *delim;
5306
5307 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5308 * have the server ID betweek p3 and delim, and the original cookie between
5309 * delim+1 and p4. Otherwise, delim==p4 :
5310 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005311 * Cookie: NAME=SRV~VALUE ;
5312 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005313 * | || || | +--> p4
5314 * | || || +--------> delim
5315 * | || |+-----------> p3
5316 * | || +------------> p2
5317 * | |+----------------> p1
5318 * | +-----------------> colon
5319 * +------------------------> cur_ptr
5320 */
5321
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005322 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005323 for (delim = p3; delim < p4; delim++)
5324 if (*delim == COOKIE_DELIM)
5325 break;
5326 }
5327 else
5328 delim = p4;
5329
5330
5331 /* Here, we'll look for the first running server which supports the cookie.
5332 * This allows to share a same cookie between several servers, for example
5333 * to dedicate backup servers to specific servers only.
5334 * However, to prevent clients from sticking to cookie-less backup server
5335 * when they have incidentely learned an empty cookie, we simply ignore
5336 * empty cookies and mark them as invalid.
5337 */
5338 if (delim == p3)
5339 srv = NULL;
5340
5341 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005342 if (srv->cookie && (srv->cklen == delim - p3) &&
5343 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005344 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005345 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005346 txn->flags &= ~TX_CK_MASK;
5347 txn->flags |= TX_CK_VALID;
5348 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005349 t->srv = srv;
5350 break;
5351 } else {
5352 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005353 txn->flags &= ~TX_CK_MASK;
5354 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005355 }
5356 }
5357 srv = srv->next;
5358 }
5359
Willy Tarreau3d300592007-03-18 18:34:41 +01005360 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005361 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005362 txn->flags &= ~TX_CK_MASK;
5363 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005364 }
5365
5366 /* depending on the cookie mode, we may have to either :
5367 * - delete the complete cookie if we're in insert+indirect mode, so that
5368 * the server never sees it ;
5369 * - remove the server id from the cookie value, and tag the cookie as an
5370 * application cookie so that it does not get accidentely removed later,
5371 * if we're in cookie prefix mode
5372 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005373 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005374 int delta; /* negative */
5375
5376 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5377 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005378 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005379 cur_end += delta;
5380 cur_next += delta;
5381 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005382 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005383
5384 del_cookie = del_colon = NULL;
5385 app_cookies++; /* protect the header from deletion */
5386 }
5387 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005388 (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 +01005389 del_cookie = p1;
5390 del_colon = colon;
5391 }
5392 } else {
5393 /* now we know that we must keep this cookie since it's
5394 * not ours. But if we wanted to delete our cookie
5395 * earlier, we cannot remove the complete header, but we
5396 * can remove the previous block itself.
5397 */
5398 app_cookies++;
5399
5400 if (del_cookie != NULL) {
5401 int delta; /* negative */
5402
5403 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5404 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005405 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005406 cur_end += delta;
5407 cur_next += delta;
5408 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005409 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005410 del_cookie = del_colon = NULL;
5411 }
5412 }
5413
Cyril Bontéb21570a2009-11-29 20:04:48 +01005414 if (t->be->appsession_name != NULL) {
5415 int cmp_len, value_len;
5416 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005417
Cyril Bontéb21570a2009-11-29 20:04:48 +01005418 if (t->be->options2 & PR_O2_AS_PFX) {
5419 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5420 value_begin = p1 + t->be->appsession_name_len;
5421 value_len = p4 - p1 - t->be->appsession_name_len;
5422 } else {
5423 cmp_len = p2 - p1;
5424 value_begin = p3;
5425 value_len = p4 - p3;
5426 }
5427
5428 /* let's see if the cookie is our appcookie */
5429 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5430 /* Cool... it's the right one */
5431 manage_client_side_appsession(t, value_begin, value_len);
5432 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005433#if defined(DEBUG_HASH)
5434 Alert("manage_client_side_cookies\n");
5435 appsession_hash_dump(&(t->be->htbl_proxy));
5436#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005437 }/* end if ((t->proxy->appsession_name != NULL) ... */
5438 }
5439
5440 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005441 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442 } /* while (p1 < cur_end) */
5443
5444 /* There's no more cookie on this line.
5445 * We may have marked the last one(s) for deletion.
5446 * We must do this now in two ways :
5447 * - if there is no app cookie, we simply delete the header ;
5448 * - if there are app cookies, we must delete the end of the
5449 * string properly, including the colon/semi-colon before
5450 * the cookie name.
5451 */
5452 if (del_cookie != NULL) {
5453 int delta;
5454 if (app_cookies) {
5455 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5456 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005457 cur_hdr->len += delta;
5458 } else {
5459 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005460
5461 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005462 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5463 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005464 cur_hdr->len = 0;
5465 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005466 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005467 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005468 }
5469
5470 /* keep the link from this header to next one */
5471 old_idx = cur_idx;
5472 } /* end of cookie processing on this header */
5473}
5474
5475
Willy Tarreaua15645d2007-03-18 16:22:39 +01005476/* Iterate the same filter through all response headers contained in <rtr>.
5477 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5478 */
5479int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5480{
5481 char term;
5482 char *cur_ptr, *cur_end, *cur_next;
5483 int cur_idx, old_idx, last_hdr;
5484 struct http_txn *txn = &t->txn;
5485 struct hdr_idx_elem *cur_hdr;
5486 int len, delta;
5487
5488 last_hdr = 0;
5489
5490 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5491 old_idx = 0;
5492
5493 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005494 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005495 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005496 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005497 (exp->action == ACT_ALLOW ||
5498 exp->action == ACT_DENY))
5499 return 0;
5500
5501 cur_idx = txn->hdr_idx.v[old_idx].next;
5502 if (!cur_idx)
5503 break;
5504
5505 cur_hdr = &txn->hdr_idx.v[cur_idx];
5506 cur_ptr = cur_next;
5507 cur_end = cur_ptr + cur_hdr->len;
5508 cur_next = cur_end + cur_hdr->cr + 1;
5509
5510 /* Now we have one header between cur_ptr and cur_end,
5511 * and the next header starts at cur_next.
5512 */
5513
5514 /* The annoying part is that pattern matching needs
5515 * that we modify the contents to null-terminate all
5516 * strings before testing them.
5517 */
5518
5519 term = *cur_end;
5520 *cur_end = '\0';
5521
5522 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5523 switch (exp->action) {
5524 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005525 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005526 last_hdr = 1;
5527 break;
5528
5529 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005530 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005531 last_hdr = 1;
5532 break;
5533
5534 case ACT_REPLACE:
5535 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5536 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5537 /* FIXME: if the user adds a newline in the replacement, the
5538 * index will not be recalculated for now, and the new line
5539 * will not be counted as a new header.
5540 */
5541
5542 cur_end += delta;
5543 cur_next += delta;
5544 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005545 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005546 break;
5547
5548 case ACT_REMOVE:
5549 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5550 cur_next += delta;
5551
Willy Tarreaufa355d42009-11-29 18:12:29 +01005552 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005553 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5554 txn->hdr_idx.used--;
5555 cur_hdr->len = 0;
5556 cur_end = NULL; /* null-term has been rewritten */
5557 break;
5558
5559 }
5560 }
5561 if (cur_end)
5562 *cur_end = term; /* restore the string terminator */
5563
5564 /* keep the link from this header to next one in case of later
5565 * removal of next header.
5566 */
5567 old_idx = cur_idx;
5568 }
5569 return 0;
5570}
5571
5572
5573/* Apply the filter to the status line in the response buffer <rtr>.
5574 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5575 * or -1 if a replacement resulted in an invalid status line.
5576 */
5577int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5578{
5579 char term;
5580 char *cur_ptr, *cur_end;
5581 int done;
5582 struct http_txn *txn = &t->txn;
5583 int len, delta;
5584
5585
Willy Tarreau3d300592007-03-18 18:34:41 +01005586 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005587 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005588 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005589 (exp->action == ACT_ALLOW ||
5590 exp->action == ACT_DENY))
5591 return 0;
5592 else if (exp->action == ACT_REMOVE)
5593 return 0;
5594
5595 done = 0;
5596
Willy Tarreau9cdde232007-05-02 20:58:19 +02005597 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005598 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5599
5600 /* Now we have the status line between cur_ptr and cur_end */
5601
5602 /* The annoying part is that pattern matching needs
5603 * that we modify the contents to null-terminate all
5604 * strings before testing them.
5605 */
5606
5607 term = *cur_end;
5608 *cur_end = '\0';
5609
5610 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5611 switch (exp->action) {
5612 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005613 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005614 done = 1;
5615 break;
5616
5617 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005618 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005619 done = 1;
5620 break;
5621
5622 case ACT_REPLACE:
5623 *cur_end = term; /* restore the string terminator */
5624 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5625 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5626 /* FIXME: if the user adds a newline in the replacement, the
5627 * index will not be recalculated for now, and the new line
5628 * will not be counted as a new header.
5629 */
5630
Willy Tarreaufa355d42009-11-29 18:12:29 +01005631 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005632 cur_end += delta;
5633
Willy Tarreau9cdde232007-05-02 20:58:19 +02005634 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005635 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005636 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005637 cur_ptr, cur_end + 1,
5638 NULL, NULL);
5639 if (unlikely(!cur_end))
5640 return -1;
5641
5642 /* we have a full respnse and we know that we have either a CR
5643 * or an LF at <ptr>.
5644 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005645 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005646 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5647 /* there is no point trying this regex on headers */
5648 return 1;
5649 }
5650 }
5651 *cur_end = term; /* restore the string terminator */
5652 return done;
5653}
5654
5655
5656
5657/*
5658 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5659 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5660 * unparsable response.
5661 */
5662int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5663{
Willy Tarreau3d300592007-03-18 18:34:41 +01005664 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005665 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005666 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005667 int ret;
5668
5669 /*
5670 * The interleaving of transformations and verdicts
5671 * makes it difficult to decide to continue or stop
5672 * the evaluation.
5673 */
5674
Willy Tarreau3d300592007-03-18 18:34:41 +01005675 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005676 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5677 exp->action == ACT_PASS)) {
5678 exp = exp->next;
5679 continue;
5680 }
5681
5682 /* Apply the filter to the status line. */
5683 ret = apply_filter_to_sts_line(t, rtr, exp);
5684 if (unlikely(ret < 0))
5685 return -1;
5686
5687 if (likely(ret == 0)) {
5688 /* The filter did not match the response, it can be
5689 * iterated through all headers.
5690 */
5691 apply_filter_to_resp_headers(t, rtr, exp);
5692 }
5693 exp = exp->next;
5694 }
5695 return 0;
5696}
5697
5698
5699
5700/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005701 * Manage server-side cookies. It can impact performance by about 2% so it is
5702 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005703 */
5704void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5705{
5706 struct http_txn *txn = &t->txn;
5707 char *p1, *p2, *p3, *p4;
5708
Willy Tarreaua15645d2007-03-18 16:22:39 +01005709 char *cur_ptr, *cur_end, *cur_next;
5710 int cur_idx, old_idx, delta;
5711
Willy Tarreaua15645d2007-03-18 16:22:39 +01005712 /* Iterate through the headers.
5713 * we start with the start line.
5714 */
5715 old_idx = 0;
5716 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5717
5718 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5719 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005720 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005721
5722 cur_hdr = &txn->hdr_idx.v[cur_idx];
5723 cur_ptr = cur_next;
5724 cur_end = cur_ptr + cur_hdr->len;
5725 cur_next = cur_end + cur_hdr->cr + 1;
5726
5727 /* We have one full header between cur_ptr and cur_end, and the
5728 * next header starts at cur_next. We're only interested in
5729 * "Cookie:" headers.
5730 */
5731
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005732 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5733 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005734 old_idx = cur_idx;
5735 continue;
5736 }
5737
5738 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005739 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005740
5741
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005742 /* maybe we only wanted to see if there was a set-cookie. Note that
5743 * the cookie capture is declared in the fronend.
5744 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005745 if (t->be->cookie_name == NULL &&
5746 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005747 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005748 return;
5749
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005750 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005751
5752 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005753 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5754 break;
5755
5756 /* p1 is at the beginning of the cookie name */
5757 p2 = p1;
5758
5759 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5760 p2++;
5761
5762 if (p2 == cur_end || *p2 == ';') /* next cookie */
5763 break;
5764
5765 p3 = p2 + 1; /* skip the '=' sign */
5766 if (p3 == cur_end)
5767 break;
5768
5769 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005770 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005771 p4++;
5772
5773 /* here, we have the cookie name between p1 and p2,
5774 * and its value between p3 and p4.
5775 * we can process it.
5776 */
5777
5778 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005779 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005780 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005781 (p4 - p1 >= t->fe->capture_namelen) &&
5782 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005783 int log_len = p4 - p1;
5784
Willy Tarreau086b3b42007-05-13 21:45:51 +02005785 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005786 Alert("HTTP logging : out of memory.\n");
5787 }
5788
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005789 if (log_len > t->fe->capture_len)
5790 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005791 memcpy(txn->srv_cookie, p1, log_len);
5792 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005793 }
5794
5795 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005796 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5797 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005798 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005799 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005800
5801 /* If the cookie is in insert mode on a known server, we'll delete
5802 * this occurrence because we'll insert another one later.
5803 * We'll delete it too if the "indirect" option is set and we're in
5804 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005805 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5806 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005807 /* this header must be deleted */
5808 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5809 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5810 txn->hdr_idx.used--;
5811 cur_hdr->len = 0;
5812 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005813 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005814
Willy Tarreau3d300592007-03-18 18:34:41 +01005815 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005816 }
5817 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005818 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005819 /* replace bytes p3->p4 with the cookie name associated
5820 * with this server since we know it.
5821 */
5822 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5823 cur_hdr->len += delta;
5824 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005825 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005826
Willy Tarreau3d300592007-03-18 18:34:41 +01005827 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005828 }
5829 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005830 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005831 /* insert the cookie name associated with this server
5832 * before existing cookie, and insert a delimitor between them..
5833 */
5834 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5835 cur_hdr->len += delta;
5836 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005837 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005838
5839 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005840 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005841 }
5842 }
5843 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005844 else if (t->be->appsession_name != NULL) {
5845 int cmp_len, value_len;
5846 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005847
Cyril Bontéb21570a2009-11-29 20:04:48 +01005848 if (t->be->options2 & PR_O2_AS_PFX) {
5849 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5850 value_begin = p1 + t->be->appsession_name_len;
5851 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5852 } else {
5853 cmp_len = p2 - p1;
5854 value_begin = p3;
5855 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005856 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005857
5858 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5859 /* Cool... it's the right one */
5860 if (t->sessid != NULL) {
5861 /* free previously allocated memory as we don't need it anymore */
5862 pool_free2(apools.sessid, t->sessid);
5863 }
5864 /* Store the sessid in the session for future use */
5865 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5866 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5867 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5868 return;
5869 }
5870 memcpy(t->sessid, value_begin, value_len);
5871 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005872 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005873 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005874 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5875 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005876 /* keep the link from this header to next one */
5877 old_idx = cur_idx;
5878 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005879
5880 if (t->sessid != NULL) {
5881 appsess *asession = NULL;
5882 /* only do insert, if lookup fails */
5883 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5884 if (asession == NULL) {
5885 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5886 Alert("Not enough Memory process_srv():asession:calloc().\n");
5887 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5888 return;
5889 }
5890 if ((asession->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(asession->sessid, t->sessid, t->be->appsession_len);
5896 asession->sessid[t->be->appsession_len] = 0;
5897
5898 size_t server_id_len = strlen(t->srv->id) + 1;
5899 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5900 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5901 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5902 return;
5903 }
5904 asession->serverid[0] = '\0';
5905 memcpy(asession->serverid, t->srv->id, server_id_len);
5906
5907 asession->request_count = 0;
5908 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5909 }
5910
5911 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5912 asession->request_count++;
5913 }
5914
5915#if defined(DEBUG_HASH)
5916 Alert("manage_server_side_cookies\n");
5917 appsession_hash_dump(&(t->be->htbl_proxy));
5918#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005919}
5920
5921
5922
5923/*
5924 * Check if response is cacheable or not. Updates t->flags.
5925 */
5926void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5927{
5928 struct http_txn *txn = &t->txn;
5929 char *p1, *p2;
5930
5931 char *cur_ptr, *cur_end, *cur_next;
5932 int cur_idx;
5933
Willy Tarreau5df51872007-11-25 16:20:08 +01005934 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005935 return;
5936
5937 /* Iterate through the headers.
5938 * we start with the start line.
5939 */
5940 cur_idx = 0;
5941 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5942
5943 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5944 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005945 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005946
5947 cur_hdr = &txn->hdr_idx.v[cur_idx];
5948 cur_ptr = cur_next;
5949 cur_end = cur_ptr + cur_hdr->len;
5950 cur_next = cur_end + cur_hdr->cr + 1;
5951
5952 /* We have one full header between cur_ptr and cur_end, and the
5953 * next header starts at cur_next. We're only interested in
5954 * "Cookie:" headers.
5955 */
5956
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005957 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5958 if (val) {
5959 if ((cur_end - (cur_ptr + val) >= 8) &&
5960 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5961 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5962 return;
5963 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005964 }
5965
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005966 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5967 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005968 continue;
5969
5970 /* OK, right now we know we have a cache-control header at cur_ptr */
5971
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005972 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005973
5974 if (p1 >= cur_end) /* no more info */
5975 continue;
5976
5977 /* p1 is at the beginning of the value */
5978 p2 = p1;
5979
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005980 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005981 p2++;
5982
5983 /* we have a complete value between p1 and p2 */
5984 if (p2 < cur_end && *p2 == '=') {
5985 /* we have something of the form no-cache="set-cookie" */
5986 if ((cur_end - p1 >= 21) &&
5987 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5988 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005989 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005990 continue;
5991 }
5992
5993 /* OK, so we know that either p2 points to the end of string or to a comma */
5994 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5995 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5996 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5997 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005998 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005999 return;
6000 }
6001
6002 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006003 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006004 continue;
6005 }
6006 }
6007}
6008
6009
Willy Tarreau58f10d72006-12-04 02:26:12 +01006010/*
6011 * Try to retrieve a known appsession in the URI, then the associated server.
6012 * If the server is found, it's assigned to the session.
6013 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006014void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006015{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006016 char *end_params, *first_param, *cur_param, *next_param;
6017 char separator;
6018 int value_len;
6019
6020 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006021
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006022 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006023 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006024 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006025 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006026
Cyril Bontéb21570a2009-11-29 20:04:48 +01006027 first_param = NULL;
6028 switch (mode) {
6029 case PR_O2_AS_M_PP:
6030 first_param = memchr(begin, ';', len);
6031 break;
6032 case PR_O2_AS_M_QS:
6033 first_param = memchr(begin, '?', len);
6034 break;
6035 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006036
Cyril Bontéb21570a2009-11-29 20:04:48 +01006037 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006038 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006039 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040
Cyril Bontéb21570a2009-11-29 20:04:48 +01006041 switch (mode) {
6042 case PR_O2_AS_M_PP:
6043 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6044 end_params = (char *) begin + len;
6045 }
6046 separator = ';';
6047 break;
6048 case PR_O2_AS_M_QS:
6049 end_params = (char *) begin + len;
6050 separator = '&';
6051 break;
6052 default:
6053 /* unknown mode, shouldn't happen */
6054 return;
6055 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006056
Cyril Bontéb21570a2009-11-29 20:04:48 +01006057 cur_param = next_param = end_params;
6058 while (cur_param > first_param) {
6059 cur_param--;
6060 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6061 /* let's see if this is the appsession parameter */
6062 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6063 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6064 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6065 /* Cool... it's the right one */
6066 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6067 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6068 if (value_len > 0) {
6069 manage_client_side_appsession(t, cur_param, value_len);
6070 }
6071 break;
6072 }
6073 next_param = cur_param;
6074 }
6075 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006077 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006078 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006079#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006080}
6081
6082
Willy Tarreaub2513902006-12-17 14:52:38 +01006083/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006084 * In a GET or HEAD request, check if the requested URI matches the stats uri
6085 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006086 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006087 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006088 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006089 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006090 *
6091 * Returns 1 if the session's state changes, otherwise 0.
6092 */
6093int stats_check_uri_auth(struct session *t, struct proxy *backend)
6094{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006095 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006096 struct uri_auth *uri_auth = backend->uri_auth;
6097 struct user_auth *user;
6098 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006099 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006100
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006101 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006103 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006104 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006105 return 0;
6106
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006107 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006108
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006109 /* the URI is in h */
6110 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006111 return 0;
6112
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006113 h += uri_auth->uri_len;
6114 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6115 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006116 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006117 break;
6118 }
6119 h++;
6120 }
6121
6122 if (uri_auth->refresh) {
6123 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6124 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6125 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006126 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006127 break;
6128 }
6129 h++;
6130 }
6131 }
6132
Willy Tarreau55bb8452007-10-17 18:44:57 +02006133 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6134 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6135 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006136 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006137 break;
6138 }
6139 h++;
6140 }
6141
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006142 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6143
Willy Tarreaub2513902006-12-17 14:52:38 +01006144 /* we are in front of a interceptable URI. Let's check
6145 * if there's an authentication and if it's valid.
6146 */
6147 user = uri_auth->users;
6148 if (!user) {
6149 /* no user auth required, it's OK */
6150 authenticated = 1;
6151 } else {
6152 authenticated = 0;
6153
6154 /* a user list is defined, we have to check.
6155 * skip 21 chars for "Authorization: Basic ".
6156 */
6157
6158 /* FIXME: this should move to an earlier place */
6159 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006160 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6161 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6162 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006163 if (len > 14 &&
6164 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006165 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006166 break;
6167 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006168 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006169 }
6170
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006171 if (txn->auth_hdr.len < 21 ||
6172 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006173 user = NULL;
6174
6175 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006176 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6177 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006178 user->user_pwd, user->user_len)) {
6179 authenticated = 1;
6180 break;
6181 }
6182 user = user->next;
6183 }
6184 }
6185
6186 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006187 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006188
6189 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006190 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6191 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006192 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006193 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006194 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006195 if (!(t->flags & SN_ERR_MASK))
6196 t->flags |= SN_ERR_PRXCOND;
6197 if (!(t->flags & SN_FINST_MASK))
6198 t->flags |= SN_FINST_R;
6199 return 1;
6200 }
6201
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006202 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006203 * data.
6204 */
Willy Tarreau70089872008-06-13 21:12:51 +02006205 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006206 t->data_source = DATA_SRC_STATS;
6207 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006208 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006209 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6210 t->rep->prod->private = t;
6211 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006212 return 1;
6213}
6214
Willy Tarreau4076a152009-04-02 15:18:36 +02006215/*
6216 * Capture a bad request or response and archive it in the proxy's structure.
6217 */
6218void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6219 struct buffer *buf, struct http_msg *msg,
6220 struct proxy *other_end)
6221{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006222 es->len = buf->r - (buf->data + msg->som);
6223 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006224 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006225 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006226 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006227 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006228 es->when = date; // user-visible date
6229 es->sid = s->uniq_id;
6230 es->srv = s->srv;
6231 es->oe = other_end;
6232 es->src = s->cli_addr;
6233}
Willy Tarreaub2513902006-12-17 14:52:38 +01006234
Willy Tarreaubaaee002006-06-26 02:48:02 +02006235/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236 * Print a debug line with a header
6237 */
6238void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6239{
6240 int len, max;
6241 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006242 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006243 max = end - start;
6244 UBOUND(max, sizeof(trash) - len - 1);
6245 len += strlcpy2(trash + len, start, max + 1);
6246 trash[len++] = '\n';
6247 write(1, trash, len);
6248}
6249
Willy Tarreau0937bc42009-12-22 15:03:09 +01006250/*
6251 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6252 * the required fields are properly allocated and that we only need to (re)init
6253 * them. This should be used before processing any new request.
6254 */
6255void http_init_txn(struct session *s)
6256{
6257 struct http_txn *txn = &s->txn;
6258 struct proxy *fe = s->fe;
6259
6260 txn->flags = 0;
6261 txn->status = -1;
6262
6263 txn->req.sol = txn->req.eol = NULL;
6264 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6265 txn->rsp.sol = txn->rsp.eol = NULL;
6266 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6267 txn->req.hdr_content_len = 0LL;
6268 txn->rsp.hdr_content_len = 0LL;
6269 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6270 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6271 chunk_reset(&txn->auth_hdr);
6272
6273 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6274 if (fe->options2 & PR_O2_REQBUG_OK)
6275 txn->req.err_pos = -1; /* let buggy requests pass */
6276
6277 if (txn->req.cap)
6278 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6279
6280 if (txn->rsp.cap)
6281 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6282
6283 if (txn->hdr_idx.v)
6284 hdr_idx_init(&txn->hdr_idx);
6285}
6286
6287/* to be used at the end of a transaction */
6288void http_end_txn(struct session *s)
6289{
6290 struct http_txn *txn = &s->txn;
6291
6292 /* these ones will have been dynamically allocated */
6293 pool_free2(pool2_requri, txn->uri);
6294 pool_free2(pool2_capture, txn->cli_cookie);
6295 pool_free2(pool2_capture, txn->srv_cookie);
6296 txn->uri = NULL;
6297 txn->srv_cookie = NULL;
6298 txn->cli_cookie = NULL;
6299}
6300
6301/* to be used at the end of a transaction to prepare a new one */
6302void http_reset_txn(struct session *s)
6303{
6304 http_end_txn(s);
6305 http_init_txn(s);
6306
6307 s->be = s->fe;
6308 s->req->analysers = s->listener->analysers;
6309 s->logs.logwait = s->fe->to_log;
6310 s->srv = s->prev_srv = s->srv_conn = NULL;
6311 s->pend_pos = NULL;
6312 s->conn_retries = s->be->conn_retries;
6313
6314 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6315
6316 s->req->rto = s->fe->timeout.client;
6317 s->req->wto = s->be->timeout.server;
6318 s->req->cto = s->be->timeout.connect;
6319
6320 s->rep->rto = s->be->timeout.server;
6321 s->rep->wto = s->fe->timeout.client;
6322 s->rep->cto = TICK_ETERNITY;
6323
6324 s->req->rex = TICK_ETERNITY;
6325 s->req->wex = TICK_ETERNITY;
6326 s->req->analyse_exp = TICK_ETERNITY;
6327 s->rep->rex = TICK_ETERNITY;
6328 s->rep->wex = TICK_ETERNITY;
6329 s->rep->analyse_exp = TICK_ETERNITY;
6330}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006331
Willy Tarreau8797c062007-05-07 00:55:35 +02006332/************************************************************************/
6333/* The code below is dedicated to ACL parsing and matching */
6334/************************************************************************/
6335
6336
6337
6338
6339/* 1. Check on METHOD
6340 * We use the pre-parsed method if it is known, and store its number as an
6341 * integer. If it is unknown, we use the pointer and the length.
6342 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006343static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006344{
6345 int len, meth;
6346
Willy Tarreauae8b7962007-06-09 23:10:04 +02006347 len = strlen(*text);
6348 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006349
6350 pattern->val.i = meth;
6351 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006352 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006353 if (!pattern->ptr.str)
6354 return 0;
6355 pattern->len = len;
6356 }
6357 return 1;
6358}
6359
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006360static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006361acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6362 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006363{
6364 int meth;
6365 struct http_txn *txn = l7;
6366
Willy Tarreaub6866442008-07-14 23:54:42 +02006367 if (!txn)
6368 return 0;
6369
Willy Tarreau655dce92009-11-08 13:10:58 +01006370 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006371 return 0;
6372
Willy Tarreau8797c062007-05-07 00:55:35 +02006373 meth = txn->meth;
6374 test->i = meth;
6375 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006376 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6377 /* ensure the indexes are not affected */
6378 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006379 test->len = txn->req.sl.rq.m_l;
6380 test->ptr = txn->req.sol;
6381 }
6382 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6383 return 1;
6384}
6385
6386static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6387{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006388 int icase;
6389
Willy Tarreau8797c062007-05-07 00:55:35 +02006390 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006391 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006392
6393 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006394 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006395
6396 /* Other method, we must compare the strings */
6397 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006398 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006399
6400 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6401 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6402 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006403 return ACL_PAT_FAIL;
6404 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006405}
6406
6407/* 2. Check on Request/Status Version
6408 * We simply compare strings here.
6409 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006410static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006411{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006412 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006413 if (!pattern->ptr.str)
6414 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006415 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006416 return 1;
6417}
6418
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006419static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006420acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6421 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006422{
6423 struct http_txn *txn = l7;
6424 char *ptr;
6425 int len;
6426
Willy Tarreaub6866442008-07-14 23:54:42 +02006427 if (!txn)
6428 return 0;
6429
Willy Tarreau655dce92009-11-08 13:10:58 +01006430 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006431 return 0;
6432
Willy Tarreau8797c062007-05-07 00:55:35 +02006433 len = txn->req.sl.rq.v_l;
6434 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6435
6436 while ((len-- > 0) && (*ptr++ != '/'));
6437 if (len <= 0)
6438 return 0;
6439
6440 test->ptr = ptr;
6441 test->len = len;
6442
6443 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6444 return 1;
6445}
6446
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006447static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006448acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6449 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006450{
6451 struct http_txn *txn = l7;
6452 char *ptr;
6453 int len;
6454
Willy Tarreaub6866442008-07-14 23:54:42 +02006455 if (!txn)
6456 return 0;
6457
Willy Tarreau655dce92009-11-08 13:10:58 +01006458 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006459 return 0;
6460
Willy Tarreau8797c062007-05-07 00:55:35 +02006461 len = txn->rsp.sl.st.v_l;
6462 ptr = txn->rsp.sol;
6463
6464 while ((len-- > 0) && (*ptr++ != '/'));
6465 if (len <= 0)
6466 return 0;
6467
6468 test->ptr = ptr;
6469 test->len = len;
6470
6471 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6472 return 1;
6473}
6474
6475/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006476static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006477acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6478 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006479{
6480 struct http_txn *txn = l7;
6481 char *ptr;
6482 int len;
6483
Willy Tarreaub6866442008-07-14 23:54:42 +02006484 if (!txn)
6485 return 0;
6486
Willy Tarreau655dce92009-11-08 13:10:58 +01006487 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006488 return 0;
6489
Willy Tarreau8797c062007-05-07 00:55:35 +02006490 len = txn->rsp.sl.st.c_l;
6491 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6492
6493 test->i = __strl2ui(ptr, len);
6494 test->flags = ACL_TEST_F_VOL_1ST;
6495 return 1;
6496}
6497
6498/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006499static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006500acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6501 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006502{
6503 struct http_txn *txn = l7;
6504
Willy Tarreaub6866442008-07-14 23:54:42 +02006505 if (!txn)
6506 return 0;
6507
Willy Tarreau655dce92009-11-08 13:10:58 +01006508 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006509 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006510
Willy Tarreauc11416f2007-06-17 16:58:38 +02006511 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6512 /* ensure the indexes are not affected */
6513 return 0;
6514
Willy Tarreau8797c062007-05-07 00:55:35 +02006515 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006516 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006517
Willy Tarreauf3d25982007-05-08 22:45:09 +02006518 /* we do not need to set READ_ONLY because the data is in a buffer */
6519 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006520 return 1;
6521}
6522
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006523static int
6524acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6525 struct acl_expr *expr, struct acl_test *test)
6526{
6527 struct http_txn *txn = l7;
6528
Willy Tarreaub6866442008-07-14 23:54:42 +02006529 if (!txn)
6530 return 0;
6531
Willy Tarreau655dce92009-11-08 13:10:58 +01006532 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006533 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006534
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006535 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6536 /* ensure the indexes are not affected */
6537 return 0;
6538
6539 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006540 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 +01006541 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6542 test->i = AF_INET;
6543
6544 /*
6545 * If we are parsing url in frontend space, we prepare backend stage
6546 * to not parse again the same url ! optimization lazyness...
6547 */
6548 if (px->options & PR_O_HTTP_PROXY)
6549 l4->flags |= SN_ADDR_SET;
6550
6551 test->flags = ACL_TEST_F_READ_ONLY;
6552 return 1;
6553}
6554
6555static int
6556acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6557 struct acl_expr *expr, struct acl_test *test)
6558{
6559 struct http_txn *txn = l7;
6560
Willy Tarreaub6866442008-07-14 23:54:42 +02006561 if (!txn)
6562 return 0;
6563
Willy Tarreau655dce92009-11-08 13:10:58 +01006564 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006565 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006566
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006567 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6568 /* ensure the indexes are not affected */
6569 return 0;
6570
6571 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006572 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 +01006573 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6574
6575 if (px->options & PR_O_HTTP_PROXY)
6576 l4->flags |= SN_ADDR_SET;
6577
6578 test->flags = ACL_TEST_F_READ_ONLY;
6579 return 1;
6580}
6581
Willy Tarreauc11416f2007-06-17 16:58:38 +02006582/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6583 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6584 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006585static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006586acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006587 struct acl_expr *expr, struct acl_test *test)
6588{
6589 struct http_txn *txn = l7;
6590 struct hdr_idx *idx = &txn->hdr_idx;
6591 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006592
Willy Tarreaub6866442008-07-14 23:54:42 +02006593 if (!txn)
6594 return 0;
6595
Willy Tarreau33a7e692007-06-10 19:45:56 +02006596 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6597 /* search for header from the beginning */
6598 ctx->idx = 0;
6599
Willy Tarreau33a7e692007-06-10 19:45:56 +02006600 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6601 test->flags |= ACL_TEST_F_FETCH_MORE;
6602 test->flags |= ACL_TEST_F_VOL_HDR;
6603 test->len = ctx->vlen;
6604 test->ptr = (char *)ctx->line + ctx->val;
6605 return 1;
6606 }
6607
6608 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6609 test->flags |= ACL_TEST_F_VOL_HDR;
6610 return 0;
6611}
6612
Willy Tarreau33a7e692007-06-10 19:45:56 +02006613static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006614acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6615 struct acl_expr *expr, struct acl_test *test)
6616{
6617 struct http_txn *txn = l7;
6618
Willy Tarreaub6866442008-07-14 23:54:42 +02006619 if (!txn)
6620 return 0;
6621
Willy Tarreau655dce92009-11-08 13:10:58 +01006622 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006623 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006624
Willy Tarreauc11416f2007-06-17 16:58:38 +02006625 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6626 /* ensure the indexes are not affected */
6627 return 0;
6628
6629 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6630}
6631
6632static int
6633acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6634 struct acl_expr *expr, struct acl_test *test)
6635{
6636 struct http_txn *txn = l7;
6637
Willy Tarreaub6866442008-07-14 23:54:42 +02006638 if (!txn)
6639 return 0;
6640
Willy Tarreau655dce92009-11-08 13:10:58 +01006641 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006642 return 0;
6643
6644 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6645}
6646
6647/* 6. Check on HTTP header count. The number of occurrences is returned.
6648 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6649 */
6650static int
6651acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006652 struct acl_expr *expr, struct acl_test *test)
6653{
6654 struct http_txn *txn = l7;
6655 struct hdr_idx *idx = &txn->hdr_idx;
6656 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006657 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006658
Willy Tarreaub6866442008-07-14 23:54:42 +02006659 if (!txn)
6660 return 0;
6661
Willy Tarreau33a7e692007-06-10 19:45:56 +02006662 ctx.idx = 0;
6663 cnt = 0;
6664 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6665 cnt++;
6666
6667 test->i = cnt;
6668 test->flags = ACL_TEST_F_VOL_HDR;
6669 return 1;
6670}
6671
Willy Tarreauc11416f2007-06-17 16:58:38 +02006672static int
6673acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6674 struct acl_expr *expr, struct acl_test *test)
6675{
6676 struct http_txn *txn = l7;
6677
Willy Tarreaub6866442008-07-14 23:54:42 +02006678 if (!txn)
6679 return 0;
6680
Willy Tarreau655dce92009-11-08 13:10:58 +01006681 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006682 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006683
Willy Tarreauc11416f2007-06-17 16:58:38 +02006684 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6685 /* ensure the indexes are not affected */
6686 return 0;
6687
6688 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6689}
6690
6691static int
6692acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6693 struct acl_expr *expr, struct acl_test *test)
6694{
6695 struct http_txn *txn = l7;
6696
Willy Tarreaub6866442008-07-14 23:54:42 +02006697 if (!txn)
6698 return 0;
6699
Willy Tarreau655dce92009-11-08 13:10:58 +01006700 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006701 return 0;
6702
6703 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6704}
6705
Willy Tarreau33a7e692007-06-10 19:45:56 +02006706/* 7. Check on HTTP header's integer value. The integer value is returned.
6707 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006708 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006709 */
6710static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006711acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006712 struct acl_expr *expr, struct acl_test *test)
6713{
6714 struct http_txn *txn = l7;
6715 struct hdr_idx *idx = &txn->hdr_idx;
6716 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006717
Willy Tarreaub6866442008-07-14 23:54:42 +02006718 if (!txn)
6719 return 0;
6720
Willy Tarreau33a7e692007-06-10 19:45:56 +02006721 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6722 /* search for header from the beginning */
6723 ctx->idx = 0;
6724
Willy Tarreau33a7e692007-06-10 19:45:56 +02006725 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6726 test->flags |= ACL_TEST_F_FETCH_MORE;
6727 test->flags |= ACL_TEST_F_VOL_HDR;
6728 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6729 return 1;
6730 }
6731
6732 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6733 test->flags |= ACL_TEST_F_VOL_HDR;
6734 return 0;
6735}
6736
Willy Tarreauc11416f2007-06-17 16:58:38 +02006737static int
6738acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6739 struct acl_expr *expr, struct acl_test *test)
6740{
6741 struct http_txn *txn = l7;
6742
Willy Tarreaub6866442008-07-14 23:54:42 +02006743 if (!txn)
6744 return 0;
6745
Willy Tarreau655dce92009-11-08 13:10:58 +01006746 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006747 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006748
Willy Tarreauc11416f2007-06-17 16:58:38 +02006749 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6750 /* ensure the indexes are not affected */
6751 return 0;
6752
6753 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6754}
6755
6756static int
6757acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6758 struct acl_expr *expr, struct acl_test *test)
6759{
6760 struct http_txn *txn = l7;
6761
Willy Tarreaub6866442008-07-14 23:54:42 +02006762 if (!txn)
6763 return 0;
6764
Willy Tarreau655dce92009-11-08 13:10:58 +01006765 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006766 return 0;
6767
6768 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6769}
6770
Willy Tarreau106f9792009-09-19 07:54:16 +02006771/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6772 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6773 */
6774static int
6775acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6776 struct acl_expr *expr, struct acl_test *test)
6777{
6778 struct http_txn *txn = l7;
6779 struct hdr_idx *idx = &txn->hdr_idx;
6780 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6781
6782 if (!txn)
6783 return 0;
6784
6785 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6786 /* search for header from the beginning */
6787 ctx->idx = 0;
6788
6789 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6790 test->flags |= ACL_TEST_F_FETCH_MORE;
6791 test->flags |= ACL_TEST_F_VOL_HDR;
6792 /* Same optimization as url_ip */
6793 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6794 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6795 test->ptr = (void *)&l4->srv_addr.sin_addr;
6796 test->i = AF_INET;
6797 return 1;
6798 }
6799
6800 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6801 test->flags |= ACL_TEST_F_VOL_HDR;
6802 return 0;
6803}
6804
6805static int
6806acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6807 struct acl_expr *expr, struct acl_test *test)
6808{
6809 struct http_txn *txn = l7;
6810
6811 if (!txn)
6812 return 0;
6813
Willy Tarreau655dce92009-11-08 13:10:58 +01006814 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006815 return 0;
6816
6817 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6818 /* ensure the indexes are not affected */
6819 return 0;
6820
6821 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6822}
6823
6824static int
6825acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6826 struct acl_expr *expr, struct acl_test *test)
6827{
6828 struct http_txn *txn = l7;
6829
6830 if (!txn)
6831 return 0;
6832
Willy Tarreau655dce92009-11-08 13:10:58 +01006833 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006834 return 0;
6835
6836 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6837}
6838
Willy Tarreau737b0c12007-06-10 21:28:46 +02006839/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6840 * the first '/' after the possible hostname, and ends before the possible '?'.
6841 */
6842static int
6843acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6844 struct acl_expr *expr, struct acl_test *test)
6845{
6846 struct http_txn *txn = l7;
6847 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006848
Willy Tarreaub6866442008-07-14 23:54:42 +02006849 if (!txn)
6850 return 0;
6851
Willy Tarreau655dce92009-11-08 13:10:58 +01006852 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006853 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006854
Willy Tarreauc11416f2007-06-17 16:58:38 +02006855 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6856 /* ensure the indexes are not affected */
6857 return 0;
6858
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006859 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 +01006860 ptr = http_get_path(txn);
6861 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006862 return 0;
6863
6864 /* OK, we got the '/' ! */
6865 test->ptr = ptr;
6866
6867 while (ptr < end && *ptr != '?')
6868 ptr++;
6869
6870 test->len = ptr - test->ptr;
6871
6872 /* we do not need to set READ_ONLY because the data is in a buffer */
6873 test->flags = ACL_TEST_F_VOL_1ST;
6874 return 1;
6875}
6876
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006877static int
6878acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6879 struct acl_expr *expr, struct acl_test *test)
6880{
6881 struct buffer *req = s->req;
6882 struct http_txn *txn = &s->txn;
6883 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006884
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006885 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6886 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6887 */
6888
6889 if (!s || !req)
6890 return 0;
6891
Willy Tarreau655dce92009-11-08 13:10:58 +01006892 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006893 /* Already decoded as OK */
6894 test->flags |= ACL_TEST_F_SET_RES_PASS;
6895 return 1;
6896 }
6897
6898 /* Try to decode HTTP request */
6899 if (likely(req->lr < req->r))
6900 http_msg_analyzer(req, msg, &txn->hdr_idx);
6901
Willy Tarreau655dce92009-11-08 13:10:58 +01006902 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006903 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6904 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6905 return 1;
6906 }
6907 /* wait for final state */
6908 test->flags |= ACL_TEST_F_MAY_CHANGE;
6909 return 0;
6910 }
6911
6912 /* OK we got a valid HTTP request. We have some minor preparation to
6913 * perform so that further checks can rely on HTTP tests.
6914 */
6915 msg->sol = req->data + msg->som;
6916 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6917 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6918 s->flags |= SN_REDIRECTABLE;
6919
6920 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6921 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6922 return 1;
6923 }
6924
6925 test->flags |= ACL_TEST_F_SET_RES_PASS;
6926 return 1;
6927}
6928
Willy Tarreau8797c062007-05-07 00:55:35 +02006929
6930/************************************************************************/
6931/* All supported keywords must be declared here. */
6932/************************************************************************/
6933
6934/* Note: must not be declared <const> as its list will be overwritten */
6935static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006936 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6937
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006938 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6939 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6940 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6941 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006942
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006943 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6944 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6945 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6946 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6947 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6948 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6949 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6950 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6951 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006952
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006953 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6954 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6955 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6956 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6957 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6958 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6959 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6960 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6961 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6962 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006963 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006964
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006965 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6966 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6967 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6968 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6969 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6970 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6971 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6972 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6973 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006974 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006975
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006976 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6977 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6978 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6979 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6980 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6981 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6982 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006983
Willy Tarreauf3d25982007-05-08 22:45:09 +02006984 { NULL, NULL, NULL, NULL },
6985
6986#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006987 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6988 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6989 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6990 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6991 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6992 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6993 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6994
Willy Tarreau8797c062007-05-07 00:55:35 +02006995 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6996 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6997 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6998 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6999 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7000 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7001 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7002 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7003
7004 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7005 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7006 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7007 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7008 { NULL, NULL, NULL, NULL },
7009#endif
7010}};
7011
7012
7013__attribute__((constructor))
7014static void __http_protocol_init(void)
7015{
7016 acl_register_keywords(&acl_kws);
7017}
7018
7019
Willy Tarreau58f10d72006-12-04 02:26:12 +01007020/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007021 * Local variables:
7022 * c-indent-level: 8
7023 * c-basic-offset: 8
7024 * End:
7025 */