blob: a3354207acad904490a6ab26ce26142b1b376086 [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;
2858 stream_int_cond_close(req->prod, &rdr);
2859 goto return_prx_cond;
2860 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 }
2862 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002863
Willy Tarreau2be39392010-01-03 17:24:51 +01002864 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2865 * If this happens, then the data will not come immediately, so we must
2866 * send all what we have without waiting. Note that due to the small gain
2867 * in waiting for the body of the request, it's easier to simply put the
2868 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2869 * itself once used.
2870 */
2871 req->flags |= BF_SEND_DONTWAIT;
2872
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002873 /* that's OK for us now, let's move on to next analysers */
2874 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002875
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002876 return_bad_req:
2877 /* We centralize bad requests processing here */
2878 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2879 /* we detected a parsing error. We want to archive this request
2880 * in the dedicated proxy area for later troubleshooting.
2881 */
2882 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2883 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002884
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002885 txn->req.msg_state = HTTP_MSG_ERROR;
2886 txn->status = 400;
2887 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002888
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002889 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002890 if (s->listener->counters)
2891 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002892
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 return_prx_cond:
2894 if (!(s->flags & SN_ERR_MASK))
2895 s->flags |= SN_ERR_PRXCOND;
2896 if (!(s->flags & SN_FINST_MASK))
2897 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002898
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002899 req->analysers = 0;
2900 req->analyse_exp = TICK_ETERNITY;
2901 return 0;
2902}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002903
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002904/* This function performs all the processing enabled for the current request.
2905 * It returns 1 if the processing can continue on next analysers, or zero if it
2906 * needs more data, encounters an error, or wants to immediately abort the
2907 * request. It relies on buffers flags, and updates s->req->analysers.
2908 */
2909int http_process_request(struct session *s, struct buffer *req, int an_bit)
2910{
2911 struct http_txn *txn = &s->txn;
2912 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002913
Willy Tarreau655dce92009-11-08 13:10:58 +01002914 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002915 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002916 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002917 return 0;
2918 }
2919
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002920 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2921 now_ms, __FUNCTION__,
2922 s,
2923 req,
2924 req->rex, req->wex,
2925 req->flags,
2926 req->l,
2927 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002928
Willy Tarreau59234e92008-11-30 23:51:27 +01002929 /*
2930 * Right now, we know that we have processed the entire headers
2931 * and that unwanted requests have been filtered out. We can do
2932 * whatever we want with the remaining request. Also, now we
2933 * may have separate values for ->fe, ->be.
2934 */
Willy Tarreau06619262006-12-17 08:37:22 +01002935
Willy Tarreau59234e92008-11-30 23:51:27 +01002936 /*
2937 * If HTTP PROXY is set we simply get remote server address
2938 * parsing incoming request.
2939 */
2940 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2941 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2942 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002943
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002945 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002946 * Note that doing so might move headers in the request, but
2947 * the fields will stay coherent and the URI will not move.
2948 * This should only be performed in the backend.
2949 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002950 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002951 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2952 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002953
Willy Tarreau59234e92008-11-30 23:51:27 +01002954 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002955 * 8: the appsession cookie was looked up very early in 1.2,
2956 * so let's do the same now.
2957 */
2958
2959 /* It needs to look into the URI */
2960 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002961 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002962 }
2963
2964 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002965 * 9: add X-Forwarded-For if either the frontend or the backend
2966 * asks for it.
2967 */
2968 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2969 if (s->cli_addr.ss_family == AF_INET) {
2970 /* Add an X-Forwarded-For header unless the source IP is
2971 * in the 'except' network range.
2972 */
2973 if ((!s->fe->except_mask.s_addr ||
2974 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2975 != s->fe->except_net.s_addr) &&
2976 (!s->be->except_mask.s_addr ||
2977 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2978 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002979 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002980 unsigned char *pn;
2981 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002982
2983 /* Note: we rely on the backend to get the header name to be used for
2984 * x-forwarded-for, because the header is really meant for the backends.
2985 * However, if the backend did not specify any option, we have to rely
2986 * on the frontend's header name.
2987 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 if (s->be->fwdfor_hdr_len) {
2989 len = s->be->fwdfor_hdr_len;
2990 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002991 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 len = s->fe->fwdfor_hdr_len;
2993 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002994 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002995 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002996
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002997 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002998 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002999 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003000 }
3001 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003002 else if (s->cli_addr.ss_family == AF_INET6) {
3003 /* FIXME: for the sake of completeness, we should also support
3004 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003005 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003006 int len;
3007 char pn[INET6_ADDRSTRLEN];
3008 inet_ntop(AF_INET6,
3009 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3010 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003011
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 /* Note: we rely on the backend to get the header name to be used for
3013 * x-forwarded-for, because the header is really meant for the backends.
3014 * However, if the backend did not specify any option, we have to rely
3015 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003016 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003017 if (s->be->fwdfor_hdr_len) {
3018 len = s->be->fwdfor_hdr_len;
3019 memcpy(trash, s->be->fwdfor_hdr_name, len);
3020 } else {
3021 len = s->fe->fwdfor_hdr_len;
3022 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003023 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003024 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003025
Willy Tarreau59234e92008-11-30 23:51:27 +01003026 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003027 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003028 goto return_bad_req;
3029 }
3030 }
3031
3032 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003033 * 10: add X-Original-To if either the frontend or the backend
3034 * asks for it.
3035 */
3036 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3037
3038 /* FIXME: don't know if IPv6 can handle that case too. */
3039 if (s->cli_addr.ss_family == AF_INET) {
3040 /* Add an X-Original-To header unless the destination IP is
3041 * in the 'except' network range.
3042 */
3043 if (!(s->flags & SN_FRT_ADDR_SET))
3044 get_frt_addr(s);
3045
3046 if ((!s->fe->except_mask_to.s_addr ||
3047 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3048 != s->fe->except_to.s_addr) &&
3049 (!s->be->except_mask_to.s_addr ||
3050 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3051 != s->be->except_to.s_addr)) {
3052 int len;
3053 unsigned char *pn;
3054 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3055
3056 /* Note: we rely on the backend to get the header name to be used for
3057 * x-original-to, because the header is really meant for the backends.
3058 * However, if the backend did not specify any option, we have to rely
3059 * on the frontend's header name.
3060 */
3061 if (s->be->orgto_hdr_len) {
3062 len = s->be->orgto_hdr_len;
3063 memcpy(trash, s->be->orgto_hdr_name, len);
3064 } else {
3065 len = s->fe->orgto_hdr_len;
3066 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003067 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003068 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3069
3070 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003071 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003072 goto return_bad_req;
3073 }
3074 }
3075 }
3076
Willy Tarreau78599912009-10-17 20:12:21 +02003077 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003078 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3079 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3080 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003081 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003082 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003083 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003084 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003085 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003086
3087 /* If we have no server assigned yet and we're balancing on url_param
3088 * with a POST request, we may be interested in checking the body for
3089 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003090 */
3091 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3092 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003093 s->be->url_param_post_limit != 0 &&
3094 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003095 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003096 buffer_dont_connect(req);
3097 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003098 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003099
Willy Tarreaud98cf932009-12-27 22:54:55 +01003100 if (txn->flags & TX_REQ_XFER_LEN)
3101 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003102
Willy Tarreau59234e92008-11-30 23:51:27 +01003103 /*************************************************************
3104 * OK, that's finished for the headers. We have done what we *
3105 * could. Let's switch to the DATA state. *
3106 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003107 req->analyse_exp = TICK_ETERNITY;
3108 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003109
Willy Tarreau59234e92008-11-30 23:51:27 +01003110 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003111 /* OK let's go on with the BODY now */
3112 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003113
Willy Tarreau59234e92008-11-30 23:51:27 +01003114 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003115 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003116 /* we detected a parsing error. We want to archive this request
3117 * in the dedicated proxy area for later troubleshooting.
3118 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003119 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003120 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003121
Willy Tarreau59234e92008-11-30 23:51:27 +01003122 txn->req.msg_state = HTTP_MSG_ERROR;
3123 txn->status = 400;
3124 req->analysers = 0;
3125 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003126
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003127 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003128 if (s->listener->counters)
3129 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003130
Willy Tarreau59234e92008-11-30 23:51:27 +01003131 if (!(s->flags & SN_ERR_MASK))
3132 s->flags |= SN_ERR_PRXCOND;
3133 if (!(s->flags & SN_FINST_MASK))
3134 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003135 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003136}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003137
Willy Tarreau60b85b02008-11-30 23:28:40 +01003138/* This function is an analyser which processes the HTTP tarpit. It always
3139 * returns zero, at the beginning because it prevents any other processing
3140 * from occurring, and at the end because it terminates the request.
3141 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003142int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003143{
3144 struct http_txn *txn = &s->txn;
3145
3146 /* This connection is being tarpitted. The CLIENT side has
3147 * already set the connect expiration date to the right
3148 * timeout. We just have to check that the client is still
3149 * there and that the timeout has not expired.
3150 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003151 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003152 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3153 !tick_is_expired(req->analyse_exp, now_ms))
3154 return 0;
3155
3156 /* We will set the queue timer to the time spent, just for
3157 * logging purposes. We fake a 500 server error, so that the
3158 * attacker will not suspect his connection has been tarpitted.
3159 * It will not cause trouble to the logs because we can exclude
3160 * the tarpitted connections by filtering on the 'PT' status flags.
3161 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003162 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3163
3164 txn->status = 500;
3165 if (req->flags != BF_READ_ERROR)
3166 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3167
3168 req->analysers = 0;
3169 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003170
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003171 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003172 if (s->listener->counters)
3173 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003174
Willy Tarreau60b85b02008-11-30 23:28:40 +01003175 if (!(s->flags & SN_ERR_MASK))
3176 s->flags |= SN_ERR_PRXCOND;
3177 if (!(s->flags & SN_FINST_MASK))
3178 s->flags |= SN_FINST_T;
3179 return 0;
3180}
3181
Willy Tarreaud34af782008-11-30 23:36:37 +01003182/* This function is an analyser which processes the HTTP request body. It looks
3183 * for parameters to be used for the load balancing algorithm (url_param). It
3184 * must only be called after the standard HTTP request processing has occurred,
3185 * because it expects the request to be parsed. It returns zero if it needs to
3186 * read more data, or 1 once it has completed its analysis.
3187 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003188int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003189{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003190 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003191 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003192 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003193
3194 /* We have to parse the HTTP request body to find any required data.
3195 * "balance url_param check_post" should have been the only way to get
3196 * into this. We were brought here after HTTP header analysis, so all
3197 * related structures are ready.
3198 */
3199
Willy Tarreau522d6c02009-12-06 18:49:18 +01003200 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3201 goto missing_data;
3202
3203 if (msg->msg_state < HTTP_MSG_100_SENT) {
3204 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3205 * send an HTTP/1.1 100 Continue intermediate response.
3206 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003207 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003208 struct hdr_ctx ctx;
3209 ctx.idx = 0;
3210 /* Expect is allowed in 1.1, look for it */
3211 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3212 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3213 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3214 }
3215 }
3216 msg->msg_state = HTTP_MSG_100_SENT;
3217 }
3218
3219 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003220 /* we have msg->col and msg->sov which both point to the first
3221 * byte of message body. msg->som still points to the beginning
3222 * of the message. We must save the body in req->lr because it
3223 * survives buffer re-alignments.
3224 */
3225 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003226 if (txn->flags & TX_REQ_TE_CHNK)
3227 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3228 else
3229 msg->msg_state = HTTP_MSG_DATA;
3230 }
3231
3232 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003233 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003234 * set ->sov and ->lr to point to the body and switch to DATA or
3235 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003236 */
3237 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003238
Willy Tarreau115acb92009-12-26 13:56:06 +01003239 if (!ret)
3240 goto missing_data;
3241 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003242 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003243 }
3244
Willy Tarreaud98cf932009-12-27 22:54:55 +01003245 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003246 * We have the first non-header byte in msg->col, which is either the
3247 * beginning of the chunk size or of the data. The first data byte is in
3248 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3249 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003250 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003251
3252 if (msg->hdr_content_len < limit)
3253 limit = msg->hdr_content_len;
3254
Willy Tarreau7c96f672009-12-27 22:47:25 +01003255 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003256 goto http_end;
3257
3258 missing_data:
3259 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003260 if (req->flags & BF_FULL)
3261 goto return_bad_req;
3262
Willy Tarreau522d6c02009-12-06 18:49:18 +01003263 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3264 txn->status = 408;
3265 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3266 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003267 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003268
3269 /* we get here if we need to wait for more data */
3270 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003271 /* Not enough data. We'll re-use the http-request
3272 * timeout here. Ideally, we should set the timeout
3273 * relative to the accept() date. We just set the
3274 * request timeout once at the beginning of the
3275 * request.
3276 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003277 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003278 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003279 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003280 return 0;
3281 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003282
3283 http_end:
3284 /* The situation will not evolve, so let's give up on the analysis. */
3285 s->logs.tv_request = now; /* update the request timer to reflect full request */
3286 req->analysers &= ~an_bit;
3287 req->analyse_exp = TICK_ETERNITY;
3288 return 1;
3289
3290 return_bad_req: /* let's centralize all bad requests */
3291 txn->req.msg_state = HTTP_MSG_ERROR;
3292 txn->status = 400;
3293 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3294
3295 return_err_msg:
3296 req->analysers = 0;
3297 s->fe->counters.failed_req++;
3298 if (s->listener->counters)
3299 s->listener->counters->failed_req++;
3300
3301 if (!(s->flags & SN_ERR_MASK))
3302 s->flags |= SN_ERR_PRXCOND;
3303 if (!(s->flags & SN_FINST_MASK))
3304 s->flags |= SN_FINST_R;
3305 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003306}
3307
Willy Tarreau610ecce2010-01-04 21:15:02 +01003308/* Terminate current transaction and prepare a new one. This is very tricky
3309 * right now but it works.
3310 */
3311void http_end_txn_clean_session(struct session *s)
3312{
3313 /* FIXME: We need a more portable way of releasing a backend's and a
3314 * server's connections. We need a safer way to reinitialize buffer
3315 * flags. We also need a more accurate method for computing per-request
3316 * data.
3317 */
3318 http_silent_debug(__LINE__, s);
3319
3320 s->req->cons->flags |= SI_FL_NOLINGER;
3321 s->req->cons->shutr(s->req->cons);
3322 s->req->cons->shutw(s->req->cons);
3323
3324 http_silent_debug(__LINE__, s);
3325
3326 if (s->flags & SN_BE_ASSIGNED)
3327 s->be->beconn--;
3328
3329 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3330 session_process_counters(s);
3331
3332 if (s->txn.status) {
3333 int n;
3334
3335 n = s->txn.status / 100;
3336 if (n < 1 || n > 5)
3337 n = 0;
3338
3339 if (s->fe->mode == PR_MODE_HTTP)
3340 s->fe->counters.p.http.rsp[n]++;
3341
3342 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3343 (s->be->mode == PR_MODE_HTTP))
3344 s->be->counters.p.http.rsp[n]++;
3345 }
3346
3347 /* don't count other requests' data */
3348 s->logs.bytes_in -= s->req->l - s->req->send_max;
3349 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3350
3351 /* let's do a final log if we need it */
3352 if (s->logs.logwait &&
3353 !(s->flags & SN_MONITOR) &&
3354 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3355 s->do_log(s);
3356 }
3357
3358 s->logs.accept_date = date; /* user-visible date for logging */
3359 s->logs.tv_accept = now; /* corrected date for internal use */
3360 tv_zero(&s->logs.tv_request);
3361 s->logs.t_queue = -1;
3362 s->logs.t_connect = -1;
3363 s->logs.t_data = -1;
3364 s->logs.t_close = 0;
3365 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3366 s->logs.srv_queue_size = 0; /* we will get this number soon */
3367
3368 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3369 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3370
3371 if (s->pend_pos)
3372 pendconn_free(s->pend_pos);
3373
3374 if (s->srv) {
3375 if (s->flags & SN_CURR_SESS) {
3376 s->flags &= ~SN_CURR_SESS;
3377 s->srv->cur_sess--;
3378 }
3379 if (may_dequeue_tasks(s->srv, s->be))
3380 process_srv_queue(s->srv);
3381 }
3382
3383 if (unlikely(s->srv_conn))
3384 sess_change_server(s, NULL);
3385 s->srv = NULL;
3386
3387 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3388 s->req->cons->fd = -1; /* just to help with debugging */
3389 s->req->cons->err_type = SI_ET_NONE;
3390 s->req->cons->err_loc = NULL;
3391 s->req->cons->exp = TICK_ETERNITY;
3392 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003393 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3394 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 +01003395 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3396 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3397 s->txn.meth = 0;
3398 http_reset_txn(s);
3399 s->txn.flags |= TX_NOT_FIRST;
3400 if (s->be->options2 & PR_O2_INDEPSTR)
3401 s->req->cons->flags |= SI_FL_INDEP_STR;
3402
3403 /* if the request buffer is not empty, it means we're
3404 * about to process another request, so send pending
3405 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003406 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003407 if (s->req->l > s->req->send_max)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003408 s->rep->flags |= BF_EXPECT_MORE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003409
3410 /* we're removing the analysers, we MUST re-enable events detection */
3411 buffer_auto_read(s->req);
3412 buffer_auto_close(s->req);
3413 buffer_auto_read(s->rep);
3414 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003415
3416 /* make ->lr point to the first non-forwarded byte */
3417 s->req->lr = s->req->w + s->req->send_max;
3418 if (s->req->lr >= s->req->data + s->req->size)
3419 s->req->lr -= s->req->size;
3420 s->rep->lr = s->rep->w + s->rep->send_max;
3421 if (s->rep->lr >= s->rep->data + s->rep->size)
3422 s->rep->lr -= s->req->size;
3423
3424 s->req->analysers |= s->fe->fe_req_ana;
3425 s->rep->analysers = 0;
3426
3427 http_silent_debug(__LINE__, s);
3428}
3429
3430
3431/* This function updates the request state machine according to the response
3432 * state machine and buffer flags. It returns 1 if it changes anything (flag
3433 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3434 * it is only used to find when a request/response couple is complete. Both
3435 * this function and its equivalent should loop until both return zero. It
3436 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3437 */
3438int http_sync_req_state(struct session *s)
3439{
3440 struct buffer *buf = s->req;
3441 struct http_txn *txn = &s->txn;
3442 unsigned int old_flags = buf->flags;
3443 unsigned int old_state = txn->req.msg_state;
3444
3445 http_silent_debug(__LINE__, s);
3446 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3447 return 0;
3448
3449 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003450 /* No need to read anymore, the request was completely parsed.
3451 * We can shut the read side unless we want to abort_on_close.
3452 */
3453 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3454 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003455
3456 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3457 goto wait_other_side;
3458
3459 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3460 /* The server has not finished to respond, so we
3461 * don't want to move in order not to upset it.
3462 */
3463 goto wait_other_side;
3464 }
3465
3466 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3467 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003468 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003469 txn->req.msg_state = HTTP_MSG_TUNNEL;
3470 goto wait_other_side;
3471 }
3472
3473 /* When we get here, it means that both the request and the
3474 * response have finished receiving. Depending on the connection
3475 * mode, we'll have to wait for the last bytes to leave in either
3476 * direction, and sometimes for a close to be effective.
3477 */
3478
3479 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3480 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3481 /* Server-close mode : queue a connection close to the server */
3482 buffer_shutw_now(buf);
3483 buf->cons->flags |= SI_FL_NOLINGER;
3484 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003485 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003486 /* Option forceclose is set, let's enforce it now
3487 * that we're not expecting any new data to come.
3488 */
3489 buffer_shutr_now(buf);
3490 buffer_shutw_now(buf);
3491 buf->cons->flags |= SI_FL_NOLINGER;
3492 }
3493 /* other modes include httpclose (no action) and keepalive (not implemented) */
3494 }
3495
3496 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3497 /* if we've just closed an output, let's switch */
3498 if (!(buf->flags & BF_OUT_EMPTY)) {
3499 txn->req.msg_state = HTTP_MSG_CLOSING;
3500 goto http_msg_closing;
3501 }
3502 else {
3503 txn->req.msg_state = HTTP_MSG_CLOSED;
3504 goto http_msg_closed;
3505 }
3506 }
3507 else {
3508 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003509 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003510 txn->req.msg_state = HTTP_MSG_TUNNEL;
3511 goto wait_other_side;
3512 }
3513 }
3514
3515 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3516 http_msg_closing:
3517 /* nothing else to forward, just waiting for the output buffer
3518 * to be empty and for the shutw_now to take effect.
3519 */
3520 if (buf->flags & BF_OUT_EMPTY) {
3521 txn->req.msg_state = HTTP_MSG_CLOSED;
3522 goto http_msg_closed;
3523 }
3524 else if (buf->flags & BF_SHUTW) {
3525 txn->req.msg_state = HTTP_MSG_ERROR;
3526 goto wait_other_side;
3527 }
3528 }
3529
3530 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3531 http_msg_closed:
3532 goto wait_other_side;
3533 }
3534
3535 wait_other_side:
3536 http_silent_debug(__LINE__, s);
3537 return txn->req.msg_state != old_state || buf->flags != old_flags;
3538}
3539
3540
3541/* This function updates the response state machine according to the request
3542 * state machine and buffer flags. It returns 1 if it changes anything (flag
3543 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3544 * it is only used to find when a request/response couple is complete. Both
3545 * this function and its equivalent should loop until both return zero. It
3546 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3547 */
3548int http_sync_res_state(struct session *s)
3549{
3550 struct buffer *buf = s->rep;
3551 struct http_txn *txn = &s->txn;
3552 unsigned int old_flags = buf->flags;
3553 unsigned int old_state = txn->rsp.msg_state;
3554
3555 http_silent_debug(__LINE__, s);
3556 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3557 return 0;
3558
3559 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3560 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003561 * still monitor the server connection for a possible close
3562 * while the request is being uploaded, so we don't disable
3563 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003564 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003565 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003566
3567 if (txn->req.msg_state == HTTP_MSG_ERROR)
3568 goto wait_other_side;
3569
3570 if (txn->req.msg_state < HTTP_MSG_DONE) {
3571 /* The client seems to still be sending data, probably
3572 * because we got an error response during an upload.
3573 * We have the choice of either breaking the connection
3574 * or letting it pass through. Let's do the later.
3575 */
3576 goto wait_other_side;
3577 }
3578
3579 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3580 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003581 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003582 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3583 goto wait_other_side;
3584 }
3585
3586 /* When we get here, it means that both the request and the
3587 * response have finished receiving. Depending on the connection
3588 * mode, we'll have to wait for the last bytes to leave in either
3589 * direction, and sometimes for a close to be effective.
3590 */
3591
3592 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3593 /* Server-close mode : shut read and wait for the request
3594 * side to close its output buffer. The caller will detect
3595 * when we're in DONE and the other is in CLOSED and will
3596 * catch that for the final cleanup.
3597 */
3598 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3599 buffer_shutr_now(buf);
3600 goto wait_other_side;
3601 }
3602 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3603 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3604 /* Option forceclose is set, let's enforce it now
3605 * that we're not expecting any new data to come.
3606 * The caller knows the session is complete once
3607 * both states are CLOSED.
3608 */
3609 buffer_shutr_now(buf);
3610 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003611 }
3612 else {
3613 /* other modes include httpclose (no action) and keepalive
3614 * (not implemented). These modes are used as a tunnel right
3615 * now.
3616 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003617 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003618 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3619 goto wait_other_side;
3620 }
3621
3622 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3623 /* if we've just closed an output, let's switch */
3624 if (!(buf->flags & BF_OUT_EMPTY)) {
3625 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3626 goto http_msg_closing;
3627 }
3628 else {
3629 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3630 goto http_msg_closed;
3631 }
3632 }
3633 goto wait_other_side;
3634 }
3635
3636 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3637 http_msg_closing:
3638 /* nothing else to forward, just waiting for the output buffer
3639 * to be empty and for the shutw_now to take effect.
3640 */
3641 if (buf->flags & BF_OUT_EMPTY) {
3642 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3643 goto http_msg_closed;
3644 }
3645 else if (buf->flags & BF_SHUTW) {
3646 txn->rsp.msg_state = HTTP_MSG_ERROR;
3647 goto wait_other_side;
3648 }
3649 }
3650
3651 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3652 http_msg_closed:
3653 /* drop any pending data */
3654 buffer_ignore(buf, buf->l - buf->send_max);
3655 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003656 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003657 goto wait_other_side;
3658 }
3659
3660 wait_other_side:
3661 http_silent_debug(__LINE__, s);
3662 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3663}
3664
3665
3666/* Resync the request and response state machines. Return 1 if either state
3667 * changes.
3668 */
3669int http_resync_states(struct session *s)
3670{
3671 struct http_txn *txn = &s->txn;
3672 int old_req_state = txn->req.msg_state;
3673 int old_res_state = txn->rsp.msg_state;
3674
3675 http_silent_debug(__LINE__, s);
3676 http_sync_req_state(s);
3677 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003678 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003679 if (!http_sync_res_state(s))
3680 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003681 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003682 if (!http_sync_req_state(s))
3683 break;
3684 }
3685 http_silent_debug(__LINE__, s);
3686 /* OK, both state machines agree on a compatible state.
3687 * There are a few cases we're interested in :
3688 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3689 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3690 * directions, so let's simply disable both analysers.
3691 * - HTTP_MSG_CLOSED on the response only means we must abort the
3692 * request.
3693 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3694 * with server-close mode means we've completed one request and we
3695 * must re-initialize the server connection.
3696 */
3697
3698 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3699 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3700 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3701 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3702 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003703 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003704 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003706 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003707 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003708 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003709 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3710 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3711 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003712 s->rep->analysers = 0;
3713 buffer_auto_close(s->rep);
3714 buffer_auto_read(s->rep);
3715 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003716 buffer_abort(s->req);
3717 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003718 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003720 }
3721 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3722 txn->rsp.msg_state == HTTP_MSG_DONE &&
3723 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3724 /* server-close: terminate this server connection and
3725 * reinitialize a fresh-new transaction.
3726 */
3727 http_end_txn_clean_session(s);
3728 }
3729
3730 http_silent_debug(__LINE__, s);
3731 return txn->req.msg_state != old_req_state ||
3732 txn->rsp.msg_state != old_res_state;
3733}
3734
Willy Tarreaud98cf932009-12-27 22:54:55 +01003735/* This function is an analyser which forwards request body (including chunk
3736 * sizes if any). It is called as soon as we must forward, even if we forward
3737 * zero byte. The only situation where it must not be called is when we're in
3738 * tunnel mode and we want to forward till the close. It's used both to forward
3739 * remaining data and to resync after end of body. It expects the msg_state to
3740 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3741 * read more data, or 1 once we can go on with next request or end the session.
3742 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3743 * bytes of pending data + the headers if not already done (between som and sov).
3744 * It eventually adjusts som to match sov after the data in between have been sent.
3745 */
3746int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3747{
3748 struct http_txn *txn = &s->txn;
3749 struct http_msg *msg = &s->txn.req;
3750
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003751 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3752 return 0;
3753
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003754 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3755 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3756 /* Output closed while we were sending data. We must abort. */
3757 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003758 buffer_auto_read(req);
3759 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003760 req->analysers &= ~an_bit;
3761 return 1;
3762 }
3763
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003764 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003765
3766 /* Note that we don't have to send 100-continue back because we don't
3767 * need the data to complete our job, and it's up to the server to
3768 * decide whether to return 100, 417 or anything else in return of
3769 * an "Expect: 100-continue" header.
3770 */
3771
3772 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3773 /* we have msg->col and msg->sov which both point to the first
3774 * byte of message body. msg->som still points to the beginning
3775 * of the message. We must save the body in req->lr because it
3776 * survives buffer re-alignments.
3777 */
3778 req->lr = req->data + msg->sov;
3779 if (txn->flags & TX_REQ_TE_CHNK)
3780 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3781 else {
3782 msg->msg_state = HTTP_MSG_DATA;
3783 }
3784 }
3785
Willy Tarreaud98cf932009-12-27 22:54:55 +01003786 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003788 /* we may have some data pending */
3789 if (msg->hdr_content_len || msg->som != msg->sov) {
3790 int bytes = msg->sov - msg->som;
3791 if (bytes < 0) /* sov may have wrapped at the end */
3792 bytes += req->size;
3793 buffer_forward(req, bytes + msg->hdr_content_len);
3794 msg->hdr_content_len = 0; /* don't forward that again */
3795 msg->som = msg->sov;
3796 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003797
Willy Tarreaucaabe412010-01-03 23:08:28 +01003798 if (msg->msg_state == HTTP_MSG_DATA) {
3799 /* must still forward */
3800 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003801 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003802
3803 /* nothing left to forward */
3804 if (txn->flags & TX_REQ_TE_CHNK)
3805 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003806 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003807 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003808 }
3809 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003810 /* read the chunk size and assign it to ->hdr_content_len, then
3811 * set ->sov and ->lr to point to the body and switch to DATA or
3812 * TRAILERS state.
3813 */
3814 int ret = http_parse_chunk_size(req, msg);
3815
3816 if (!ret)
3817 goto missing_data;
3818 else if (ret < 0)
3819 goto return_bad_req;
3820 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003821 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003822 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3823 /* we want the CRLF after the data */
3824 int ret;
3825
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003826 req->lr = req->w + req->send_max;
3827 if (req->lr >= req->data + req->size)
3828 req->lr -= req->size;
3829
Willy Tarreaud98cf932009-12-27 22:54:55 +01003830 ret = http_skip_chunk_crlf(req, msg);
3831
3832 if (ret == 0)
3833 goto missing_data;
3834 else if (ret < 0)
3835 goto return_bad_req;
3836 /* we're in MSG_CHUNK_SIZE now */
3837 }
3838 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3839 int ret = http_forward_trailers(req, msg);
3840
3841 if (ret == 0)
3842 goto missing_data;
3843 else if (ret < 0)
3844 goto return_bad_req;
3845 /* we're in HTTP_MSG_DONE now */
3846 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003847 else {
3848 /* other states, DONE...TUNNEL */
3849 if (http_resync_states(s)) {
3850 /* some state changes occurred, maybe the analyser
3851 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003852 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3854 goto return_bad_req;
3855 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003856 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003857 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003858 }
3859 }
3860
Willy Tarreaud98cf932009-12-27 22:54:55 +01003861 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003862 /* stop waiting for data if the input is closed before the end */
3863 if (req->flags & BF_SHUTR)
3864 goto return_bad_req;
3865
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003866 /* waiting for the last bits to leave the buffer */
3867 if (req->flags & BF_SHUTW)
3868 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003869
3870 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003871 return 0;
3872
Willy Tarreaud98cf932009-12-27 22:54:55 +01003873 return_bad_req: /* let's centralize all bad requests */
3874 txn->req.msg_state = HTTP_MSG_ERROR;
3875 txn->status = 400;
3876 /* Note: we don't send any error if some data were already sent */
3877 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3878
Willy Tarreau90deb182010-01-07 00:20:41 +01003879 buffer_auto_read(req);
3880 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003881 req->analysers = 0;
3882 s->fe->counters.failed_req++;
3883 if (s->listener->counters)
3884 s->listener->counters->failed_req++;
3885
3886 if (!(s->flags & SN_ERR_MASK))
3887 s->flags |= SN_ERR_PRXCOND;
3888 if (!(s->flags & SN_FINST_MASK))
3889 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003890 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003891 return 0;
3892}
3893
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003894/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3895 * processing can continue on next analysers, or zero if it either needs more
3896 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3897 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3898 * when it has nothing left to do, and may remove any analyser when it wants to
3899 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003900 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003901int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003902{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003903 struct http_txn *txn = &s->txn;
3904 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003905 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003906 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003907 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003908 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003909
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003910 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 +02003911 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003912 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003913 rep,
3914 rep->rex, rep->wex,
3915 rep->flags,
3916 rep->l,
3917 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003918
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003919 /*
3920 * Now parse the partial (or complete) lines.
3921 * We will check the response syntax, and also join multi-line
3922 * headers. An index of all the lines will be elaborated while
3923 * parsing.
3924 *
3925 * For the parsing, we use a 28 states FSM.
3926 *
3927 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003928 * rep->data + msg->som = beginning of response
3929 * rep->data + msg->eoh = end of processed headers / start of current one
3930 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003931 * rep->lr = first non-visited byte
3932 * rep->r = end of data
3933 */
3934
Willy Tarreau83e3af02009-12-28 17:39:57 +01003935 /* There's a protected area at the end of the buffer for rewriting
3936 * purposes. We don't want to start to parse the request if the
3937 * protected area is affected, because we may have to move processed
3938 * data later, which is much more complicated.
3939 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003940 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3941 if (unlikely((rep->flags & BF_FULL) ||
3942 rep->r < rep->lr ||
3943 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3944 if (rep->send_max) {
3945 /* some data has still not left the buffer, wake us once that's done */
3946 buffer_dont_close(rep);
3947 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3948 return 0;
3949 }
3950 if (rep->l <= rep->size - global.tune.maxrewrite)
3951 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003952 }
3953
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003954 if (likely(rep->lr < rep->r))
3955 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003956 }
3957
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003958 /* 1: we might have to print this header in debug mode */
3959 if (unlikely((global.mode & MODE_DEBUG) &&
3960 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003961 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003962 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003963
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003964 sol = rep->data + msg->som;
3965 eol = sol + msg->sl.rq.l;
3966 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003967
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003968 sol += hdr_idx_first_pos(&txn->hdr_idx);
3969 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003970
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003971 while (cur_idx) {
3972 eol = sol + txn->hdr_idx.v[cur_idx].len;
3973 debug_hdr("srvhdr", s, sol, eol);
3974 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3975 cur_idx = txn->hdr_idx.v[cur_idx].next;
3976 }
3977 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003978
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003979 /*
3980 * Now we quickly check if we have found a full valid response.
3981 * If not so, we check the FD and buffer states before leaving.
3982 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003983 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003984 * responses are checked first.
3985 *
3986 * Depending on whether the client is still there or not, we
3987 * may send an error response back or not. Note that normally
3988 * we should only check for HTTP status there, and check I/O
3989 * errors somewhere else.
3990 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003991
Willy Tarreau655dce92009-11-08 13:10:58 +01003992 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003993 /* Invalid response */
3994 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3995 /* we detected a parsing error. We want to archive this response
3996 * in the dedicated proxy area for later troubleshooting.
3997 */
3998 hdr_response_bad:
3999 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4000 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4001
4002 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004003 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004004 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004005 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4006 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004007
Willy Tarreau90deb182010-01-07 00:20:41 +01004008 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004009 rep->analysers = 0;
4010 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004011 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004012 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4013
4014 if (!(s->flags & SN_ERR_MASK))
4015 s->flags |= SN_ERR_PRXCOND;
4016 if (!(s->flags & SN_FINST_MASK))
4017 s->flags |= SN_FINST_H;
4018
4019 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004020 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004021
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004022 /* too large response does not fit in buffer. */
4023 else if (rep->flags & BF_FULL) {
4024 goto hdr_response_bad;
4025 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004026
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004027 /* read error */
4028 else if (rep->flags & BF_READ_ERROR) {
4029 if (msg->err_pos >= 0)
4030 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004031
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004032 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004033 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004034 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004035 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4036 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004037
Willy Tarreau90deb182010-01-07 00:20:41 +01004038 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004039 rep->analysers = 0;
4040 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004041 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004042 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004043
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004044 if (!(s->flags & SN_ERR_MASK))
4045 s->flags |= SN_ERR_SRVCL;
4046 if (!(s->flags & SN_FINST_MASK))
4047 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004048 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004049 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004050
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004051 /* read timeout : return a 504 to the client. */
4052 else if (rep->flags & BF_READ_TIMEOUT) {
4053 if (msg->err_pos >= 0)
4054 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004055
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004056 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004057 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004058 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004059 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4060 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004061
Willy Tarreau90deb182010-01-07 00:20:41 +01004062 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004063 rep->analysers = 0;
4064 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004065 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004066 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004068 if (!(s->flags & SN_ERR_MASK))
4069 s->flags |= SN_ERR_SRVTO;
4070 if (!(s->flags & SN_FINST_MASK))
4071 s->flags |= SN_FINST_H;
4072 return 0;
4073 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004074
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004075 /* close from server */
4076 else if (rep->flags & BF_SHUTR) {
4077 if (msg->err_pos >= 0)
4078 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004080 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004081 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004082 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004083 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4084 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004085
Willy Tarreau90deb182010-01-07 00:20:41 +01004086 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004087 rep->analysers = 0;
4088 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004089 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004090 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004091
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004092 if (!(s->flags & SN_ERR_MASK))
4093 s->flags |= SN_ERR_SRVCL;
4094 if (!(s->flags & SN_FINST_MASK))
4095 s->flags |= SN_FINST_H;
4096 return 0;
4097 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004098
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004099 /* write error to client (we don't send any message then) */
4100 else if (rep->flags & BF_WRITE_ERROR) {
4101 if (msg->err_pos >= 0)
4102 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004104 s->be->counters.failed_resp++;
4105 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004106 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004107
4108 if (!(s->flags & SN_ERR_MASK))
4109 s->flags |= SN_ERR_CLICL;
4110 if (!(s->flags & SN_FINST_MASK))
4111 s->flags |= SN_FINST_H;
4112
4113 /* process_session() will take care of the error */
4114 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004115 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004116
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004117 buffer_dont_close(rep);
4118 return 0;
4119 }
4120
4121 /* More interesting part now : we know that we have a complete
4122 * response which at least looks like HTTP. We have an indicator
4123 * of each header's length, so we can parse them quickly.
4124 */
4125
4126 if (unlikely(msg->err_pos >= 0))
4127 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4128
4129 /* ensure we keep this pointer to the beginning of the message */
4130 msg->sol = rep->data + msg->som;
4131
4132 /*
4133 * 1: get the status code
4134 */
4135 n = rep->data[msg->sl.st.c] - '0';
4136 if (n < 1 || n > 5)
4137 n = 0;
4138 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004139
Willy Tarreau5b154472009-12-21 20:11:07 +01004140 /* check if the response is HTTP/1.1 or above */
4141 if ((msg->sl.st.v_l == 8) &&
4142 ((rep->data[msg->som + 5] > '1') ||
4143 ((rep->data[msg->som + 5] == '1') &&
4144 (rep->data[msg->som + 7] >= '1'))))
4145 txn->flags |= TX_RES_VER_11;
4146
4147 /* "connection" has not been parsed yet */
4148 txn->flags &= ~TX_CON_HDR_PARS;
4149
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004150 /* transfer length unknown*/
4151 txn->flags &= ~TX_RES_XFER_LEN;
4152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004153 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
4154
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004155 if (txn->status >= 100 && txn->status < 500)
4156 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4157 else
4158 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4159
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004160 /*
4161 * 2: check for cacheability.
4162 */
4163
4164 switch (txn->status) {
4165 case 200:
4166 case 203:
4167 case 206:
4168 case 300:
4169 case 301:
4170 case 410:
4171 /* RFC2616 @13.4:
4172 * "A response received with a status code of
4173 * 200, 203, 206, 300, 301 or 410 MAY be stored
4174 * by a cache (...) unless a cache-control
4175 * directive prohibits caching."
4176 *
4177 * RFC2616 @9.5: POST method :
4178 * "Responses to this method are not cacheable,
4179 * unless the response includes appropriate
4180 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004181 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004182 if (likely(txn->meth != HTTP_METH_POST) &&
4183 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4184 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4185 break;
4186 default:
4187 break;
4188 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004189
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004190 /*
4191 * 3: we may need to capture headers
4192 */
4193 s->logs.logwait &= ~LW_RESP;
4194 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
4195 capture_headers(rep->data + msg->som, &txn->hdr_idx,
4196 txn->rsp.cap, s->fe->rsp_cap);
4197
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004198 /* 4: determine the transfer-length.
4199 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4200 * the presence of a message-body in a RESPONSE and its transfer length
4201 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004202 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004203 * All responses to the HEAD request method MUST NOT include a
4204 * message-body, even though the presence of entity-header fields
4205 * might lead one to believe they do. All 1xx (informational), 204
4206 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4207 * message-body. All other responses do include a message-body,
4208 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004209 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004210 * 1. Any response which "MUST NOT" include a message-body (such as the
4211 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4212 * always terminated by the first empty line after the header fields,
4213 * regardless of the entity-header fields present in the message.
4214 *
4215 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4216 * the "chunked" transfer-coding (Section 6.2) is used, the
4217 * transfer-length is defined by the use of this transfer-coding.
4218 * If a Transfer-Encoding header field is present and the "chunked"
4219 * transfer-coding is not present, the transfer-length is defined by
4220 * the sender closing the connection.
4221 *
4222 * 3. If a Content-Length header field is present, its decimal value in
4223 * OCTETs represents both the entity-length and the transfer-length.
4224 * If a message is received with both a Transfer-Encoding header
4225 * field and a Content-Length header field, the latter MUST be ignored.
4226 *
4227 * 4. If the message uses the media type "multipart/byteranges", and
4228 * the transfer-length is not otherwise specified, then this self-
4229 * delimiting media type defines the transfer-length. This media
4230 * type MUST NOT be used unless the sender knows that the recipient
4231 * can parse it; the presence in a request of a Range header with
4232 * multiple byte-range specifiers from a 1.1 client implies that the
4233 * client can parse multipart/byteranges responses.
4234 *
4235 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004236 */
4237
4238 /* Skip parsing if no content length is possible. The response flags
4239 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004240 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004241 * FIXME: should we parse anyway and return an error on chunked encoding ?
4242 */
4243 if (txn->meth == HTTP_METH_HEAD ||
4244 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004245 txn->status == 204 || txn->status == 304) {
4246 txn->flags |= TX_RES_XFER_LEN;
4247 goto skip_content_length;
4248 }
4249
4250 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004251 goto skip_content_length;
4252
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004253 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004254 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004255 while ((txn->flags & TX_RES_VER_11) &&
4256 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004257 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4258 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4259 else if (txn->flags & TX_RES_TE_CHNK) {
4260 /* bad transfer-encoding (chunked followed by something else) */
4261 use_close_only = 1;
4262 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4263 break;
4264 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004265 }
4266
4267 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4268 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004269 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004270 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4271 signed long long cl;
4272
4273 if (!ctx.vlen)
4274 goto hdr_response_bad;
4275
4276 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4277 goto hdr_response_bad; /* parse failure */
4278
4279 if (cl < 0)
4280 goto hdr_response_bad;
4281
4282 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4283 goto hdr_response_bad; /* already specified, was different */
4284
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004285 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004286 msg->hdr_content_len = cl;
4287 }
4288
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004289 /* FIXME: we should also implement the multipart/byterange method.
4290 * For now on, we resort to close mode in this case (unknown length).
4291 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004292skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004293
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004294 /* end of job, return OK */
4295 rep->analysers &= ~an_bit;
4296 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004297 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004298 return 1;
4299}
4300
4301/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004302 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4303 * and updates t->rep->analysers. It might make sense to explode it into several
4304 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004305 */
4306int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4307{
4308 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004309 struct http_msg *msg = &txn->rsp;
4310 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004311 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004312 int conn_ka = 0, conn_cl = 0;
4313 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004314 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004315
4316 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4317 now_ms, __FUNCTION__,
4318 t,
4319 rep,
4320 rep->rex, rep->wex,
4321 rep->flags,
4322 rep->l,
4323 rep->analysers);
4324
Willy Tarreau655dce92009-11-08 13:10:58 +01004325 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004326 return 0;
4327
4328 rep->analysers &= ~an_bit;
4329 rep->analyse_exp = TICK_ETERNITY;
4330
Willy Tarreau5b154472009-12-21 20:11:07 +01004331 /* Now we have to check if we need to modify the Connection header.
4332 * This is more difficult on the response than it is on the request,
4333 * because we can have two different HTTP versions and we don't know
4334 * how the client will interprete a response. For instance, let's say
4335 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4336 * HTTP/1.1 response without any header. Maybe it will bound itself to
4337 * HTTP/1.0 because it only knows about it, and will consider the lack
4338 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4339 * the lack of header as a keep-alive. Thus we will use two flags
4340 * indicating how a request MAY be understood by the client. In case
4341 * of multiple possibilities, we'll fix the header to be explicit. If
4342 * ambiguous cases such as both close and keepalive are seen, then we
4343 * will fall back to explicit close. Note that we won't take risks with
4344 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4345 */
4346
4347 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004348 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4349 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4350 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004351 int may_keep = 0, may_close = 0; /* how it may be understood */
4352 struct hdr_ctx ctx;
4353
4354 ctx.idx = 0;
4355 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4356 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4357 conn_cl = 1;
4358 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4359 conn_ka = 1;
4360 }
4361
4362 if (conn_cl) {
4363 /* close header present */
4364 may_close = 1;
4365 if (conn_ka) /* we have both close and keep-alive */
4366 may_keep = 1;
4367 }
4368 else if (conn_ka) {
4369 /* keep-alive alone */
4370 may_keep = 1;
4371 }
4372 else {
4373 /* no close nor keep-alive header */
4374 if (txn->flags & TX_RES_VER_11)
4375 may_keep = 1;
4376 else
4377 may_close = 1;
4378
4379 if (txn->flags & TX_REQ_VER_11)
4380 may_keep = 1;
4381 else
4382 may_close = 1;
4383 }
4384
4385 /* let's update the transaction status to reflect any close.
4386 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004387 * handled. We also explicitly state that we will close in
4388 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004389 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004390 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004391 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4392 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004393 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4394
4395 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004396 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004397 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4398 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004399 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004400 if (may_keep &&
4401 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4402 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004403 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004404 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4405 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4406 must_del_close = 1;
4407 if (!(txn->flags & TX_REQ_VER_11))
4408 must_keep = 1;
4409 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004410
4411 txn->flags |= TX_CON_HDR_PARS;
4412 }
4413
4414 /* We might have to check for "Connection:" if the server
4415 * returns a connection status that is not compatible with
4416 * the client's or with the config.
4417 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004418 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004419 char *cur_ptr, *cur_end, *cur_next;
4420 int cur_idx, old_idx, delta, val;
4421 int must_delete;
4422 struct hdr_idx_elem *cur_hdr;
4423
4424 /* we just have to remove the headers if both sides are 1.0 */
4425 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004426
4427 /* same if we want to re-enable keep-alive on 1.1 */
4428 must_delete |= must_del_close;
4429
Willy Tarreau5b154472009-12-21 20:11:07 +01004430 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4431
4432 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4433 cur_hdr = &txn->hdr_idx.v[cur_idx];
4434 cur_ptr = cur_next;
4435 cur_end = cur_ptr + cur_hdr->len;
4436 cur_next = cur_end + cur_hdr->cr + 1;
4437
4438 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4439 if (!val)
4440 continue;
4441
4442 /* 3 possibilities :
4443 * - we have already set "Connection: close" or we're in
4444 * HTTP/1.0, so we remove this line.
4445 * - we have not yet set "Connection: close", but this line
4446 * indicates close. We leave it untouched and set the flag.
4447 * - we have not yet set "Connection: close", and this line
4448 * indicates non-close. We replace it and set the flag.
4449 */
4450 if (must_delete) {
4451 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4452 http_msg_move_end(&txn->rsp, delta);
4453 cur_next += delta;
4454 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4455 txn->hdr_idx.used--;
4456 cur_hdr->len = 0;
4457 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004458 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004459 } else {
4460 if (cur_end - cur_ptr - val != 5 ||
4461 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4462 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4463 "close", 5);
4464 cur_next += delta;
4465 cur_hdr->len += delta;
4466 http_msg_move_end(&txn->rsp, delta);
4467 }
4468 must_delete = 1;
4469 must_close = 0;
4470 }
4471 } /* for loop */
4472 } /* if must close keep-alive */
4473
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004474 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004475 /*
4476 * 3: we will have to evaluate the filters.
4477 * As opposed to version 1.2, now they will be evaluated in the
4478 * filters order and not in the header order. This means that
4479 * each filter has to be validated among all headers.
4480 *
4481 * Filters are tried with ->be first, then with ->fe if it is
4482 * different from ->be.
4483 */
4484
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004485 cur_proxy = t->be;
4486 while (1) {
4487 struct proxy *rule_set = cur_proxy;
4488
4489 /* try headers filters */
4490 if (rule_set->rsp_exp != NULL) {
4491 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4492 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004493 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004494 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004495 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4496 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004497 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004498 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004499 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004500 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004501 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004502 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004503 if (!(t->flags & SN_ERR_MASK))
4504 t->flags |= SN_ERR_PRXCOND;
4505 if (!(t->flags & SN_FINST_MASK))
4506 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004507 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004508 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004509 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004510
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004511 /* has the response been denied ? */
4512 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004513 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004514 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004515
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004516 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004517 if (t->listener->counters)
4518 t->listener->counters->denied_resp++;
4519
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004520 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004521 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004522
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004523 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004524 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004525 if (txn->status < 200)
4526 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004527 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004528 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004529 }
4530
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004531 /* check whether we're already working on the frontend */
4532 if (cur_proxy == t->fe)
4533 break;
4534 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004535 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004536
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004537 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004538 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4539 * in which case this is not the right response, and we're waiting for the
4540 * next one. Let's allow this response to go to the client and wait for the
4541 * next one.
4542 */
4543 if (txn->status < 200) {
4544 hdr_idx_init(&txn->hdr_idx);
4545 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4546 msg->msg_state = HTTP_MSG_RPBEFORE;
4547 txn->status = 0;
4548 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4549 return 1;
4550 }
4551
4552 /* we don't have any 1xx status code now */
4553
4554 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004555 * 4: check for server cookie.
4556 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004557 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4558 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004559 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004560
Willy Tarreaubaaee002006-06-26 02:48:02 +02004561
Willy Tarreaua15645d2007-03-18 16:22:39 +01004562 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004563 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004564 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004565 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004566 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004567
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004568 /*
4569 * 6: add server cookie in the response if needed
4570 */
4571 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004572 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004573 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004574
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004575 /* the server is known, it's not the one the client requested, we have to
4576 * insert a set-cookie here, except if we want to insert only on POST
4577 * requests and this one isn't. Note that servers which don't have cookies
4578 * (eg: some backup servers) will return a full cookie removal request.
4579 */
4580 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4581 t->be->cookie_name,
4582 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004583
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584 if (t->be->cookie_domain)
4585 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004586
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004587 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004588 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589 goto return_bad_resp;
4590 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004591
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004592 /* Here, we will tell an eventual cache on the client side that we don't
4593 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4594 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4595 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4596 */
4597 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004598
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004599 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4600
4601 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004602 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004603 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004604 }
4605 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004606
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004607 /*
4608 * 7: check if result will be cacheable with a cookie.
4609 * We'll block the response if security checks have caught
4610 * nasty things such as a cacheable cookie.
4611 */
4612 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4613 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004614 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004615
4616 /* we're in presence of a cacheable response containing
4617 * a set-cookie header. We'll block it as requested by
4618 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004619 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004620 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004621 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004622
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004623 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004624 if (t->listener->counters)
4625 t->listener->counters->denied_resp++;
4626
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004627 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4628 t->be->id, t->srv?t->srv->id:"<dispatch>");
4629 send_log(t->be, LOG_ALERT,
4630 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4631 t->be->id, t->srv?t->srv->id:"<dispatch>");
4632 goto return_srv_prx_502;
4633 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004634
4635 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004636 * 8: add "Connection: close" if needed and not yet set. This is
4637 * only needed for 1.1 responses since we know there is no other
4638 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004639 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004640 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004641 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004642 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004643 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004644 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004645 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004646 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4647 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4648 "Connection: keep-alive", 22) < 0))
4649 goto return_bad_resp;
4650 must_keep = 0;
4651 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004652
Willy Tarreaud98cf932009-12-27 22:54:55 +01004653 if (txn->flags & TX_RES_XFER_LEN)
4654 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004655
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004656 /*************************************************************
4657 * OK, that's finished for the headers. We have done what we *
4658 * could. Let's switch to the DATA state. *
4659 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004660
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004661 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004662
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004663 /* if the user wants to log as soon as possible, without counting
4664 * bytes from the server, then this is the right moment. We have
4665 * to temporarily assign bytes_out to log what we currently have.
4666 */
4667 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4668 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4669 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004670 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004671 t->logs.bytes_out = 0;
4672 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004673
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004674 /* Note: we must not try to cheat by jumping directly to DATA,
4675 * otherwise we would not let the client side wake up.
4676 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004677
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004678 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004679 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004680 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004681}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004682
Willy Tarreaud98cf932009-12-27 22:54:55 +01004683/* This function is an analyser which forwards response body (including chunk
4684 * sizes if any). It is called as soon as we must forward, even if we forward
4685 * zero byte. The only situation where it must not be called is when we're in
4686 * tunnel mode and we want to forward till the close. It's used both to forward
4687 * remaining data and to resync after end of body. It expects the msg_state to
4688 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4689 * read more data, or 1 once we can go on with next request or end the session.
4690 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4691 * bytes of pending data + the headers if not already done (between som and sov).
4692 * It eventually adjusts som to match sov after the data in between have been sent.
4693 */
4694int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4695{
4696 struct http_txn *txn = &s->txn;
4697 struct http_msg *msg = &s->txn.rsp;
4698
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004699 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4700 return 0;
4701
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004702 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004703 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004704 !s->req->analysers) {
4705 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4706 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004707 buffer_auto_read(res);
4708 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004709 res->analysers &= ~an_bit;
4710 return 1;
4711 }
4712
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004713 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004714
Willy Tarreaud98cf932009-12-27 22:54:55 +01004715 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4716 /* we have msg->col and msg->sov which both point to the first
4717 * byte of message body. msg->som still points to the beginning
4718 * of the message. We must save the body in req->lr because it
4719 * survives buffer re-alignments.
4720 */
4721 res->lr = res->data + msg->sov;
4722 if (txn->flags & TX_RES_TE_CHNK)
4723 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4724 else {
4725 msg->msg_state = HTTP_MSG_DATA;
4726 }
4727 }
4728
Willy Tarreaud98cf932009-12-27 22:54:55 +01004729 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004731 /* we may have some data pending */
4732 if (msg->hdr_content_len || msg->som != msg->sov) {
4733 int bytes = msg->sov - msg->som;
4734 if (bytes < 0) /* sov may have wrapped at the end */
4735 bytes += res->size;
4736 buffer_forward(res, bytes + msg->hdr_content_len);
4737 msg->hdr_content_len = 0; /* don't forward that again */
4738 msg->som = msg->sov;
4739 }
4740
Willy Tarreaucaabe412010-01-03 23:08:28 +01004741 if (msg->msg_state == HTTP_MSG_DATA) {
4742 /* must still forward */
4743 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004744 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004745
4746 /* nothing left to forward */
4747 if (txn->flags & TX_RES_TE_CHNK)
4748 msg->msg_state = HTTP_MSG_DATA_CRLF;
4749 else
4750 msg->msg_state = HTTP_MSG_DONE;
4751 }
4752 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004753 /* read the chunk size and assign it to ->hdr_content_len, then
4754 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4755 */
4756 int ret = http_parse_chunk_size(res, msg);
4757
4758 if (!ret)
4759 goto missing_data;
4760 else if (ret < 0)
4761 goto return_bad_res;
4762 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4765 /* we want the CRLF after the data */
4766 int ret;
4767
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004768 res->lr = res->w + res->send_max;
4769 if (res->lr >= res->data + res->size)
4770 res->lr -= res->size;
4771
Willy Tarreaud98cf932009-12-27 22:54:55 +01004772 ret = http_skip_chunk_crlf(res, msg);
4773
4774 if (!ret)
4775 goto missing_data;
4776 else if (ret < 0)
4777 goto return_bad_res;
4778 /* we're in MSG_CHUNK_SIZE now */
4779 }
4780 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4781 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004782
Willy Tarreaud98cf932009-12-27 22:54:55 +01004783 if (ret == 0)
4784 goto missing_data;
4785 else if (ret < 0)
4786 goto return_bad_res;
4787 /* we're in HTTP_MSG_DONE now */
4788 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004789 else {
4790 /* other states, DONE...TUNNEL */
4791 if (http_resync_states(s)) {
4792 http_silent_debug(__LINE__, s);
4793 /* some state changes occurred, maybe the analyser
4794 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004795 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004796 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4797 goto return_bad_res;
4798 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004799 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004801 }
4802 }
4803
Willy Tarreaud98cf932009-12-27 22:54:55 +01004804 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004805 /* stop waiting for data if the input is closed before the end */
4806 if (res->flags & BF_SHUTR)
4807 goto return_bad_res;
4808
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809 if (!s->req->analysers)
4810 goto return_bad_res;
4811
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812 /* forward the chunk size as well as any pending data */
4813 if (msg->hdr_content_len || msg->som != msg->sov) {
4814 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4815 msg->hdr_content_len = 0; /* don't forward that again */
4816 msg->som = msg->sov;
4817 }
4818
Willy Tarreaud98cf932009-12-27 22:54:55 +01004819 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004820 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821 return 0;
4822
4823 return_bad_res: /* let's centralize all bad resuests */
4824 txn->rsp.msg_state = HTTP_MSG_ERROR;
4825 txn->status = 502;
4826 stream_int_cond_close(res->cons, NULL);
4827
Willy Tarreau90deb182010-01-07 00:20:41 +01004828 buffer_auto_close(res);
4829 buffer_auto_read(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004830 res->analysers = 0;
4831 s->be->counters.failed_resp++;
4832 if (s->srv) {
4833 s->srv->counters.failed_resp++;
4834 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4835 }
4836
4837 if (!(s->flags & SN_ERR_MASK))
4838 s->flags |= SN_ERR_PRXCOND;
4839 if (!(s->flags & SN_FINST_MASK))
4840 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004841 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004842 return 0;
4843}
4844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004845/* Iterate the same filter through all request headers.
4846 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004847 * Since it can manage the switch to another backend, it updates the per-proxy
4848 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004849 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004850int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004851{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004852 char term;
4853 char *cur_ptr, *cur_end, *cur_next;
4854 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004855 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004856 struct hdr_idx_elem *cur_hdr;
4857 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004859 last_hdr = 0;
4860
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004861 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004862 old_idx = 0;
4863
4864 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004865 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004866 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004867 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004868 (exp->action == ACT_ALLOW ||
4869 exp->action == ACT_DENY ||
4870 exp->action == ACT_TARPIT))
4871 return 0;
4872
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004873 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004874 if (!cur_idx)
4875 break;
4876
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004877 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004878 cur_ptr = cur_next;
4879 cur_end = cur_ptr + cur_hdr->len;
4880 cur_next = cur_end + cur_hdr->cr + 1;
4881
4882 /* Now we have one header between cur_ptr and cur_end,
4883 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004884 */
4885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004886 /* The annoying part is that pattern matching needs
4887 * that we modify the contents to null-terminate all
4888 * strings before testing them.
4889 */
4890
4891 term = *cur_end;
4892 *cur_end = '\0';
4893
4894 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4895 switch (exp->action) {
4896 case ACT_SETBE:
4897 /* It is not possible to jump a second time.
4898 * FIXME: should we return an HTTP/500 here so that
4899 * the admin knows there's a problem ?
4900 */
4901 if (t->be != t->fe)
4902 break;
4903
4904 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004905 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004906 last_hdr = 1;
4907 break;
4908
4909 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004910 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004911 last_hdr = 1;
4912 break;
4913
4914 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004915 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004916 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004917
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004918 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004919 if (t->listener->counters)
4920 t->listener->counters->denied_resp++;
4921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004922 break;
4923
4924 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004925 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004926 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004927
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004928 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004929 if (t->listener->counters)
4930 t->listener->counters->denied_resp++;
4931
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004932 break;
4933
4934 case ACT_REPLACE:
4935 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4936 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4937 /* FIXME: if the user adds a newline in the replacement, the
4938 * index will not be recalculated for now, and the new line
4939 * will not be counted as a new header.
4940 */
4941
4942 cur_end += delta;
4943 cur_next += delta;
4944 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004945 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004946 break;
4947
4948 case ACT_REMOVE:
4949 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4950 cur_next += delta;
4951
Willy Tarreaufa355d42009-11-29 18:12:29 +01004952 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004953 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4954 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004955 cur_hdr->len = 0;
4956 cur_end = NULL; /* null-term has been rewritten */
4957 break;
4958
4959 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004960 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004961 if (cur_end)
4962 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004963
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004964 /* keep the link from this header to next one in case of later
4965 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004966 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004967 old_idx = cur_idx;
4968 }
4969 return 0;
4970}
4971
4972
4973/* Apply the filter to the request line.
4974 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4975 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004976 * Since it can manage the switch to another backend, it updates the per-proxy
4977 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004978 */
4979int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4980{
4981 char term;
4982 char *cur_ptr, *cur_end;
4983 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004984 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004985 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004986
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987
Willy Tarreau3d300592007-03-18 18:34:41 +01004988 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004989 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004990 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004991 (exp->action == ACT_ALLOW ||
4992 exp->action == ACT_DENY ||
4993 exp->action == ACT_TARPIT))
4994 return 0;
4995 else if (exp->action == ACT_REMOVE)
4996 return 0;
4997
4998 done = 0;
4999
Willy Tarreau9cdde232007-05-02 20:58:19 +02005000 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005001 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005002
5003 /* Now we have the request line between cur_ptr and cur_end */
5004
5005 /* The annoying part is that pattern matching needs
5006 * that we modify the contents to null-terminate all
5007 * strings before testing them.
5008 */
5009
5010 term = *cur_end;
5011 *cur_end = '\0';
5012
5013 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5014 switch (exp->action) {
5015 case ACT_SETBE:
5016 /* It is not possible to jump a second time.
5017 * FIXME: should we return an HTTP/500 here so that
5018 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005019 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005020 if (t->be != t->fe)
5021 break;
5022
5023 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005024 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005025 done = 1;
5026 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005027
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005028 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005029 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005030 done = 1;
5031 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005032
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005033 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005034 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005035
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005036 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005037 if (t->listener->counters)
5038 t->listener->counters->denied_resp++;
5039
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005040 done = 1;
5041 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005043 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005044 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005045
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005046 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005047 if (t->listener->counters)
5048 t->listener->counters->denied_resp++;
5049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005050 done = 1;
5051 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005052
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005053 case ACT_REPLACE:
5054 *cur_end = term; /* restore the string terminator */
5055 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5056 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5057 /* FIXME: if the user adds a newline in the replacement, the
5058 * index will not be recalculated for now, and the new line
5059 * will not be counted as a new header.
5060 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005061
Willy Tarreaufa355d42009-11-29 18:12:29 +01005062 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005063 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005064
Willy Tarreau9cdde232007-05-02 20:58:19 +02005065 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005066 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005067 HTTP_MSG_RQMETH,
5068 cur_ptr, cur_end + 1,
5069 NULL, NULL);
5070 if (unlikely(!cur_end))
5071 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005073 /* we have a full request and we know that we have either a CR
5074 * or an LF at <ptr>.
5075 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005076 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5077 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005078 /* there is no point trying this regex on headers */
5079 return 1;
5080 }
5081 }
5082 *cur_end = term; /* restore the string terminator */
5083 return done;
5084}
Willy Tarreau97de6242006-12-27 17:18:38 +01005085
Willy Tarreau58f10d72006-12-04 02:26:12 +01005086
Willy Tarreau58f10d72006-12-04 02:26:12 +01005087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005088/*
5089 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5090 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005091 * unparsable request. Since it can manage the switch to another backend, it
5092 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005093 */
5094int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5095{
Willy Tarreau3d300592007-03-18 18:34:41 +01005096 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005097 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005098 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005099 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005100
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005101 /*
5102 * The interleaving of transformations and verdicts
5103 * makes it difficult to decide to continue or stop
5104 * the evaluation.
5105 */
5106
Willy Tarreau3d300592007-03-18 18:34:41 +01005107 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005108 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5109 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5110 exp = exp->next;
5111 continue;
5112 }
5113
5114 /* Apply the filter to the request line. */
5115 ret = apply_filter_to_req_line(t, req, exp);
5116 if (unlikely(ret < 0))
5117 return -1;
5118
5119 if (likely(ret == 0)) {
5120 /* The filter did not match the request, it can be
5121 * iterated through all headers.
5122 */
5123 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005124 }
5125 exp = exp->next;
5126 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005128}
5129
5130
Willy Tarreaua15645d2007-03-18 16:22:39 +01005131
Willy Tarreau58f10d72006-12-04 02:26:12 +01005132/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005133 * Try to retrieve the server associated to the appsession.
5134 * If the server is found, it's assigned to the session.
5135 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005136void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005137 struct http_txn *txn = &t->txn;
5138 appsess *asession = NULL;
5139 char *sessid_temp = NULL;
5140
Cyril Bontéb21570a2009-11-29 20:04:48 +01005141 if (len > t->be->appsession_len) {
5142 len = t->be->appsession_len;
5143 }
5144
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005145 if (t->be->options2 & PR_O2_AS_REQL) {
5146 /* request-learn option is enabled : store the sessid in the session for future use */
5147 if (t->sessid != NULL) {
5148 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5149 pool_free2(apools.sessid, t->sessid);
5150 }
5151
5152 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5153 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5154 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5155 return;
5156 }
5157
Cyril Bontéb21570a2009-11-29 20:04:48 +01005158 memcpy(t->sessid, buf, len);
5159 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005160 }
5161
5162 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5163 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5164 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5165 return;
5166 }
5167
Cyril Bontéb21570a2009-11-29 20:04:48 +01005168 memcpy(sessid_temp, buf, len);
5169 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005170
5171 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5172 /* free previously allocated memory */
5173 pool_free2(apools.sessid, sessid_temp);
5174
5175 if (asession != NULL) {
5176 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5177 if (!(t->be->options2 & PR_O2_AS_REQL))
5178 asession->request_count++;
5179
5180 if (asession->serverid != NULL) {
5181 struct server *srv = t->be->srv;
5182 while (srv) {
5183 if (strcmp(srv->id, asession->serverid) == 0) {
5184 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5185 /* we found the server and it's usable */
5186 txn->flags &= ~TX_CK_MASK;
5187 txn->flags |= TX_CK_VALID;
5188 t->flags |= SN_DIRECT | SN_ASSIGNED;
5189 t->srv = srv;
5190 break;
5191 } else {
5192 txn->flags &= ~TX_CK_MASK;
5193 txn->flags |= TX_CK_DOWN;
5194 }
5195 }
5196 srv = srv->next;
5197 }
5198 }
5199 }
5200}
5201
5202/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005203 * Manage client-side cookie. It can impact performance by about 2% so it is
5204 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005205 */
5206void manage_client_side_cookies(struct session *t, struct buffer *req)
5207{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005208 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005209 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005210 char *del_colon, *del_cookie, *colon;
5211 int app_cookies;
5212
Willy Tarreau58f10d72006-12-04 02:26:12 +01005213 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005214 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005215
Willy Tarreau2a324282006-12-05 00:05:46 +01005216 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005217 * we start with the start line.
5218 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005219 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005220 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005221
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005222 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005223 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005224 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005225
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005226 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005227 cur_ptr = cur_next;
5228 cur_end = cur_ptr + cur_hdr->len;
5229 cur_next = cur_end + cur_hdr->cr + 1;
5230
5231 /* We have one full header between cur_ptr and cur_end, and the
5232 * next header starts at cur_next. We're only interested in
5233 * "Cookie:" headers.
5234 */
5235
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005236 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5237 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005238 old_idx = cur_idx;
5239 continue;
5240 }
5241
5242 /* Now look for cookies. Conforming to RFC2109, we have to support
5243 * attributes whose name begin with a '$', and associate them with
5244 * the right cookie, if we want to delete this cookie.
5245 * So there are 3 cases for each cookie read :
5246 * 1) it's a special attribute, beginning with a '$' : ignore it.
5247 * 2) it's a server id cookie that we *MAY* want to delete : save
5248 * some pointers on it (last semi-colon, beginning of cookie...)
5249 * 3) it's an application cookie : we *MAY* have to delete a previous
5250 * "special" cookie.
5251 * At the end of loop, if a "special" cookie remains, we may have to
5252 * remove it. If no application cookie persists in the header, we
5253 * *MUST* delete it
5254 */
5255
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005256 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005257
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258 /* del_cookie == NULL => nothing to be deleted */
5259 del_colon = del_cookie = NULL;
5260 app_cookies = 0;
5261
5262 while (p1 < cur_end) {
5263 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005264 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005265 while (p1 < cur_end) {
5266 if (*p1 == ';' || *p1 == ',')
5267 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005268 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005269 break;
5270 p1++;
5271 }
5272
5273 if (p1 == cur_end)
5274 break;
5275
5276 /* p1 is at the beginning of the cookie name */
5277 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005278 while (p2 < cur_end && *p2 != '=') {
5279 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5280 /* oops, the cookie name was truncated, resync */
5281 p1 = p2;
5282 goto resync_name;
5283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005284 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005285 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005286
5287 if (p2 == cur_end)
5288 break;
5289
5290 p3 = p2 + 1; /* skips the '=' sign */
5291 if (p3 == cur_end)
5292 break;
5293
Willy Tarreau305ae852010-01-03 19:45:54 +01005294 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5295 p5 = p4 = p3;
5296 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5297 if (!isspace((unsigned char)*p5))
5298 p4 = p5 + 1;
5299 p5++;
5300 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005301
5302 /* here, we have the cookie name between p1 and p2,
5303 * and its value between p3 and p4.
5304 * we can process it :
5305 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005306 * Cookie: NAME=VALUE ;
5307 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005308 * | || || +--> p4
5309 * | || |+-------> p3
5310 * | || +--------> p2
5311 * | |+------------> p1
5312 * | +-------------> colon
5313 * +--------------------> cur_ptr
5314 */
5315
5316 if (*p1 == '$') {
5317 /* skip this one */
5318 }
5319 else {
5320 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005321 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005322 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005323 (p4 - p1 >= t->fe->capture_namelen) &&
5324 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005325 int log_len = p4 - p1;
5326
Willy Tarreau086b3b42007-05-13 21:45:51 +02005327 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005328 Alert("HTTP logging : out of memory.\n");
5329 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005330 if (log_len > t->fe->capture_len)
5331 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005332 memcpy(txn->cli_cookie, p1, log_len);
5333 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005334 }
5335 }
5336
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005337 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5338 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005339 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005340 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005341 char *delim;
5342
5343 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5344 * have the server ID betweek p3 and delim, and the original cookie between
5345 * delim+1 and p4. Otherwise, delim==p4 :
5346 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005347 * Cookie: NAME=SRV~VALUE ;
5348 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005349 * | || || | +--> p4
5350 * | || || +--------> delim
5351 * | || |+-----------> p3
5352 * | || +------------> p2
5353 * | |+----------------> p1
5354 * | +-----------------> colon
5355 * +------------------------> cur_ptr
5356 */
5357
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005358 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005359 for (delim = p3; delim < p4; delim++)
5360 if (*delim == COOKIE_DELIM)
5361 break;
5362 }
5363 else
5364 delim = p4;
5365
5366
5367 /* Here, we'll look for the first running server which supports the cookie.
5368 * This allows to share a same cookie between several servers, for example
5369 * to dedicate backup servers to specific servers only.
5370 * However, to prevent clients from sticking to cookie-less backup server
5371 * when they have incidentely learned an empty cookie, we simply ignore
5372 * empty cookies and mark them as invalid.
5373 */
5374 if (delim == p3)
5375 srv = NULL;
5376
5377 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005378 if (srv->cookie && (srv->cklen == delim - p3) &&
5379 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005380 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005381 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005382 txn->flags &= ~TX_CK_MASK;
5383 txn->flags |= TX_CK_VALID;
5384 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005385 t->srv = srv;
5386 break;
5387 } else {
5388 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005389 txn->flags &= ~TX_CK_MASK;
5390 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005391 }
5392 }
5393 srv = srv->next;
5394 }
5395
Willy Tarreau3d300592007-03-18 18:34:41 +01005396 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005397 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005398 txn->flags &= ~TX_CK_MASK;
5399 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005400 }
5401
5402 /* depending on the cookie mode, we may have to either :
5403 * - delete the complete cookie if we're in insert+indirect mode, so that
5404 * the server never sees it ;
5405 * - remove the server id from the cookie value, and tag the cookie as an
5406 * application cookie so that it does not get accidentely removed later,
5407 * if we're in cookie prefix mode
5408 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005409 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005410 int delta; /* negative */
5411
5412 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5413 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005414 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005415 cur_end += delta;
5416 cur_next += delta;
5417 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005418 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005419
5420 del_cookie = del_colon = NULL;
5421 app_cookies++; /* protect the header from deletion */
5422 }
5423 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005424 (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 +01005425 del_cookie = p1;
5426 del_colon = colon;
5427 }
5428 } else {
5429 /* now we know that we must keep this cookie since it's
5430 * not ours. But if we wanted to delete our cookie
5431 * earlier, we cannot remove the complete header, but we
5432 * can remove the previous block itself.
5433 */
5434 app_cookies++;
5435
5436 if (del_cookie != NULL) {
5437 int delta; /* negative */
5438
5439 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5440 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005441 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442 cur_end += delta;
5443 cur_next += delta;
5444 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005445 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005446 del_cookie = del_colon = NULL;
5447 }
5448 }
5449
Cyril Bontéb21570a2009-11-29 20:04:48 +01005450 if (t->be->appsession_name != NULL) {
5451 int cmp_len, value_len;
5452 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005453
Cyril Bontéb21570a2009-11-29 20:04:48 +01005454 if (t->be->options2 & PR_O2_AS_PFX) {
5455 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5456 value_begin = p1 + t->be->appsession_name_len;
5457 value_len = p4 - p1 - t->be->appsession_name_len;
5458 } else {
5459 cmp_len = p2 - p1;
5460 value_begin = p3;
5461 value_len = p4 - p3;
5462 }
5463
5464 /* let's see if the cookie is our appcookie */
5465 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5466 /* Cool... it's the right one */
5467 manage_client_side_appsession(t, value_begin, value_len);
5468 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005469#if defined(DEBUG_HASH)
5470 Alert("manage_client_side_cookies\n");
5471 appsession_hash_dump(&(t->be->htbl_proxy));
5472#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005473 }/* end if ((t->proxy->appsession_name != NULL) ... */
5474 }
5475
5476 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005477 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005478 } /* while (p1 < cur_end) */
5479
5480 /* There's no more cookie on this line.
5481 * We may have marked the last one(s) for deletion.
5482 * We must do this now in two ways :
5483 * - if there is no app cookie, we simply delete the header ;
5484 * - if there are app cookies, we must delete the end of the
5485 * string properly, including the colon/semi-colon before
5486 * the cookie name.
5487 */
5488 if (del_cookie != NULL) {
5489 int delta;
5490 if (app_cookies) {
5491 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5492 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005493 cur_hdr->len += delta;
5494 } else {
5495 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005496
5497 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005498 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5499 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005500 cur_hdr->len = 0;
5501 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005502 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005503 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005504 }
5505
5506 /* keep the link from this header to next one */
5507 old_idx = cur_idx;
5508 } /* end of cookie processing on this header */
5509}
5510
5511
Willy Tarreaua15645d2007-03-18 16:22:39 +01005512/* Iterate the same filter through all response headers contained in <rtr>.
5513 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5514 */
5515int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5516{
5517 char term;
5518 char *cur_ptr, *cur_end, *cur_next;
5519 int cur_idx, old_idx, last_hdr;
5520 struct http_txn *txn = &t->txn;
5521 struct hdr_idx_elem *cur_hdr;
5522 int len, delta;
5523
5524 last_hdr = 0;
5525
5526 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5527 old_idx = 0;
5528
5529 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005530 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005531 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005532 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005533 (exp->action == ACT_ALLOW ||
5534 exp->action == ACT_DENY))
5535 return 0;
5536
5537 cur_idx = txn->hdr_idx.v[old_idx].next;
5538 if (!cur_idx)
5539 break;
5540
5541 cur_hdr = &txn->hdr_idx.v[cur_idx];
5542 cur_ptr = cur_next;
5543 cur_end = cur_ptr + cur_hdr->len;
5544 cur_next = cur_end + cur_hdr->cr + 1;
5545
5546 /* Now we have one header between cur_ptr and cur_end,
5547 * and the next header starts at cur_next.
5548 */
5549
5550 /* The annoying part is that pattern matching needs
5551 * that we modify the contents to null-terminate all
5552 * strings before testing them.
5553 */
5554
5555 term = *cur_end;
5556 *cur_end = '\0';
5557
5558 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5559 switch (exp->action) {
5560 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005561 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005562 last_hdr = 1;
5563 break;
5564
5565 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005566 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005567 last_hdr = 1;
5568 break;
5569
5570 case ACT_REPLACE:
5571 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5572 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5573 /* FIXME: if the user adds a newline in the replacement, the
5574 * index will not be recalculated for now, and the new line
5575 * will not be counted as a new header.
5576 */
5577
5578 cur_end += delta;
5579 cur_next += delta;
5580 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005581 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005582 break;
5583
5584 case ACT_REMOVE:
5585 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5586 cur_next += delta;
5587
Willy Tarreaufa355d42009-11-29 18:12:29 +01005588 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005589 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5590 txn->hdr_idx.used--;
5591 cur_hdr->len = 0;
5592 cur_end = NULL; /* null-term has been rewritten */
5593 break;
5594
5595 }
5596 }
5597 if (cur_end)
5598 *cur_end = term; /* restore the string terminator */
5599
5600 /* keep the link from this header to next one in case of later
5601 * removal of next header.
5602 */
5603 old_idx = cur_idx;
5604 }
5605 return 0;
5606}
5607
5608
5609/* Apply the filter to the status line in the response buffer <rtr>.
5610 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5611 * or -1 if a replacement resulted in an invalid status line.
5612 */
5613int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5614{
5615 char term;
5616 char *cur_ptr, *cur_end;
5617 int done;
5618 struct http_txn *txn = &t->txn;
5619 int len, delta;
5620
5621
Willy Tarreau3d300592007-03-18 18:34:41 +01005622 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005623 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005624 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005625 (exp->action == ACT_ALLOW ||
5626 exp->action == ACT_DENY))
5627 return 0;
5628 else if (exp->action == ACT_REMOVE)
5629 return 0;
5630
5631 done = 0;
5632
Willy Tarreau9cdde232007-05-02 20:58:19 +02005633 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005634 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5635
5636 /* Now we have the status line between cur_ptr and cur_end */
5637
5638 /* The annoying part is that pattern matching needs
5639 * that we modify the contents to null-terminate all
5640 * strings before testing them.
5641 */
5642
5643 term = *cur_end;
5644 *cur_end = '\0';
5645
5646 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5647 switch (exp->action) {
5648 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005649 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005650 done = 1;
5651 break;
5652
5653 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005654 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005655 done = 1;
5656 break;
5657
5658 case ACT_REPLACE:
5659 *cur_end = term; /* restore the string terminator */
5660 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5661 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5662 /* FIXME: if the user adds a newline in the replacement, the
5663 * index will not be recalculated for now, and the new line
5664 * will not be counted as a new header.
5665 */
5666
Willy Tarreaufa355d42009-11-29 18:12:29 +01005667 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005668 cur_end += delta;
5669
Willy Tarreau9cdde232007-05-02 20:58:19 +02005670 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005671 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005672 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005673 cur_ptr, cur_end + 1,
5674 NULL, NULL);
5675 if (unlikely(!cur_end))
5676 return -1;
5677
5678 /* we have a full respnse and we know that we have either a CR
5679 * or an LF at <ptr>.
5680 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005681 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005682 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5683 /* there is no point trying this regex on headers */
5684 return 1;
5685 }
5686 }
5687 *cur_end = term; /* restore the string terminator */
5688 return done;
5689}
5690
5691
5692
5693/*
5694 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5695 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5696 * unparsable response.
5697 */
5698int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5699{
Willy Tarreau3d300592007-03-18 18:34:41 +01005700 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005701 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005702 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005703 int ret;
5704
5705 /*
5706 * The interleaving of transformations and verdicts
5707 * makes it difficult to decide to continue or stop
5708 * the evaluation.
5709 */
5710
Willy Tarreau3d300592007-03-18 18:34:41 +01005711 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005712 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5713 exp->action == ACT_PASS)) {
5714 exp = exp->next;
5715 continue;
5716 }
5717
5718 /* Apply the filter to the status line. */
5719 ret = apply_filter_to_sts_line(t, rtr, exp);
5720 if (unlikely(ret < 0))
5721 return -1;
5722
5723 if (likely(ret == 0)) {
5724 /* The filter did not match the response, it can be
5725 * iterated through all headers.
5726 */
5727 apply_filter_to_resp_headers(t, rtr, exp);
5728 }
5729 exp = exp->next;
5730 }
5731 return 0;
5732}
5733
5734
5735
5736/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005737 * Manage server-side cookies. It can impact performance by about 2% so it is
5738 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005739 */
5740void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5741{
5742 struct http_txn *txn = &t->txn;
5743 char *p1, *p2, *p3, *p4;
5744
Willy Tarreaua15645d2007-03-18 16:22:39 +01005745 char *cur_ptr, *cur_end, *cur_next;
5746 int cur_idx, old_idx, delta;
5747
Willy Tarreaua15645d2007-03-18 16:22:39 +01005748 /* Iterate through the headers.
5749 * we start with the start line.
5750 */
5751 old_idx = 0;
5752 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5753
5754 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5755 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005756 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005757
5758 cur_hdr = &txn->hdr_idx.v[cur_idx];
5759 cur_ptr = cur_next;
5760 cur_end = cur_ptr + cur_hdr->len;
5761 cur_next = cur_end + cur_hdr->cr + 1;
5762
5763 /* We have one full header between cur_ptr and cur_end, and the
5764 * next header starts at cur_next. We're only interested in
5765 * "Cookie:" headers.
5766 */
5767
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005768 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5769 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005770 old_idx = cur_idx;
5771 continue;
5772 }
5773
5774 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005775 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005776
5777
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005778 /* maybe we only wanted to see if there was a set-cookie. Note that
5779 * the cookie capture is declared in the fronend.
5780 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005781 if (t->be->cookie_name == NULL &&
5782 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005783 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005784 return;
5785
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005786 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005787
5788 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005789 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5790 break;
5791
5792 /* p1 is at the beginning of the cookie name */
5793 p2 = p1;
5794
5795 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5796 p2++;
5797
5798 if (p2 == cur_end || *p2 == ';') /* next cookie */
5799 break;
5800
5801 p3 = p2 + 1; /* skip the '=' sign */
5802 if (p3 == cur_end)
5803 break;
5804
5805 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005806 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005807 p4++;
5808
5809 /* here, we have the cookie name between p1 and p2,
5810 * and its value between p3 and p4.
5811 * we can process it.
5812 */
5813
5814 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005815 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005816 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005817 (p4 - p1 >= t->fe->capture_namelen) &&
5818 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005819 int log_len = p4 - p1;
5820
Willy Tarreau086b3b42007-05-13 21:45:51 +02005821 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005822 Alert("HTTP logging : out of memory.\n");
5823 }
5824
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005825 if (log_len > t->fe->capture_len)
5826 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005827 memcpy(txn->srv_cookie, p1, log_len);
5828 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005829 }
5830
5831 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005832 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5833 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005834 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005835 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005836
5837 /* If the cookie is in insert mode on a known server, we'll delete
5838 * this occurrence because we'll insert another one later.
5839 * We'll delete it too if the "indirect" option is set and we're in
5840 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005841 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5842 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005843 /* this header must be deleted */
5844 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5845 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5846 txn->hdr_idx.used--;
5847 cur_hdr->len = 0;
5848 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005849 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005850
Willy Tarreau3d300592007-03-18 18:34:41 +01005851 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005852 }
5853 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005854 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005855 /* replace bytes p3->p4 with the cookie name associated
5856 * with this server since we know it.
5857 */
5858 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5859 cur_hdr->len += delta;
5860 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005861 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005862
Willy Tarreau3d300592007-03-18 18:34:41 +01005863 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005864 }
5865 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005866 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005867 /* insert the cookie name associated with this server
5868 * before existing cookie, and insert a delimitor between them..
5869 */
5870 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5871 cur_hdr->len += delta;
5872 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005873 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005874
5875 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005876 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877 }
5878 }
5879 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005880 else if (t->be->appsession_name != NULL) {
5881 int cmp_len, value_len;
5882 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005883
Cyril Bontéb21570a2009-11-29 20:04:48 +01005884 if (t->be->options2 & PR_O2_AS_PFX) {
5885 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5886 value_begin = p1 + t->be->appsession_name_len;
5887 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5888 } else {
5889 cmp_len = p2 - p1;
5890 value_begin = p3;
5891 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005892 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005893
5894 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5895 /* Cool... it's the right one */
5896 if (t->sessid != NULL) {
5897 /* free previously allocated memory as we don't need it anymore */
5898 pool_free2(apools.sessid, t->sessid);
5899 }
5900 /* Store the sessid in the session for future use */
5901 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5902 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5903 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5904 return;
5905 }
5906 memcpy(t->sessid, value_begin, value_len);
5907 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005908 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005909 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005910 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5911 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005912 /* keep the link from this header to next one */
5913 old_idx = cur_idx;
5914 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005915
5916 if (t->sessid != NULL) {
5917 appsess *asession = NULL;
5918 /* only do insert, if lookup fails */
5919 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5920 if (asession == NULL) {
5921 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5922 Alert("Not enough Memory process_srv():asession:calloc().\n");
5923 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5924 return;
5925 }
5926 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5927 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5928 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5929 return;
5930 }
5931 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5932 asession->sessid[t->be->appsession_len] = 0;
5933
5934 size_t server_id_len = strlen(t->srv->id) + 1;
5935 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5936 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5937 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5938 return;
5939 }
5940 asession->serverid[0] = '\0';
5941 memcpy(asession->serverid, t->srv->id, server_id_len);
5942
5943 asession->request_count = 0;
5944 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5945 }
5946
5947 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5948 asession->request_count++;
5949 }
5950
5951#if defined(DEBUG_HASH)
5952 Alert("manage_server_side_cookies\n");
5953 appsession_hash_dump(&(t->be->htbl_proxy));
5954#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005955}
5956
5957
5958
5959/*
5960 * Check if response is cacheable or not. Updates t->flags.
5961 */
5962void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5963{
5964 struct http_txn *txn = &t->txn;
5965 char *p1, *p2;
5966
5967 char *cur_ptr, *cur_end, *cur_next;
5968 int cur_idx;
5969
Willy Tarreau5df51872007-11-25 16:20:08 +01005970 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971 return;
5972
5973 /* Iterate through the headers.
5974 * we start with the start line.
5975 */
5976 cur_idx = 0;
5977 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5978
5979 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5980 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005981 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005982
5983 cur_hdr = &txn->hdr_idx.v[cur_idx];
5984 cur_ptr = cur_next;
5985 cur_end = cur_ptr + cur_hdr->len;
5986 cur_next = cur_end + cur_hdr->cr + 1;
5987
5988 /* We have one full header between cur_ptr and cur_end, and the
5989 * next header starts at cur_next. We're only interested in
5990 * "Cookie:" headers.
5991 */
5992
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005993 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5994 if (val) {
5995 if ((cur_end - (cur_ptr + val) >= 8) &&
5996 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5997 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5998 return;
5999 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006000 }
6001
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006002 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6003 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006004 continue;
6005
6006 /* OK, right now we know we have a cache-control header at cur_ptr */
6007
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006008 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006009
6010 if (p1 >= cur_end) /* no more info */
6011 continue;
6012
6013 /* p1 is at the beginning of the value */
6014 p2 = p1;
6015
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006016 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006017 p2++;
6018
6019 /* we have a complete value between p1 and p2 */
6020 if (p2 < cur_end && *p2 == '=') {
6021 /* we have something of the form no-cache="set-cookie" */
6022 if ((cur_end - p1 >= 21) &&
6023 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6024 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006025 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006026 continue;
6027 }
6028
6029 /* OK, so we know that either p2 points to the end of string or to a comma */
6030 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6031 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6032 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6033 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006034 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006035 return;
6036 }
6037
6038 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006039 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006040 continue;
6041 }
6042 }
6043}
6044
6045
Willy Tarreau58f10d72006-12-04 02:26:12 +01006046/*
6047 * Try to retrieve a known appsession in the URI, then the associated server.
6048 * If the server is found, it's assigned to the session.
6049 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006052 char *end_params, *first_param, *cur_param, *next_param;
6053 char separator;
6054 int value_len;
6055
6056 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006057
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006058 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006059 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006060 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006061 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006062
Cyril Bontéb21570a2009-11-29 20:04:48 +01006063 first_param = NULL;
6064 switch (mode) {
6065 case PR_O2_AS_M_PP:
6066 first_param = memchr(begin, ';', len);
6067 break;
6068 case PR_O2_AS_M_QS:
6069 first_param = memchr(begin, '?', len);
6070 break;
6071 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006072
Cyril Bontéb21570a2009-11-29 20:04:48 +01006073 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006074 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006075 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076
Cyril Bontéb21570a2009-11-29 20:04:48 +01006077 switch (mode) {
6078 case PR_O2_AS_M_PP:
6079 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6080 end_params = (char *) begin + len;
6081 }
6082 separator = ';';
6083 break;
6084 case PR_O2_AS_M_QS:
6085 end_params = (char *) begin + len;
6086 separator = '&';
6087 break;
6088 default:
6089 /* unknown mode, shouldn't happen */
6090 return;
6091 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092
Cyril Bontéb21570a2009-11-29 20:04:48 +01006093 cur_param = next_param = end_params;
6094 while (cur_param > first_param) {
6095 cur_param--;
6096 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6097 /* let's see if this is the appsession parameter */
6098 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6099 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6100 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6101 /* Cool... it's the right one */
6102 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6103 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6104 if (value_len > 0) {
6105 manage_client_side_appsession(t, cur_param, value_len);
6106 }
6107 break;
6108 }
6109 next_param = cur_param;
6110 }
6111 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006112#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006113 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006114 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006116}
6117
6118
Willy Tarreaub2513902006-12-17 14:52:38 +01006119/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006120 * In a GET or HEAD request, check if the requested URI matches the stats uri
6121 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006122 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006123 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006124 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006125 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006126 *
6127 * Returns 1 if the session's state changes, otherwise 0.
6128 */
6129int stats_check_uri_auth(struct session *t, struct proxy *backend)
6130{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006131 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006132 struct uri_auth *uri_auth = backend->uri_auth;
6133 struct user_auth *user;
6134 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006135 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006136
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006137 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006140 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006141 return 0;
6142
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006143 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006144
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006145 /* the URI is in h */
6146 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006147 return 0;
6148
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006149 h += uri_auth->uri_len;
6150 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6151 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006152 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006153 break;
6154 }
6155 h++;
6156 }
6157
6158 if (uri_auth->refresh) {
6159 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6160 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6161 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006162 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006163 break;
6164 }
6165 h++;
6166 }
6167 }
6168
Willy Tarreau55bb8452007-10-17 18:44:57 +02006169 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6170 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6171 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006172 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006173 break;
6174 }
6175 h++;
6176 }
6177
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006178 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6179
Willy Tarreaub2513902006-12-17 14:52:38 +01006180 /* we are in front of a interceptable URI. Let's check
6181 * if there's an authentication and if it's valid.
6182 */
6183 user = uri_auth->users;
6184 if (!user) {
6185 /* no user auth required, it's OK */
6186 authenticated = 1;
6187 } else {
6188 authenticated = 0;
6189
6190 /* a user list is defined, we have to check.
6191 * skip 21 chars for "Authorization: Basic ".
6192 */
6193
6194 /* FIXME: this should move to an earlier place */
6195 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006196 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6197 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6198 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006199 if (len > 14 &&
6200 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006201 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006202 break;
6203 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006204 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006205 }
6206
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006207 if (txn->auth_hdr.len < 21 ||
6208 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006209 user = NULL;
6210
6211 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006212 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6213 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006214 user->user_pwd, user->user_len)) {
6215 authenticated = 1;
6216 break;
6217 }
6218 user = user->next;
6219 }
6220 }
6221
6222 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006223 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006224
6225 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006226 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6227 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006228 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006229 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006230 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006231 if (!(t->flags & SN_ERR_MASK))
6232 t->flags |= SN_ERR_PRXCOND;
6233 if (!(t->flags & SN_FINST_MASK))
6234 t->flags |= SN_FINST_R;
6235 return 1;
6236 }
6237
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006238 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006239 * data.
6240 */
Willy Tarreau70089872008-06-13 21:12:51 +02006241 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006242 t->data_source = DATA_SRC_STATS;
6243 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006244 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006245 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6246 t->rep->prod->private = t;
6247 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006248 return 1;
6249}
6250
Willy Tarreau4076a152009-04-02 15:18:36 +02006251/*
6252 * Capture a bad request or response and archive it in the proxy's structure.
6253 */
6254void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6255 struct buffer *buf, struct http_msg *msg,
6256 struct proxy *other_end)
6257{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006258 es->len = buf->r - (buf->data + msg->som);
6259 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006260 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006261 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006262 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006263 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006264 es->when = date; // user-visible date
6265 es->sid = s->uniq_id;
6266 es->srv = s->srv;
6267 es->oe = other_end;
6268 es->src = s->cli_addr;
6269}
Willy Tarreaub2513902006-12-17 14:52:38 +01006270
Willy Tarreaubaaee002006-06-26 02:48:02 +02006271/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006272 * Print a debug line with a header
6273 */
6274void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6275{
6276 int len, max;
6277 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006278 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279 max = end - start;
6280 UBOUND(max, sizeof(trash) - len - 1);
6281 len += strlcpy2(trash + len, start, max + 1);
6282 trash[len++] = '\n';
6283 write(1, trash, len);
6284}
6285
Willy Tarreau0937bc42009-12-22 15:03:09 +01006286/*
6287 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6288 * the required fields are properly allocated and that we only need to (re)init
6289 * them. This should be used before processing any new request.
6290 */
6291void http_init_txn(struct session *s)
6292{
6293 struct http_txn *txn = &s->txn;
6294 struct proxy *fe = s->fe;
6295
6296 txn->flags = 0;
6297 txn->status = -1;
6298
6299 txn->req.sol = txn->req.eol = NULL;
6300 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6301 txn->rsp.sol = txn->rsp.eol = NULL;
6302 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6303 txn->req.hdr_content_len = 0LL;
6304 txn->rsp.hdr_content_len = 0LL;
6305 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6306 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6307 chunk_reset(&txn->auth_hdr);
6308
6309 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6310 if (fe->options2 & PR_O2_REQBUG_OK)
6311 txn->req.err_pos = -1; /* let buggy requests pass */
6312
Willy Tarreau46023632010-01-07 22:51:47 +01006313 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006314 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6315
Willy Tarreau46023632010-01-07 22:51:47 +01006316 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006317 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6318
6319 if (txn->hdr_idx.v)
6320 hdr_idx_init(&txn->hdr_idx);
6321}
6322
6323/* to be used at the end of a transaction */
6324void http_end_txn(struct session *s)
6325{
6326 struct http_txn *txn = &s->txn;
6327
6328 /* these ones will have been dynamically allocated */
6329 pool_free2(pool2_requri, txn->uri);
6330 pool_free2(pool2_capture, txn->cli_cookie);
6331 pool_free2(pool2_capture, txn->srv_cookie);
6332 txn->uri = NULL;
6333 txn->srv_cookie = NULL;
6334 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006335
6336 if (txn->req.cap) {
6337 struct cap_hdr *h;
6338 for (h = s->fe->req_cap; h; h = h->next)
6339 pool_free2(h->pool, txn->req.cap[h->index]);
6340 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6341 }
6342
6343 if (txn->rsp.cap) {
6344 struct cap_hdr *h;
6345 for (h = s->fe->rsp_cap; h; h = h->next)
6346 pool_free2(h->pool, txn->rsp.cap[h->index]);
6347 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6348 }
6349
Willy Tarreau0937bc42009-12-22 15:03:09 +01006350}
6351
6352/* to be used at the end of a transaction to prepare a new one */
6353void http_reset_txn(struct session *s)
6354{
6355 http_end_txn(s);
6356 http_init_txn(s);
6357
6358 s->be = s->fe;
6359 s->req->analysers = s->listener->analysers;
6360 s->logs.logwait = s->fe->to_log;
6361 s->srv = s->prev_srv = s->srv_conn = NULL;
6362 s->pend_pos = NULL;
6363 s->conn_retries = s->be->conn_retries;
6364
6365 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6366
6367 s->req->rto = s->fe->timeout.client;
6368 s->req->wto = s->be->timeout.server;
6369 s->req->cto = s->be->timeout.connect;
6370
6371 s->rep->rto = s->be->timeout.server;
6372 s->rep->wto = s->fe->timeout.client;
6373 s->rep->cto = TICK_ETERNITY;
6374
6375 s->req->rex = TICK_ETERNITY;
6376 s->req->wex = TICK_ETERNITY;
6377 s->req->analyse_exp = TICK_ETERNITY;
6378 s->rep->rex = TICK_ETERNITY;
6379 s->rep->wex = TICK_ETERNITY;
6380 s->rep->analyse_exp = TICK_ETERNITY;
6381}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006382
Willy Tarreau8797c062007-05-07 00:55:35 +02006383/************************************************************************/
6384/* The code below is dedicated to ACL parsing and matching */
6385/************************************************************************/
6386
6387
6388
6389
6390/* 1. Check on METHOD
6391 * We use the pre-parsed method if it is known, and store its number as an
6392 * integer. If it is unknown, we use the pointer and the length.
6393 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006394static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006395{
6396 int len, meth;
6397
Willy Tarreauae8b7962007-06-09 23:10:04 +02006398 len = strlen(*text);
6399 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006400
6401 pattern->val.i = meth;
6402 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006403 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006404 if (!pattern->ptr.str)
6405 return 0;
6406 pattern->len = len;
6407 }
6408 return 1;
6409}
6410
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006411static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006412acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6413 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006414{
6415 int meth;
6416 struct http_txn *txn = l7;
6417
Willy Tarreaub6866442008-07-14 23:54:42 +02006418 if (!txn)
6419 return 0;
6420
Willy Tarreau655dce92009-11-08 13:10:58 +01006421 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006422 return 0;
6423
Willy Tarreau8797c062007-05-07 00:55:35 +02006424 meth = txn->meth;
6425 test->i = meth;
6426 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006427 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6428 /* ensure the indexes are not affected */
6429 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006430 test->len = txn->req.sl.rq.m_l;
6431 test->ptr = txn->req.sol;
6432 }
6433 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6434 return 1;
6435}
6436
6437static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6438{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006439 int icase;
6440
Willy Tarreau8797c062007-05-07 00:55:35 +02006441 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006442 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006443
6444 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006445 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006446
6447 /* Other method, we must compare the strings */
6448 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006449 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006450
6451 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6452 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6453 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006454 return ACL_PAT_FAIL;
6455 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006456}
6457
6458/* 2. Check on Request/Status Version
6459 * We simply compare strings here.
6460 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006461static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006462{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006463 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006464 if (!pattern->ptr.str)
6465 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006466 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006467 return 1;
6468}
6469
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006470static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006471acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6472 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006473{
6474 struct http_txn *txn = l7;
6475 char *ptr;
6476 int len;
6477
Willy Tarreaub6866442008-07-14 23:54:42 +02006478 if (!txn)
6479 return 0;
6480
Willy Tarreau655dce92009-11-08 13:10:58 +01006481 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006482 return 0;
6483
Willy Tarreau8797c062007-05-07 00:55:35 +02006484 len = txn->req.sl.rq.v_l;
6485 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6486
6487 while ((len-- > 0) && (*ptr++ != '/'));
6488 if (len <= 0)
6489 return 0;
6490
6491 test->ptr = ptr;
6492 test->len = len;
6493
6494 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6495 return 1;
6496}
6497
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006498static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006499acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6500 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006501{
6502 struct http_txn *txn = l7;
6503 char *ptr;
6504 int len;
6505
Willy Tarreaub6866442008-07-14 23:54:42 +02006506 if (!txn)
6507 return 0;
6508
Willy Tarreau655dce92009-11-08 13:10:58 +01006509 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006510 return 0;
6511
Willy Tarreau8797c062007-05-07 00:55:35 +02006512 len = txn->rsp.sl.st.v_l;
6513 ptr = txn->rsp.sol;
6514
6515 while ((len-- > 0) && (*ptr++ != '/'));
6516 if (len <= 0)
6517 return 0;
6518
6519 test->ptr = ptr;
6520 test->len = len;
6521
6522 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6523 return 1;
6524}
6525
6526/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006527static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006528acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6529 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006530{
6531 struct http_txn *txn = l7;
6532 char *ptr;
6533 int len;
6534
Willy Tarreaub6866442008-07-14 23:54:42 +02006535 if (!txn)
6536 return 0;
6537
Willy Tarreau655dce92009-11-08 13:10:58 +01006538 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006539 return 0;
6540
Willy Tarreau8797c062007-05-07 00:55:35 +02006541 len = txn->rsp.sl.st.c_l;
6542 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6543
6544 test->i = __strl2ui(ptr, len);
6545 test->flags = ACL_TEST_F_VOL_1ST;
6546 return 1;
6547}
6548
6549/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006550static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006551acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6552 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006553{
6554 struct http_txn *txn = l7;
6555
Willy Tarreaub6866442008-07-14 23:54:42 +02006556 if (!txn)
6557 return 0;
6558
Willy Tarreau655dce92009-11-08 13:10:58 +01006559 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006560 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006561
Willy Tarreauc11416f2007-06-17 16:58:38 +02006562 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6563 /* ensure the indexes are not affected */
6564 return 0;
6565
Willy Tarreau8797c062007-05-07 00:55:35 +02006566 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006567 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006568
Willy Tarreauf3d25982007-05-08 22:45:09 +02006569 /* we do not need to set READ_ONLY because the data is in a buffer */
6570 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006571 return 1;
6572}
6573
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006574static int
6575acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6576 struct acl_expr *expr, struct acl_test *test)
6577{
6578 struct http_txn *txn = l7;
6579
Willy Tarreaub6866442008-07-14 23:54:42 +02006580 if (!txn)
6581 return 0;
6582
Willy Tarreau655dce92009-11-08 13:10:58 +01006583 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006584 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006585
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006586 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6587 /* ensure the indexes are not affected */
6588 return 0;
6589
6590 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006591 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 +01006592 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6593 test->i = AF_INET;
6594
6595 /*
6596 * If we are parsing url in frontend space, we prepare backend stage
6597 * to not parse again the same url ! optimization lazyness...
6598 */
6599 if (px->options & PR_O_HTTP_PROXY)
6600 l4->flags |= SN_ADDR_SET;
6601
6602 test->flags = ACL_TEST_F_READ_ONLY;
6603 return 1;
6604}
6605
6606static int
6607acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6608 struct acl_expr *expr, struct acl_test *test)
6609{
6610 struct http_txn *txn = l7;
6611
Willy Tarreaub6866442008-07-14 23:54:42 +02006612 if (!txn)
6613 return 0;
6614
Willy Tarreau655dce92009-11-08 13:10:58 +01006615 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006616 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006617
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006618 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6619 /* ensure the indexes are not affected */
6620 return 0;
6621
6622 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006623 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 +01006624 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6625
6626 if (px->options & PR_O_HTTP_PROXY)
6627 l4->flags |= SN_ADDR_SET;
6628
6629 test->flags = ACL_TEST_F_READ_ONLY;
6630 return 1;
6631}
6632
Willy Tarreauc11416f2007-06-17 16:58:38 +02006633/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6634 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6635 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006636static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006637acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006638 struct acl_expr *expr, struct acl_test *test)
6639{
6640 struct http_txn *txn = l7;
6641 struct hdr_idx *idx = &txn->hdr_idx;
6642 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006643
Willy Tarreaub6866442008-07-14 23:54:42 +02006644 if (!txn)
6645 return 0;
6646
Willy Tarreau33a7e692007-06-10 19:45:56 +02006647 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6648 /* search for header from the beginning */
6649 ctx->idx = 0;
6650
Willy Tarreau33a7e692007-06-10 19:45:56 +02006651 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6652 test->flags |= ACL_TEST_F_FETCH_MORE;
6653 test->flags |= ACL_TEST_F_VOL_HDR;
6654 test->len = ctx->vlen;
6655 test->ptr = (char *)ctx->line + ctx->val;
6656 return 1;
6657 }
6658
6659 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6660 test->flags |= ACL_TEST_F_VOL_HDR;
6661 return 0;
6662}
6663
Willy Tarreau33a7e692007-06-10 19:45:56 +02006664static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006665acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6666 struct acl_expr *expr, struct acl_test *test)
6667{
6668 struct http_txn *txn = l7;
6669
Willy Tarreaub6866442008-07-14 23:54:42 +02006670 if (!txn)
6671 return 0;
6672
Willy Tarreau655dce92009-11-08 13:10:58 +01006673 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006674 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006675
Willy Tarreauc11416f2007-06-17 16:58:38 +02006676 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6677 /* ensure the indexes are not affected */
6678 return 0;
6679
6680 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6681}
6682
6683static int
6684acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6685 struct acl_expr *expr, struct acl_test *test)
6686{
6687 struct http_txn *txn = l7;
6688
Willy Tarreaub6866442008-07-14 23:54:42 +02006689 if (!txn)
6690 return 0;
6691
Willy Tarreau655dce92009-11-08 13:10:58 +01006692 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006693 return 0;
6694
6695 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6696}
6697
6698/* 6. Check on HTTP header count. The number of occurrences is returned.
6699 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6700 */
6701static int
6702acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006703 struct acl_expr *expr, struct acl_test *test)
6704{
6705 struct http_txn *txn = l7;
6706 struct hdr_idx *idx = &txn->hdr_idx;
6707 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006708 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006709
Willy Tarreaub6866442008-07-14 23:54:42 +02006710 if (!txn)
6711 return 0;
6712
Willy Tarreau33a7e692007-06-10 19:45:56 +02006713 ctx.idx = 0;
6714 cnt = 0;
6715 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6716 cnt++;
6717
6718 test->i = cnt;
6719 test->flags = ACL_TEST_F_VOL_HDR;
6720 return 1;
6721}
6722
Willy Tarreauc11416f2007-06-17 16:58:38 +02006723static int
6724acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6725 struct acl_expr *expr, struct acl_test *test)
6726{
6727 struct http_txn *txn = l7;
6728
Willy Tarreaub6866442008-07-14 23:54:42 +02006729 if (!txn)
6730 return 0;
6731
Willy Tarreau655dce92009-11-08 13:10:58 +01006732 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006733 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006734
Willy Tarreauc11416f2007-06-17 16:58:38 +02006735 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6736 /* ensure the indexes are not affected */
6737 return 0;
6738
6739 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6740}
6741
6742static int
6743acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6744 struct acl_expr *expr, struct acl_test *test)
6745{
6746 struct http_txn *txn = l7;
6747
Willy Tarreaub6866442008-07-14 23:54:42 +02006748 if (!txn)
6749 return 0;
6750
Willy Tarreau655dce92009-11-08 13:10:58 +01006751 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006752 return 0;
6753
6754 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6755}
6756
Willy Tarreau33a7e692007-06-10 19:45:56 +02006757/* 7. Check on HTTP header's integer value. The integer value is returned.
6758 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006759 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006760 */
6761static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006762acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006763 struct acl_expr *expr, struct acl_test *test)
6764{
6765 struct http_txn *txn = l7;
6766 struct hdr_idx *idx = &txn->hdr_idx;
6767 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006768
Willy Tarreaub6866442008-07-14 23:54:42 +02006769 if (!txn)
6770 return 0;
6771
Willy Tarreau33a7e692007-06-10 19:45:56 +02006772 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6773 /* search for header from the beginning */
6774 ctx->idx = 0;
6775
Willy Tarreau33a7e692007-06-10 19:45:56 +02006776 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6777 test->flags |= ACL_TEST_F_FETCH_MORE;
6778 test->flags |= ACL_TEST_F_VOL_HDR;
6779 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6780 return 1;
6781 }
6782
6783 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6784 test->flags |= ACL_TEST_F_VOL_HDR;
6785 return 0;
6786}
6787
Willy Tarreauc11416f2007-06-17 16:58:38 +02006788static int
6789acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6790 struct acl_expr *expr, struct acl_test *test)
6791{
6792 struct http_txn *txn = l7;
6793
Willy Tarreaub6866442008-07-14 23:54:42 +02006794 if (!txn)
6795 return 0;
6796
Willy Tarreau655dce92009-11-08 13:10:58 +01006797 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006798 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006799
Willy Tarreauc11416f2007-06-17 16:58:38 +02006800 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6801 /* ensure the indexes are not affected */
6802 return 0;
6803
6804 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6805}
6806
6807static int
6808acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6809 struct acl_expr *expr, struct acl_test *test)
6810{
6811 struct http_txn *txn = l7;
6812
Willy Tarreaub6866442008-07-14 23:54:42 +02006813 if (!txn)
6814 return 0;
6815
Willy Tarreau655dce92009-11-08 13:10:58 +01006816 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006817 return 0;
6818
6819 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6820}
6821
Willy Tarreau106f9792009-09-19 07:54:16 +02006822/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6823 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6824 */
6825static int
6826acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6827 struct acl_expr *expr, struct acl_test *test)
6828{
6829 struct http_txn *txn = l7;
6830 struct hdr_idx *idx = &txn->hdr_idx;
6831 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6832
6833 if (!txn)
6834 return 0;
6835
6836 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6837 /* search for header from the beginning */
6838 ctx->idx = 0;
6839
6840 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6841 test->flags |= ACL_TEST_F_FETCH_MORE;
6842 test->flags |= ACL_TEST_F_VOL_HDR;
6843 /* Same optimization as url_ip */
6844 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6845 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6846 test->ptr = (void *)&l4->srv_addr.sin_addr;
6847 test->i = AF_INET;
6848 return 1;
6849 }
6850
6851 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6852 test->flags |= ACL_TEST_F_VOL_HDR;
6853 return 0;
6854}
6855
6856static int
6857acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6858 struct acl_expr *expr, struct acl_test *test)
6859{
6860 struct http_txn *txn = l7;
6861
6862 if (!txn)
6863 return 0;
6864
Willy Tarreau655dce92009-11-08 13:10:58 +01006865 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006866 return 0;
6867
6868 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6869 /* ensure the indexes are not affected */
6870 return 0;
6871
6872 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6873}
6874
6875static int
6876acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6877 struct acl_expr *expr, struct acl_test *test)
6878{
6879 struct http_txn *txn = l7;
6880
6881 if (!txn)
6882 return 0;
6883
Willy Tarreau655dce92009-11-08 13:10:58 +01006884 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006885 return 0;
6886
6887 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6888}
6889
Willy Tarreau737b0c12007-06-10 21:28:46 +02006890/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6891 * the first '/' after the possible hostname, and ends before the possible '?'.
6892 */
6893static int
6894acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6895 struct acl_expr *expr, struct acl_test *test)
6896{
6897 struct http_txn *txn = l7;
6898 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006899
Willy Tarreaub6866442008-07-14 23:54:42 +02006900 if (!txn)
6901 return 0;
6902
Willy Tarreau655dce92009-11-08 13:10:58 +01006903 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006904 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006905
Willy Tarreauc11416f2007-06-17 16:58:38 +02006906 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6907 /* ensure the indexes are not affected */
6908 return 0;
6909
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006910 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 +01006911 ptr = http_get_path(txn);
6912 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006913 return 0;
6914
6915 /* OK, we got the '/' ! */
6916 test->ptr = ptr;
6917
6918 while (ptr < end && *ptr != '?')
6919 ptr++;
6920
6921 test->len = ptr - test->ptr;
6922
6923 /* we do not need to set READ_ONLY because the data is in a buffer */
6924 test->flags = ACL_TEST_F_VOL_1ST;
6925 return 1;
6926}
6927
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006928static int
6929acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6930 struct acl_expr *expr, struct acl_test *test)
6931{
6932 struct buffer *req = s->req;
6933 struct http_txn *txn = &s->txn;
6934 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006935
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006936 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6937 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6938 */
6939
6940 if (!s || !req)
6941 return 0;
6942
Willy Tarreau655dce92009-11-08 13:10:58 +01006943 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006944 /* Already decoded as OK */
6945 test->flags |= ACL_TEST_F_SET_RES_PASS;
6946 return 1;
6947 }
6948
6949 /* Try to decode HTTP request */
6950 if (likely(req->lr < req->r))
6951 http_msg_analyzer(req, msg, &txn->hdr_idx);
6952
Willy Tarreau655dce92009-11-08 13:10:58 +01006953 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006954 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6955 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6956 return 1;
6957 }
6958 /* wait for final state */
6959 test->flags |= ACL_TEST_F_MAY_CHANGE;
6960 return 0;
6961 }
6962
6963 /* OK we got a valid HTTP request. We have some minor preparation to
6964 * perform so that further checks can rely on HTTP tests.
6965 */
6966 msg->sol = req->data + msg->som;
6967 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6968 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6969 s->flags |= SN_REDIRECTABLE;
6970
6971 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6972 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6973 return 1;
6974 }
6975
6976 test->flags |= ACL_TEST_F_SET_RES_PASS;
6977 return 1;
6978}
6979
Willy Tarreau8797c062007-05-07 00:55:35 +02006980
6981/************************************************************************/
6982/* All supported keywords must be declared here. */
6983/************************************************************************/
6984
6985/* Note: must not be declared <const> as its list will be overwritten */
6986static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006987 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6988
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006989 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6990 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6991 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6992 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006993
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006994 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6995 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6996 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6997 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6998 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6999 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7000 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7001 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7002 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007003
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007004 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7005 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7006 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7007 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7008 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7009 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7010 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7011 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7012 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7013 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007014 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007015
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007016 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7017 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7018 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7019 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7020 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7021 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7022 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7023 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7024 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007025 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007026
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007027 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7028 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7029 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7030 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7031 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7032 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7033 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007034
Willy Tarreauf3d25982007-05-08 22:45:09 +02007035 { NULL, NULL, NULL, NULL },
7036
7037#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007038 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7039 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7040 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7041 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7042 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7043 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7044 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7045
Willy Tarreau8797c062007-05-07 00:55:35 +02007046 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7047 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7048 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7049 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7050 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7051 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7052 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7053 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7054
7055 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7056 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7057 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7058 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7059 { NULL, NULL, NULL, NULL },
7060#endif
7061}};
7062
7063
7064__attribute__((constructor))
7065static void __http_protocol_init(void)
7066{
7067 acl_register_keywords(&acl_kws);
7068}
7069
7070
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007072 * Local variables:
7073 * c-indent-level: 8
7074 * c-basic-offset: 8
7075 * End:
7076 */