blob: 915abc244429cc896128972ae7ce5bbd02e0126a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaua9679ac2010-01-03 17:32:57 +010082/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020083const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
103const char *HTTP_401_fmt =
104 "HTTP/1.0 401 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200107 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
114 [HTTP_ERR_400] = 400,
115 [HTTP_ERR_403] = 403,
116 [HTTP_ERR_408] = 408,
117 [HTTP_ERR_500] = 500,
118 [HTTP_ERR_502] = 502,
119 [HTTP_ERR_503] = 503,
120 [HTTP_ERR_504] = 504,
121};
122
Willy Tarreau80587432006-12-24 17:47:20 +0100123static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100125 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 "Cache-Control: no-cache\r\n"
127 "Connection: close\r\n"
128 "Content-Type: text/html\r\n"
129 "\r\n"
130 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
131
132 [HTTP_ERR_403] =
133 "HTTP/1.0 403 Forbidden\r\n"
134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
139
140 [HTTP_ERR_408] =
141 "HTTP/1.0 408 Request Time-out\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
147
148 [HTTP_ERR_500] =
149 "HTTP/1.0 500 Server Error\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
155
156 [HTTP_ERR_502] =
157 "HTTP/1.0 502 Bad Gateway\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
163
164 [HTTP_ERR_503] =
165 "HTTP/1.0 503 Service Unavailable\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
171
172 [HTTP_ERR_504] =
173 "HTTP/1.0 504 Gateway Time-out\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
179
180};
181
Willy Tarreau80587432006-12-24 17:47:20 +0100182/* We must put the messages here since GCC cannot initialize consts depending
183 * on strlen().
184 */
185struct chunk http_err_chunks[HTTP_ERR_SIZE];
186
Willy Tarreau42250582007-04-01 01:30:43 +0200187#define FD_SETS_ARE_BITFIELDS
188#ifdef FD_SETS_ARE_BITFIELDS
189/*
190 * This map is used with all the FD_* macros to check whether a particular bit
191 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
192 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
193 * byte should be encoded. Be careful to always pass bytes from 0 to 255
194 * exclusively to the macros.
195 */
196fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
198
199#else
200#error "Check if your OS uses bitfields for fd_sets"
201#endif
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203void init_proto_http()
204{
Willy Tarreau42250582007-04-01 01:30:43 +0200205 int i;
206 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100207 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200208
Willy Tarreau80587432006-12-24 17:47:20 +0100209 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
210 if (!http_err_msgs[msg]) {
211 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
212 abort();
213 }
214
215 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
216 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
217 }
Willy Tarreau42250582007-04-01 01:30:43 +0200218
219 /* initialize the log header encoding map : '{|}"#' should be encoded with
220 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
221 * URL encoding only requires '"', '#' to be encoded as well as non-
222 * printable characters above.
223 */
224 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
225 memset(url_encode_map, 0, sizeof(url_encode_map));
226 for (i = 0; i < 32; i++) {
227 FD_SET(i, hdr_encode_map);
228 FD_SET(i, url_encode_map);
229 }
230 for (i = 127; i < 256; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234
235 tmp = "\"#{|}";
236 while (*tmp) {
237 FD_SET(*tmp, hdr_encode_map);
238 tmp++;
239 }
240
241 tmp = "\"#";
242 while (*tmp) {
243 FD_SET(*tmp, url_encode_map);
244 tmp++;
245 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200246
247 /* memory allocations */
248 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200249 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100250}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
Willy Tarreau53b6c742006-12-17 13:37:46 +0100252/*
253 * We have 26 list of methods (1 per first letter), each of which can have
254 * up to 3 entries (2 valid, 1 null).
255 */
256struct http_method_desc {
257 http_meth_t meth;
258 int len;
259 const char text[8];
260};
261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100262const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263 ['C' - 'A'] = {
264 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
265 },
266 ['D' - 'A'] = {
267 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
268 },
269 ['G' - 'A'] = {
270 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
271 },
272 ['H' - 'A'] = {
273 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
274 },
275 ['P' - 'A'] = {
276 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
277 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
278 },
279 ['T' - 'A'] = {
280 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
281 },
282 /* rest is empty like this :
283 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
284 */
285};
286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200288 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289 * RFC2616 for those chars.
290 */
291
292const char http_is_spht[256] = {
293 [' '] = 1, ['\t'] = 1,
294};
295
296const char http_is_crlf[256] = {
297 ['\r'] = 1, ['\n'] = 1,
298};
299
300const char http_is_lws[256] = {
301 [' '] = 1, ['\t'] = 1,
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_sep[256] = {
306 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
307 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
308 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
309 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
310 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
311};
312
313const char http_is_ctl[256] = {
314 [0 ... 31] = 1,
315 [127] = 1,
316};
317
318/*
319 * A token is any ASCII char that is neither a separator nor a CTL char.
320 * Do not overwrite values in assignment since gcc-2.95 will not handle
321 * them correctly. Instead, define every non-CTL char's status.
322 */
323const char http_is_token[256] = {
324 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
325 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
326 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
327 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
328 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
329 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
330 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
331 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
332 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
333 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
334 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
335 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
336 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
337 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
338 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
339 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
340 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
341 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
342 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
343 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
344 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
345 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
346 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
347 ['|'] = 1, ['}'] = 0, ['~'] = 1,
348};
349
350
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100351/*
352 * An http ver_token is any ASCII which can be found in an HTTP version,
353 * which includes 'H', 'T', 'P', '/', '.' and any digit.
354 */
355const char http_is_ver_token[256] = {
356 ['.'] = 1, ['/'] = 1,
357 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
358 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
359 ['H'] = 1, ['P'] = 1, ['T'] = 1,
360};
361
362
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100363/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100364 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
365 */
366#if defined(DEBUG_FSM)
367static void http_silent_debug(int line, struct session *s)
368{
369 int size = 0;
370 size += snprintf(trash + size, sizeof(trash) - size,
371 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
372 line,
373 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
374 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
375 write(-1, trash, size);
376 size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
379 line,
380 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
381 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
382
383 write(-1, trash, size);
384}
385#else
386#define http_silent_debug(l,s) do { } while (0)
387#endif
388
389/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100390 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
391 * CRLF. Text length is measured first, so it cannot be NULL.
392 * The header is also automatically added to the index <hdr_idx>, and the end
393 * of headers is automatically adjusted. The number of bytes added is returned
394 * on success, otherwise <0 is returned indicating an error.
395 */
396int http_header_add_tail(struct buffer *b, struct http_msg *msg,
397 struct hdr_idx *hdr_idx, const char *text)
398{
399 int bytes, len;
400
401 len = strlen(text);
402 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
403 if (!bytes)
404 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100405 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100406 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
407}
408
409/*
410 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
411 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
412 * the buffer is only opened and the space reserved, but nothing is copied.
413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
417int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
418 struct hdr_idx *hdr_idx, const char *text, int len)
419{
420 int bytes;
421
422 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428
429/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100430 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
431 * If so, returns the position of the first non-space character relative to
432 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
433 * to return a pointer to the place after the first space. Returns 0 if the
434 * header name does not match. Checks are case-insensitive.
435 */
436int http_header_match2(const char *hdr, const char *end,
437 const char *name, int len)
438{
439 const char *val;
440
441 if (hdr + len >= end)
442 return 0;
443 if (hdr[len] != ':')
444 return 0;
445 if (strncasecmp(hdr, name, len) != 0)
446 return 0;
447 val = hdr + len + 1;
448 while (val < end && HTTP_IS_SPHT(*val))
449 val++;
450 if ((val >= end) && (len + 2 <= end - hdr))
451 return len + 2; /* we may replace starting from second space */
452 return val - hdr;
453}
454
Willy Tarreau33a7e692007-06-10 19:45:56 +0200455/* Find the end of the header value contained between <s> and <e>.
456 * See RFC2616, par 2.2 for more information. Note that it requires
457 * a valid header to return a valid result.
458 */
459const char *find_hdr_value_end(const char *s, const char *e)
460{
461 int quoted, qdpair;
462
463 quoted = qdpair = 0;
464 for (; s < e; s++) {
465 if (qdpair) qdpair = 0;
466 else if (quoted && *s == '\\') qdpair = 1;
467 else if (quoted && *s == '"') quoted = 0;
468 else if (*s == '"') quoted = 1;
469 else if (*s == ',') return s;
470 }
471 return s;
472}
473
474/* Find the first or next occurrence of header <name> in message buffer <sol>
475 * using headers index <idx>, and return it in the <ctx> structure. This
476 * structure holds everything necessary to use the header and find next
477 * occurrence. If its <idx> member is 0, the header is searched from the
478 * beginning. Otherwise, the next occurrence is returned. The function returns
479 * 1 when it finds a value, and 0 when there is no more.
480 */
481int http_find_header2(const char *name, int len,
482 const char *sol, struct hdr_idx *idx,
483 struct hdr_ctx *ctx)
484{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200485 const char *eol, *sov;
486 int cur_idx;
487
488 if (ctx->idx) {
489 /* We have previously returned a value, let's search
490 * another one on the same line.
491 */
492 cur_idx = ctx->idx;
493 sol = ctx->line;
494 sov = sol + ctx->val + ctx->vlen;
495 eol = sol + idx->v[cur_idx].len;
496
497 if (sov >= eol)
498 /* no more values in this header */
499 goto next_hdr;
500
501 /* values remaining for this header, skip the comma */
502 sov++;
503 while (sov < eol && http_is_lws[(unsigned char)*sov])
504 sov++;
505
506 goto return_hdr;
507 }
508
509 /* first request for this header */
510 sol += hdr_idx_first_pos(idx);
511 cur_idx = hdr_idx_first_idx(idx);
512
513 while (cur_idx) {
514 eol = sol + idx->v[cur_idx].len;
515
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200516 if (len == 0) {
517 /* No argument was passed, we want any header.
518 * To achieve this, we simply build a fake request. */
519 while (sol + len < eol && sol[len] != ':')
520 len++;
521 name = sol;
522 }
523
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 if ((len < eol - sol) &&
525 (sol[len] == ':') &&
526 (strncasecmp(sol, name, len) == 0)) {
527
528 sov = sol + len + 1;
529 while (sov < eol && http_is_lws[(unsigned char)*sov])
530 sov++;
531 return_hdr:
532 ctx->line = sol;
533 ctx->idx = cur_idx;
534 ctx->val = sov - sol;
535
536 eol = find_hdr_value_end(sov, eol);
537 ctx->vlen = eol - sov;
538 return 1;
539 }
540 next_hdr:
541 sol = eol + idx->v[cur_idx].cr + 1;
542 cur_idx = idx->v[cur_idx].next;
543 }
544 return 0;
545}
546
547int http_find_header(const char *name,
548 const char *sol, struct hdr_idx *idx,
549 struct hdr_ctx *ctx)
550{
551 return http_find_header2(name, strlen(name), sol, idx, ctx);
552}
553
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100554/* This function handles a server error at the stream interface level. The
555 * stream interface is assumed to be already in a closed state. An optional
556 * message is copied into the input buffer, and an HTTP status code stored.
557 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100558 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100560static void http_server_error(struct session *t, struct stream_interface *si,
561 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100563 buffer_erase(si->ob);
564 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200565 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100566 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100567 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100568 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100569 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 }
571 if (!(t->flags & SN_ERR_MASK))
572 t->flags |= err;
573 if (!(t->flags & SN_FINST_MASK))
574 t->flags |= finst;
575}
576
Willy Tarreau80587432006-12-24 17:47:20 +0100577/* This function returns the appropriate error location for the given session
578 * and message.
579 */
580
581struct chunk *error_message(struct session *s, int msgnum)
582{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200583 if (s->be->errmsg[msgnum].str)
584 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100585 else if (s->fe->errmsg[msgnum].str)
586 return &s->fe->errmsg[msgnum];
587 else
588 return &http_err_chunks[msgnum];
589}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591/*
592 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
593 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
594 */
595static http_meth_t find_http_meth(const char *str, const int len)
596{
597 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100598 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100599
600 m = ((unsigned)*str - 'A');
601
602 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100603 for (h = http_methods[m]; h->len > 0; h++) {
604 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100605 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100606 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100607 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100608 };
609 return HTTP_METH_OTHER;
610 }
611 return HTTP_METH_NONE;
612
613}
614
Willy Tarreau21d2af32008-02-14 20:25:24 +0100615/* Parse the URI from the given transaction (which is assumed to be in request
616 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
617 * It is returned otherwise.
618 */
619static char *
620http_get_path(struct http_txn *txn)
621{
622 char *ptr, *end;
623
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100624 ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100625 end = ptr + txn->req.sl.rq.u_l;
626
627 if (ptr >= end)
628 return NULL;
629
630 /* RFC2616, par. 5.1.2 :
631 * Request-URI = "*" | absuri | abspath | authority
632 */
633
634 if (*ptr == '*')
635 return NULL;
636
637 if (isalpha((unsigned char)*ptr)) {
638 /* this is a scheme as described by RFC3986, par. 3.1 */
639 ptr++;
640 while (ptr < end &&
641 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
642 ptr++;
643 /* skip '://' */
644 if (ptr == end || *ptr++ != ':')
645 return NULL;
646 if (ptr == end || *ptr++ != '/')
647 return NULL;
648 if (ptr == end || *ptr++ != '/')
649 return NULL;
650 }
651 /* skip [user[:passwd]@]host[:[port]] */
652
653 while (ptr < end && *ptr != '/')
654 ptr++;
655
656 if (ptr == end)
657 return NULL;
658
659 /* OK, we got the '/' ! */
660 return ptr;
661}
662
Willy Tarreauefb453c2008-10-26 20:49:47 +0100663/* Returns a 302 for a redirectable request. This may only be called just after
664 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
665 * left unchanged and will follow normal proxy processing.
666 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100668{
669 struct http_txn *txn;
670 struct chunk rdr;
671 char *path;
672 int len;
673
674 /* 1: create the response header */
675 rdr.len = strlen(HTTP_302);
676 rdr.str = trash;
677 memcpy(rdr.str, HTTP_302, rdr.len);
678
679 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200680 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100681 return;
682
683 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
684 rdr.len += s->srv->rdr_len;
685
686 /* 3: add the request URI */
687 txn = &s->txn;
688 path = http_get_path(txn);
689 if (!path)
690 return;
691
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100692 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 +0200693 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100694 return;
695
696 memcpy(rdr.str + rdr.len, path, len);
697 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100698 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
699 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100700
701 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100702 si->shutr(si);
703 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100704 si->err_type = SI_ET_NONE;
705 si->err_loc = NULL;
706 si->state = SI_ST_CLO;
707
708 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100709 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710
711 /* FIXME: we should increase a counter of redirects per server and per backend. */
712 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100713 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100714}
715
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100716/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100717 * that the server side is closed. Note that err_type is actually a
718 * bitmask, where almost only aborts may be cumulated with other
719 * values. We consider that aborted operations are more important
720 * than timeouts or errors due to the fact that nobody else in the
721 * logs might explain incomplete retries. All others should avoid
722 * being cumulated. It should normally not be possible to have multiple
723 * aborts at once, but just in case, the first one in sequence is reported.
724 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100725void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100726{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100727 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100728
729 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100730 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
731 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100732 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100733 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
734 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100735 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100736 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
737 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100738 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100739 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
740 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100741 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100742 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
743 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100744 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100745 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
746 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100747 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100748 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
749 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100750}
751
Willy Tarreau42250582007-04-01 01:30:43 +0200752extern const char sess_term_cond[8];
753extern const char sess_fin_state[8];
754extern const char *monthname[12];
755const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
756const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
757 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
758 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200759struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200760struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100761
Emeric Brun3a058f32009-06-30 18:26:00 +0200762void http_sess_clflog(struct session *s)
763{
764 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
765 struct proxy *fe = s->fe;
766 struct proxy *be = s->be;
767 struct proxy *prx_log;
768 struct http_txn *txn = &s->txn;
769 int tolog, level, err;
770 char *uri, *h;
771 char *svid;
772 struct tm tm;
773 static char tmpline[MAX_SYSLOG_LEN];
774 int hdr;
775 size_t w;
776 int t_request;
777
778 prx_log = fe;
779 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
780 (s->conn_retries != be->conn_retries) ||
781 txn->status >= 500;
782
783 if (s->cli_addr.ss_family == AF_INET)
784 inet_ntop(AF_INET,
785 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
786 pn, sizeof(pn));
787 else
788 inet_ntop(AF_INET6,
789 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
790 pn, sizeof(pn));
791
792 get_gmtime(s->logs.accept_date.tv_sec, &tm);
793
794 /* FIXME: let's limit ourselves to frontend logging for now. */
795 tolog = fe->to_log;
796
797 h = tmpline;
798
799 w = snprintf(h, sizeof(tmpline),
800 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
801 pn,
802 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
803 tm.tm_hour, tm.tm_min, tm.tm_sec);
804 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
805 goto trunc;
806 h += w;
807
808 if (h >= tmpline + sizeof(tmpline) - 4)
809 goto trunc;
810
811 *(h++) = ' ';
812 *(h++) = '\"';
813 uri = txn->uri ? txn->uri : "<BADREQ>";
814 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
815 '#', url_encode_map, uri);
816 *(h++) = '\"';
817
818 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
819 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
820 goto trunc;
821 h += w;
822
823 if (h >= tmpline + sizeof(tmpline) - 9)
824 goto trunc;
825 memcpy(h, " \"-\" \"-\"", 8);
826 h += 8;
827
828 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
829 " %d %03d",
830 (s->cli_addr.ss_family == AF_INET) ?
831 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
832 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
833 (int)s->logs.accept_date.tv_usec/1000);
834 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
835 goto trunc;
836 h += w;
837
838 w = strlen(fe->id);
839 if (h >= tmpline + sizeof(tmpline) - 4 - w)
840 goto trunc;
841 *(h++) = ' ';
842 *(h++) = '\"';
843 memcpy(h, fe->id, w);
844 h += w;
845 *(h++) = '\"';
846
847 w = strlen(be->id);
848 if (h >= tmpline + sizeof(tmpline) - 4 - w)
849 goto trunc;
850 *(h++) = ' ';
851 *(h++) = '\"';
852 memcpy(h, be->id, w);
853 h += w;
854 *(h++) = '\"';
855
856 svid = (tolog & LW_SVID) ?
857 (s->data_source != DATA_SRC_STATS) ?
858 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
859
860 w = strlen(svid);
861 if (h >= tmpline + sizeof(tmpline) - 4 - w)
862 goto trunc;
863 *(h++) = ' ';
864 *(h++) = '\"';
865 memcpy(h, svid, w);
866 h += w;
867 *(h++) = '\"';
868
869 t_request = -1;
870 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
871 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
872 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
873 " %d %ld %ld %ld %ld",
874 t_request,
875 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
876 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
877 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
878 s->logs.t_close);
879 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
880 goto trunc;
881 h += w;
882
883 if (h >= tmpline + sizeof(tmpline) - 8)
884 goto trunc;
885 *(h++) = ' ';
886 *(h++) = '\"';
887 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
888 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
889 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
890 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
891 *(h++) = '\"';
892
893 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
894 " %d %d %d %d %d %ld %ld",
895 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
896 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
897 s->logs.srv_queue_size, s->logs.prx_queue_size);
898
899 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
900 goto trunc;
901 h += w;
902
903 if (txn->cli_cookie) {
904 w = strlen(txn->cli_cookie);
905 if (h >= tmpline + sizeof(tmpline) - 4 - w)
906 goto trunc;
907 *(h++) = ' ';
908 *(h++) = '\"';
909 memcpy(h, txn->cli_cookie, w);
910 h += w;
911 *(h++) = '\"';
912 } else {
913 if (h >= tmpline + sizeof(tmpline) - 5)
914 goto trunc;
915 memcpy(h, " \"-\"", 4);
916 h += 4;
917 }
918
919 if (txn->srv_cookie) {
920 w = strlen(txn->srv_cookie);
921 if (h >= tmpline + sizeof(tmpline) - 4 - w)
922 goto trunc;
923 *(h++) = ' ';
924 *(h++) = '\"';
925 memcpy(h, txn->srv_cookie, w);
926 h += w;
927 *(h++) = '\"';
928 } else {
929 if (h >= tmpline + sizeof(tmpline) - 5)
930 goto trunc;
931 memcpy(h, " \"-\"", 4);
932 h += 4;
933 }
934
935 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
936 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
937 if (h >= sizeof (tmpline) + tmpline - 4)
938 goto trunc;
939 *(h++) = ' ';
940 *(h++) = '\"';
941 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
942 '#', hdr_encode_map, txn->req.cap[hdr]);
943 *(h++) = '\"';
944 }
945 }
946
947 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
948 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
949 if (h >= sizeof (tmpline) + tmpline - 4)
950 goto trunc;
951 *(h++) = ' ';
952 *(h++) = '\"';
953 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
954 '#', hdr_encode_map, txn->rsp.cap[hdr]);
955 *(h++) = '\"';
956 }
957 }
958
959trunc:
960 *h = '\0';
961
962 level = LOG_INFO;
963 if (err && (fe->options2 & PR_O2_LOGERRORS))
964 level = LOG_ERR;
965
966 send_log(prx_log, level, "%s\n", tmpline);
967
968 s->logs.logwait = 0;
969}
970
Willy Tarreau42250582007-04-01 01:30:43 +0200971/*
972 * send a log for the session when we have enough info about it.
973 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100974 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200976{
977 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
978 struct proxy *fe = s->fe;
979 struct proxy *be = s->be;
980 struct proxy *prx_log;
981 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200982 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200983 char *uri, *h;
984 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200985 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200986 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200987 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200988 int hdr;
989
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200990 /* if we don't want to log normal traffic, return now */
991 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
992 (s->conn_retries != be->conn_retries) ||
993 txn->status >= 500;
994 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
995 return;
996
Willy Tarreau42250582007-04-01 01:30:43 +0200997 if (fe->logfac1 < 0 && fe->logfac2 < 0)
998 return;
999 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001000
Emeric Brun3a058f32009-06-30 18:26:00 +02001001 if (prx_log->options2 & PR_O2_CLFLOG)
1002 return http_sess_clflog(s);
1003
Willy Tarreau42250582007-04-01 01:30:43 +02001004 if (s->cli_addr.ss_family == AF_INET)
1005 inet_ntop(AF_INET,
1006 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1007 pn, sizeof(pn));
1008 else
1009 inet_ntop(AF_INET6,
1010 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1011 pn, sizeof(pn));
1012
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001013 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001014
1015 /* FIXME: let's limit ourselves to frontend logging for now. */
1016 tolog = fe->to_log;
1017
1018 h = tmpline;
1019 if (fe->to_log & LW_REQHDR &&
1020 txn->req.cap &&
1021 (h < tmpline + sizeof(tmpline) - 10)) {
1022 *(h++) = ' ';
1023 *(h++) = '{';
1024 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1025 if (hdr)
1026 *(h++) = '|';
1027 if (txn->req.cap[hdr] != NULL)
1028 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1029 '#', hdr_encode_map, txn->req.cap[hdr]);
1030 }
1031 *(h++) = '}';
1032 }
1033
1034 if (fe->to_log & LW_RSPHDR &&
1035 txn->rsp.cap &&
1036 (h < tmpline + sizeof(tmpline) - 7)) {
1037 *(h++) = ' ';
1038 *(h++) = '{';
1039 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1040 if (hdr)
1041 *(h++) = '|';
1042 if (txn->rsp.cap[hdr] != NULL)
1043 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1044 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1045 }
1046 *(h++) = '}';
1047 }
1048
1049 if (h < tmpline + sizeof(tmpline) - 4) {
1050 *(h++) = ' ';
1051 *(h++) = '"';
1052 uri = txn->uri ? txn->uri : "<BADREQ>";
1053 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1054 '#', url_encode_map, uri);
1055 *(h++) = '"';
1056 }
1057 *h = '\0';
1058
1059 svid = (tolog & LW_SVID) ?
1060 (s->data_source != DATA_SRC_STATS) ?
1061 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1062
Willy Tarreau70089872008-06-13 21:12:51 +02001063 t_request = -1;
1064 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1065 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1066
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001067 level = LOG_INFO;
1068 if (err && (fe->options2 & PR_O2_LOGERRORS))
1069 level = LOG_ERR;
1070
1071 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001072 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001073 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1074 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001075 pn,
1076 (s->cli_addr.ss_family == AF_INET) ?
1077 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1078 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001079 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001080 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001081 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001082 t_request,
1083 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001084 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1085 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1086 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1087 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001088 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001089 txn->cli_cookie ? txn->cli_cookie : "-",
1090 txn->srv_cookie ? txn->srv_cookie : "-",
1091 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1092 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1093 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1094 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1095 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001096 (s->flags & SN_REDISP)?"+":"",
1097 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001098 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1099
1100 s->logs.logwait = 0;
1101}
1102
Willy Tarreau117f59e2007-03-04 18:17:17 +01001103
1104/*
1105 * Capture headers from message starting at <som> according to header list
1106 * <cap_hdr>, and fill the <idx> structure appropriately.
1107 */
1108void capture_headers(char *som, struct hdr_idx *idx,
1109 char **cap, struct cap_hdr *cap_hdr)
1110{
1111 char *eol, *sol, *col, *sov;
1112 int cur_idx;
1113 struct cap_hdr *h;
1114 int len;
1115
1116 sol = som + hdr_idx_first_pos(idx);
1117 cur_idx = hdr_idx_first_idx(idx);
1118
1119 while (cur_idx) {
1120 eol = sol + idx->v[cur_idx].len;
1121
1122 col = sol;
1123 while (col < eol && *col != ':')
1124 col++;
1125
1126 sov = col + 1;
1127 while (sov < eol && http_is_lws[(unsigned char)*sov])
1128 sov++;
1129
1130 for (h = cap_hdr; h; h = h->next) {
1131 if ((h->namelen == col - sol) &&
1132 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1133 if (cap[h->index] == NULL)
1134 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001135 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001136
1137 if (cap[h->index] == NULL) {
1138 Alert("HTTP capture : out of memory.\n");
1139 continue;
1140 }
1141
1142 len = eol - sov;
1143 if (len > h->len)
1144 len = h->len;
1145
1146 memcpy(cap[h->index], sov, len);
1147 cap[h->index][len]=0;
1148 }
1149 }
1150 sol = eol + idx->v[cur_idx].cr + 1;
1151 cur_idx = idx->v[cur_idx].next;
1152 }
1153}
1154
1155
Willy Tarreau42250582007-04-01 01:30:43 +02001156/* either we find an LF at <ptr> or we jump to <bad>.
1157 */
1158#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1159
1160/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1161 * otherwise to <http_msg_ood> with <state> set to <st>.
1162 */
1163#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1164 ptr++; \
1165 if (likely(ptr < end)) \
1166 goto good; \
1167 else { \
1168 state = (st); \
1169 goto http_msg_ood; \
1170 } \
1171 } while (0)
1172
1173
Willy Tarreaubaaee002006-06-26 02:48:02 +02001174/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001175 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001176 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1177 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1178 * will give undefined results.
1179 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1180 * and that msg->sol points to the beginning of the response.
1181 * If a complete line is found (which implies that at least one CR or LF is
1182 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1183 * returned indicating an incomplete line (which does not mean that parts have
1184 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1185 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1186 * upon next call.
1187 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001188 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1190 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001191 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001193const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1194 unsigned int state, const char *ptr, const char *end,
1195 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001196{
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 switch (state) {
1198 http_msg_rpver:
1199 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001200 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001201 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1202
1203 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001204 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1206 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001207 state = HTTP_MSG_ERROR;
1208 break;
1209
Willy Tarreau8973c702007-01-21 23:58:29 +01001210 http_msg_rpver_sp:
1211 case HTTP_MSG_RPVER_SP:
1212 if (likely(!HTTP_IS_LWS(*ptr))) {
1213 msg->sl.st.c = ptr - msg_buf;
1214 goto http_msg_rpcode;
1215 }
1216 if (likely(HTTP_IS_SPHT(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1218 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001219 state = HTTP_MSG_ERROR;
1220 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001221
1222 http_msg_rpcode:
1223 case HTTP_MSG_RPCODE:
1224 if (likely(!HTTP_IS_LWS(*ptr)))
1225 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1226
1227 if (likely(HTTP_IS_SPHT(*ptr))) {
1228 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1230 }
1231
1232 /* so it's a CR/LF, so there is no reason phrase */
1233 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1234 http_msg_rsp_reason:
1235 /* FIXME: should we support HTTP responses without any reason phrase ? */
1236 msg->sl.st.r = ptr - msg_buf;
1237 msg->sl.st.r_l = 0;
1238 goto http_msg_rpline_eol;
1239
1240 http_msg_rpcode_sp:
1241 case HTTP_MSG_RPCODE_SP:
1242 if (likely(!HTTP_IS_LWS(*ptr))) {
1243 msg->sl.st.r = ptr - msg_buf;
1244 goto http_msg_rpreason;
1245 }
1246 if (likely(HTTP_IS_SPHT(*ptr)))
1247 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1248 /* so it's a CR/LF, so there is no reason phrase */
1249 goto http_msg_rsp_reason;
1250
1251 http_msg_rpreason:
1252 case HTTP_MSG_RPREASON:
1253 if (likely(!HTTP_IS_CRLF(*ptr)))
1254 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1255 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1256 http_msg_rpline_eol:
1257 /* We have seen the end of line. Note that we do not
1258 * necessarily have the \n yet, but at least we know that we
1259 * have EITHER \r OR \n, otherwise the response would not be
1260 * complete. We can then record the response length and return
1261 * to the caller which will be able to register it.
1262 */
1263 msg->sl.st.l = ptr - msg->sol;
1264 return ptr;
1265
1266#ifdef DEBUG_FULL
1267 default:
1268 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1269 exit(1);
1270#endif
1271 }
1272
1273 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001274 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001275 if (ret_state)
1276 *ret_state = state;
1277 if (ret_ptr)
1278 *ret_ptr = (char *)ptr;
1279 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001280}
1281
1282
1283/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 * This function parses a request line between <ptr> and <end>, starting with
1285 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1286 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1287 * will give undefined results.
1288 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1289 * and that msg->sol points to the beginning of the request.
1290 * If a complete line is found (which implies that at least one CR or LF is
1291 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1292 * returned indicating an incomplete line (which does not mean that parts have
1293 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1294 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1295 * upon next call.
1296 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001297 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1299 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001300 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001301 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001302const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1303 unsigned int state, const char *ptr, const char *end,
1304 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001305{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 switch (state) {
1307 http_msg_rqmeth:
1308 case HTTP_MSG_RQMETH:
1309 if (likely(HTTP_IS_TOKEN(*ptr)))
1310 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001313 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1315 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 if (likely(HTTP_IS_CRLF(*ptr))) {
1318 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001319 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 http_msg_req09_uri:
1321 msg->sl.rq.u = ptr - msg_buf;
1322 http_msg_req09_uri_e:
1323 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1324 http_msg_req09_ver:
1325 msg->sl.rq.v = ptr - msg_buf;
1326 msg->sl.rq.v_l = 0;
1327 goto http_msg_rqline_eol;
1328 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001329 state = HTTP_MSG_ERROR;
1330 break;
1331
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 http_msg_rqmeth_sp:
1333 case HTTP_MSG_RQMETH_SP:
1334 if (likely(!HTTP_IS_LWS(*ptr))) {
1335 msg->sl.rq.u = ptr - msg_buf;
1336 goto http_msg_rquri;
1337 }
1338 if (likely(HTTP_IS_SPHT(*ptr)))
1339 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1340 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1341 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 http_msg_rquri:
1344 case HTTP_MSG_RQURI:
1345 if (likely(!HTTP_IS_LWS(*ptr)))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 if (likely(HTTP_IS_SPHT(*ptr))) {
1349 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1351 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001352
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1354 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 http_msg_rquri_sp:
1357 case HTTP_MSG_RQURI_SP:
1358 if (likely(!HTTP_IS_LWS(*ptr))) {
1359 msg->sl.rq.v = ptr - msg_buf;
1360 goto http_msg_rqver;
1361 }
1362 if (likely(HTTP_IS_SPHT(*ptr)))
1363 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1364 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1365 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 http_msg_rqver:
1368 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001369 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001371
1372 if (likely(HTTP_IS_CRLF(*ptr))) {
1373 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1374 http_msg_rqline_eol:
1375 /* We have seen the end of line. Note that we do not
1376 * necessarily have the \n yet, but at least we know that we
1377 * have EITHER \r OR \n, otherwise the request would not be
1378 * complete. We can then record the request length and return
1379 * to the caller which will be able to register it.
1380 */
1381 msg->sl.rq.l = ptr - msg->sol;
1382 return ptr;
1383 }
1384
1385 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001386 state = HTTP_MSG_ERROR;
1387 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389#ifdef DEBUG_FULL
1390 default:
1391 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1392 exit(1);
1393#endif
1394 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001397 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 if (ret_state)
1399 *ret_state = state;
1400 if (ret_ptr)
1401 *ret_ptr = (char *)ptr;
1402 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001404
1405
Willy Tarreau8973c702007-01-21 23:58:29 +01001406/*
1407 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001408 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 * when data are missing and recalled at the exact same location with no
1410 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001411 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001412 * fields. Note that msg->som and msg->sol will be initialized after completing
1413 * the first state, so that none of the msg pointers has to be initialized
1414 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001415 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1417{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001418 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001420
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001421 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 ptr = buf->lr;
1423 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 if (unlikely(ptr >= end))
1426 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 /*
1430 * First, states that are specific to the response only.
1431 * We check them first so that request and headers are
1432 * closer to each other (accessed more often).
1433 */
1434 http_msg_rpbefore:
1435 case HTTP_MSG_RPBEFORE:
1436 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001437 /* we have a start of message, but we have to check
1438 * first if we need to remove some CRLF. We can only
1439 * do this when send_max=0.
1440 */
1441 char *beg = buf->w + buf->send_max;
1442 if (beg >= buf->data + buf->size)
1443 beg -= buf->size;
1444 if (unlikely(ptr != beg)) {
1445 if (buf->send_max)
1446 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001447 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001448 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001450 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001451 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 hdr_idx_init(idx);
1453 state = HTTP_MSG_RPVER;
1454 goto http_msg_rpver;
1455 }
1456
1457 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1458 goto http_msg_invalid;
1459
1460 if (unlikely(*ptr == '\n'))
1461 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1462 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1463 /* stop here */
1464
1465 http_msg_rpbefore_cr:
1466 case HTTP_MSG_RPBEFORE_CR:
1467 EXPECT_LF_HERE(ptr, http_msg_invalid);
1468 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1469 /* stop here */
1470
1471 http_msg_rpver:
1472 case HTTP_MSG_RPVER:
1473 case HTTP_MSG_RPVER_SP:
1474 case HTTP_MSG_RPCODE:
1475 case HTTP_MSG_RPCODE_SP:
1476 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001477 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001478 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 if (unlikely(!ptr))
1480 return;
1481
1482 /* we have a full response and we know that we have either a CR
1483 * or an LF at <ptr>.
1484 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001485 //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 +01001486 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1487
1488 msg->sol = ptr;
1489 if (likely(*ptr == '\r'))
1490 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1491 goto http_msg_rpline_end;
1492
1493 http_msg_rpline_end:
1494 case HTTP_MSG_RPLINE_END:
1495 /* msg->sol must point to the first of CR or LF. */
1496 EXPECT_LF_HERE(ptr, http_msg_invalid);
1497 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1498 /* stop here */
1499
1500 /*
1501 * Second, states that are specific to the request only
1502 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 http_msg_rqbefore:
1504 case HTTP_MSG_RQBEFORE:
1505 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001506 /* we have a start of message, but we have to check
1507 * first if we need to remove some CRLF. We can only
1508 * do this when send_max=0.
1509 */
1510 char *beg = buf->w + buf->send_max;
1511 if (beg >= buf->data + buf->size)
1512 beg -= buf->size;
1513 if (likely(ptr != beg)) {
1514 if (buf->send_max)
1515 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001516 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001517 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001519 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001520 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001521 /* we will need this when keep-alive will be supported
1522 hdr_idx_init(idx);
1523 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 state = HTTP_MSG_RQMETH;
1525 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1529 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 if (unlikely(*ptr == '\n'))
1532 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1533 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 http_msg_rqbefore_cr:
1537 case HTTP_MSG_RQBEFORE_CR:
1538 EXPECT_LF_HERE(ptr, http_msg_invalid);
1539 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001540 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 http_msg_rqmeth:
1543 case HTTP_MSG_RQMETH:
1544 case HTTP_MSG_RQMETH_SP:
1545 case HTTP_MSG_RQURI:
1546 case HTTP_MSG_RQURI_SP:
1547 case HTTP_MSG_RQVER:
1548 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001549 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 if (unlikely(!ptr))
1551 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 /* we have a full request and we know that we have either a CR
1554 * or an LF at <ptr>.
1555 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001556 //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 +01001557 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 msg->sol = ptr;
1560 if (likely(*ptr == '\r'))
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 http_msg_rqline_end:
1565 case HTTP_MSG_RQLINE_END:
1566 /* check for HTTP/0.9 request : no version information available.
1567 * msg->sol must point to the first of CR or LF.
1568 */
1569 if (unlikely(msg->sl.rq.v_l == 0))
1570 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 EXPECT_LF_HERE(ptr, http_msg_invalid);
1573 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001574 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001575
Willy Tarreau8973c702007-01-21 23:58:29 +01001576 /*
1577 * Common states below
1578 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 http_msg_hdr_first:
1580 case HTTP_MSG_HDR_FIRST:
1581 msg->sol = ptr;
1582 if (likely(!HTTP_IS_CRLF(*ptr))) {
1583 goto http_msg_hdr_name;
1584 }
1585
1586 if (likely(*ptr == '\r'))
1587 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1588 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 http_msg_hdr_name:
1591 case HTTP_MSG_HDR_NAME:
1592 /* assumes msg->sol points to the first char */
1593 if (likely(HTTP_IS_TOKEN(*ptr)))
1594 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 if (likely(*ptr == ':')) {
1597 msg->col = ptr - buf->data;
1598 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1599 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001600
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001601 if (likely(msg->err_pos < -1) || *ptr == '\n')
1602 goto http_msg_invalid;
1603
1604 if (msg->err_pos == -1) /* capture error pointer */
1605 msg->err_pos = ptr - buf->data; /* >= 0 now */
1606
1607 /* and we still accept this non-token character */
1608 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 http_msg_hdr_l1_sp:
1611 case HTTP_MSG_HDR_L1_SP:
1612 /* assumes msg->sol points to the first char and msg->col to the colon */
1613 if (likely(HTTP_IS_SPHT(*ptr)))
1614 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 /* header value can be basically anything except CR/LF */
1617 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 if (likely(!HTTP_IS_CRLF(*ptr))) {
1620 goto http_msg_hdr_val;
1621 }
1622
1623 if (likely(*ptr == '\r'))
1624 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1625 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001626
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 http_msg_hdr_l1_lf:
1628 case HTTP_MSG_HDR_L1_LF:
1629 EXPECT_LF_HERE(ptr, http_msg_invalid);
1630 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 http_msg_hdr_l1_lws:
1633 case HTTP_MSG_HDR_L1_LWS:
1634 if (likely(HTTP_IS_SPHT(*ptr))) {
1635 /* replace HT,CR,LF with spaces */
1636 for (; buf->data+msg->sov < ptr; msg->sov++)
1637 buf->data[msg->sov] = ' ';
1638 goto http_msg_hdr_l1_sp;
1639 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001640 /* we had a header consisting only in spaces ! */
1641 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 goto http_msg_complete_header;
1643
1644 http_msg_hdr_val:
1645 case HTTP_MSG_HDR_VAL:
1646 /* assumes msg->sol points to the first char, msg->col to the
1647 * colon, and msg->sov points to the first character of the
1648 * value.
1649 */
1650 if (likely(!HTTP_IS_CRLF(*ptr)))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 msg->eol = ptr;
1654 /* Note: we could also copy eol into ->eoh so that we have the
1655 * real header end in case it ends with lots of LWS, but is this
1656 * really needed ?
1657 */
1658 if (likely(*ptr == '\r'))
1659 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1660 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 http_msg_hdr_l2_lf:
1663 case HTTP_MSG_HDR_L2_LF:
1664 EXPECT_LF_HERE(ptr, http_msg_invalid);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 http_msg_hdr_l2_lws:
1668 case HTTP_MSG_HDR_L2_LWS:
1669 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1670 /* LWS: replace HT,CR,LF with spaces */
1671 for (; msg->eol < ptr; msg->eol++)
1672 *msg->eol = ' ';
1673 goto http_msg_hdr_val;
1674 }
1675 http_msg_complete_header:
1676 /*
1677 * It was a new header, so the last one is finished.
1678 * Assumes msg->sol points to the first char, msg->col to the
1679 * colon, msg->sov points to the first character of the value
1680 * and msg->eol to the first CR or LF so we know how the line
1681 * ends. We insert last header into the index.
1682 */
1683 /*
1684 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1685 write(2, msg->sol, msg->eol-msg->sol);
1686 fprintf(stderr,"\n");
1687 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1690 idx, idx->tail) < 0))
1691 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 msg->sol = ptr;
1694 if (likely(!HTTP_IS_CRLF(*ptr))) {
1695 goto http_msg_hdr_name;
1696 }
1697
1698 if (likely(*ptr == '\r'))
1699 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1700 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 http_msg_last_lf:
1703 case HTTP_MSG_LAST_LF:
1704 /* Assumes msg->sol points to the first of either CR or LF */
1705 EXPECT_LF_HERE(ptr, http_msg_invalid);
1706 ptr++;
1707 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001708 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001710 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 return;
1712#ifdef DEBUG_FULL
1713 default:
1714 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1715 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001716#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 }
1718 http_msg_ood:
1719 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001720 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 buf->lr = ptr;
1722 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 http_msg_invalid:
1725 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001726 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001727 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 return;
1729}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001730
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001731/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1732 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1733 * nothing is done and 1 is returned.
1734 */
1735static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1736{
1737 int delta;
1738 char *cur_end;
1739
1740 if (msg->sl.rq.v_l != 0)
1741 return 1;
1742
1743 msg->sol = req->data + msg->som;
1744 cur_end = msg->sol + msg->sl.rq.l;
1745 delta = 0;
1746
1747 if (msg->sl.rq.u_l == 0) {
1748 /* if no URI was set, add "/" */
1749 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1750 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001751 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001752 }
1753 /* add HTTP version */
1754 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001755 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001756 cur_end += delta;
1757 cur_end = (char *)http_parse_reqline(msg, req->data,
1758 HTTP_MSG_RQMETH,
1759 msg->sol, cur_end + 1,
1760 NULL, NULL);
1761 if (unlikely(!cur_end))
1762 return 0;
1763
1764 /* we have a full HTTP/1.0 request now and we know that
1765 * we have either a CR or an LF at <ptr>.
1766 */
1767 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1768 return 1;
1769}
1770
Willy Tarreau5b154472009-12-21 20:11:07 +01001771/* Parse the Connection: headaer of an HTTP request, and set the transaction
1772 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1773 * is also set so that we don't parse a second time. If some dangerous values
1774 * are encountered, we leave the status to indicate that the request might be
1775 * interpreted as keep-alive, but we also set the connection flags to indicate
1776 * that we WANT it to be a close, so that the header will be fixed. This
1777 * function should only be called when we know we're interested in checking
1778 * the request (not a CONNECT, and FE or BE mangles the header).
1779 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001780void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001781{
1782 struct http_msg *msg = &txn->req;
1783 struct hdr_ctx ctx;
1784 int conn_cl, conn_ka;
1785
1786 if (txn->flags & TX_CON_HDR_PARS)
1787 return;
1788
1789 conn_cl = 0;
1790 conn_ka = 0;
1791 ctx.idx = 0;
1792
1793 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1794 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1795 conn_cl = 1;
1796 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1797 conn_ka = 1;
1798 }
1799
1800 /* Determine if the client wishes keep-alive or close.
1801 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1802 * are persistent unless "Connection: close" is explicitly specified.
1803 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1804 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1805 * they explicitly specify "Connection: Keep-Alive", regardless of any
1806 * optional "Keep-Alive" header.
1807 * Note that if we find a request with both "Connection: close" and
1808 * "Connection: Keep-Alive", we indicate we want a close but don't have
1809 * it, so that it can be enforced later.
1810 */
1811
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001812 if (conn_cl && conn_ka) {
1813 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1814 }
1815 else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
Willy Tarreau5b154472009-12-21 20:11:07 +01001816 if (conn_cl) {
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001817 txn->flags |= TX_REQ_CONN_CLO;
1818 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1819 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 }
1821 } else { /* HTTP/1.0 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001822 if (!conn_ka) {
1823 txn->flags |= TX_REQ_CONN_CLO;
1824 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1825 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1826 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 }
1828 txn->flags |= TX_CON_HDR_PARS;
1829}
1830
Willy Tarreaud98cf932009-12-27 22:54:55 +01001831/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1832 * first byte of body, and increments msg->sov by the number of bytes parsed,
1833 * so that we know we can forward between ->som and ->sov. Note that due to
1834 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1835 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001836 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001837 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001839int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001840{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001841 char *ptr = buf->lr;
1842 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001843 unsigned int chunk = 0;
1844
1845 /* The chunk size is in the following form, though we are only
1846 * interested in the size and CRLF :
1847 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1848 */
1849 while (1) {
1850 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001851 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001852 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001853 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001854 if (c < 0) /* not a hex digit anymore */
1855 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856 if (++ptr >= end)
1857 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001858 if (chunk & 0xF000000) /* overflow will occur */
1859 return -1;
1860 chunk = (chunk << 4) + c;
1861 }
1862
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 /* empty size not allowed */
1864 if (ptr == buf->lr)
1865 return -1;
1866
1867 while (http_is_spht[(unsigned char)*ptr]) {
1868 if (++ptr >= end)
1869 ptr = buf->data;
1870 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001871 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001872 }
1873
Willy Tarreaud98cf932009-12-27 22:54:55 +01001874 /* Up to there, we know that at least one byte is present at *ptr. Check
1875 * for the end of chunk size.
1876 */
1877 while (1) {
1878 if (likely(HTTP_IS_CRLF(*ptr))) {
1879 /* we now have a CR or an LF at ptr */
1880 if (likely(*ptr == '\r')) {
1881 if (++ptr >= end)
1882 ptr = buf->data;
1883 if (ptr == buf->r)
1884 return 0;
1885 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001886
Willy Tarreaud98cf932009-12-27 22:54:55 +01001887 if (*ptr != '\n')
1888 return -1;
1889 if (++ptr >= end)
1890 ptr = buf->data;
1891 /* done */
1892 break;
1893 }
1894 else if (*ptr == ';') {
1895 /* chunk extension, ends at next CRLF */
1896 if (++ptr >= end)
1897 ptr = buf->data;
1898 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001899 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900
1901 while (!HTTP_IS_CRLF(*ptr)) {
1902 if (++ptr >= end)
1903 ptr = buf->data;
1904 if (ptr == buf->r)
1905 return 0;
1906 }
1907 /* we have a CRLF now, loop above */
1908 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001909 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001910 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001911 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 }
1913
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 /* OK we found our CRLF and now <ptr> points to the next byte,
1915 * which may or may not be present. We save that into ->lr and
1916 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001917 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 msg->sov += ptr - buf->lr;
1919 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001922 return 1;
1923}
1924
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925/* This function skips trailers in the buffer <buf> associated with HTTP
1926 * message <msg>. The first visited position is buf->lr. If the end of
1927 * the trailers is found, it is automatically scheduled to be forwarded,
1928 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1929 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001930 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1931 * zero. If a parse error is encountered, the function returns < 0 and does not
1932 * change anything except maybe buf->lr and msg->sov. Note that the message
1933 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1934 * which implies that all non-trailers data have already been scheduled for
1935 * forwarding, and that the difference between msg->som and msg->sov exactly
1936 * matches the length of trailers already parsed and not forwarded. It is also
1937 * important to note that this function is designed to be able to parse wrapped
1938 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001939 */
1940int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1941{
1942 /* we have buf->lr which points to next line. Look for CRLF. */
1943 while (1) {
1944 char *p1 = NULL, *p2 = NULL;
1945 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001946 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947
1948 /* scan current line and stop at LF or CRLF */
1949 while (1) {
1950 if (ptr == buf->r)
1951 return 0;
1952
1953 if (*ptr == '\n') {
1954 if (!p1)
1955 p1 = ptr;
1956 p2 = ptr;
1957 break;
1958 }
1959
1960 if (*ptr == '\r') {
1961 if (p1)
1962 return -1;
1963 p1 = ptr;
1964 }
1965
1966 ptr++;
1967 if (ptr >= buf->data + buf->size)
1968 ptr = buf->data;
1969 }
1970
1971 /* after LF; point to beginning of next line */
1972 p2++;
1973 if (p2 >= buf->data + buf->size)
1974 p2 = buf->data;
1975
Willy Tarreau638cd022010-01-03 07:42:04 +01001976 bytes = p2 - buf->lr;
1977 if (bytes < 0)
1978 bytes += buf->size;
1979
1980 /* schedule this line for forwarding */
1981 msg->sov += bytes;
1982 if (msg->sov >= buf->size)
1983 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001984
Willy Tarreau638cd022010-01-03 07:42:04 +01001985 if (p1 == buf->lr) {
1986 /* LF/CRLF at beginning of line => end of trailers at p2.
1987 * Everything was scheduled for forwarding, there's nothing
1988 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001989 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001990 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001991 msg->msg_state = HTTP_MSG_DONE;
1992 return 1;
1993 }
1994 /* OK, next line then */
1995 buf->lr = p2;
1996 }
1997}
1998
1999/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2000 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2001 * ->som, buf->lr in order to include this part into the next forwarding phase.
2002 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2003 * not enough data are available, the function does not change anything and
2004 * returns zero. If a parse error is encountered, the function returns < 0 and
2005 * does not change anything. Note: this function is designed to parse wrapped
2006 * CRLF at the end of the buffer.
2007 */
2008int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2009{
2010 char *ptr;
2011 int bytes;
2012
2013 /* NB: we'll check data availabilty at the end. It's not a
2014 * problem because whatever we match first will be checked
2015 * against the correct length.
2016 */
2017 bytes = 1;
2018 ptr = buf->lr;
2019 if (*ptr == '\r') {
2020 bytes++;
2021 ptr++;
2022 if (ptr >= buf->data + buf->size)
2023 ptr = buf->data;
2024 }
2025
2026 if (buf->l < bytes)
2027 return 0;
2028
2029 if (*ptr != '\n')
2030 return -1;
2031
2032 ptr++;
2033 if (ptr >= buf->data + buf->size)
2034 ptr = buf->data;
2035 buf->lr = ptr;
2036 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2037 msg->sov = ptr - buf->data;
2038 msg->som = msg->sov - bytes;
2039 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2040 return 1;
2041}
Willy Tarreau5b154472009-12-21 20:11:07 +01002042
Willy Tarreau83e3af02009-12-28 17:39:57 +01002043void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2044{
2045 char *end = buf->data + buf->size;
2046 int off = buf->data + buf->size - buf->w;
2047
2048 /* two possible cases :
2049 * - the buffer is in one contiguous block, we move it in-place
2050 * - the buffer is in two blocks, we move it via the trash
2051 */
2052 if (buf->l) {
2053 int block1 = buf->l;
2054 int block2 = 0;
2055 if (buf->r <= buf->w) {
2056 /* non-contiguous block */
2057 block1 = buf->data + buf->size - buf->w;
2058 block2 = buf->r - buf->data;
2059 }
2060 if (block2)
2061 memcpy(trash, buf->data, block2);
2062 memmove(buf->data, buf->w, block1);
2063 if (block2)
2064 memcpy(buf->data + block1, trash, block2);
2065 }
2066
2067 /* adjust all known pointers */
2068 buf->w = buf->data;
2069 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2070 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2071 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2072 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2073
2074 /* adjust relative pointers */
2075 msg->som = 0;
2076 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2077 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2078 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2079
2080 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2081 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2082
2083 if (msg->err_pos >= 0) {
2084 msg->err_pos += off;
2085 if (msg->err_pos >= buf->size)
2086 msg->err_pos -= buf->size;
2087 }
2088
2089 buf->flags &= ~BF_FULL;
2090 if (buf->l >= buffer_max_len(buf))
2091 buf->flags |= BF_FULL;
2092}
2093
Willy Tarreaud787e662009-07-07 10:14:51 +02002094/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2095 * processing can continue on next analysers, or zero if it either needs more
2096 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2097 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2098 * when it has nothing left to do, and may remove any analyser when it wants to
2099 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002100 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002101int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002102{
Willy Tarreau59234e92008-11-30 23:51:27 +01002103 /*
2104 * We will parse the partial (or complete) lines.
2105 * We will check the request syntax, and also join multi-line
2106 * headers. An index of all the lines will be elaborated while
2107 * parsing.
2108 *
2109 * For the parsing, we use a 28 states FSM.
2110 *
2111 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002112 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002113 * req->data + msg->eoh = end of processed headers / start of current one
2114 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002115 * req->lr = first non-visited byte
2116 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002117 *
2118 * At end of parsing, we may perform a capture of the error (if any), and
2119 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2120 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2121 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002122 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002123
Willy Tarreau59234e92008-11-30 23:51:27 +01002124 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002125 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002126 struct http_txn *txn = &s->txn;
2127 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002128 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002129
Willy Tarreau6bf17362009-02-24 10:48:35 +01002130 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2131 now_ms, __FUNCTION__,
2132 s,
2133 req,
2134 req->rex, req->wex,
2135 req->flags,
2136 req->l,
2137 req->analysers);
2138
Willy Tarreau52a0c602009-08-16 22:45:38 +02002139 /* we're speaking HTTP here, so let's speak HTTP to the client */
2140 s->srv_error = http_return_srv_error;
2141
Willy Tarreau83e3af02009-12-28 17:39:57 +01002142 /* There's a protected area at the end of the buffer for rewriting
2143 * purposes. We don't want to start to parse the request if the
2144 * protected area is affected, because we may have to move processed
2145 * data later, which is much more complicated.
2146 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002147 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
2148 if (unlikely((req->flags & BF_FULL) ||
2149 req->r < req->lr ||
2150 req->r > req->data + req->size - global.tune.maxrewrite)) {
2151 if (req->send_max) {
2152 /* some data has still not left the buffer, wake us once that's done */
2153 buffer_dont_connect(req);
2154 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2155 return 0;
2156 }
2157 if (req->l <= req->size - global.tune.maxrewrite)
2158 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002159 }
2160
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002161 if (likely(req->lr < req->r))
2162 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002163 }
2164
Willy Tarreau59234e92008-11-30 23:51:27 +01002165 /* 1: we might have to print this header in debug mode */
2166 if (unlikely((global.mode & MODE_DEBUG) &&
2167 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002168 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002170
Willy Tarreau59234e92008-11-30 23:51:27 +01002171 sol = req->data + msg->som;
2172 eol = sol + msg->sl.rq.l;
2173 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002174
Willy Tarreau59234e92008-11-30 23:51:27 +01002175 sol += hdr_idx_first_pos(&txn->hdr_idx);
2176 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002177
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 while (cur_idx) {
2179 eol = sol + txn->hdr_idx.v[cur_idx].len;
2180 debug_hdr("clihdr", s, sol, eol);
2181 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2182 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002183 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 }
2185
Willy Tarreau58f10d72006-12-04 02:26:12 +01002186
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 /*
2188 * Now we quickly check if we have found a full valid request.
2189 * If not so, we check the FD and buffer states before leaving.
2190 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002191 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 * requests are checked first.
2193 *
2194 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002195
Willy Tarreau655dce92009-11-08 13:10:58 +01002196 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002197 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002199 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2201 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002202
Willy Tarreau59234e92008-11-30 23:51:27 +01002203 /* 1: Since we are in header mode, if there's no space
2204 * left for headers, we won't be able to free more
2205 * later, so the session will never terminate. We
2206 * must terminate it now.
2207 */
2208 if (unlikely(req->flags & BF_FULL)) {
2209 /* FIXME: check if URI is set and return Status
2210 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002211 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002212 goto return_bad_req;
2213 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 /* 2: have we encountered a read error ? */
2216 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002217 if (txn->flags & TX_NOT_FIRST)
2218 goto failed_keep_alive;
2219
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002221 if (msg->err_pos >= 0)
2222 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002223 msg->msg_state = HTTP_MSG_ERROR;
2224 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002225
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002226 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002227 if (s->listener->counters)
2228 s->listener->counters->failed_req++;
2229
Willy Tarreau59234e92008-11-30 23:51:27 +01002230 if (!(s->flags & SN_ERR_MASK))
2231 s->flags |= SN_ERR_CLICL;
2232 if (!(s->flags & SN_FINST_MASK))
2233 s->flags |= SN_FINST_R;
2234 return 0;
2235 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002236
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 /* 3: has the read timeout expired ? */
2238 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002239 if (txn->flags & TX_NOT_FIRST)
2240 goto failed_keep_alive;
2241
Willy Tarreau59234e92008-11-30 23:51:27 +01002242 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002243 if (msg->err_pos >= 0)
2244 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 txn->status = 408;
2246 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2247 msg->msg_state = HTTP_MSG_ERROR;
2248 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002249
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002250 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002251 if (s->listener->counters)
2252 s->listener->counters->failed_req++;
2253
Willy Tarreau59234e92008-11-30 23:51:27 +01002254 if (!(s->flags & SN_ERR_MASK))
2255 s->flags |= SN_ERR_CLITO;
2256 if (!(s->flags & SN_FINST_MASK))
2257 s->flags |= SN_FINST_R;
2258 return 0;
2259 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002260
Willy Tarreau59234e92008-11-30 23:51:27 +01002261 /* 4: have we encountered a close ? */
2262 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002263 if (txn->flags & TX_NOT_FIRST)
2264 goto failed_keep_alive;
2265
Willy Tarreau4076a152009-04-02 15:18:36 +02002266 if (msg->err_pos >= 0)
2267 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 txn->status = 400;
2269 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2270 msg->msg_state = HTTP_MSG_ERROR;
2271 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002272
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002273 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002274 if (s->listener->counters)
2275 s->listener->counters->failed_req++;
2276
Willy Tarreau59234e92008-11-30 23:51:27 +01002277 if (!(s->flags & SN_ERR_MASK))
2278 s->flags |= SN_ERR_CLICL;
2279 if (!(s->flags & SN_FINST_MASK))
2280 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002281 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002282 }
2283
Willy Tarreau520d95e2009-09-19 21:04:57 +02002284 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002285 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2286
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 /* just set the request timeout once at the beginning of the request */
2288 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002289 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 /* we're not ready yet */
2292 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002293
2294 failed_keep_alive:
2295 /* Here we process low-level errors for keep-alive requests. In
2296 * short, if the request is not the first one and it experiences
2297 * a timeout, read error or shutdown, we just silently close so
2298 * that the client can try again.
2299 */
2300 txn->status = 0;
2301 msg->msg_state = HTTP_MSG_RQBEFORE;
2302 req->analysers = 0;
2303 s->logs.logwait = 0;
2304 stream_int_cond_close(req->prod, NULL);
2305 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002307
Willy Tarreaud787e662009-07-07 10:14:51 +02002308 /* OK now we have a complete HTTP request with indexed headers. Let's
2309 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002310 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2311 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2312 * points to the CRLF of the request line. req->lr points to the first
2313 * byte after the last LF. msg->col and msg->sov point to the first
2314 * byte of data. msg->eol cannot be trusted because it may have been
2315 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002316 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002317
Willy Tarreaud787e662009-07-07 10:14:51 +02002318 /* Maybe we found in invalid header name while we were configured not
2319 * to block on that, so we have to capture it now.
2320 */
2321 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002322 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2323
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 /* ensure we keep this pointer to the beginning of the message */
2325 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002326
Willy Tarreau59234e92008-11-30 23:51:27 +01002327 /*
2328 * 1: identify the method
2329 */
2330 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2331
2332 /* we can make use of server redirect on GET and HEAD */
2333 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2334 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002335
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 /*
2337 * 2: check if the URI matches the monitor_uri.
2338 * We have to do this for every request which gets in, because
2339 * the monitor-uri is defined by the frontend.
2340 */
2341 if (unlikely((s->fe->monitor_uri_len != 0) &&
2342 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2343 !memcmp(&req->data[msg->sl.rq.u],
2344 s->fe->monitor_uri,
2345 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002346 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002348 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002354 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2355 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 ret = acl_pass(ret);
2358 if (cond->pol == ACL_COND_UNLESS)
2359 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 if (ret) {
2362 /* we fail this request, let's return 503 service unavail */
2363 txn->status = 503;
2364 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2365 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002366 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002367 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 /* nothing to fail, let's reply normaly */
2370 txn->status = 200;
2371 stream_int_retnclose(req->prod, &http_200_chunk);
2372 goto return_prx_cond;
2373 }
2374
2375 /*
2376 * 3: Maybe we have to copy the original REQURI for the logs ?
2377 * Note: we cannot log anymore if the request has been
2378 * classified as invalid.
2379 */
2380 if (unlikely(s->logs.logwait & LW_REQ)) {
2381 /* we have a complete HTTP request that we must log */
2382 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2383 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 if (urilen >= REQURI_LEN)
2386 urilen = REQURI_LEN - 1;
2387 memcpy(txn->uri, &req->data[msg->som], urilen);
2388 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 if (!(s->logs.logwait &= ~LW_REQ))
2391 s->do_log(s);
2392 } else {
2393 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002394 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 }
Willy Tarreau06619262006-12-17 08:37:22 +01002396
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002398 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2399 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002400
Willy Tarreau5b154472009-12-21 20:11:07 +01002401 /* ... and check if the request is HTTP/1.1 or above */
2402 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002403 ((req->data[msg->sl.rq.v + 5] > '1') ||
2404 ((req->data[msg->sl.rq.v + 5] == '1') &&
2405 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002406 txn->flags |= TX_REQ_VER_11;
2407
2408 /* "connection" has not been parsed yet */
2409 txn->flags &= ~TX_CON_HDR_PARS;
2410
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002411 /* transfer length unknown*/
2412 txn->flags &= ~TX_REQ_XFER_LEN;
2413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 /* 5: we may need to capture headers */
2415 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2416 capture_headers(req->data + msg->som, &txn->hdr_idx,
2417 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002418
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002419 /* 6: determine the transfer-length.
2420 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2421 * the presence of a message-body in a REQUEST and its transfer length
2422 * must be determined that way (in order of precedence) :
2423 * 1. The presence of a message-body in a request is signaled by the
2424 * inclusion of a Content-Length or Transfer-Encoding header field
2425 * in the request's header fields. When a request message contains
2426 * both a message-body of non-zero length and a method that does
2427 * not define any semantics for that request message-body, then an
2428 * origin server SHOULD either ignore the message-body or respond
2429 * with an appropriate error message (e.g., 413). A proxy or
2430 * gateway, when presented the same request, SHOULD either forward
2431 * the request inbound with the message- body or ignore the
2432 * message-body when determining a response.
2433 *
2434 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2435 * and the "chunked" transfer-coding (Section 6.2) is used, the
2436 * transfer-length is defined by the use of this transfer-coding.
2437 * If a Transfer-Encoding header field is present and the "chunked"
2438 * transfer-coding is not present, the transfer-length is defined
2439 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002440 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002441 * 3. If a Content-Length header field is present, its decimal value in
2442 * OCTETs represents both the entity-length and the transfer-length.
2443 * If a message is received with both a Transfer-Encoding header
2444 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002445 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002446 * 4. By the server closing the connection. (Closing the connection
2447 * cannot be used to indicate the end of a request body, since that
2448 * would leave no possibility for the server to send back a response.)
2449 *
2450 * Whenever a transfer-coding is applied to a message-body, the set of
2451 * transfer-codings MUST include "chunked", unless the message indicates
2452 * it is terminated by closing the connection. When the "chunked"
2453 * transfer-coding is used, it MUST be the last transfer-coding applied
2454 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002455 */
2456
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002457 /* CONNECT sets a tunnel and ignores everything else */
2458 if (txn->meth == HTTP_METH_CONNECT)
2459 goto skip_xfer_len;
2460
2461 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002464 while ((txn->flags & TX_REQ_VER_11) &&
2465 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002466 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2467 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2468 else if (txn->flags & TX_REQ_TE_CHNK) {
2469 /* bad transfer-encoding (chunked followed by something else) */
2470 use_close_only = 1;
2471 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2472 break;
2473 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 }
2475
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2479 signed long long cl;
2480
2481 if (!ctx.vlen)
2482 goto return_bad_req;
2483
2484 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2485 goto return_bad_req; /* parse failure */
2486
2487 if (cl < 0)
2488 goto return_bad_req;
2489
2490 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2491 goto return_bad_req; /* already specified, was different */
2492
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002493 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002494 msg->hdr_content_len = cl;
2495 }
2496
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002497 /* bodyless requests have a known length */
2498 if (!use_close_only)
2499 txn->flags |= TX_REQ_XFER_LEN;
2500
2501 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002502 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002503 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002504 req->analyse_exp = TICK_ETERNITY;
2505 return 1;
2506
2507 return_bad_req:
2508 /* We centralize bad requests processing here */
2509 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2510 /* we detected a parsing error. We want to archive this request
2511 * in the dedicated proxy area for later troubleshooting.
2512 */
2513 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2514 }
2515
2516 txn->req.msg_state = HTTP_MSG_ERROR;
2517 txn->status = 400;
2518 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002519
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002520 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002521 if (s->listener->counters)
2522 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002523
2524 return_prx_cond:
2525 if (!(s->flags & SN_ERR_MASK))
2526 s->flags |= SN_ERR_PRXCOND;
2527 if (!(s->flags & SN_FINST_MASK))
2528 s->flags |= SN_FINST_R;
2529
2530 req->analysers = 0;
2531 req->analyse_exp = TICK_ETERNITY;
2532 return 0;
2533}
2534
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002535/* This stream analyser runs all HTTP request processing which is common to
2536 * frontends and backends, which means blocking ACLs, filters, connection-close,
2537 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002538 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002539 * either needs more data or wants to immediately abort the request (eg: deny,
2540 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002541 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002542int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002543{
Willy Tarreaud787e662009-07-07 10:14:51 +02002544 struct http_txn *txn = &s->txn;
2545 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002546 struct acl_cond *cond;
2547 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002548 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002549 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002550
Willy Tarreau655dce92009-11-08 13:10:58 +01002551 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002552 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002553 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002554 return 0;
2555 }
2556
Willy Tarreau3a816292009-07-07 10:55:49 +02002557 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 req->analyse_exp = TICK_ETERNITY;
2559
2560 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2561 now_ms, __FUNCTION__,
2562 s,
2563 req,
2564 req->rex, req->wex,
2565 req->flags,
2566 req->l,
2567 req->analysers);
2568
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002569 /* first check whether we have some ACLs set to block this request */
2570 list_for_each_entry(cond, &px->block_cond, list) {
2571 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002572
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002573 ret = acl_pass(ret);
2574 if (cond->pol == ACL_COND_UNLESS)
2575 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002576
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002577 if (ret) {
2578 txn->status = 403;
2579 /* let's log the request time */
2580 s->logs.tv_request = now;
2581 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2582 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002584 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002585
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002586 /* try headers filters */
2587 if (px->req_exp != NULL) {
2588 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2589 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002590
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 /* has the request been denied ? */
2592 if (txn->flags & TX_CLDENY) {
2593 /* no need to go further */
2594 txn->status = 403;
2595 /* let's log the request time */
2596 s->logs.tv_request = now;
2597 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2598 goto return_prx_cond;
2599 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002600
2601 /* When a connection is tarpitted, we use the tarpit timeout,
2602 * which may be the same as the connect timeout if unspecified.
2603 * If unset, then set it to zero because we really want it to
2604 * eventually expire. We build the tarpit as an analyser.
2605 */
2606 if (txn->flags & TX_CLTARPIT) {
2607 buffer_erase(s->req);
2608 /* wipe the request out so that we can drop the connection early
2609 * if the client closes first.
2610 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002611 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002612 req->analysers = 0; /* remove switching rules etc... */
2613 req->analysers |= AN_REQ_HTTP_TARPIT;
2614 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2615 if (!req->analyse_exp)
2616 req->analyse_exp = tick_add(now_ms, 0);
2617 return 1;
2618 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002619 }
Willy Tarreau06619262006-12-17 08:37:22 +01002620
Willy Tarreau5b154472009-12-21 20:11:07 +01002621 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2622 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002623 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2624 * in which headers are mangled. However, if another mode is set, it will
2625 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002626 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002627
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002628 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002629 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002630 int tmp = TX_CON_WANT_TUN;
2631 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2632 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002633 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2634 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002635 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002636 tmp = TX_CON_WANT_CLO;
2637
Willy Tarreau5b154472009-12-21 20:11:07 +01002638 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2639 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002640
Willy Tarreau8db1c172010-01-05 23:12:12 +01002641 if (!(txn->flags & TX_CON_HDR_PARS))
2642 http_req_parse_connection_header(txn);
2643
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002644 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2645 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2646 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2647 if (!(txn->flags & TX_REQ_XFER_LEN))
2648 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2649 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002650 }
2651
2652 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2653 * once we know the backend, because the tunnel mode can be implied by the
2654 * lack of any close/keepalive options in both the FE and the BE. Since
2655 * this information can evolve with time, we proceed by trying to make the
2656 * header status match the desired status. For this, we'll have to adjust
2657 * the "Connection" header. The test for persistent connections has already
2658 * been performed, so we only enter here if there is a risk the connection
2659 * is considered as persistent and we want it to be closed on the server
2660 * side. It would be nice if we could enter this place only when a
2661 * Connection header exists. Note that a CONNECT method will not enter
2662 * here.
2663 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002664 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2665 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2666 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002667 char *cur_ptr, *cur_end, *cur_next;
2668 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002669 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002670 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002671
Willy Tarreau5b154472009-12-21 20:11:07 +01002672 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002673 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002674
Willy Tarreau5b154472009-12-21 20:11:07 +01002675 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002676 cur_hdr = &txn->hdr_idx.v[cur_idx];
2677 cur_ptr = cur_next;
2678 cur_end = cur_ptr + cur_hdr->len;
2679 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002680
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002681 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002682 if (!val)
2683 continue;
2684
2685 /* 3 possibilities :
2686 * - we have already set "Connection: close" or we're in
2687 * HTTP/1.0, so we remove this line.
2688 * - we have not yet set "Connection: close", but this line
2689 * indicates close. We leave it untouched and set the flag.
2690 * - we have not yet set "Connection: close", and this line
2691 * indicates non-close. We replace it and set the flag.
2692 */
2693 if (must_delete) {
2694 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2695 http_msg_move_end(&txn->req, delta);
2696 cur_next += delta;
2697 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2698 txn->hdr_idx.used--;
2699 cur_hdr->len = 0;
2700 txn->flags |= TX_REQ_CONN_CLO;
2701 } else {
2702 if (cur_end - cur_ptr - val != 5 ||
2703 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2704 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2705 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002707 cur_hdr->len += delta;
2708 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002709 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002710 txn->flags |= TX_REQ_CONN_CLO;
2711 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002712 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002713 } /* for loop */
2714 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002715
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002716 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002717 list_for_each_entry(wl, &px->req_add, list) {
2718 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719 goto return_bad_req;
2720 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002721
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722 /* check if stats URI was requested, and if an auth is needed */
2723 if (px->uri_auth != NULL &&
2724 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2725 /* we have to check the URI and auth for this request.
2726 * FIXME!!! that one is rather dangerous, we want to
2727 * make it follow standard rules (eg: clear req->analysers).
2728 */
2729 if (stats_check_uri_auth(s, px)) {
2730 req->analysers = 0;
2731 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002734
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002735 /* check whether we have some ACLs set to redirect this request */
2736 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002737 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002738
Willy Tarreauf285f542010-01-03 20:03:03 +01002739 if (rule->cond) {
2740 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2741 ret = acl_pass(ret);
2742 if (rule->cond->pol == ACL_COND_UNLESS)
2743 ret = !ret;
2744 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002745
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002746 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002747 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002749
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750 /* build redirect message */
2751 switch(rule->code) {
2752 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002753 msg_fmt = HTTP_303;
2754 break;
2755 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 msg_fmt = HTTP_301;
2757 break;
2758 case 302:
2759 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 msg_fmt = HTTP_302;
2761 break;
2762 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002763
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002764 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002766
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 switch(rule->type) {
2768 case REDIRECT_TYPE_PREFIX: {
2769 const char *path;
2770 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002771
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 path = http_get_path(txn);
2773 /* build message using path */
2774 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002775 pathlen = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002776 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2777 int qs = 0;
2778 while (qs < pathlen) {
2779 if (path[qs] == '?') {
2780 pathlen = qs;
2781 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002782 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002783 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002784 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002785 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 } else {
2787 path = "/";
2788 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002789 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002790
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002791 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002792 goto return_bad_req;
2793
2794 /* add prefix. Note that if prefix == "/", we don't want to
2795 * add anything, otherwise it makes it hard for the user to
2796 * configure a self-redirection.
2797 */
2798 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002799 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2800 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002801 }
2802
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 /* add path */
2804 memcpy(rdr.str + rdr.len, path, pathlen);
2805 rdr.len += pathlen;
2806 break;
2807 }
2808 case REDIRECT_TYPE_LOCATION:
2809 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002810 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002812
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002813 /* add location */
2814 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2815 rdr.len += rule->rdr_len;
2816 break;
2817 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002818
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002819 if (rule->cookie_len) {
2820 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2821 rdr.len += 14;
2822 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2823 rdr.len += rule->cookie_len;
2824 memcpy(rdr.str + rdr.len, "\r\n", 2);
2825 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002826 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002827
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002828 /* add end of headers and the keep-alive/close status.
2829 * We may choose to set keep-alive if the Location begins
2830 * with a slash, because the client will come back to the
2831 * same server.
2832 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 txn->status = rule->code;
2834 /* let's log the request time */
2835 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002836
2837 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2838 (txn->flags & TX_REQ_XFER_LEN) &&
2839 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2840 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2841 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2842 /* keep-alive possible */
2843 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2844 rdr.len += 28;
2845 buffer_write(req->prod->ob, rdr.str, rdr.len);
2846 /* "eat" the request */
2847 buffer_ignore(req, msg->sov - msg->som);
2848 msg->som = msg->sov;
2849 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002850 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2851 txn->req.msg_state = HTTP_MSG_CLOSED;
2852 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002853 break;
2854 } else {
2855 /* keep-alive not possible */
2856 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2857 rdr.len += 23;
Willy Tarreauea65e682010-01-08 00:26:50 +01002858 buffer_write(req->prod->ob, rdr.str, rdr.len);
2859 stream_int_cond_close(req->prod, NULL);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002860 goto return_prx_cond;
2861 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002862 }
2863 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002864
Willy Tarreau2be39392010-01-03 17:24:51 +01002865 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2866 * If this happens, then the data will not come immediately, so we must
2867 * send all what we have without waiting. Note that due to the small gain
2868 * in waiting for the body of the request, it's easier to simply put the
2869 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2870 * itself once used.
2871 */
2872 req->flags |= BF_SEND_DONTWAIT;
2873
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002874 /* that's OK for us now, let's move on to next analysers */
2875 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002876
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002877 return_bad_req:
2878 /* We centralize bad requests processing here */
2879 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2880 /* we detected a parsing error. We want to archive this request
2881 * in the dedicated proxy area for later troubleshooting.
2882 */
2883 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2884 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002885
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002886 txn->req.msg_state = HTTP_MSG_ERROR;
2887 txn->status = 400;
2888 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002889
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002890 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002891 if (s->listener->counters)
2892 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002893
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002894 return_prx_cond:
2895 if (!(s->flags & SN_ERR_MASK))
2896 s->flags |= SN_ERR_PRXCOND;
2897 if (!(s->flags & SN_FINST_MASK))
2898 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002899
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002900 req->analysers = 0;
2901 req->analyse_exp = TICK_ETERNITY;
2902 return 0;
2903}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002904
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002905/* This function performs all the processing enabled for the current request.
2906 * It returns 1 if the processing can continue on next analysers, or zero if it
2907 * needs more data, encounters an error, or wants to immediately abort the
2908 * request. It relies on buffers flags, and updates s->req->analysers.
2909 */
2910int http_process_request(struct session *s, struct buffer *req, int an_bit)
2911{
2912 struct http_txn *txn = &s->txn;
2913 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002914
Willy Tarreau655dce92009-11-08 13:10:58 +01002915 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002916 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002917 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002918 return 0;
2919 }
2920
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002921 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2922 now_ms, __FUNCTION__,
2923 s,
2924 req,
2925 req->rex, req->wex,
2926 req->flags,
2927 req->l,
2928 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002929
Willy Tarreau59234e92008-11-30 23:51:27 +01002930 /*
2931 * Right now, we know that we have processed the entire headers
2932 * and that unwanted requests have been filtered out. We can do
2933 * whatever we want with the remaining request. Also, now we
2934 * may have separate values for ->fe, ->be.
2935 */
Willy Tarreau06619262006-12-17 08:37:22 +01002936
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 /*
2938 * If HTTP PROXY is set we simply get remote server address
2939 * parsing incoming request.
2940 */
2941 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2942 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2943 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002944
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002946 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002947 * Note that doing so might move headers in the request, but
2948 * the fields will stay coherent and the URI will not move.
2949 * This should only be performed in the backend.
2950 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002951 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002952 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2953 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002954
Willy Tarreau59234e92008-11-30 23:51:27 +01002955 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002956 * 8: the appsession cookie was looked up very early in 1.2,
2957 * so let's do the same now.
2958 */
2959
2960 /* It needs to look into the URI */
2961 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002962 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002963 }
2964
2965 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002966 * 9: add X-Forwarded-For if either the frontend or the backend
2967 * asks for it.
2968 */
2969 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2970 if (s->cli_addr.ss_family == AF_INET) {
2971 /* Add an X-Forwarded-For header unless the source IP is
2972 * in the 'except' network range.
2973 */
2974 if ((!s->fe->except_mask.s_addr ||
2975 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2976 != s->fe->except_net.s_addr) &&
2977 (!s->be->except_mask.s_addr ||
2978 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2979 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002980 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002981 unsigned char *pn;
2982 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002983
2984 /* Note: we rely on the backend to get the header name to be used for
2985 * x-forwarded-for, because the header is really meant for the backends.
2986 * However, if the backend did not specify any option, we have to rely
2987 * on the frontend's header name.
2988 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 if (s->be->fwdfor_hdr_len) {
2990 len = s->be->fwdfor_hdr_len;
2991 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002992 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002993 len = s->fe->fwdfor_hdr_len;
2994 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002995 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002997
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002998 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002999 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003000 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003001 }
3002 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 else if (s->cli_addr.ss_family == AF_INET6) {
3004 /* FIXME: for the sake of completeness, we should also support
3005 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003006 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003007 int len;
3008 char pn[INET6_ADDRSTRLEN];
3009 inet_ntop(AF_INET6,
3010 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3011 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003012
Willy Tarreau59234e92008-11-30 23:51:27 +01003013 /* Note: we rely on the backend to get the header name to be used for
3014 * x-forwarded-for, because the header is really meant for the backends.
3015 * However, if the backend did not specify any option, we have to rely
3016 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003017 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003018 if (s->be->fwdfor_hdr_len) {
3019 len = s->be->fwdfor_hdr_len;
3020 memcpy(trash, s->be->fwdfor_hdr_name, len);
3021 } else {
3022 len = s->fe->fwdfor_hdr_len;
3023 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003024 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003025 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003026
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003028 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003029 goto return_bad_req;
3030 }
3031 }
3032
3033 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003034 * 10: add X-Original-To if either the frontend or the backend
3035 * asks for it.
3036 */
3037 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3038
3039 /* FIXME: don't know if IPv6 can handle that case too. */
3040 if (s->cli_addr.ss_family == AF_INET) {
3041 /* Add an X-Original-To header unless the destination IP is
3042 * in the 'except' network range.
3043 */
3044 if (!(s->flags & SN_FRT_ADDR_SET))
3045 get_frt_addr(s);
3046
3047 if ((!s->fe->except_mask_to.s_addr ||
3048 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3049 != s->fe->except_to.s_addr) &&
3050 (!s->be->except_mask_to.s_addr ||
3051 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3052 != s->be->except_to.s_addr)) {
3053 int len;
3054 unsigned char *pn;
3055 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3056
3057 /* Note: we rely on the backend to get the header name to be used for
3058 * x-original-to, because the header is really meant for the backends.
3059 * However, if the backend did not specify any option, we have to rely
3060 * on the frontend's header name.
3061 */
3062 if (s->be->orgto_hdr_len) {
3063 len = s->be->orgto_hdr_len;
3064 memcpy(trash, s->be->orgto_hdr_name, len);
3065 } else {
3066 len = s->fe->orgto_hdr_len;
3067 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003068 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003069 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3070
3071 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003072 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003073 goto return_bad_req;
3074 }
3075 }
3076 }
3077
Willy Tarreau78599912009-10-17 20:12:21 +02003078 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003079 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3080 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3081 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003082 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003083 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003084 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003085 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003086 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003087
3088 /* If we have no server assigned yet and we're balancing on url_param
3089 * with a POST request, we may be interested in checking the body for
3090 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003091 */
3092 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3093 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003094 s->be->url_param_post_limit != 0 &&
3095 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003096 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003097 buffer_dont_connect(req);
3098 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003099 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003100
Willy Tarreaud98cf932009-12-27 22:54:55 +01003101 if (txn->flags & TX_REQ_XFER_LEN)
3102 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003103
Willy Tarreau59234e92008-11-30 23:51:27 +01003104 /*************************************************************
3105 * OK, that's finished for the headers. We have done what we *
3106 * could. Let's switch to the DATA state. *
3107 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003108 req->analyse_exp = TICK_ETERNITY;
3109 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003110
Willy Tarreau59234e92008-11-30 23:51:27 +01003111 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003112 /* OK let's go on with the BODY now */
3113 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003114
Willy Tarreau59234e92008-11-30 23:51:27 +01003115 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003116 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003117 /* we detected a parsing error. We want to archive this request
3118 * in the dedicated proxy area for later troubleshooting.
3119 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003120 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003121 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003122
Willy Tarreau59234e92008-11-30 23:51:27 +01003123 txn->req.msg_state = HTTP_MSG_ERROR;
3124 txn->status = 400;
3125 req->analysers = 0;
3126 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003127
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003128 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003129 if (s->listener->counters)
3130 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003131
Willy Tarreau59234e92008-11-30 23:51:27 +01003132 if (!(s->flags & SN_ERR_MASK))
3133 s->flags |= SN_ERR_PRXCOND;
3134 if (!(s->flags & SN_FINST_MASK))
3135 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003136 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003137}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003138
Willy Tarreau60b85b02008-11-30 23:28:40 +01003139/* This function is an analyser which processes the HTTP tarpit. It always
3140 * returns zero, at the beginning because it prevents any other processing
3141 * from occurring, and at the end because it terminates the request.
3142 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003143int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003144{
3145 struct http_txn *txn = &s->txn;
3146
3147 /* This connection is being tarpitted. The CLIENT side has
3148 * already set the connect expiration date to the right
3149 * timeout. We just have to check that the client is still
3150 * there and that the timeout has not expired.
3151 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003152 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003153 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3154 !tick_is_expired(req->analyse_exp, now_ms))
3155 return 0;
3156
3157 /* We will set the queue timer to the time spent, just for
3158 * logging purposes. We fake a 500 server error, so that the
3159 * attacker will not suspect his connection has been tarpitted.
3160 * It will not cause trouble to the logs because we can exclude
3161 * the tarpitted connections by filtering on the 'PT' status flags.
3162 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003163 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3164
3165 txn->status = 500;
3166 if (req->flags != BF_READ_ERROR)
3167 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3168
3169 req->analysers = 0;
3170 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003171
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003172 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003173 if (s->listener->counters)
3174 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003175
Willy Tarreau60b85b02008-11-30 23:28:40 +01003176 if (!(s->flags & SN_ERR_MASK))
3177 s->flags |= SN_ERR_PRXCOND;
3178 if (!(s->flags & SN_FINST_MASK))
3179 s->flags |= SN_FINST_T;
3180 return 0;
3181}
3182
Willy Tarreaud34af782008-11-30 23:36:37 +01003183/* This function is an analyser which processes the HTTP request body. It looks
3184 * for parameters to be used for the load balancing algorithm (url_param). It
3185 * must only be called after the standard HTTP request processing has occurred,
3186 * because it expects the request to be parsed. It returns zero if it needs to
3187 * read more data, or 1 once it has completed its analysis.
3188 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003189int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003190{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003191 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003192 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003193 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003194
3195 /* We have to parse the HTTP request body to find any required data.
3196 * "balance url_param check_post" should have been the only way to get
3197 * into this. We were brought here after HTTP header analysis, so all
3198 * related structures are ready.
3199 */
3200
Willy Tarreau522d6c02009-12-06 18:49:18 +01003201 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3202 goto missing_data;
3203
3204 if (msg->msg_state < HTTP_MSG_100_SENT) {
3205 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3206 * send an HTTP/1.1 100 Continue intermediate response.
3207 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003208 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003209 struct hdr_ctx ctx;
3210 ctx.idx = 0;
3211 /* Expect is allowed in 1.1, look for it */
3212 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3213 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3214 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3215 }
3216 }
3217 msg->msg_state = HTTP_MSG_100_SENT;
3218 }
3219
3220 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003221 /* we have msg->col and msg->sov which both point to the first
3222 * byte of message body. msg->som still points to the beginning
3223 * of the message. We must save the body in req->lr because it
3224 * survives buffer re-alignments.
3225 */
3226 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003227 if (txn->flags & TX_REQ_TE_CHNK)
3228 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3229 else
3230 msg->msg_state = HTTP_MSG_DATA;
3231 }
3232
3233 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003234 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003235 * set ->sov and ->lr to point to the body and switch to DATA or
3236 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003237 */
3238 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003239
Willy Tarreau115acb92009-12-26 13:56:06 +01003240 if (!ret)
3241 goto missing_data;
3242 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003243 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003244 }
3245
Willy Tarreaud98cf932009-12-27 22:54:55 +01003246 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003247 * We have the first non-header byte in msg->col, which is either the
3248 * beginning of the chunk size or of the data. The first data byte is in
3249 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3250 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003251 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003252
3253 if (msg->hdr_content_len < limit)
3254 limit = msg->hdr_content_len;
3255
Willy Tarreau7c96f672009-12-27 22:47:25 +01003256 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003257 goto http_end;
3258
3259 missing_data:
3260 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003261 if (req->flags & BF_FULL)
3262 goto return_bad_req;
3263
Willy Tarreau522d6c02009-12-06 18:49:18 +01003264 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3265 txn->status = 408;
3266 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3267 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003268 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003269
3270 /* we get here if we need to wait for more data */
3271 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003272 /* Not enough data. We'll re-use the http-request
3273 * timeout here. Ideally, we should set the timeout
3274 * relative to the accept() date. We just set the
3275 * request timeout once at the beginning of the
3276 * request.
3277 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003278 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003279 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003280 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003281 return 0;
3282 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003283
3284 http_end:
3285 /* The situation will not evolve, so let's give up on the analysis. */
3286 s->logs.tv_request = now; /* update the request timer to reflect full request */
3287 req->analysers &= ~an_bit;
3288 req->analyse_exp = TICK_ETERNITY;
3289 return 1;
3290
3291 return_bad_req: /* let's centralize all bad requests */
3292 txn->req.msg_state = HTTP_MSG_ERROR;
3293 txn->status = 400;
3294 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3295
3296 return_err_msg:
3297 req->analysers = 0;
3298 s->fe->counters.failed_req++;
3299 if (s->listener->counters)
3300 s->listener->counters->failed_req++;
3301
3302 if (!(s->flags & SN_ERR_MASK))
3303 s->flags |= SN_ERR_PRXCOND;
3304 if (!(s->flags & SN_FINST_MASK))
3305 s->flags |= SN_FINST_R;
3306 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003307}
3308
Willy Tarreau610ecce2010-01-04 21:15:02 +01003309/* Terminate current transaction and prepare a new one. This is very tricky
3310 * right now but it works.
3311 */
3312void http_end_txn_clean_session(struct session *s)
3313{
3314 /* FIXME: We need a more portable way of releasing a backend's and a
3315 * server's connections. We need a safer way to reinitialize buffer
3316 * flags. We also need a more accurate method for computing per-request
3317 * data.
3318 */
3319 http_silent_debug(__LINE__, s);
3320
3321 s->req->cons->flags |= SI_FL_NOLINGER;
3322 s->req->cons->shutr(s->req->cons);
3323 s->req->cons->shutw(s->req->cons);
3324
3325 http_silent_debug(__LINE__, s);
3326
3327 if (s->flags & SN_BE_ASSIGNED)
3328 s->be->beconn--;
3329
3330 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3331 session_process_counters(s);
3332
3333 if (s->txn.status) {
3334 int n;
3335
3336 n = s->txn.status / 100;
3337 if (n < 1 || n > 5)
3338 n = 0;
3339
3340 if (s->fe->mode == PR_MODE_HTTP)
3341 s->fe->counters.p.http.rsp[n]++;
3342
3343 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3344 (s->be->mode == PR_MODE_HTTP))
3345 s->be->counters.p.http.rsp[n]++;
3346 }
3347
3348 /* don't count other requests' data */
3349 s->logs.bytes_in -= s->req->l - s->req->send_max;
3350 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3351
3352 /* let's do a final log if we need it */
3353 if (s->logs.logwait &&
3354 !(s->flags & SN_MONITOR) &&
3355 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3356 s->do_log(s);
3357 }
3358
3359 s->logs.accept_date = date; /* user-visible date for logging */
3360 s->logs.tv_accept = now; /* corrected date for internal use */
3361 tv_zero(&s->logs.tv_request);
3362 s->logs.t_queue = -1;
3363 s->logs.t_connect = -1;
3364 s->logs.t_data = -1;
3365 s->logs.t_close = 0;
3366 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3367 s->logs.srv_queue_size = 0; /* we will get this number soon */
3368
3369 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3370 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3371
3372 if (s->pend_pos)
3373 pendconn_free(s->pend_pos);
3374
3375 if (s->srv) {
3376 if (s->flags & SN_CURR_SESS) {
3377 s->flags &= ~SN_CURR_SESS;
3378 s->srv->cur_sess--;
3379 }
3380 if (may_dequeue_tasks(s->srv, s->be))
3381 process_srv_queue(s->srv);
3382 }
3383
3384 if (unlikely(s->srv_conn))
3385 sess_change_server(s, NULL);
3386 s->srv = NULL;
3387
3388 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3389 s->req->cons->fd = -1; /* just to help with debugging */
3390 s->req->cons->err_type = SI_ET_NONE;
3391 s->req->cons->err_loc = NULL;
3392 s->req->cons->exp = TICK_ETERNITY;
3393 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003394 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3395 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003396 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3397 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3398 s->txn.meth = 0;
3399 http_reset_txn(s);
3400 s->txn.flags |= TX_NOT_FIRST;
3401 if (s->be->options2 & PR_O2_INDEPSTR)
3402 s->req->cons->flags |= SI_FL_INDEP_STR;
3403
3404 /* if the request buffer is not empty, it means we're
3405 * about to process another request, so send pending
3406 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003407 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003408 if (s->req->l > s->req->send_max)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003409 s->rep->flags |= BF_EXPECT_MORE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003410
3411 /* we're removing the analysers, we MUST re-enable events detection */
3412 buffer_auto_read(s->req);
3413 buffer_auto_close(s->req);
3414 buffer_auto_read(s->rep);
3415 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003416
3417 /* make ->lr point to the first non-forwarded byte */
3418 s->req->lr = s->req->w + s->req->send_max;
3419 if (s->req->lr >= s->req->data + s->req->size)
3420 s->req->lr -= s->req->size;
3421 s->rep->lr = s->rep->w + s->rep->send_max;
3422 if (s->rep->lr >= s->rep->data + s->rep->size)
3423 s->rep->lr -= s->req->size;
3424
3425 s->req->analysers |= s->fe->fe_req_ana;
3426 s->rep->analysers = 0;
3427
3428 http_silent_debug(__LINE__, s);
3429}
3430
3431
3432/* This function updates the request state machine according to the response
3433 * state machine and buffer flags. It returns 1 if it changes anything (flag
3434 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3435 * it is only used to find when a request/response couple is complete. Both
3436 * this function and its equivalent should loop until both return zero. It
3437 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3438 */
3439int http_sync_req_state(struct session *s)
3440{
3441 struct buffer *buf = s->req;
3442 struct http_txn *txn = &s->txn;
3443 unsigned int old_flags = buf->flags;
3444 unsigned int old_state = txn->req.msg_state;
3445
3446 http_silent_debug(__LINE__, s);
3447 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3448 return 0;
3449
3450 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003451 /* No need to read anymore, the request was completely parsed.
3452 * We can shut the read side unless we want to abort_on_close.
3453 */
3454 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3455 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003456
3457 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3458 goto wait_other_side;
3459
3460 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3461 /* The server has not finished to respond, so we
3462 * don't want to move in order not to upset it.
3463 */
3464 goto wait_other_side;
3465 }
3466
3467 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3468 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003469 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003470 txn->req.msg_state = HTTP_MSG_TUNNEL;
3471 goto wait_other_side;
3472 }
3473
3474 /* When we get here, it means that both the request and the
3475 * response have finished receiving. Depending on the connection
3476 * mode, we'll have to wait for the last bytes to leave in either
3477 * direction, and sometimes for a close to be effective.
3478 */
3479
3480 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3481 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3482 /* Server-close mode : queue a connection close to the server */
3483 buffer_shutw_now(buf);
3484 buf->cons->flags |= SI_FL_NOLINGER;
3485 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003486 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003487 /* Option forceclose is set, let's enforce it now
3488 * that we're not expecting any new data to come.
3489 */
3490 buffer_shutr_now(buf);
3491 buffer_shutw_now(buf);
3492 buf->cons->flags |= SI_FL_NOLINGER;
3493 }
3494 /* other modes include httpclose (no action) and keepalive (not implemented) */
3495 }
3496
3497 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3498 /* if we've just closed an output, let's switch */
3499 if (!(buf->flags & BF_OUT_EMPTY)) {
3500 txn->req.msg_state = HTTP_MSG_CLOSING;
3501 goto http_msg_closing;
3502 }
3503 else {
3504 txn->req.msg_state = HTTP_MSG_CLOSED;
3505 goto http_msg_closed;
3506 }
3507 }
3508 else {
3509 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003510 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003511 txn->req.msg_state = HTTP_MSG_TUNNEL;
3512 goto wait_other_side;
3513 }
3514 }
3515
3516 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3517 http_msg_closing:
3518 /* nothing else to forward, just waiting for the output buffer
3519 * to be empty and for the shutw_now to take effect.
3520 */
3521 if (buf->flags & BF_OUT_EMPTY) {
3522 txn->req.msg_state = HTTP_MSG_CLOSED;
3523 goto http_msg_closed;
3524 }
3525 else if (buf->flags & BF_SHUTW) {
3526 txn->req.msg_state = HTTP_MSG_ERROR;
3527 goto wait_other_side;
3528 }
3529 }
3530
3531 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3532 http_msg_closed:
3533 goto wait_other_side;
3534 }
3535
3536 wait_other_side:
3537 http_silent_debug(__LINE__, s);
3538 return txn->req.msg_state != old_state || buf->flags != old_flags;
3539}
3540
3541
3542/* This function updates the response state machine according to the request
3543 * state machine and buffer flags. It returns 1 if it changes anything (flag
3544 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3545 * it is only used to find when a request/response couple is complete. Both
3546 * this function and its equivalent should loop until both return zero. It
3547 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3548 */
3549int http_sync_res_state(struct session *s)
3550{
3551 struct buffer *buf = s->rep;
3552 struct http_txn *txn = &s->txn;
3553 unsigned int old_flags = buf->flags;
3554 unsigned int old_state = txn->rsp.msg_state;
3555
3556 http_silent_debug(__LINE__, s);
3557 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3558 return 0;
3559
3560 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3561 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003562 * still monitor the server connection for a possible close
3563 * while the request is being uploaded, so we don't disable
3564 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003565 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003566 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003567
3568 if (txn->req.msg_state == HTTP_MSG_ERROR)
3569 goto wait_other_side;
3570
3571 if (txn->req.msg_state < HTTP_MSG_DONE) {
3572 /* The client seems to still be sending data, probably
3573 * because we got an error response during an upload.
3574 * We have the choice of either breaking the connection
3575 * or letting it pass through. Let's do the later.
3576 */
3577 goto wait_other_side;
3578 }
3579
3580 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3581 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003582 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003583 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3584 goto wait_other_side;
3585 }
3586
3587 /* When we get here, it means that both the request and the
3588 * response have finished receiving. Depending on the connection
3589 * mode, we'll have to wait for the last bytes to leave in either
3590 * direction, and sometimes for a close to be effective.
3591 */
3592
3593 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3594 /* Server-close mode : shut read and wait for the request
3595 * side to close its output buffer. The caller will detect
3596 * when we're in DONE and the other is in CLOSED and will
3597 * catch that for the final cleanup.
3598 */
3599 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3600 buffer_shutr_now(buf);
3601 goto wait_other_side;
3602 }
3603 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3604 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3605 /* Option forceclose is set, let's enforce it now
3606 * that we're not expecting any new data to come.
3607 * The caller knows the session is complete once
3608 * both states are CLOSED.
3609 */
3610 buffer_shutr_now(buf);
3611 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003612 }
3613 else {
3614 /* other modes include httpclose (no action) and keepalive
3615 * (not implemented). These modes are used as a tunnel right
3616 * now.
3617 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003618 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003619 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3620 goto wait_other_side;
3621 }
3622
3623 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3624 /* if we've just closed an output, let's switch */
3625 if (!(buf->flags & BF_OUT_EMPTY)) {
3626 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3627 goto http_msg_closing;
3628 }
3629 else {
3630 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3631 goto http_msg_closed;
3632 }
3633 }
3634 goto wait_other_side;
3635 }
3636
3637 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3638 http_msg_closing:
3639 /* nothing else to forward, just waiting for the output buffer
3640 * to be empty and for the shutw_now to take effect.
3641 */
3642 if (buf->flags & BF_OUT_EMPTY) {
3643 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3644 goto http_msg_closed;
3645 }
3646 else if (buf->flags & BF_SHUTW) {
3647 txn->rsp.msg_state = HTTP_MSG_ERROR;
3648 goto wait_other_side;
3649 }
3650 }
3651
3652 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3653 http_msg_closed:
3654 /* drop any pending data */
3655 buffer_ignore(buf, buf->l - buf->send_max);
3656 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003657 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003658 goto wait_other_side;
3659 }
3660
3661 wait_other_side:
3662 http_silent_debug(__LINE__, s);
3663 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3664}
3665
3666
3667/* Resync the request and response state machines. Return 1 if either state
3668 * changes.
3669 */
3670int http_resync_states(struct session *s)
3671{
3672 struct http_txn *txn = &s->txn;
3673 int old_req_state = txn->req.msg_state;
3674 int old_res_state = txn->rsp.msg_state;
3675
3676 http_silent_debug(__LINE__, s);
3677 http_sync_req_state(s);
3678 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003679 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003680 if (!http_sync_res_state(s))
3681 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003682 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003683 if (!http_sync_req_state(s))
3684 break;
3685 }
3686 http_silent_debug(__LINE__, s);
3687 /* OK, both state machines agree on a compatible state.
3688 * There are a few cases we're interested in :
3689 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3690 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3691 * directions, so let's simply disable both analysers.
3692 * - HTTP_MSG_CLOSED on the response only means we must abort the
3693 * request.
3694 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3695 * with server-close mode means we've completed one request and we
3696 * must re-initialize the server connection.
3697 */
3698
3699 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3700 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3701 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3702 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3703 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003704 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003705 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003706 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003707 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003708 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003710 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3711 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3712 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003713 s->rep->analysers = 0;
3714 buffer_auto_close(s->rep);
3715 buffer_auto_read(s->rep);
3716 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717 buffer_abort(s->req);
3718 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003719 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003720 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003721 }
3722 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3723 txn->rsp.msg_state == HTTP_MSG_DONE &&
3724 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3725 /* server-close: terminate this server connection and
3726 * reinitialize a fresh-new transaction.
3727 */
3728 http_end_txn_clean_session(s);
3729 }
3730
3731 http_silent_debug(__LINE__, s);
3732 return txn->req.msg_state != old_req_state ||
3733 txn->rsp.msg_state != old_res_state;
3734}
3735
Willy Tarreaud98cf932009-12-27 22:54:55 +01003736/* This function is an analyser which forwards request body (including chunk
3737 * sizes if any). It is called as soon as we must forward, even if we forward
3738 * zero byte. The only situation where it must not be called is when we're in
3739 * tunnel mode and we want to forward till the close. It's used both to forward
3740 * remaining data and to resync after end of body. It expects the msg_state to
3741 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3742 * read more data, or 1 once we can go on with next request or end the session.
3743 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3744 * bytes of pending data + the headers if not already done (between som and sov).
3745 * It eventually adjusts som to match sov after the data in between have been sent.
3746 */
3747int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3748{
3749 struct http_txn *txn = &s->txn;
3750 struct http_msg *msg = &s->txn.req;
3751
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003752 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3753 return 0;
3754
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003755 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3756 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3757 /* Output closed while we were sending data. We must abort. */
3758 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003759 buffer_auto_read(req);
3760 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003761 req->analysers &= ~an_bit;
3762 return 1;
3763 }
3764
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003765 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003766
3767 /* Note that we don't have to send 100-continue back because we don't
3768 * need the data to complete our job, and it's up to the server to
3769 * decide whether to return 100, 417 or anything else in return of
3770 * an "Expect: 100-continue" header.
3771 */
3772
3773 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3774 /* we have msg->col and msg->sov which both point to the first
3775 * byte of message body. msg->som still points to the beginning
3776 * of the message. We must save the body in req->lr because it
3777 * survives buffer re-alignments.
3778 */
3779 req->lr = req->data + msg->sov;
3780 if (txn->flags & TX_REQ_TE_CHNK)
3781 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3782 else {
3783 msg->msg_state = HTTP_MSG_DATA;
3784 }
3785 }
3786
Willy Tarreaud98cf932009-12-27 22:54:55 +01003787 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003789 /* we may have some data pending */
3790 if (msg->hdr_content_len || msg->som != msg->sov) {
3791 int bytes = msg->sov - msg->som;
3792 if (bytes < 0) /* sov may have wrapped at the end */
3793 bytes += req->size;
3794 buffer_forward(req, bytes + msg->hdr_content_len);
3795 msg->hdr_content_len = 0; /* don't forward that again */
3796 msg->som = msg->sov;
3797 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003798
Willy Tarreaucaabe412010-01-03 23:08:28 +01003799 if (msg->msg_state == HTTP_MSG_DATA) {
3800 /* must still forward */
3801 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003802 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003803
3804 /* nothing left to forward */
3805 if (txn->flags & TX_REQ_TE_CHNK)
3806 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003807 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003808 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003809 }
3810 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003811 /* read the chunk size and assign it to ->hdr_content_len, then
3812 * set ->sov and ->lr to point to the body and switch to DATA or
3813 * TRAILERS state.
3814 */
3815 int ret = http_parse_chunk_size(req, msg);
3816
3817 if (!ret)
3818 goto missing_data;
3819 else if (ret < 0)
3820 goto return_bad_req;
3821 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003822 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003823 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3824 /* we want the CRLF after the data */
3825 int ret;
3826
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003827 req->lr = req->w + req->send_max;
3828 if (req->lr >= req->data + req->size)
3829 req->lr -= req->size;
3830
Willy Tarreaud98cf932009-12-27 22:54:55 +01003831 ret = http_skip_chunk_crlf(req, msg);
3832
3833 if (ret == 0)
3834 goto missing_data;
3835 else if (ret < 0)
3836 goto return_bad_req;
3837 /* we're in MSG_CHUNK_SIZE now */
3838 }
3839 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3840 int ret = http_forward_trailers(req, msg);
3841
3842 if (ret == 0)
3843 goto missing_data;
3844 else if (ret < 0)
3845 goto return_bad_req;
3846 /* we're in HTTP_MSG_DONE now */
3847 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003848 else {
3849 /* other states, DONE...TUNNEL */
3850 if (http_resync_states(s)) {
3851 /* some state changes occurred, maybe the analyser
3852 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003853 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003854 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3855 goto return_bad_req;
3856 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003857 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003858 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003859 }
3860 }
3861
Willy Tarreaud98cf932009-12-27 22:54:55 +01003862 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003863 /* stop waiting for data if the input is closed before the end */
3864 if (req->flags & BF_SHUTR)
3865 goto return_bad_req;
3866
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003867 /* waiting for the last bits to leave the buffer */
3868 if (req->flags & BF_SHUTW)
3869 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003870
3871 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003872 return 0;
3873
Willy Tarreaud98cf932009-12-27 22:54:55 +01003874 return_bad_req: /* let's centralize all bad requests */
3875 txn->req.msg_state = HTTP_MSG_ERROR;
3876 txn->status = 400;
3877 /* Note: we don't send any error if some data were already sent */
3878 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3879
Willy Tarreau90deb182010-01-07 00:20:41 +01003880 buffer_auto_read(req);
3881 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003882 req->analysers = 0;
3883 s->fe->counters.failed_req++;
3884 if (s->listener->counters)
3885 s->listener->counters->failed_req++;
3886
3887 if (!(s->flags & SN_ERR_MASK))
3888 s->flags |= SN_ERR_PRXCOND;
3889 if (!(s->flags & SN_FINST_MASK))
3890 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003892 return 0;
3893}
3894
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003895/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3896 * processing can continue on next analysers, or zero if it either needs more
3897 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3898 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3899 * when it has nothing left to do, and may remove any analyser when it wants to
3900 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003901 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003902int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003903{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003904 struct http_txn *txn = &s->txn;
3905 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003906 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003907 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003908 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003909 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003910
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003911 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 +02003912 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003913 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003914 rep,
3915 rep->rex, rep->wex,
3916 rep->flags,
3917 rep->l,
3918 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003919
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003920 /*
3921 * Now parse the partial (or complete) lines.
3922 * We will check the response syntax, and also join multi-line
3923 * headers. An index of all the lines will be elaborated while
3924 * parsing.
3925 *
3926 * For the parsing, we use a 28 states FSM.
3927 *
3928 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003929 * rep->data + msg->som = beginning of response
3930 * rep->data + msg->eoh = end of processed headers / start of current one
3931 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003932 * rep->lr = first non-visited byte
3933 * rep->r = end of data
3934 */
3935
Willy Tarreau83e3af02009-12-28 17:39:57 +01003936 /* There's a protected area at the end of the buffer for rewriting
3937 * purposes. We don't want to start to parse the request if the
3938 * protected area is affected, because we may have to move processed
3939 * data later, which is much more complicated.
3940 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003941 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3942 if (unlikely((rep->flags & BF_FULL) ||
3943 rep->r < rep->lr ||
3944 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3945 if (rep->send_max) {
3946 /* some data has still not left the buffer, wake us once that's done */
3947 buffer_dont_close(rep);
3948 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3949 return 0;
3950 }
3951 if (rep->l <= rep->size - global.tune.maxrewrite)
3952 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003953 }
3954
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003955 if (likely(rep->lr < rep->r))
3956 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003957 }
3958
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003959 /* 1: we might have to print this header in debug mode */
3960 if (unlikely((global.mode & MODE_DEBUG) &&
3961 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003962 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003963 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003964
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003965 sol = rep->data + msg->som;
3966 eol = sol + msg->sl.rq.l;
3967 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003969 sol += hdr_idx_first_pos(&txn->hdr_idx);
3970 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003971
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003972 while (cur_idx) {
3973 eol = sol + txn->hdr_idx.v[cur_idx].len;
3974 debug_hdr("srvhdr", s, sol, eol);
3975 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3976 cur_idx = txn->hdr_idx.v[cur_idx].next;
3977 }
3978 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003979
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003980 /*
3981 * Now we quickly check if we have found a full valid response.
3982 * If not so, we check the FD and buffer states before leaving.
3983 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003984 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003985 * responses are checked first.
3986 *
3987 * Depending on whether the client is still there or not, we
3988 * may send an error response back or not. Note that normally
3989 * we should only check for HTTP status there, and check I/O
3990 * errors somewhere else.
3991 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003992
Willy Tarreau655dce92009-11-08 13:10:58 +01003993 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003994 /* Invalid response */
3995 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3996 /* we detected a parsing error. We want to archive this response
3997 * in the dedicated proxy area for later troubleshooting.
3998 */
3999 hdr_response_bad:
4000 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4001 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4002
4003 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004004 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004005 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004006 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4007 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004008
Willy Tarreau90deb182010-01-07 00:20:41 +01004009 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004010 rep->analysers = 0;
4011 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004012 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004013 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4014
4015 if (!(s->flags & SN_ERR_MASK))
4016 s->flags |= SN_ERR_PRXCOND;
4017 if (!(s->flags & SN_FINST_MASK))
4018 s->flags |= SN_FINST_H;
4019
4020 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004021 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004022
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004023 /* too large response does not fit in buffer. */
4024 else if (rep->flags & BF_FULL) {
4025 goto hdr_response_bad;
4026 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004027
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004028 /* read error */
4029 else if (rep->flags & BF_READ_ERROR) {
4030 if (msg->err_pos >= 0)
4031 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004032
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004033 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004034 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004035 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004036 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4037 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004038
Willy Tarreau90deb182010-01-07 00:20:41 +01004039 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004040 rep->analysers = 0;
4041 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004042 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004043 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004044
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004045 if (!(s->flags & SN_ERR_MASK))
4046 s->flags |= SN_ERR_SRVCL;
4047 if (!(s->flags & SN_FINST_MASK))
4048 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004049 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004050 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004051
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004052 /* read timeout : return a 504 to the client. */
4053 else if (rep->flags & BF_READ_TIMEOUT) {
4054 if (msg->err_pos >= 0)
4055 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004056
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004057 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004058 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004059 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004060 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4061 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004062
Willy Tarreau90deb182010-01-07 00:20:41 +01004063 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004064 rep->analysers = 0;
4065 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004066 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004067 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004068
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004069 if (!(s->flags & SN_ERR_MASK))
4070 s->flags |= SN_ERR_SRVTO;
4071 if (!(s->flags & SN_FINST_MASK))
4072 s->flags |= SN_FINST_H;
4073 return 0;
4074 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004075
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004076 /* close from server */
4077 else if (rep->flags & BF_SHUTR) {
4078 if (msg->err_pos >= 0)
4079 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004080
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004081 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004082 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004083 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004084 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4085 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004086
Willy Tarreau90deb182010-01-07 00:20:41 +01004087 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004088 rep->analysers = 0;
4089 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004090 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004091 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004092
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004093 if (!(s->flags & SN_ERR_MASK))
4094 s->flags |= SN_ERR_SRVCL;
4095 if (!(s->flags & SN_FINST_MASK))
4096 s->flags |= SN_FINST_H;
4097 return 0;
4098 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004099
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004100 /* write error to client (we don't send any message then) */
4101 else if (rep->flags & BF_WRITE_ERROR) {
4102 if (msg->err_pos >= 0)
4103 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004104
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004105 s->be->counters.failed_resp++;
4106 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004107 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004108
4109 if (!(s->flags & SN_ERR_MASK))
4110 s->flags |= SN_ERR_CLICL;
4111 if (!(s->flags & SN_FINST_MASK))
4112 s->flags |= SN_FINST_H;
4113
4114 /* process_session() will take care of the error */
4115 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004116 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004117
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004118 buffer_dont_close(rep);
4119 return 0;
4120 }
4121
4122 /* More interesting part now : we know that we have a complete
4123 * response which at least looks like HTTP. We have an indicator
4124 * of each header's length, so we can parse them quickly.
4125 */
4126
4127 if (unlikely(msg->err_pos >= 0))
4128 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4129
4130 /* ensure we keep this pointer to the beginning of the message */
4131 msg->sol = rep->data + msg->som;
4132
4133 /*
4134 * 1: get the status code
4135 */
4136 n = rep->data[msg->sl.st.c] - '0';
4137 if (n < 1 || n > 5)
4138 n = 0;
4139 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004140
Willy Tarreau5b154472009-12-21 20:11:07 +01004141 /* check if the response is HTTP/1.1 or above */
4142 if ((msg->sl.st.v_l == 8) &&
4143 ((rep->data[msg->som + 5] > '1') ||
4144 ((rep->data[msg->som + 5] == '1') &&
4145 (rep->data[msg->som + 7] >= '1'))))
4146 txn->flags |= TX_RES_VER_11;
4147
4148 /* "connection" has not been parsed yet */
4149 txn->flags &= ~TX_CON_HDR_PARS;
4150
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004151 /* transfer length unknown*/
4152 txn->flags &= ~TX_RES_XFER_LEN;
4153
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004154 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
4155
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004156 if (txn->status >= 100 && txn->status < 500)
4157 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4158 else
4159 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4160
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004161 /*
4162 * 2: check for cacheability.
4163 */
4164
4165 switch (txn->status) {
4166 case 200:
4167 case 203:
4168 case 206:
4169 case 300:
4170 case 301:
4171 case 410:
4172 /* RFC2616 @13.4:
4173 * "A response received with a status code of
4174 * 200, 203, 206, 300, 301 or 410 MAY be stored
4175 * by a cache (...) unless a cache-control
4176 * directive prohibits caching."
4177 *
4178 * RFC2616 @9.5: POST method :
4179 * "Responses to this method are not cacheable,
4180 * unless the response includes appropriate
4181 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004182 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004183 if (likely(txn->meth != HTTP_METH_POST) &&
4184 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4185 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4186 break;
4187 default:
4188 break;
4189 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004190
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004191 /*
4192 * 3: we may need to capture headers
4193 */
4194 s->logs.logwait &= ~LW_RESP;
4195 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
4196 capture_headers(rep->data + msg->som, &txn->hdr_idx,
4197 txn->rsp.cap, s->fe->rsp_cap);
4198
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004199 /* 4: determine the transfer-length.
4200 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4201 * the presence of a message-body in a RESPONSE and its transfer length
4202 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004203 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004204 * All responses to the HEAD request method MUST NOT include a
4205 * message-body, even though the presence of entity-header fields
4206 * might lead one to believe they do. All 1xx (informational), 204
4207 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4208 * message-body. All other responses do include a message-body,
4209 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004210 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004211 * 1. Any response which "MUST NOT" include a message-body (such as the
4212 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4213 * always terminated by the first empty line after the header fields,
4214 * regardless of the entity-header fields present in the message.
4215 *
4216 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4217 * the "chunked" transfer-coding (Section 6.2) is used, the
4218 * transfer-length is defined by the use of this transfer-coding.
4219 * If a Transfer-Encoding header field is present and the "chunked"
4220 * transfer-coding is not present, the transfer-length is defined by
4221 * the sender closing the connection.
4222 *
4223 * 3. If a Content-Length header field is present, its decimal value in
4224 * OCTETs represents both the entity-length and the transfer-length.
4225 * If a message is received with both a Transfer-Encoding header
4226 * field and a Content-Length header field, the latter MUST be ignored.
4227 *
4228 * 4. If the message uses the media type "multipart/byteranges", and
4229 * the transfer-length is not otherwise specified, then this self-
4230 * delimiting media type defines the transfer-length. This media
4231 * type MUST NOT be used unless the sender knows that the recipient
4232 * can parse it; the presence in a request of a Range header with
4233 * multiple byte-range specifiers from a 1.1 client implies that the
4234 * client can parse multipart/byteranges responses.
4235 *
4236 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004237 */
4238
4239 /* Skip parsing if no content length is possible. The response flags
4240 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004241 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004242 * FIXME: should we parse anyway and return an error on chunked encoding ?
4243 */
4244 if (txn->meth == HTTP_METH_HEAD ||
4245 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004246 txn->status == 204 || txn->status == 304) {
4247 txn->flags |= TX_RES_XFER_LEN;
4248 goto skip_content_length;
4249 }
4250
4251 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004252 goto skip_content_length;
4253
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004254 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004255 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004256 while ((txn->flags & TX_RES_VER_11) &&
4257 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004258 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4259 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4260 else if (txn->flags & TX_RES_TE_CHNK) {
4261 /* bad transfer-encoding (chunked followed by something else) */
4262 use_close_only = 1;
4263 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4264 break;
4265 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004266 }
4267
4268 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4269 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004270 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004271 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4272 signed long long cl;
4273
4274 if (!ctx.vlen)
4275 goto hdr_response_bad;
4276
4277 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4278 goto hdr_response_bad; /* parse failure */
4279
4280 if (cl < 0)
4281 goto hdr_response_bad;
4282
4283 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4284 goto hdr_response_bad; /* already specified, was different */
4285
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004286 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004287 msg->hdr_content_len = cl;
4288 }
4289
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004290 /* FIXME: we should also implement the multipart/byterange method.
4291 * For now on, we resort to close mode in this case (unknown length).
4292 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004293skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004294
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004295 /* end of job, return OK */
4296 rep->analysers &= ~an_bit;
4297 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004298 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004299 return 1;
4300}
4301
4302/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004303 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4304 * and updates t->rep->analysers. It might make sense to explode it into several
4305 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004306 */
4307int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4308{
4309 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004310 struct http_msg *msg = &txn->rsp;
4311 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004312 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004313 int conn_ka = 0, conn_cl = 0;
4314 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004315 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004316
4317 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4318 now_ms, __FUNCTION__,
4319 t,
4320 rep,
4321 rep->rex, rep->wex,
4322 rep->flags,
4323 rep->l,
4324 rep->analysers);
4325
Willy Tarreau655dce92009-11-08 13:10:58 +01004326 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004327 return 0;
4328
4329 rep->analysers &= ~an_bit;
4330 rep->analyse_exp = TICK_ETERNITY;
4331
Willy Tarreau5b154472009-12-21 20:11:07 +01004332 /* Now we have to check if we need to modify the Connection header.
4333 * This is more difficult on the response than it is on the request,
4334 * because we can have two different HTTP versions and we don't know
4335 * how the client will interprete a response. For instance, let's say
4336 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4337 * HTTP/1.1 response without any header. Maybe it will bound itself to
4338 * HTTP/1.0 because it only knows about it, and will consider the lack
4339 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4340 * the lack of header as a keep-alive. Thus we will use two flags
4341 * indicating how a request MAY be understood by the client. In case
4342 * of multiple possibilities, we'll fix the header to be explicit. If
4343 * ambiguous cases such as both close and keepalive are seen, then we
4344 * will fall back to explicit close. Note that we won't take risks with
4345 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4346 */
4347
4348 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004349 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4350 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4351 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004352 int may_keep = 0, may_close = 0; /* how it may be understood */
4353 struct hdr_ctx ctx;
4354
4355 ctx.idx = 0;
4356 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4357 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4358 conn_cl = 1;
4359 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4360 conn_ka = 1;
4361 }
4362
4363 if (conn_cl) {
4364 /* close header present */
4365 may_close = 1;
4366 if (conn_ka) /* we have both close and keep-alive */
4367 may_keep = 1;
4368 }
4369 else if (conn_ka) {
4370 /* keep-alive alone */
4371 may_keep = 1;
4372 }
4373 else {
4374 /* no close nor keep-alive header */
4375 if (txn->flags & TX_RES_VER_11)
4376 may_keep = 1;
4377 else
4378 may_close = 1;
4379
4380 if (txn->flags & TX_REQ_VER_11)
4381 may_keep = 1;
4382 else
4383 may_close = 1;
4384 }
4385
4386 /* let's update the transaction status to reflect any close.
4387 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004388 * handled. We also explicitly state that we will close in
4389 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004390 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004391 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004392 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4393 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004394 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4395
4396 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004397 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004398 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4399 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004400 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004401 if (may_keep &&
4402 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4403 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004404 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004405 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4406 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4407 must_del_close = 1;
4408 if (!(txn->flags & TX_REQ_VER_11))
4409 must_keep = 1;
4410 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004411
4412 txn->flags |= TX_CON_HDR_PARS;
4413 }
4414
4415 /* We might have to check for "Connection:" if the server
4416 * returns a connection status that is not compatible with
4417 * the client's or with the config.
4418 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004419 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004420 char *cur_ptr, *cur_end, *cur_next;
4421 int cur_idx, old_idx, delta, val;
4422 int must_delete;
4423 struct hdr_idx_elem *cur_hdr;
4424
4425 /* we just have to remove the headers if both sides are 1.0 */
4426 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004427
4428 /* same if we want to re-enable keep-alive on 1.1 */
4429 must_delete |= must_del_close;
4430
Willy Tarreau5b154472009-12-21 20:11:07 +01004431 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4432
4433 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4434 cur_hdr = &txn->hdr_idx.v[cur_idx];
4435 cur_ptr = cur_next;
4436 cur_end = cur_ptr + cur_hdr->len;
4437 cur_next = cur_end + cur_hdr->cr + 1;
4438
4439 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4440 if (!val)
4441 continue;
4442
4443 /* 3 possibilities :
4444 * - we have already set "Connection: close" or we're in
4445 * HTTP/1.0, so we remove this line.
4446 * - we have not yet set "Connection: close", but this line
4447 * indicates close. We leave it untouched and set the flag.
4448 * - we have not yet set "Connection: close", and this line
4449 * indicates non-close. We replace it and set the flag.
4450 */
4451 if (must_delete) {
4452 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4453 http_msg_move_end(&txn->rsp, delta);
4454 cur_next += delta;
4455 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4456 txn->hdr_idx.used--;
4457 cur_hdr->len = 0;
4458 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004459 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004460 } else {
4461 if (cur_end - cur_ptr - val != 5 ||
4462 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4463 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4464 "close", 5);
4465 cur_next += delta;
4466 cur_hdr->len += delta;
4467 http_msg_move_end(&txn->rsp, delta);
4468 }
4469 must_delete = 1;
4470 must_close = 0;
4471 }
4472 } /* for loop */
4473 } /* if must close keep-alive */
4474
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004475 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004476 /*
4477 * 3: we will have to evaluate the filters.
4478 * As opposed to version 1.2, now they will be evaluated in the
4479 * filters order and not in the header order. This means that
4480 * each filter has to be validated among all headers.
4481 *
4482 * Filters are tried with ->be first, then with ->fe if it is
4483 * different from ->be.
4484 */
4485
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004486 cur_proxy = t->be;
4487 while (1) {
4488 struct proxy *rule_set = cur_proxy;
4489
4490 /* try headers filters */
4491 if (rule_set->rsp_exp != NULL) {
4492 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4493 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004494 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004495 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004496 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4497 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004498 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004499 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004500 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004501 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004502 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004503 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004504 if (!(t->flags & SN_ERR_MASK))
4505 t->flags |= SN_ERR_PRXCOND;
4506 if (!(t->flags & SN_FINST_MASK))
4507 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004508 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004509 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004510 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004511
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004512 /* has the response been denied ? */
4513 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004514 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004515 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004516
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004517 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004518 if (t->listener->counters)
4519 t->listener->counters->denied_resp++;
4520
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004521 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004522 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004523
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004524 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004525 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004526 if (txn->status < 200)
4527 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004528 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004529 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004530 }
4531
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004532 /* check whether we're already working on the frontend */
4533 if (cur_proxy == t->fe)
4534 break;
4535 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004536 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004537
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004538 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004539 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4540 * in which case this is not the right response, and we're waiting for the
4541 * next one. Let's allow this response to go to the client and wait for the
4542 * next one.
4543 */
4544 if (txn->status < 200) {
4545 hdr_idx_init(&txn->hdr_idx);
4546 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4547 msg->msg_state = HTTP_MSG_RPBEFORE;
4548 txn->status = 0;
4549 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4550 return 1;
4551 }
4552
4553 /* we don't have any 1xx status code now */
4554
4555 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004556 * 4: check for server cookie.
4557 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004558 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4559 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004560 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004561
Willy Tarreaubaaee002006-06-26 02:48:02 +02004562
Willy Tarreaua15645d2007-03-18 16:22:39 +01004563 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004564 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004565 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004566 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004567 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004568
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004569 /*
4570 * 6: add server cookie in the response if needed
4571 */
4572 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004573 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004574 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004575
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576 /* the server is known, it's not the one the client requested, we have to
4577 * insert a set-cookie here, except if we want to insert only on POST
4578 * requests and this one isn't. Note that servers which don't have cookies
4579 * (eg: some backup servers) will return a full cookie removal request.
4580 */
4581 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4582 t->be->cookie_name,
4583 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004584
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585 if (t->be->cookie_domain)
4586 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004587
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004588 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004589 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004590 goto return_bad_resp;
4591 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004592
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004593 /* Here, we will tell an eventual cache on the client side that we don't
4594 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4595 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4596 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4597 */
4598 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004599
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004600 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4601
4602 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004603 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004604 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004605 }
4606 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004607
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004608 /*
4609 * 7: check if result will be cacheable with a cookie.
4610 * We'll block the response if security checks have caught
4611 * nasty things such as a cacheable cookie.
4612 */
4613 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4614 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004615 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004616
4617 /* we're in presence of a cacheable response containing
4618 * a set-cookie header. We'll block it as requested by
4619 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004620 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004621 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004622 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004623
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004624 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004625 if (t->listener->counters)
4626 t->listener->counters->denied_resp++;
4627
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004628 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4629 t->be->id, t->srv?t->srv->id:"<dispatch>");
4630 send_log(t->be, LOG_ALERT,
4631 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4632 t->be->id, t->srv?t->srv->id:"<dispatch>");
4633 goto return_srv_prx_502;
4634 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004635
4636 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004637 * 8: add "Connection: close" if needed and not yet set. This is
4638 * only needed for 1.1 responses since we know there is no other
4639 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004640 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004641 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004642 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004643 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004644 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004645 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004646 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004647 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4648 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4649 "Connection: keep-alive", 22) < 0))
4650 goto return_bad_resp;
4651 must_keep = 0;
4652 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004653
Willy Tarreaud98cf932009-12-27 22:54:55 +01004654 if (txn->flags & TX_RES_XFER_LEN)
4655 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004656
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004657 /*************************************************************
4658 * OK, that's finished for the headers. We have done what we *
4659 * could. Let's switch to the DATA state. *
4660 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004661
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004662 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004663
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004664 /* if the user wants to log as soon as possible, without counting
4665 * bytes from the server, then this is the right moment. We have
4666 * to temporarily assign bytes_out to log what we currently have.
4667 */
4668 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4669 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4670 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004671 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004672 t->logs.bytes_out = 0;
4673 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004674
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004675 /* Note: we must not try to cheat by jumping directly to DATA,
4676 * otherwise we would not let the client side wake up.
4677 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004678
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004679 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004680 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004681 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004682}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004683
Willy Tarreaud98cf932009-12-27 22:54:55 +01004684/* This function is an analyser which forwards response body (including chunk
4685 * sizes if any). It is called as soon as we must forward, even if we forward
4686 * zero byte. The only situation where it must not be called is when we're in
4687 * tunnel mode and we want to forward till the close. It's used both to forward
4688 * remaining data and to resync after end of body. It expects the msg_state to
4689 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4690 * read more data, or 1 once we can go on with next request or end the session.
4691 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4692 * bytes of pending data + the headers if not already done (between som and sov).
4693 * It eventually adjusts som to match sov after the data in between have been sent.
4694 */
4695int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4696{
4697 struct http_txn *txn = &s->txn;
4698 struct http_msg *msg = &s->txn.rsp;
4699
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004700 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4701 return 0;
4702
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004703 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004704 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004705 !s->req->analysers) {
4706 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4707 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004708 buffer_auto_read(res);
4709 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004710 res->analysers &= ~an_bit;
4711 return 1;
4712 }
4713
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004714 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004715
Willy Tarreaud98cf932009-12-27 22:54:55 +01004716 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4717 /* we have msg->col and msg->sov which both point to the first
4718 * byte of message body. msg->som still points to the beginning
4719 * of the message. We must save the body in req->lr because it
4720 * survives buffer re-alignments.
4721 */
4722 res->lr = res->data + msg->sov;
4723 if (txn->flags & TX_RES_TE_CHNK)
4724 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4725 else {
4726 msg->msg_state = HTTP_MSG_DATA;
4727 }
4728 }
4729
Willy Tarreaud98cf932009-12-27 22:54:55 +01004730 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004731 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004732 /* we may have some data pending */
4733 if (msg->hdr_content_len || msg->som != msg->sov) {
4734 int bytes = msg->sov - msg->som;
4735 if (bytes < 0) /* sov may have wrapped at the end */
4736 bytes += res->size;
4737 buffer_forward(res, bytes + msg->hdr_content_len);
4738 msg->hdr_content_len = 0; /* don't forward that again */
4739 msg->som = msg->sov;
4740 }
4741
Willy Tarreaucaabe412010-01-03 23:08:28 +01004742 if (msg->msg_state == HTTP_MSG_DATA) {
4743 /* must still forward */
4744 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004745 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004746
4747 /* nothing left to forward */
4748 if (txn->flags & TX_RES_TE_CHNK)
4749 msg->msg_state = HTTP_MSG_DATA_CRLF;
4750 else
4751 msg->msg_state = HTTP_MSG_DONE;
4752 }
4753 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004754 /* read the chunk size and assign it to ->hdr_content_len, then
4755 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4756 */
4757 int ret = http_parse_chunk_size(res, msg);
4758
4759 if (!ret)
4760 goto missing_data;
4761 else if (ret < 0)
4762 goto return_bad_res;
4763 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004765 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4766 /* we want the CRLF after the data */
4767 int ret;
4768
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004769 res->lr = res->w + res->send_max;
4770 if (res->lr >= res->data + res->size)
4771 res->lr -= res->size;
4772
Willy Tarreaud98cf932009-12-27 22:54:55 +01004773 ret = http_skip_chunk_crlf(res, msg);
4774
4775 if (!ret)
4776 goto missing_data;
4777 else if (ret < 0)
4778 goto return_bad_res;
4779 /* we're in MSG_CHUNK_SIZE now */
4780 }
4781 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4782 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004783
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784 if (ret == 0)
4785 goto missing_data;
4786 else if (ret < 0)
4787 goto return_bad_res;
4788 /* we're in HTTP_MSG_DONE now */
4789 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004790 else {
4791 /* other states, DONE...TUNNEL */
4792 if (http_resync_states(s)) {
4793 http_silent_debug(__LINE__, s);
4794 /* some state changes occurred, maybe the analyser
4795 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004796 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004797 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4798 goto return_bad_res;
4799 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004800 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004802 }
4803 }
4804
Willy Tarreaud98cf932009-12-27 22:54:55 +01004805 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004806 /* stop waiting for data if the input is closed before the end */
4807 if (res->flags & BF_SHUTR)
4808 goto return_bad_res;
4809
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810 if (!s->req->analysers)
4811 goto return_bad_res;
4812
Willy Tarreaud98cf932009-12-27 22:54:55 +01004813 /* forward the chunk size as well as any pending data */
4814 if (msg->hdr_content_len || msg->som != msg->sov) {
4815 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4816 msg->hdr_content_len = 0; /* don't forward that again */
4817 msg->som = msg->sov;
4818 }
4819
Willy Tarreaud98cf932009-12-27 22:54:55 +01004820 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822 return 0;
4823
4824 return_bad_res: /* let's centralize all bad resuests */
4825 txn->rsp.msg_state = HTTP_MSG_ERROR;
4826 txn->status = 502;
4827 stream_int_cond_close(res->cons, NULL);
4828
Willy Tarreau90deb182010-01-07 00:20:41 +01004829 buffer_auto_close(res);
4830 buffer_auto_read(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004831 res->analysers = 0;
4832 s->be->counters.failed_resp++;
4833 if (s->srv) {
4834 s->srv->counters.failed_resp++;
4835 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4836 }
4837
4838 if (!(s->flags & SN_ERR_MASK))
4839 s->flags |= SN_ERR_PRXCOND;
4840 if (!(s->flags & SN_FINST_MASK))
4841 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004842 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004843 return 0;
4844}
4845
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004846/* Iterate the same filter through all request headers.
4847 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004848 * Since it can manage the switch to another backend, it updates the per-proxy
4849 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004850 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004851int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004852{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004853 char term;
4854 char *cur_ptr, *cur_end, *cur_next;
4855 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004856 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004857 struct hdr_idx_elem *cur_hdr;
4858 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004859
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004860 last_hdr = 0;
4861
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004862 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004863 old_idx = 0;
4864
4865 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004866 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004867 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004868 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004869 (exp->action == ACT_ALLOW ||
4870 exp->action == ACT_DENY ||
4871 exp->action == ACT_TARPIT))
4872 return 0;
4873
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004874 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004875 if (!cur_idx)
4876 break;
4877
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004878 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004879 cur_ptr = cur_next;
4880 cur_end = cur_ptr + cur_hdr->len;
4881 cur_next = cur_end + cur_hdr->cr + 1;
4882
4883 /* Now we have one header between cur_ptr and cur_end,
4884 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004885 */
4886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004887 /* The annoying part is that pattern matching needs
4888 * that we modify the contents to null-terminate all
4889 * strings before testing them.
4890 */
4891
4892 term = *cur_end;
4893 *cur_end = '\0';
4894
4895 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4896 switch (exp->action) {
4897 case ACT_SETBE:
4898 /* It is not possible to jump a second time.
4899 * FIXME: should we return an HTTP/500 here so that
4900 * the admin knows there's a problem ?
4901 */
4902 if (t->be != t->fe)
4903 break;
4904
4905 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004906 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004907 last_hdr = 1;
4908 break;
4909
4910 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004911 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004912 last_hdr = 1;
4913 break;
4914
4915 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004916 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004917 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004918
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004919 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004920 if (t->listener->counters)
4921 t->listener->counters->denied_resp++;
4922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004923 break;
4924
4925 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004926 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004927 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004928
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004929 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004930 if (t->listener->counters)
4931 t->listener->counters->denied_resp++;
4932
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004933 break;
4934
4935 case ACT_REPLACE:
4936 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4937 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4938 /* FIXME: if the user adds a newline in the replacement, the
4939 * index will not be recalculated for now, and the new line
4940 * will not be counted as a new header.
4941 */
4942
4943 cur_end += delta;
4944 cur_next += delta;
4945 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004946 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004947 break;
4948
4949 case ACT_REMOVE:
4950 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4951 cur_next += delta;
4952
Willy Tarreaufa355d42009-11-29 18:12:29 +01004953 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004954 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4955 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004956 cur_hdr->len = 0;
4957 cur_end = NULL; /* null-term has been rewritten */
4958 break;
4959
4960 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004961 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004962 if (cur_end)
4963 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004964
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004965 /* keep the link from this header to next one in case of later
4966 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004967 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004968 old_idx = cur_idx;
4969 }
4970 return 0;
4971}
4972
4973
4974/* Apply the filter to the request line.
4975 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4976 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004977 * Since it can manage the switch to another backend, it updates the per-proxy
4978 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004979 */
4980int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4981{
4982 char term;
4983 char *cur_ptr, *cur_end;
4984 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004985 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004986 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987
Willy Tarreau58f10d72006-12-04 02:26:12 +01004988
Willy Tarreau3d300592007-03-18 18:34:41 +01004989 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004990 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004991 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004992 (exp->action == ACT_ALLOW ||
4993 exp->action == ACT_DENY ||
4994 exp->action == ACT_TARPIT))
4995 return 0;
4996 else if (exp->action == ACT_REMOVE)
4997 return 0;
4998
4999 done = 0;
5000
Willy Tarreau9cdde232007-05-02 20:58:19 +02005001 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005002 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005003
5004 /* Now we have the request line between cur_ptr and cur_end */
5005
5006 /* The annoying part is that pattern matching needs
5007 * that we modify the contents to null-terminate all
5008 * strings before testing them.
5009 */
5010
5011 term = *cur_end;
5012 *cur_end = '\0';
5013
5014 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5015 switch (exp->action) {
5016 case ACT_SETBE:
5017 /* It is not possible to jump a second time.
5018 * FIXME: should we return an HTTP/500 here so that
5019 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005020 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005021 if (t->be != t->fe)
5022 break;
5023
5024 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005025 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005026 done = 1;
5027 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005028
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005029 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005030 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005031 done = 1;
5032 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005033
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005034 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005035 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005036
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005037 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005038 if (t->listener->counters)
5039 t->listener->counters->denied_resp++;
5040
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005041 done = 1;
5042 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005044 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005045 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005046
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005047 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005048 if (t->listener->counters)
5049 t->listener->counters->denied_resp++;
5050
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005051 done = 1;
5052 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005053
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005054 case ACT_REPLACE:
5055 *cur_end = term; /* restore the string terminator */
5056 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5057 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5058 /* FIXME: if the user adds a newline in the replacement, the
5059 * index will not be recalculated for now, and the new line
5060 * will not be counted as a new header.
5061 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005062
Willy Tarreaufa355d42009-11-29 18:12:29 +01005063 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005064 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005065
Willy Tarreau9cdde232007-05-02 20:58:19 +02005066 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005067 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005068 HTTP_MSG_RQMETH,
5069 cur_ptr, cur_end + 1,
5070 NULL, NULL);
5071 if (unlikely(!cur_end))
5072 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005074 /* we have a full request and we know that we have either a CR
5075 * or an LF at <ptr>.
5076 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005077 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5078 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005079 /* there is no point trying this regex on headers */
5080 return 1;
5081 }
5082 }
5083 *cur_end = term; /* restore the string terminator */
5084 return done;
5085}
Willy Tarreau97de6242006-12-27 17:18:38 +01005086
Willy Tarreau58f10d72006-12-04 02:26:12 +01005087
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005089/*
5090 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5091 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005092 * unparsable request. Since it can manage the switch to another backend, it
5093 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005094 */
5095int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5096{
Willy Tarreau3d300592007-03-18 18:34:41 +01005097 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005098 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005099 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005100 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005102 /*
5103 * The interleaving of transformations and verdicts
5104 * makes it difficult to decide to continue or stop
5105 * the evaluation.
5106 */
5107
Willy Tarreau3d300592007-03-18 18:34:41 +01005108 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005109 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5110 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5111 exp = exp->next;
5112 continue;
5113 }
5114
5115 /* Apply the filter to the request line. */
5116 ret = apply_filter_to_req_line(t, req, exp);
5117 if (unlikely(ret < 0))
5118 return -1;
5119
5120 if (likely(ret == 0)) {
5121 /* The filter did not match the request, it can be
5122 * iterated through all headers.
5123 */
5124 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005125 }
5126 exp = exp->next;
5127 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005128 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005129}
5130
5131
Willy Tarreaua15645d2007-03-18 16:22:39 +01005132
Willy Tarreau58f10d72006-12-04 02:26:12 +01005133/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005134 * Try to retrieve the server associated to the appsession.
5135 * If the server is found, it's assigned to the session.
5136 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005137void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005138 struct http_txn *txn = &t->txn;
5139 appsess *asession = NULL;
5140 char *sessid_temp = NULL;
5141
Cyril Bontéb21570a2009-11-29 20:04:48 +01005142 if (len > t->be->appsession_len) {
5143 len = t->be->appsession_len;
5144 }
5145
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005146 if (t->be->options2 & PR_O2_AS_REQL) {
5147 /* request-learn option is enabled : store the sessid in the session for future use */
5148 if (t->sessid != NULL) {
5149 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5150 pool_free2(apools.sessid, t->sessid);
5151 }
5152
5153 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5154 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5155 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5156 return;
5157 }
5158
Cyril Bontéb21570a2009-11-29 20:04:48 +01005159 memcpy(t->sessid, buf, len);
5160 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005161 }
5162
5163 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5164 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5165 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5166 return;
5167 }
5168
Cyril Bontéb21570a2009-11-29 20:04:48 +01005169 memcpy(sessid_temp, buf, len);
5170 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005171
5172 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5173 /* free previously allocated memory */
5174 pool_free2(apools.sessid, sessid_temp);
5175
5176 if (asession != NULL) {
5177 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5178 if (!(t->be->options2 & PR_O2_AS_REQL))
5179 asession->request_count++;
5180
5181 if (asession->serverid != NULL) {
5182 struct server *srv = t->be->srv;
5183 while (srv) {
5184 if (strcmp(srv->id, asession->serverid) == 0) {
5185 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5186 /* we found the server and it's usable */
5187 txn->flags &= ~TX_CK_MASK;
5188 txn->flags |= TX_CK_VALID;
5189 t->flags |= SN_DIRECT | SN_ASSIGNED;
5190 t->srv = srv;
5191 break;
5192 } else {
5193 txn->flags &= ~TX_CK_MASK;
5194 txn->flags |= TX_CK_DOWN;
5195 }
5196 }
5197 srv = srv->next;
5198 }
5199 }
5200 }
5201}
5202
5203/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005204 * Manage client-side cookie. It can impact performance by about 2% so it is
5205 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005206 */
5207void manage_client_side_cookies(struct session *t, struct buffer *req)
5208{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005209 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005210 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005211 char *del_colon, *del_cookie, *colon;
5212 int app_cookies;
5213
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005215 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005216
Willy Tarreau2a324282006-12-05 00:05:46 +01005217 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005218 * we start with the start line.
5219 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005220 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005221 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005222
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005223 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005224 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005225 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005226
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005227 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005228 cur_ptr = cur_next;
5229 cur_end = cur_ptr + cur_hdr->len;
5230 cur_next = cur_end + cur_hdr->cr + 1;
5231
5232 /* We have one full header between cur_ptr and cur_end, and the
5233 * next header starts at cur_next. We're only interested in
5234 * "Cookie:" headers.
5235 */
5236
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005237 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5238 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005239 old_idx = cur_idx;
5240 continue;
5241 }
5242
5243 /* Now look for cookies. Conforming to RFC2109, we have to support
5244 * attributes whose name begin with a '$', and associate them with
5245 * the right cookie, if we want to delete this cookie.
5246 * So there are 3 cases for each cookie read :
5247 * 1) it's a special attribute, beginning with a '$' : ignore it.
5248 * 2) it's a server id cookie that we *MAY* want to delete : save
5249 * some pointers on it (last semi-colon, beginning of cookie...)
5250 * 3) it's an application cookie : we *MAY* have to delete a previous
5251 * "special" cookie.
5252 * At the end of loop, if a "special" cookie remains, we may have to
5253 * remove it. If no application cookie persists in the header, we
5254 * *MUST* delete it
5255 */
5256
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005257 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258
Willy Tarreau58f10d72006-12-04 02:26:12 +01005259 /* del_cookie == NULL => nothing to be deleted */
5260 del_colon = del_cookie = NULL;
5261 app_cookies = 0;
5262
5263 while (p1 < cur_end) {
5264 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005265 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005266 while (p1 < cur_end) {
5267 if (*p1 == ';' || *p1 == ',')
5268 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005269 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005270 break;
5271 p1++;
5272 }
5273
5274 if (p1 == cur_end)
5275 break;
5276
5277 /* p1 is at the beginning of the cookie name */
5278 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005279 while (p2 < cur_end && *p2 != '=') {
5280 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5281 /* oops, the cookie name was truncated, resync */
5282 p1 = p2;
5283 goto resync_name;
5284 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005285 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005286 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005287
5288 if (p2 == cur_end)
5289 break;
5290
5291 p3 = p2 + 1; /* skips the '=' sign */
5292 if (p3 == cur_end)
5293 break;
5294
Willy Tarreau305ae852010-01-03 19:45:54 +01005295 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5296 p5 = p4 = p3;
5297 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5298 if (!isspace((unsigned char)*p5))
5299 p4 = p5 + 1;
5300 p5++;
5301 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005302
5303 /* here, we have the cookie name between p1 and p2,
5304 * and its value between p3 and p4.
5305 * we can process it :
5306 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005307 * Cookie: NAME=VALUE ;
5308 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005309 * | || || +--> p4
5310 * | || |+-------> p3
5311 * | || +--------> p2
5312 * | |+------------> p1
5313 * | +-------------> colon
5314 * +--------------------> cur_ptr
5315 */
5316
5317 if (*p1 == '$') {
5318 /* skip this one */
5319 }
5320 else {
5321 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005322 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005323 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005324 (p4 - p1 >= t->fe->capture_namelen) &&
5325 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005326 int log_len = p4 - p1;
5327
Willy Tarreau086b3b42007-05-13 21:45:51 +02005328 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005329 Alert("HTTP logging : out of memory.\n");
5330 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005331 if (log_len > t->fe->capture_len)
5332 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005333 memcpy(txn->cli_cookie, p1, log_len);
5334 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005335 }
5336 }
5337
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005338 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5339 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005340 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005341 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005342 char *delim;
5343
5344 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5345 * have the server ID betweek p3 and delim, and the original cookie between
5346 * delim+1 and p4. Otherwise, delim==p4 :
5347 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005348 * Cookie: NAME=SRV~VALUE ;
5349 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005350 * | || || | +--> p4
5351 * | || || +--------> delim
5352 * | || |+-----------> p3
5353 * | || +------------> p2
5354 * | |+----------------> p1
5355 * | +-----------------> colon
5356 * +------------------------> cur_ptr
5357 */
5358
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005359 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005360 for (delim = p3; delim < p4; delim++)
5361 if (*delim == COOKIE_DELIM)
5362 break;
5363 }
5364 else
5365 delim = p4;
5366
5367
5368 /* Here, we'll look for the first running server which supports the cookie.
5369 * This allows to share a same cookie between several servers, for example
5370 * to dedicate backup servers to specific servers only.
5371 * However, to prevent clients from sticking to cookie-less backup server
5372 * when they have incidentely learned an empty cookie, we simply ignore
5373 * empty cookies and mark them as invalid.
5374 */
5375 if (delim == p3)
5376 srv = NULL;
5377
5378 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005379 if (srv->cookie && (srv->cklen == delim - p3) &&
5380 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005381 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005382 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005383 txn->flags &= ~TX_CK_MASK;
5384 txn->flags |= TX_CK_VALID;
5385 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005386 t->srv = srv;
5387 break;
5388 } else {
5389 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005390 txn->flags &= ~TX_CK_MASK;
5391 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005392 }
5393 }
5394 srv = srv->next;
5395 }
5396
Willy Tarreau3d300592007-03-18 18:34:41 +01005397 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005398 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005399 txn->flags &= ~TX_CK_MASK;
5400 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005401 }
5402
5403 /* depending on the cookie mode, we may have to either :
5404 * - delete the complete cookie if we're in insert+indirect mode, so that
5405 * the server never sees it ;
5406 * - remove the server id from the cookie value, and tag the cookie as an
5407 * application cookie so that it does not get accidentely removed later,
5408 * if we're in cookie prefix mode
5409 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005410 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005411 int delta; /* negative */
5412
5413 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5414 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005415 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005416 cur_end += delta;
5417 cur_next += delta;
5418 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005419 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005420
5421 del_cookie = del_colon = NULL;
5422 app_cookies++; /* protect the header from deletion */
5423 }
5424 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005425 (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 +01005426 del_cookie = p1;
5427 del_colon = colon;
5428 }
5429 } else {
5430 /* now we know that we must keep this cookie since it's
5431 * not ours. But if we wanted to delete our cookie
5432 * earlier, we cannot remove the complete header, but we
5433 * can remove the previous block itself.
5434 */
5435 app_cookies++;
5436
5437 if (del_cookie != NULL) {
5438 int delta; /* negative */
5439
5440 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5441 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005442 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005443 cur_end += delta;
5444 cur_next += delta;
5445 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005446 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005447 del_cookie = del_colon = NULL;
5448 }
5449 }
5450
Cyril Bontéb21570a2009-11-29 20:04:48 +01005451 if (t->be->appsession_name != NULL) {
5452 int cmp_len, value_len;
5453 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005454
Cyril Bontéb21570a2009-11-29 20:04:48 +01005455 if (t->be->options2 & PR_O2_AS_PFX) {
5456 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5457 value_begin = p1 + t->be->appsession_name_len;
5458 value_len = p4 - p1 - t->be->appsession_name_len;
5459 } else {
5460 cmp_len = p2 - p1;
5461 value_begin = p3;
5462 value_len = p4 - p3;
5463 }
5464
5465 /* let's see if the cookie is our appcookie */
5466 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5467 /* Cool... it's the right one */
5468 manage_client_side_appsession(t, value_begin, value_len);
5469 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005470#if defined(DEBUG_HASH)
5471 Alert("manage_client_side_cookies\n");
5472 appsession_hash_dump(&(t->be->htbl_proxy));
5473#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005474 }/* end if ((t->proxy->appsession_name != NULL) ... */
5475 }
5476
5477 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005478 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005479 } /* while (p1 < cur_end) */
5480
5481 /* There's no more cookie on this line.
5482 * We may have marked the last one(s) for deletion.
5483 * We must do this now in two ways :
5484 * - if there is no app cookie, we simply delete the header ;
5485 * - if there are app cookies, we must delete the end of the
5486 * string properly, including the colon/semi-colon before
5487 * the cookie name.
5488 */
5489 if (del_cookie != NULL) {
5490 int delta;
5491 if (app_cookies) {
5492 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5493 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005494 cur_hdr->len += delta;
5495 } else {
5496 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005497
5498 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005499 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5500 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005501 cur_hdr->len = 0;
5502 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005503 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005504 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005505 }
5506
5507 /* keep the link from this header to next one */
5508 old_idx = cur_idx;
5509 } /* end of cookie processing on this header */
5510}
5511
5512
Willy Tarreaua15645d2007-03-18 16:22:39 +01005513/* Iterate the same filter through all response headers contained in <rtr>.
5514 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5515 */
5516int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5517{
5518 char term;
5519 char *cur_ptr, *cur_end, *cur_next;
5520 int cur_idx, old_idx, last_hdr;
5521 struct http_txn *txn = &t->txn;
5522 struct hdr_idx_elem *cur_hdr;
5523 int len, delta;
5524
5525 last_hdr = 0;
5526
5527 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5528 old_idx = 0;
5529
5530 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005531 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005532 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005533 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005534 (exp->action == ACT_ALLOW ||
5535 exp->action == ACT_DENY))
5536 return 0;
5537
5538 cur_idx = txn->hdr_idx.v[old_idx].next;
5539 if (!cur_idx)
5540 break;
5541
5542 cur_hdr = &txn->hdr_idx.v[cur_idx];
5543 cur_ptr = cur_next;
5544 cur_end = cur_ptr + cur_hdr->len;
5545 cur_next = cur_end + cur_hdr->cr + 1;
5546
5547 /* Now we have one header between cur_ptr and cur_end,
5548 * and the next header starts at cur_next.
5549 */
5550
5551 /* The annoying part is that pattern matching needs
5552 * that we modify the contents to null-terminate all
5553 * strings before testing them.
5554 */
5555
5556 term = *cur_end;
5557 *cur_end = '\0';
5558
5559 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5560 switch (exp->action) {
5561 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005562 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005563 last_hdr = 1;
5564 break;
5565
5566 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005567 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005568 last_hdr = 1;
5569 break;
5570
5571 case ACT_REPLACE:
5572 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5573 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5574 /* FIXME: if the user adds a newline in the replacement, the
5575 * index will not be recalculated for now, and the new line
5576 * will not be counted as a new header.
5577 */
5578
5579 cur_end += delta;
5580 cur_next += delta;
5581 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005582 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005583 break;
5584
5585 case ACT_REMOVE:
5586 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5587 cur_next += delta;
5588
Willy Tarreaufa355d42009-11-29 18:12:29 +01005589 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005590 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5591 txn->hdr_idx.used--;
5592 cur_hdr->len = 0;
5593 cur_end = NULL; /* null-term has been rewritten */
5594 break;
5595
5596 }
5597 }
5598 if (cur_end)
5599 *cur_end = term; /* restore the string terminator */
5600
5601 /* keep the link from this header to next one in case of later
5602 * removal of next header.
5603 */
5604 old_idx = cur_idx;
5605 }
5606 return 0;
5607}
5608
5609
5610/* Apply the filter to the status line in the response buffer <rtr>.
5611 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5612 * or -1 if a replacement resulted in an invalid status line.
5613 */
5614int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5615{
5616 char term;
5617 char *cur_ptr, *cur_end;
5618 int done;
5619 struct http_txn *txn = &t->txn;
5620 int len, delta;
5621
5622
Willy Tarreau3d300592007-03-18 18:34:41 +01005623 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005624 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005625 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005626 (exp->action == ACT_ALLOW ||
5627 exp->action == ACT_DENY))
5628 return 0;
5629 else if (exp->action == ACT_REMOVE)
5630 return 0;
5631
5632 done = 0;
5633
Willy Tarreau9cdde232007-05-02 20:58:19 +02005634 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005635 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5636
5637 /* Now we have the status line between cur_ptr and cur_end */
5638
5639 /* The annoying part is that pattern matching needs
5640 * that we modify the contents to null-terminate all
5641 * strings before testing them.
5642 */
5643
5644 term = *cur_end;
5645 *cur_end = '\0';
5646
5647 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5648 switch (exp->action) {
5649 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005650 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005651 done = 1;
5652 break;
5653
5654 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005655 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005656 done = 1;
5657 break;
5658
5659 case ACT_REPLACE:
5660 *cur_end = term; /* restore the string terminator */
5661 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5662 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5663 /* FIXME: if the user adds a newline in the replacement, the
5664 * index will not be recalculated for now, and the new line
5665 * will not be counted as a new header.
5666 */
5667
Willy Tarreaufa355d42009-11-29 18:12:29 +01005668 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005669 cur_end += delta;
5670
Willy Tarreau9cdde232007-05-02 20:58:19 +02005671 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005672 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005673 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005674 cur_ptr, cur_end + 1,
5675 NULL, NULL);
5676 if (unlikely(!cur_end))
5677 return -1;
5678
5679 /* we have a full respnse and we know that we have either a CR
5680 * or an LF at <ptr>.
5681 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005682 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005683 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5684 /* there is no point trying this regex on headers */
5685 return 1;
5686 }
5687 }
5688 *cur_end = term; /* restore the string terminator */
5689 return done;
5690}
5691
5692
5693
5694/*
5695 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5696 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5697 * unparsable response.
5698 */
5699int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5700{
Willy Tarreau3d300592007-03-18 18:34:41 +01005701 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005702 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005703 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005704 int ret;
5705
5706 /*
5707 * The interleaving of transformations and verdicts
5708 * makes it difficult to decide to continue or stop
5709 * the evaluation.
5710 */
5711
Willy Tarreau3d300592007-03-18 18:34:41 +01005712 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005713 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5714 exp->action == ACT_PASS)) {
5715 exp = exp->next;
5716 continue;
5717 }
5718
5719 /* Apply the filter to the status line. */
5720 ret = apply_filter_to_sts_line(t, rtr, exp);
5721 if (unlikely(ret < 0))
5722 return -1;
5723
5724 if (likely(ret == 0)) {
5725 /* The filter did not match the response, it can be
5726 * iterated through all headers.
5727 */
5728 apply_filter_to_resp_headers(t, rtr, exp);
5729 }
5730 exp = exp->next;
5731 }
5732 return 0;
5733}
5734
5735
5736
5737/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005738 * Manage server-side cookies. It can impact performance by about 2% so it is
5739 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005740 */
5741void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5742{
5743 struct http_txn *txn = &t->txn;
5744 char *p1, *p2, *p3, *p4;
5745
Willy Tarreaua15645d2007-03-18 16:22:39 +01005746 char *cur_ptr, *cur_end, *cur_next;
5747 int cur_idx, old_idx, delta;
5748
Willy Tarreaua15645d2007-03-18 16:22:39 +01005749 /* Iterate through the headers.
5750 * we start with the start line.
5751 */
5752 old_idx = 0;
5753 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5754
5755 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5756 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005757 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005758
5759 cur_hdr = &txn->hdr_idx.v[cur_idx];
5760 cur_ptr = cur_next;
5761 cur_end = cur_ptr + cur_hdr->len;
5762 cur_next = cur_end + cur_hdr->cr + 1;
5763
5764 /* We have one full header between cur_ptr and cur_end, and the
5765 * next header starts at cur_next. We're only interested in
5766 * "Cookie:" headers.
5767 */
5768
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005769 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5770 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005771 old_idx = cur_idx;
5772 continue;
5773 }
5774
5775 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005776 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005777
5778
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005779 /* maybe we only wanted to see if there was a set-cookie. Note that
5780 * the cookie capture is declared in the fronend.
5781 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005782 if (t->be->cookie_name == NULL &&
5783 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005784 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005785 return;
5786
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005787 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005788
5789 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005790 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5791 break;
5792
5793 /* p1 is at the beginning of the cookie name */
5794 p2 = p1;
5795
5796 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5797 p2++;
5798
5799 if (p2 == cur_end || *p2 == ';') /* next cookie */
5800 break;
5801
5802 p3 = p2 + 1; /* skip the '=' sign */
5803 if (p3 == cur_end)
5804 break;
5805
5806 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005807 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005808 p4++;
5809
5810 /* here, we have the cookie name between p1 and p2,
5811 * and its value between p3 and p4.
5812 * we can process it.
5813 */
5814
5815 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005816 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005817 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005818 (p4 - p1 >= t->fe->capture_namelen) &&
5819 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005820 int log_len = p4 - p1;
5821
Willy Tarreau086b3b42007-05-13 21:45:51 +02005822 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005823 Alert("HTTP logging : out of memory.\n");
5824 }
5825
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005826 if (log_len > t->fe->capture_len)
5827 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005828 memcpy(txn->srv_cookie, p1, log_len);
5829 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005830 }
5831
5832 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005833 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5834 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005835 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005836 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005837
5838 /* If the cookie is in insert mode on a known server, we'll delete
5839 * this occurrence because we'll insert another one later.
5840 * We'll delete it too if the "indirect" option is set and we're in
5841 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005842 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5843 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005844 /* this header must be deleted */
5845 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5846 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5847 txn->hdr_idx.used--;
5848 cur_hdr->len = 0;
5849 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005850 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005851
Willy Tarreau3d300592007-03-18 18:34:41 +01005852 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005853 }
5854 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005855 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005856 /* replace bytes p3->p4 with the cookie name associated
5857 * with this server since we know it.
5858 */
5859 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5860 cur_hdr->len += delta;
5861 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005862 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005863
Willy Tarreau3d300592007-03-18 18:34:41 +01005864 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005865 }
5866 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005867 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868 /* insert the cookie name associated with this server
5869 * before existing cookie, and insert a delimitor between them..
5870 */
5871 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5872 cur_hdr->len += delta;
5873 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005874 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005875
5876 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005877 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005878 }
5879 }
5880 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005881 else if (t->be->appsession_name != NULL) {
5882 int cmp_len, value_len;
5883 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005884
Cyril Bontéb21570a2009-11-29 20:04:48 +01005885 if (t->be->options2 & PR_O2_AS_PFX) {
5886 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5887 value_begin = p1 + t->be->appsession_name_len;
5888 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5889 } else {
5890 cmp_len = p2 - p1;
5891 value_begin = p3;
5892 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005893 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005894
5895 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5896 /* Cool... it's the right one */
5897 if (t->sessid != NULL) {
5898 /* free previously allocated memory as we don't need it anymore */
5899 pool_free2(apools.sessid, t->sessid);
5900 }
5901 /* Store the sessid in the session for future use */
5902 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5903 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5904 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5905 return;
5906 }
5907 memcpy(t->sessid, value_begin, value_len);
5908 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005909 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005910 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005911 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5912 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005913 /* keep the link from this header to next one */
5914 old_idx = cur_idx;
5915 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005916
5917 if (t->sessid != NULL) {
5918 appsess *asession = NULL;
5919 /* only do insert, if lookup fails */
5920 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5921 if (asession == NULL) {
5922 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5923 Alert("Not enough Memory process_srv():asession:calloc().\n");
5924 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5925 return;
5926 }
5927 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5928 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5929 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5930 return;
5931 }
5932 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5933 asession->sessid[t->be->appsession_len] = 0;
5934
5935 size_t server_id_len = strlen(t->srv->id) + 1;
5936 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5937 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5938 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5939 return;
5940 }
5941 asession->serverid[0] = '\0';
5942 memcpy(asession->serverid, t->srv->id, server_id_len);
5943
5944 asession->request_count = 0;
5945 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5946 }
5947
5948 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5949 asession->request_count++;
5950 }
5951
5952#if defined(DEBUG_HASH)
5953 Alert("manage_server_side_cookies\n");
5954 appsession_hash_dump(&(t->be->htbl_proxy));
5955#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005956}
5957
5958
5959
5960/*
5961 * Check if response is cacheable or not. Updates t->flags.
5962 */
5963void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5964{
5965 struct http_txn *txn = &t->txn;
5966 char *p1, *p2;
5967
5968 char *cur_ptr, *cur_end, *cur_next;
5969 int cur_idx;
5970
Willy Tarreau5df51872007-11-25 16:20:08 +01005971 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005972 return;
5973
5974 /* Iterate through the headers.
5975 * we start with the start line.
5976 */
5977 cur_idx = 0;
5978 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5979
5980 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5981 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005982 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005983
5984 cur_hdr = &txn->hdr_idx.v[cur_idx];
5985 cur_ptr = cur_next;
5986 cur_end = cur_ptr + cur_hdr->len;
5987 cur_next = cur_end + cur_hdr->cr + 1;
5988
5989 /* We have one full header between cur_ptr and cur_end, and the
5990 * next header starts at cur_next. We're only interested in
5991 * "Cookie:" headers.
5992 */
5993
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005994 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5995 if (val) {
5996 if ((cur_end - (cur_ptr + val) >= 8) &&
5997 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5998 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5999 return;
6000 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006001 }
6002
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006003 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6004 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006005 continue;
6006
6007 /* OK, right now we know we have a cache-control header at cur_ptr */
6008
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006009 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006010
6011 if (p1 >= cur_end) /* no more info */
6012 continue;
6013
6014 /* p1 is at the beginning of the value */
6015 p2 = p1;
6016
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006017 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006018 p2++;
6019
6020 /* we have a complete value between p1 and p2 */
6021 if (p2 < cur_end && *p2 == '=') {
6022 /* we have something of the form no-cache="set-cookie" */
6023 if ((cur_end - p1 >= 21) &&
6024 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6025 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006026 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006027 continue;
6028 }
6029
6030 /* OK, so we know that either p2 points to the end of string or to a comma */
6031 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6032 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6033 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6034 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006035 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006036 return;
6037 }
6038
6039 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006040 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006041 continue;
6042 }
6043 }
6044}
6045
6046
Willy Tarreau58f10d72006-12-04 02:26:12 +01006047/*
6048 * Try to retrieve a known appsession in the URI, then the associated server.
6049 * If the server is found, it's assigned to the session.
6050 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006051void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006052{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006053 char *end_params, *first_param, *cur_param, *next_param;
6054 char separator;
6055 int value_len;
6056
6057 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006058
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006059 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006060 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006061 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006062 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063
Cyril Bontéb21570a2009-11-29 20:04:48 +01006064 first_param = NULL;
6065 switch (mode) {
6066 case PR_O2_AS_M_PP:
6067 first_param = memchr(begin, ';', len);
6068 break;
6069 case PR_O2_AS_M_QS:
6070 first_param = memchr(begin, '?', len);
6071 break;
6072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006073
Cyril Bontéb21570a2009-11-29 20:04:48 +01006074 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006075 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006077
Cyril Bontéb21570a2009-11-29 20:04:48 +01006078 switch (mode) {
6079 case PR_O2_AS_M_PP:
6080 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6081 end_params = (char *) begin + len;
6082 }
6083 separator = ';';
6084 break;
6085 case PR_O2_AS_M_QS:
6086 end_params = (char *) begin + len;
6087 separator = '&';
6088 break;
6089 default:
6090 /* unknown mode, shouldn't happen */
6091 return;
6092 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006093
Cyril Bontéb21570a2009-11-29 20:04:48 +01006094 cur_param = next_param = end_params;
6095 while (cur_param > first_param) {
6096 cur_param--;
6097 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6098 /* let's see if this is the appsession parameter */
6099 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6100 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6101 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6102 /* Cool... it's the right one */
6103 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6104 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6105 if (value_len > 0) {
6106 manage_client_side_appsession(t, cur_param, value_len);
6107 }
6108 break;
6109 }
6110 next_param = cur_param;
6111 }
6112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006114 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006115 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006116#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006117}
6118
6119
Willy Tarreaub2513902006-12-17 14:52:38 +01006120/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006121 * In a GET or HEAD request, check if the requested URI matches the stats uri
6122 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006123 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006124 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006125 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006126 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006127 *
6128 * Returns 1 if the session's state changes, otherwise 0.
6129 */
6130int stats_check_uri_auth(struct session *t, struct proxy *backend)
6131{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006132 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006133 struct uri_auth *uri_auth = backend->uri_auth;
6134 struct user_auth *user;
6135 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006136 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006137
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006138 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006140 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006141 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006142 return 0;
6143
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006144 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006145
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006146 /* the URI is in h */
6147 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006148 return 0;
6149
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006150 h += uri_auth->uri_len;
6151 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6152 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006153 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006154 break;
6155 }
6156 h++;
6157 }
6158
6159 if (uri_auth->refresh) {
6160 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6161 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6162 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006163 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006164 break;
6165 }
6166 h++;
6167 }
6168 }
6169
Willy Tarreau55bb8452007-10-17 18:44:57 +02006170 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6171 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6172 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006173 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006174 break;
6175 }
6176 h++;
6177 }
6178
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006179 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6180
Willy Tarreaub2513902006-12-17 14:52:38 +01006181 /* we are in front of a interceptable URI. Let's check
6182 * if there's an authentication and if it's valid.
6183 */
6184 user = uri_auth->users;
6185 if (!user) {
6186 /* no user auth required, it's OK */
6187 authenticated = 1;
6188 } else {
6189 authenticated = 0;
6190
6191 /* a user list is defined, we have to check.
6192 * skip 21 chars for "Authorization: Basic ".
6193 */
6194
6195 /* FIXME: this should move to an earlier place */
6196 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006197 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6198 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6199 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006200 if (len > 14 &&
6201 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006202 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006203 break;
6204 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006205 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006206 }
6207
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006208 if (txn->auth_hdr.len < 21 ||
6209 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006210 user = NULL;
6211
6212 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006213 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6214 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006215 user->user_pwd, user->user_len)) {
6216 authenticated = 1;
6217 break;
6218 }
6219 user = user->next;
6220 }
6221 }
6222
6223 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006224 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006225
6226 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006227 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6228 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006229 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006230 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006231 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006232 if (!(t->flags & SN_ERR_MASK))
6233 t->flags |= SN_ERR_PRXCOND;
6234 if (!(t->flags & SN_FINST_MASK))
6235 t->flags |= SN_FINST_R;
6236 return 1;
6237 }
6238
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006239 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006240 * data.
6241 */
Willy Tarreau70089872008-06-13 21:12:51 +02006242 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006243 t->data_source = DATA_SRC_STATS;
6244 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006245 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006246 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6247 t->rep->prod->private = t;
6248 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006249 return 1;
6250}
6251
Willy Tarreau4076a152009-04-02 15:18:36 +02006252/*
6253 * Capture a bad request or response and archive it in the proxy's structure.
6254 */
6255void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6256 struct buffer *buf, struct http_msg *msg,
6257 struct proxy *other_end)
6258{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006259 es->len = buf->r - (buf->data + msg->som);
6260 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006261 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006262 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006263 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006264 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006265 es->when = date; // user-visible date
6266 es->sid = s->uniq_id;
6267 es->srv = s->srv;
6268 es->oe = other_end;
6269 es->src = s->cli_addr;
6270}
Willy Tarreaub2513902006-12-17 14:52:38 +01006271
Willy Tarreaubaaee002006-06-26 02:48:02 +02006272/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006273 * Print a debug line with a header
6274 */
6275void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6276{
6277 int len, max;
6278 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006279 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280 max = end - start;
6281 UBOUND(max, sizeof(trash) - len - 1);
6282 len += strlcpy2(trash + len, start, max + 1);
6283 trash[len++] = '\n';
6284 write(1, trash, len);
6285}
6286
Willy Tarreau0937bc42009-12-22 15:03:09 +01006287/*
6288 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6289 * the required fields are properly allocated and that we only need to (re)init
6290 * them. This should be used before processing any new request.
6291 */
6292void http_init_txn(struct session *s)
6293{
6294 struct http_txn *txn = &s->txn;
6295 struct proxy *fe = s->fe;
6296
6297 txn->flags = 0;
6298 txn->status = -1;
6299
6300 txn->req.sol = txn->req.eol = NULL;
6301 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6302 txn->rsp.sol = txn->rsp.eol = NULL;
6303 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6304 txn->req.hdr_content_len = 0LL;
6305 txn->rsp.hdr_content_len = 0LL;
6306 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6307 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6308 chunk_reset(&txn->auth_hdr);
6309
6310 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6311 if (fe->options2 & PR_O2_REQBUG_OK)
6312 txn->req.err_pos = -1; /* let buggy requests pass */
6313
Willy Tarreau46023632010-01-07 22:51:47 +01006314 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006315 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6316
Willy Tarreau46023632010-01-07 22:51:47 +01006317 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006318 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6319
6320 if (txn->hdr_idx.v)
6321 hdr_idx_init(&txn->hdr_idx);
6322}
6323
6324/* to be used at the end of a transaction */
6325void http_end_txn(struct session *s)
6326{
6327 struct http_txn *txn = &s->txn;
6328
6329 /* these ones will have been dynamically allocated */
6330 pool_free2(pool2_requri, txn->uri);
6331 pool_free2(pool2_capture, txn->cli_cookie);
6332 pool_free2(pool2_capture, txn->srv_cookie);
6333 txn->uri = NULL;
6334 txn->srv_cookie = NULL;
6335 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006336
6337 if (txn->req.cap) {
6338 struct cap_hdr *h;
6339 for (h = s->fe->req_cap; h; h = h->next)
6340 pool_free2(h->pool, txn->req.cap[h->index]);
6341 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6342 }
6343
6344 if (txn->rsp.cap) {
6345 struct cap_hdr *h;
6346 for (h = s->fe->rsp_cap; h; h = h->next)
6347 pool_free2(h->pool, txn->rsp.cap[h->index]);
6348 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6349 }
6350
Willy Tarreau0937bc42009-12-22 15:03:09 +01006351}
6352
6353/* to be used at the end of a transaction to prepare a new one */
6354void http_reset_txn(struct session *s)
6355{
6356 http_end_txn(s);
6357 http_init_txn(s);
6358
6359 s->be = s->fe;
6360 s->req->analysers = s->listener->analysers;
6361 s->logs.logwait = s->fe->to_log;
6362 s->srv = s->prev_srv = s->srv_conn = NULL;
6363 s->pend_pos = NULL;
6364 s->conn_retries = s->be->conn_retries;
6365
6366 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6367
6368 s->req->rto = s->fe->timeout.client;
6369 s->req->wto = s->be->timeout.server;
6370 s->req->cto = s->be->timeout.connect;
6371
6372 s->rep->rto = s->be->timeout.server;
6373 s->rep->wto = s->fe->timeout.client;
6374 s->rep->cto = TICK_ETERNITY;
6375
6376 s->req->rex = TICK_ETERNITY;
6377 s->req->wex = TICK_ETERNITY;
6378 s->req->analyse_exp = TICK_ETERNITY;
6379 s->rep->rex = TICK_ETERNITY;
6380 s->rep->wex = TICK_ETERNITY;
6381 s->rep->analyse_exp = TICK_ETERNITY;
6382}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006383
Willy Tarreau8797c062007-05-07 00:55:35 +02006384/************************************************************************/
6385/* The code below is dedicated to ACL parsing and matching */
6386/************************************************************************/
6387
6388
6389
6390
6391/* 1. Check on METHOD
6392 * We use the pre-parsed method if it is known, and store its number as an
6393 * integer. If it is unknown, we use the pointer and the length.
6394 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006395static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006396{
6397 int len, meth;
6398
Willy Tarreauae8b7962007-06-09 23:10:04 +02006399 len = strlen(*text);
6400 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006401
6402 pattern->val.i = meth;
6403 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006404 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006405 if (!pattern->ptr.str)
6406 return 0;
6407 pattern->len = len;
6408 }
6409 return 1;
6410}
6411
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006412static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006413acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6414 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006415{
6416 int meth;
6417 struct http_txn *txn = l7;
6418
Willy Tarreaub6866442008-07-14 23:54:42 +02006419 if (!txn)
6420 return 0;
6421
Willy Tarreau655dce92009-11-08 13:10:58 +01006422 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006423 return 0;
6424
Willy Tarreau8797c062007-05-07 00:55:35 +02006425 meth = txn->meth;
6426 test->i = meth;
6427 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006428 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6429 /* ensure the indexes are not affected */
6430 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006431 test->len = txn->req.sl.rq.m_l;
6432 test->ptr = txn->req.sol;
6433 }
6434 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6435 return 1;
6436}
6437
6438static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6439{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006440 int icase;
6441
Willy Tarreau8797c062007-05-07 00:55:35 +02006442 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006443 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006444
6445 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006446 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006447
6448 /* Other method, we must compare the strings */
6449 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006450 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006451
6452 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6453 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6454 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006455 return ACL_PAT_FAIL;
6456 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006457}
6458
6459/* 2. Check on Request/Status Version
6460 * We simply compare strings here.
6461 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006462static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006463{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006464 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006465 if (!pattern->ptr.str)
6466 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006467 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006468 return 1;
6469}
6470
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006471static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006472acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6473 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006474{
6475 struct http_txn *txn = l7;
6476 char *ptr;
6477 int len;
6478
Willy Tarreaub6866442008-07-14 23:54:42 +02006479 if (!txn)
6480 return 0;
6481
Willy Tarreau655dce92009-11-08 13:10:58 +01006482 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006483 return 0;
6484
Willy Tarreau8797c062007-05-07 00:55:35 +02006485 len = txn->req.sl.rq.v_l;
6486 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6487
6488 while ((len-- > 0) && (*ptr++ != '/'));
6489 if (len <= 0)
6490 return 0;
6491
6492 test->ptr = ptr;
6493 test->len = len;
6494
6495 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6496 return 1;
6497}
6498
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006499static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006500acl_fetch_stver(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 char *ptr;
6505 int len;
6506
Willy Tarreaub6866442008-07-14 23:54:42 +02006507 if (!txn)
6508 return 0;
6509
Willy Tarreau655dce92009-11-08 13:10:58 +01006510 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006511 return 0;
6512
Willy Tarreau8797c062007-05-07 00:55:35 +02006513 len = txn->rsp.sl.st.v_l;
6514 ptr = txn->rsp.sol;
6515
6516 while ((len-- > 0) && (*ptr++ != '/'));
6517 if (len <= 0)
6518 return 0;
6519
6520 test->ptr = ptr;
6521 test->len = len;
6522
6523 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6524 return 1;
6525}
6526
6527/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006528static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006529acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6530 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006531{
6532 struct http_txn *txn = l7;
6533 char *ptr;
6534 int len;
6535
Willy Tarreaub6866442008-07-14 23:54:42 +02006536 if (!txn)
6537 return 0;
6538
Willy Tarreau655dce92009-11-08 13:10:58 +01006539 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006540 return 0;
6541
Willy Tarreau8797c062007-05-07 00:55:35 +02006542 len = txn->rsp.sl.st.c_l;
6543 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6544
6545 test->i = __strl2ui(ptr, len);
6546 test->flags = ACL_TEST_F_VOL_1ST;
6547 return 1;
6548}
6549
6550/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006551static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006552acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6553 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006554{
6555 struct http_txn *txn = l7;
6556
Willy Tarreaub6866442008-07-14 23:54:42 +02006557 if (!txn)
6558 return 0;
6559
Willy Tarreau655dce92009-11-08 13:10:58 +01006560 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006561 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006562
Willy Tarreauc11416f2007-06-17 16:58:38 +02006563 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6564 /* ensure the indexes are not affected */
6565 return 0;
6566
Willy Tarreau8797c062007-05-07 00:55:35 +02006567 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006568 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006569
Willy Tarreauf3d25982007-05-08 22:45:09 +02006570 /* we do not need to set READ_ONLY because the data is in a buffer */
6571 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006572 return 1;
6573}
6574
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006575static int
6576acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6577 struct acl_expr *expr, struct acl_test *test)
6578{
6579 struct http_txn *txn = l7;
6580
Willy Tarreaub6866442008-07-14 23:54:42 +02006581 if (!txn)
6582 return 0;
6583
Willy Tarreau655dce92009-11-08 13:10:58 +01006584 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006585 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006586
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006587 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6588 /* ensure the indexes are not affected */
6589 return 0;
6590
6591 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006592 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 +01006593 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6594 test->i = AF_INET;
6595
6596 /*
6597 * If we are parsing url in frontend space, we prepare backend stage
6598 * to not parse again the same url ! optimization lazyness...
6599 */
6600 if (px->options & PR_O_HTTP_PROXY)
6601 l4->flags |= SN_ADDR_SET;
6602
6603 test->flags = ACL_TEST_F_READ_ONLY;
6604 return 1;
6605}
6606
6607static int
6608acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6609 struct acl_expr *expr, struct acl_test *test)
6610{
6611 struct http_txn *txn = l7;
6612
Willy Tarreaub6866442008-07-14 23:54:42 +02006613 if (!txn)
6614 return 0;
6615
Willy Tarreau655dce92009-11-08 13:10:58 +01006616 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006617 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006618
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006619 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6620 /* ensure the indexes are not affected */
6621 return 0;
6622
6623 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006624 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 +01006625 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6626
6627 if (px->options & PR_O_HTTP_PROXY)
6628 l4->flags |= SN_ADDR_SET;
6629
6630 test->flags = ACL_TEST_F_READ_ONLY;
6631 return 1;
6632}
6633
Willy Tarreauc11416f2007-06-17 16:58:38 +02006634/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6635 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6636 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006637static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006638acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006639 struct acl_expr *expr, struct acl_test *test)
6640{
6641 struct http_txn *txn = l7;
6642 struct hdr_idx *idx = &txn->hdr_idx;
6643 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006644
Willy Tarreaub6866442008-07-14 23:54:42 +02006645 if (!txn)
6646 return 0;
6647
Willy Tarreau33a7e692007-06-10 19:45:56 +02006648 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6649 /* search for header from the beginning */
6650 ctx->idx = 0;
6651
Willy Tarreau33a7e692007-06-10 19:45:56 +02006652 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6653 test->flags |= ACL_TEST_F_FETCH_MORE;
6654 test->flags |= ACL_TEST_F_VOL_HDR;
6655 test->len = ctx->vlen;
6656 test->ptr = (char *)ctx->line + ctx->val;
6657 return 1;
6658 }
6659
6660 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6661 test->flags |= ACL_TEST_F_VOL_HDR;
6662 return 0;
6663}
6664
Willy Tarreau33a7e692007-06-10 19:45:56 +02006665static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006666acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6667 struct acl_expr *expr, struct acl_test *test)
6668{
6669 struct http_txn *txn = l7;
6670
Willy Tarreaub6866442008-07-14 23:54:42 +02006671 if (!txn)
6672 return 0;
6673
Willy Tarreau655dce92009-11-08 13:10:58 +01006674 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006675 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006676
Willy Tarreauc11416f2007-06-17 16:58:38 +02006677 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6678 /* ensure the indexes are not affected */
6679 return 0;
6680
6681 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6682}
6683
6684static int
6685acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6686 struct acl_expr *expr, struct acl_test *test)
6687{
6688 struct http_txn *txn = l7;
6689
Willy Tarreaub6866442008-07-14 23:54:42 +02006690 if (!txn)
6691 return 0;
6692
Willy Tarreau655dce92009-11-08 13:10:58 +01006693 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006694 return 0;
6695
6696 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6697}
6698
6699/* 6. Check on HTTP header count. The number of occurrences is returned.
6700 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6701 */
6702static int
6703acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006704 struct acl_expr *expr, struct acl_test *test)
6705{
6706 struct http_txn *txn = l7;
6707 struct hdr_idx *idx = &txn->hdr_idx;
6708 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006709 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006710
Willy Tarreaub6866442008-07-14 23:54:42 +02006711 if (!txn)
6712 return 0;
6713
Willy Tarreau33a7e692007-06-10 19:45:56 +02006714 ctx.idx = 0;
6715 cnt = 0;
6716 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6717 cnt++;
6718
6719 test->i = cnt;
6720 test->flags = ACL_TEST_F_VOL_HDR;
6721 return 1;
6722}
6723
Willy Tarreauc11416f2007-06-17 16:58:38 +02006724static int
6725acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6726 struct acl_expr *expr, struct acl_test *test)
6727{
6728 struct http_txn *txn = l7;
6729
Willy Tarreaub6866442008-07-14 23:54:42 +02006730 if (!txn)
6731 return 0;
6732
Willy Tarreau655dce92009-11-08 13:10:58 +01006733 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006734 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006735
Willy Tarreauc11416f2007-06-17 16:58:38 +02006736 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6737 /* ensure the indexes are not affected */
6738 return 0;
6739
6740 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6741}
6742
6743static int
6744acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6745 struct acl_expr *expr, struct acl_test *test)
6746{
6747 struct http_txn *txn = l7;
6748
Willy Tarreaub6866442008-07-14 23:54:42 +02006749 if (!txn)
6750 return 0;
6751
Willy Tarreau655dce92009-11-08 13:10:58 +01006752 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006753 return 0;
6754
6755 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6756}
6757
Willy Tarreau33a7e692007-06-10 19:45:56 +02006758/* 7. Check on HTTP header's integer value. The integer value is returned.
6759 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006760 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006761 */
6762static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006763acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006764 struct acl_expr *expr, struct acl_test *test)
6765{
6766 struct http_txn *txn = l7;
6767 struct hdr_idx *idx = &txn->hdr_idx;
6768 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006769
Willy Tarreaub6866442008-07-14 23:54:42 +02006770 if (!txn)
6771 return 0;
6772
Willy Tarreau33a7e692007-06-10 19:45:56 +02006773 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6774 /* search for header from the beginning */
6775 ctx->idx = 0;
6776
Willy Tarreau33a7e692007-06-10 19:45:56 +02006777 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6778 test->flags |= ACL_TEST_F_FETCH_MORE;
6779 test->flags |= ACL_TEST_F_VOL_HDR;
6780 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6781 return 1;
6782 }
6783
6784 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6785 test->flags |= ACL_TEST_F_VOL_HDR;
6786 return 0;
6787}
6788
Willy Tarreauc11416f2007-06-17 16:58:38 +02006789static int
6790acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6791 struct acl_expr *expr, struct acl_test *test)
6792{
6793 struct http_txn *txn = l7;
6794
Willy Tarreaub6866442008-07-14 23:54:42 +02006795 if (!txn)
6796 return 0;
6797
Willy Tarreau655dce92009-11-08 13:10:58 +01006798 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006799 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006800
Willy Tarreauc11416f2007-06-17 16:58:38 +02006801 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6802 /* ensure the indexes are not affected */
6803 return 0;
6804
6805 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6806}
6807
6808static int
6809acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6810 struct acl_expr *expr, struct acl_test *test)
6811{
6812 struct http_txn *txn = l7;
6813
Willy Tarreaub6866442008-07-14 23:54:42 +02006814 if (!txn)
6815 return 0;
6816
Willy Tarreau655dce92009-11-08 13:10:58 +01006817 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006818 return 0;
6819
6820 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6821}
6822
Willy Tarreau106f9792009-09-19 07:54:16 +02006823/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6824 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6825 */
6826static int
6827acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6828 struct acl_expr *expr, struct acl_test *test)
6829{
6830 struct http_txn *txn = l7;
6831 struct hdr_idx *idx = &txn->hdr_idx;
6832 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6833
6834 if (!txn)
6835 return 0;
6836
6837 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6838 /* search for header from the beginning */
6839 ctx->idx = 0;
6840
6841 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6842 test->flags |= ACL_TEST_F_FETCH_MORE;
6843 test->flags |= ACL_TEST_F_VOL_HDR;
6844 /* Same optimization as url_ip */
6845 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6846 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6847 test->ptr = (void *)&l4->srv_addr.sin_addr;
6848 test->i = AF_INET;
6849 return 1;
6850 }
6851
6852 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6853 test->flags |= ACL_TEST_F_VOL_HDR;
6854 return 0;
6855}
6856
6857static int
6858acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6859 struct acl_expr *expr, struct acl_test *test)
6860{
6861 struct http_txn *txn = l7;
6862
6863 if (!txn)
6864 return 0;
6865
Willy Tarreau655dce92009-11-08 13:10:58 +01006866 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006867 return 0;
6868
6869 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6870 /* ensure the indexes are not affected */
6871 return 0;
6872
6873 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6874}
6875
6876static int
6877acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6878 struct acl_expr *expr, struct acl_test *test)
6879{
6880 struct http_txn *txn = l7;
6881
6882 if (!txn)
6883 return 0;
6884
Willy Tarreau655dce92009-11-08 13:10:58 +01006885 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006886 return 0;
6887
6888 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6889}
6890
Willy Tarreau737b0c12007-06-10 21:28:46 +02006891/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6892 * the first '/' after the possible hostname, and ends before the possible '?'.
6893 */
6894static int
6895acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6896 struct acl_expr *expr, struct acl_test *test)
6897{
6898 struct http_txn *txn = l7;
6899 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006900
Willy Tarreaub6866442008-07-14 23:54:42 +02006901 if (!txn)
6902 return 0;
6903
Willy Tarreau655dce92009-11-08 13:10:58 +01006904 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006905 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006906
Willy Tarreauc11416f2007-06-17 16:58:38 +02006907 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6908 /* ensure the indexes are not affected */
6909 return 0;
6910
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006911 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 +01006912 ptr = http_get_path(txn);
6913 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006914 return 0;
6915
6916 /* OK, we got the '/' ! */
6917 test->ptr = ptr;
6918
6919 while (ptr < end && *ptr != '?')
6920 ptr++;
6921
6922 test->len = ptr - test->ptr;
6923
6924 /* we do not need to set READ_ONLY because the data is in a buffer */
6925 test->flags = ACL_TEST_F_VOL_1ST;
6926 return 1;
6927}
6928
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006929static int
6930acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6931 struct acl_expr *expr, struct acl_test *test)
6932{
6933 struct buffer *req = s->req;
6934 struct http_txn *txn = &s->txn;
6935 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006936
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006937 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6938 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6939 */
6940
6941 if (!s || !req)
6942 return 0;
6943
Willy Tarreau655dce92009-11-08 13:10:58 +01006944 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006945 /* Already decoded as OK */
6946 test->flags |= ACL_TEST_F_SET_RES_PASS;
6947 return 1;
6948 }
6949
6950 /* Try to decode HTTP request */
6951 if (likely(req->lr < req->r))
6952 http_msg_analyzer(req, msg, &txn->hdr_idx);
6953
Willy Tarreau655dce92009-11-08 13:10:58 +01006954 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006955 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6956 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6957 return 1;
6958 }
6959 /* wait for final state */
6960 test->flags |= ACL_TEST_F_MAY_CHANGE;
6961 return 0;
6962 }
6963
6964 /* OK we got a valid HTTP request. We have some minor preparation to
6965 * perform so that further checks can rely on HTTP tests.
6966 */
6967 msg->sol = req->data + msg->som;
6968 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6969 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6970 s->flags |= SN_REDIRECTABLE;
6971
6972 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6973 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6974 return 1;
6975 }
6976
6977 test->flags |= ACL_TEST_F_SET_RES_PASS;
6978 return 1;
6979}
6980
Willy Tarreau8797c062007-05-07 00:55:35 +02006981
6982/************************************************************************/
6983/* All supported keywords must be declared here. */
6984/************************************************************************/
6985
6986/* Note: must not be declared <const> as its list will be overwritten */
6987static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006988 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6989
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006990 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6991 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6992 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6993 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006994
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006995 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6996 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6997 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6998 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6999 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7000 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7001 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7002 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7003 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007004
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007005 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7006 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7007 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7008 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7009 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7010 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7011 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7012 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7013 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7014 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007015 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007016
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007017 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7018 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7019 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7020 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7021 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7022 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7023 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7024 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7025 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007026 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007027
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007028 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7029 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7030 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7031 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7032 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7033 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7034 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007035
Willy Tarreauf3d25982007-05-08 22:45:09 +02007036 { NULL, NULL, NULL, NULL },
7037
7038#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007039 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7040 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7041 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7042 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7043 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7044 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7045 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7046
Willy Tarreau8797c062007-05-07 00:55:35 +02007047 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7048 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7049 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7050 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7051 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7052 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7053 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7054 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7055
7056 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7057 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7058 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7059 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7060 { NULL, NULL, NULL, NULL },
7061#endif
7062}};
7063
7064
7065__attribute__((constructor))
7066static void __http_protocol_init(void)
7067{
7068 acl_register_keywords(&acl_kws);
7069}
7070
7071
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007073 * Local variables:
7074 * c-indent-level: 8
7075 * c-basic-offset: 8
7076 * End:
7077 */