blob: 32284fe2733e7930b776095133305024fb285004 [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) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002148 if ((txn->flags & TX_NOT_FIRST) &&
2149 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002150 req->r < req->lr ||
2151 req->r > req->data + req->size - global.tune.maxrewrite)) {
2152 if (req->send_max) {
2153 /* some data has still not left the buffer, wake us once that's done */
2154 buffer_dont_connect(req);
2155 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2156 return 0;
2157 }
2158 if (req->l <= req->size - global.tune.maxrewrite)
2159 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002160 }
2161
Willy Tarreau065e8332010-01-08 00:30:20 +01002162 /* Note that we have the same problem with the response ; we
2163 * may want to send a redirect, error or anything which requires
2164 * some spare space. So we'll ensure that we have at least
2165 * maxrewrite bytes available in the response buffer before
2166 * processing that one. This will only affect pipelined
2167 * keep-alive requests.
2168 */
2169 if ((txn->flags & TX_NOT_FIRST) &&
2170 unlikely((s->rep->flags & BF_FULL) ||
2171 s->rep->r < s->rep->lr ||
2172 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2173 if (s->rep->send_max) {
2174 /* don't let a connection request be initiated */
2175 buffer_dont_connect(req);
2176 return 0;
2177 }
2178 }
2179
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002180 if (likely(req->lr < req->r))
2181 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002182 }
2183
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 /* 1: we might have to print this header in debug mode */
2185 if (unlikely((global.mode & MODE_DEBUG) &&
2186 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002187 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002188 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002189
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 sol = req->data + msg->som;
2191 eol = sol + msg->sl.rq.l;
2192 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002193
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 sol += hdr_idx_first_pos(&txn->hdr_idx);
2195 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002196
Willy Tarreau59234e92008-11-30 23:51:27 +01002197 while (cur_idx) {
2198 eol = sol + txn->hdr_idx.v[cur_idx].len;
2199 debug_hdr("clihdr", s, sol, eol);
2200 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2201 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002202 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002203 }
2204
Willy Tarreau58f10d72006-12-04 02:26:12 +01002205
Willy Tarreau59234e92008-11-30 23:51:27 +01002206 /*
2207 * Now we quickly check if we have found a full valid request.
2208 * If not so, we check the FD and buffer states before leaving.
2209 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002210 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 * requests are checked first.
2212 *
2213 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002214
Willy Tarreau655dce92009-11-08 13:10:58 +01002215 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002216 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002217 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002218 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002219 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2220 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002221
Willy Tarreau59234e92008-11-30 23:51:27 +01002222 /* 1: Since we are in header mode, if there's no space
2223 * left for headers, we won't be able to free more
2224 * later, so the session will never terminate. We
2225 * must terminate it now.
2226 */
2227 if (unlikely(req->flags & BF_FULL)) {
2228 /* FIXME: check if URI is set and return Status
2229 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002230 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002231 goto return_bad_req;
2232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002233
Willy Tarreau59234e92008-11-30 23:51:27 +01002234 /* 2: have we encountered a read error ? */
2235 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002236 if (txn->flags & TX_NOT_FIRST)
2237 goto failed_keep_alive;
2238
Willy Tarreau59234e92008-11-30 23:51:27 +01002239 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002240 if (msg->err_pos >= 0)
2241 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002242 msg->msg_state = HTTP_MSG_ERROR;
2243 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002244
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002245 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002246 if (s->listener->counters)
2247 s->listener->counters->failed_req++;
2248
Willy Tarreau59234e92008-11-30 23:51:27 +01002249 if (!(s->flags & SN_ERR_MASK))
2250 s->flags |= SN_ERR_CLICL;
2251 if (!(s->flags & SN_FINST_MASK))
2252 s->flags |= SN_FINST_R;
2253 return 0;
2254 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002255
Willy Tarreau59234e92008-11-30 23:51:27 +01002256 /* 3: has the read timeout expired ? */
2257 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002258 if (txn->flags & TX_NOT_FIRST)
2259 goto failed_keep_alive;
2260
Willy Tarreau59234e92008-11-30 23:51:27 +01002261 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002262 if (msg->err_pos >= 0)
2263 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 txn->status = 408;
2265 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2266 msg->msg_state = HTTP_MSG_ERROR;
2267 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002268
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002269 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002270 if (s->listener->counters)
2271 s->listener->counters->failed_req++;
2272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 if (!(s->flags & SN_ERR_MASK))
2274 s->flags |= SN_ERR_CLITO;
2275 if (!(s->flags & SN_FINST_MASK))
2276 s->flags |= SN_FINST_R;
2277 return 0;
2278 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002279
Willy Tarreau59234e92008-11-30 23:51:27 +01002280 /* 4: have we encountered a close ? */
2281 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002282 if (txn->flags & TX_NOT_FIRST)
2283 goto failed_keep_alive;
2284
Willy Tarreau4076a152009-04-02 15:18:36 +02002285 if (msg->err_pos >= 0)
2286 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 txn->status = 400;
2288 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2289 msg->msg_state = HTTP_MSG_ERROR;
2290 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002291
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002292 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002293 if (s->listener->counters)
2294 s->listener->counters->failed_req++;
2295
Willy Tarreau59234e92008-11-30 23:51:27 +01002296 if (!(s->flags & SN_ERR_MASK))
2297 s->flags |= SN_ERR_CLICL;
2298 if (!(s->flags & SN_FINST_MASK))
2299 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002300 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002301 }
2302
Willy Tarreau520d95e2009-09-19 21:04:57 +02002303 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002304 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2305
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 /* just set the request timeout once at the beginning of the request */
2307 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002308 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002309
Willy Tarreau59234e92008-11-30 23:51:27 +01002310 /* we're not ready yet */
2311 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002312
2313 failed_keep_alive:
2314 /* Here we process low-level errors for keep-alive requests. In
2315 * short, if the request is not the first one and it experiences
2316 * a timeout, read error or shutdown, we just silently close so
2317 * that the client can try again.
2318 */
2319 txn->status = 0;
2320 msg->msg_state = HTTP_MSG_RQBEFORE;
2321 req->analysers = 0;
2322 s->logs.logwait = 0;
2323 stream_int_cond_close(req->prod, NULL);
2324 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002326
Willy Tarreaud787e662009-07-07 10:14:51 +02002327 /* OK now we have a complete HTTP request with indexed headers. Let's
2328 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002329 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2330 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2331 * points to the CRLF of the request line. req->lr points to the first
2332 * byte after the last LF. msg->col and msg->sov point to the first
2333 * byte of data. msg->eol cannot be trusted because it may have been
2334 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002335 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002336
Willy Tarreaud787e662009-07-07 10:14:51 +02002337 /* Maybe we found in invalid header name while we were configured not
2338 * to block on that, so we have to capture it now.
2339 */
2340 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002341 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2342
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 /* ensure we keep this pointer to the beginning of the message */
2344 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 /*
2347 * 1: identify the method
2348 */
2349 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2350
2351 /* we can make use of server redirect on GET and HEAD */
2352 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2353 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002354
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 /*
2356 * 2: check if the URI matches the monitor_uri.
2357 * We have to do this for every request which gets in, because
2358 * the monitor-uri is defined by the frontend.
2359 */
2360 if (unlikely((s->fe->monitor_uri_len != 0) &&
2361 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2362 !memcmp(&req->data[msg->sl.rq.u],
2363 s->fe->monitor_uri,
2364 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002365 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002367 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002369
Willy Tarreau59234e92008-11-30 23:51:27 +01002370 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002371
Willy Tarreau59234e92008-11-30 23:51:27 +01002372 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002373 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2374 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002375
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 ret = acl_pass(ret);
2377 if (cond->pol == ACL_COND_UNLESS)
2378 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002379
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 if (ret) {
2381 /* we fail this request, let's return 503 service unavail */
2382 txn->status = 503;
2383 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2384 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002385 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002386 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002387
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 /* nothing to fail, let's reply normaly */
2389 txn->status = 200;
2390 stream_int_retnclose(req->prod, &http_200_chunk);
2391 goto return_prx_cond;
2392 }
2393
2394 /*
2395 * 3: Maybe we have to copy the original REQURI for the logs ?
2396 * Note: we cannot log anymore if the request has been
2397 * classified as invalid.
2398 */
2399 if (unlikely(s->logs.logwait & LW_REQ)) {
2400 /* we have a complete HTTP request that we must log */
2401 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2402 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002403
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 if (urilen >= REQURI_LEN)
2405 urilen = REQURI_LEN - 1;
2406 memcpy(txn->uri, &req->data[msg->som], urilen);
2407 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 if (!(s->logs.logwait &= ~LW_REQ))
2410 s->do_log(s);
2411 } else {
2412 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002413 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 }
Willy Tarreau06619262006-12-17 08:37:22 +01002415
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002417 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2418 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002419
Willy Tarreau5b154472009-12-21 20:11:07 +01002420 /* ... and check if the request is HTTP/1.1 or above */
2421 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002422 ((req->data[msg->sl.rq.v + 5] > '1') ||
2423 ((req->data[msg->sl.rq.v + 5] == '1') &&
2424 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002425 txn->flags |= TX_REQ_VER_11;
2426
2427 /* "connection" has not been parsed yet */
2428 txn->flags &= ~TX_CON_HDR_PARS;
2429
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002430 /* transfer length unknown*/
2431 txn->flags &= ~TX_REQ_XFER_LEN;
2432
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 /* 5: we may need to capture headers */
2434 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2435 capture_headers(req->data + msg->som, &txn->hdr_idx,
2436 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002437
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002438 /* 6: determine the transfer-length.
2439 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2440 * the presence of a message-body in a REQUEST and its transfer length
2441 * must be determined that way (in order of precedence) :
2442 * 1. The presence of a message-body in a request is signaled by the
2443 * inclusion of a Content-Length or Transfer-Encoding header field
2444 * in the request's header fields. When a request message contains
2445 * both a message-body of non-zero length and a method that does
2446 * not define any semantics for that request message-body, then an
2447 * origin server SHOULD either ignore the message-body or respond
2448 * with an appropriate error message (e.g., 413). A proxy or
2449 * gateway, when presented the same request, SHOULD either forward
2450 * the request inbound with the message- body or ignore the
2451 * message-body when determining a response.
2452 *
2453 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2454 * and the "chunked" transfer-coding (Section 6.2) is used, the
2455 * transfer-length is defined by the use of this transfer-coding.
2456 * If a Transfer-Encoding header field is present and the "chunked"
2457 * transfer-coding is not present, the transfer-length is defined
2458 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002459 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002460 * 3. If a Content-Length header field is present, its decimal value in
2461 * OCTETs represents both the entity-length and the transfer-length.
2462 * If a message is received with both a Transfer-Encoding header
2463 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002464 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002465 * 4. By the server closing the connection. (Closing the connection
2466 * cannot be used to indicate the end of a request body, since that
2467 * would leave no possibility for the server to send back a response.)
2468 *
2469 * Whenever a transfer-coding is applied to a message-body, the set of
2470 * transfer-codings MUST include "chunked", unless the message indicates
2471 * it is terminated by closing the connection. When the "chunked"
2472 * transfer-coding is used, it MUST be the last transfer-coding applied
2473 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 */
2475
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002476 /* CONNECT sets a tunnel and ignores everything else */
2477 if (txn->meth == HTTP_METH_CONNECT)
2478 goto skip_xfer_len;
2479
2480 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002481 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002482 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002483 while ((txn->flags & TX_REQ_VER_11) &&
2484 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002485 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2486 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2487 else if (txn->flags & TX_REQ_TE_CHNK) {
2488 /* bad transfer-encoding (chunked followed by something else) */
2489 use_close_only = 1;
2490 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2491 break;
2492 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 }
2494
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002496 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002497 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2498 signed long long cl;
2499
2500 if (!ctx.vlen)
2501 goto return_bad_req;
2502
2503 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2504 goto return_bad_req; /* parse failure */
2505
2506 if (cl < 0)
2507 goto return_bad_req;
2508
2509 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2510 goto return_bad_req; /* already specified, was different */
2511
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002512 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002513 msg->hdr_content_len = cl;
2514 }
2515
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002516 /* bodyless requests have a known length */
2517 if (!use_close_only)
2518 txn->flags |= TX_REQ_XFER_LEN;
2519
2520 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002522 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002523 req->analyse_exp = TICK_ETERNITY;
2524 return 1;
2525
2526 return_bad_req:
2527 /* We centralize bad requests processing here */
2528 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2529 /* we detected a parsing error. We want to archive this request
2530 * in the dedicated proxy area for later troubleshooting.
2531 */
2532 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2533 }
2534
2535 txn->req.msg_state = HTTP_MSG_ERROR;
2536 txn->status = 400;
2537 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002538
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002539 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002540 if (s->listener->counters)
2541 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002542
2543 return_prx_cond:
2544 if (!(s->flags & SN_ERR_MASK))
2545 s->flags |= SN_ERR_PRXCOND;
2546 if (!(s->flags & SN_FINST_MASK))
2547 s->flags |= SN_FINST_R;
2548
2549 req->analysers = 0;
2550 req->analyse_exp = TICK_ETERNITY;
2551 return 0;
2552}
2553
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002554/* This stream analyser runs all HTTP request processing which is common to
2555 * frontends and backends, which means blocking ACLs, filters, connection-close,
2556 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002557 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002558 * either needs more data or wants to immediately abort the request (eg: deny,
2559 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002560 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002561int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002562{
Willy Tarreaud787e662009-07-07 10:14:51 +02002563 struct http_txn *txn = &s->txn;
2564 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002565 struct acl_cond *cond;
2566 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002567 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002568 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002569
Willy Tarreau655dce92009-11-08 13:10:58 +01002570 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002571 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002572 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002573 return 0;
2574 }
2575
Willy Tarreau3a816292009-07-07 10:55:49 +02002576 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002577 req->analyse_exp = TICK_ETERNITY;
2578
2579 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2580 now_ms, __FUNCTION__,
2581 s,
2582 req,
2583 req->rex, req->wex,
2584 req->flags,
2585 req->l,
2586 req->analysers);
2587
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002588 /* first check whether we have some ACLs set to block this request */
2589 list_for_each_entry(cond, &px->block_cond, list) {
2590 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002591
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002592 ret = acl_pass(ret);
2593 if (cond->pol == ACL_COND_UNLESS)
2594 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002595
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002596 if (ret) {
2597 txn->status = 403;
2598 /* let's log the request time */
2599 s->logs.tv_request = now;
2600 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2601 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002602 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002603 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002604
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002605 /* try headers filters */
2606 if (px->req_exp != NULL) {
2607 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2608 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 /* has the request been denied ? */
2611 if (txn->flags & TX_CLDENY) {
2612 /* no need to go further */
2613 txn->status = 403;
2614 /* let's log the request time */
2615 s->logs.tv_request = now;
2616 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2617 goto return_prx_cond;
2618 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002619
2620 /* When a connection is tarpitted, we use the tarpit timeout,
2621 * which may be the same as the connect timeout if unspecified.
2622 * If unset, then set it to zero because we really want it to
2623 * eventually expire. We build the tarpit as an analyser.
2624 */
2625 if (txn->flags & TX_CLTARPIT) {
2626 buffer_erase(s->req);
2627 /* wipe the request out so that we can drop the connection early
2628 * if the client closes first.
2629 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002630 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002631 req->analysers = 0; /* remove switching rules etc... */
2632 req->analysers |= AN_REQ_HTTP_TARPIT;
2633 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2634 if (!req->analyse_exp)
2635 req->analyse_exp = tick_add(now_ms, 0);
2636 return 1;
2637 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002638 }
Willy Tarreau06619262006-12-17 08:37:22 +01002639
Willy Tarreau5b154472009-12-21 20:11:07 +01002640 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2641 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002642 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2643 * in which headers are mangled. However, if another mode is set, it will
2644 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002645 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002646
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002647 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002648 ((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 +01002649 int tmp = TX_CON_WANT_TUN;
2650 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2651 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002652 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2653 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002654 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002655 tmp = TX_CON_WANT_CLO;
2656
Willy Tarreau5b154472009-12-21 20:11:07 +01002657 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2658 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002659
Willy Tarreau8db1c172010-01-05 23:12:12 +01002660 if (!(txn->flags & TX_CON_HDR_PARS))
2661 http_req_parse_connection_header(txn);
2662
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002663 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2664 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2665 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2666 if (!(txn->flags & TX_REQ_XFER_LEN))
2667 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2668 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002669 }
2670
2671 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2672 * once we know the backend, because the tunnel mode can be implied by the
2673 * lack of any close/keepalive options in both the FE and the BE. Since
2674 * this information can evolve with time, we proceed by trying to make the
2675 * header status match the desired status. For this, we'll have to adjust
2676 * the "Connection" header. The test for persistent connections has already
2677 * been performed, so we only enter here if there is a risk the connection
2678 * is considered as persistent and we want it to be closed on the server
2679 * side. It would be nice if we could enter this place only when a
2680 * Connection header exists. Note that a CONNECT method will not enter
2681 * here.
2682 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002683 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2684 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2685 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002686 char *cur_ptr, *cur_end, *cur_next;
2687 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002688 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002689 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002690
Willy Tarreau5b154472009-12-21 20:11:07 +01002691 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002692 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002693
Willy Tarreau5b154472009-12-21 20:11:07 +01002694 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002695 cur_hdr = &txn->hdr_idx.v[cur_idx];
2696 cur_ptr = cur_next;
2697 cur_end = cur_ptr + cur_hdr->len;
2698 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002699
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002700 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002701 if (!val)
2702 continue;
2703
2704 /* 3 possibilities :
2705 * - we have already set "Connection: close" or we're in
2706 * HTTP/1.0, so we remove this line.
2707 * - we have not yet set "Connection: close", but this line
2708 * indicates close. We leave it untouched and set the flag.
2709 * - we have not yet set "Connection: close", and this line
2710 * indicates non-close. We replace it and set the flag.
2711 */
2712 if (must_delete) {
2713 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2714 http_msg_move_end(&txn->req, delta);
2715 cur_next += delta;
2716 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2717 txn->hdr_idx.used--;
2718 cur_hdr->len = 0;
2719 txn->flags |= TX_REQ_CONN_CLO;
2720 } else {
2721 if (cur_end - cur_ptr - val != 5 ||
2722 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2723 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2724 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002725 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002726 cur_hdr->len += delta;
2727 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002728 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002729 txn->flags |= TX_REQ_CONN_CLO;
2730 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002731 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002732 } /* for loop */
2733 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002734
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002735 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002736 list_for_each_entry(wl, &px->req_add, list) {
2737 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002738 goto return_bad_req;
2739 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002740
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002741 /* check if stats URI was requested, and if an auth is needed */
2742 if (px->uri_auth != NULL &&
2743 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2744 /* we have to check the URI and auth for this request.
2745 * FIXME!!! that one is rather dangerous, we want to
2746 * make it follow standard rules (eg: clear req->analysers).
2747 */
2748 if (stats_check_uri_auth(s, px)) {
2749 req->analysers = 0;
2750 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002753
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 /* check whether we have some ACLs set to redirect this request */
2755 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002756 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002757
Willy Tarreauf285f542010-01-03 20:03:03 +01002758 if (rule->cond) {
2759 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2760 ret = acl_pass(ret);
2761 if (rule->cond->pol == ACL_COND_UNLESS)
2762 ret = !ret;
2763 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002764
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002766 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002768
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769 /* build redirect message */
2770 switch(rule->code) {
2771 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 msg_fmt = HTTP_303;
2773 break;
2774 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 msg_fmt = HTTP_301;
2776 break;
2777 case 302:
2778 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002779 msg_fmt = HTTP_302;
2780 break;
2781 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002782
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002783 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002784 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002785
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 switch(rule->type) {
2787 case REDIRECT_TYPE_PREFIX: {
2788 const char *path;
2789 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002790
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791 path = http_get_path(txn);
2792 /* build message using path */
2793 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002794 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 +02002795 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2796 int qs = 0;
2797 while (qs < pathlen) {
2798 if (path[qs] == '?') {
2799 pathlen = qs;
2800 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002801 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002802 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002803 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002804 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002805 } else {
2806 path = "/";
2807 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002808 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002809
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002810 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 goto return_bad_req;
2812
2813 /* add prefix. Note that if prefix == "/", we don't want to
2814 * add anything, otherwise it makes it hard for the user to
2815 * configure a self-redirection.
2816 */
2817 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002818 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2819 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002820 }
2821
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002822 /* add path */
2823 memcpy(rdr.str + rdr.len, path, pathlen);
2824 rdr.len += pathlen;
2825 break;
2826 }
2827 case REDIRECT_TYPE_LOCATION:
2828 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002829 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002830 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002831
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002832 /* add location */
2833 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2834 rdr.len += rule->rdr_len;
2835 break;
2836 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002837
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002838 if (rule->cookie_len) {
2839 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2840 rdr.len += 14;
2841 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2842 rdr.len += rule->cookie_len;
2843 memcpy(rdr.str + rdr.len, "\r\n", 2);
2844 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002845 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002846
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002847 /* add end of headers and the keep-alive/close status.
2848 * We may choose to set keep-alive if the Location begins
2849 * with a slash, because the client will come back to the
2850 * same server.
2851 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002852 txn->status = rule->code;
2853 /* let's log the request time */
2854 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002855
2856 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2857 (txn->flags & TX_REQ_XFER_LEN) &&
2858 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2859 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2860 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2861 /* keep-alive possible */
2862 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2863 rdr.len += 28;
2864 buffer_write(req->prod->ob, rdr.str, rdr.len);
2865 /* "eat" the request */
2866 buffer_ignore(req, msg->sov - msg->som);
2867 msg->som = msg->sov;
2868 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002869 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2870 txn->req.msg_state = HTTP_MSG_CLOSED;
2871 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002872 break;
2873 } else {
2874 /* keep-alive not possible */
2875 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2876 rdr.len += 23;
Willy Tarreauea65e682010-01-08 00:26:50 +01002877 buffer_write(req->prod->ob, rdr.str, rdr.len);
2878 stream_int_cond_close(req->prod, NULL);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002879 goto return_prx_cond;
2880 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002881 }
2882 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002883
Willy Tarreau2be39392010-01-03 17:24:51 +01002884 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2885 * If this happens, then the data will not come immediately, so we must
2886 * send all what we have without waiting. Note that due to the small gain
2887 * in waiting for the body of the request, it's easier to simply put the
2888 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2889 * itself once used.
2890 */
2891 req->flags |= BF_SEND_DONTWAIT;
2892
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 /* that's OK for us now, let's move on to next analysers */
2894 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002895
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002896 return_bad_req:
2897 /* We centralize bad requests processing here */
2898 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2899 /* we detected a parsing error. We want to archive this request
2900 * in the dedicated proxy area for later troubleshooting.
2901 */
2902 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2903 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002904
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002905 txn->req.msg_state = HTTP_MSG_ERROR;
2906 txn->status = 400;
2907 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002908
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002909 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002910 if (s->listener->counters)
2911 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002912
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002913 return_prx_cond:
2914 if (!(s->flags & SN_ERR_MASK))
2915 s->flags |= SN_ERR_PRXCOND;
2916 if (!(s->flags & SN_FINST_MASK))
2917 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002918
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002919 req->analysers = 0;
2920 req->analyse_exp = TICK_ETERNITY;
2921 return 0;
2922}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002923
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002924/* This function performs all the processing enabled for the current request.
2925 * It returns 1 if the processing can continue on next analysers, or zero if it
2926 * needs more data, encounters an error, or wants to immediately abort the
2927 * request. It relies on buffers flags, and updates s->req->analysers.
2928 */
2929int http_process_request(struct session *s, struct buffer *req, int an_bit)
2930{
2931 struct http_txn *txn = &s->txn;
2932 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002933
Willy Tarreau655dce92009-11-08 13:10:58 +01002934 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002935 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002936 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002937 return 0;
2938 }
2939
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002940 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2941 now_ms, __FUNCTION__,
2942 s,
2943 req,
2944 req->rex, req->wex,
2945 req->flags,
2946 req->l,
2947 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002948
Willy Tarreau59234e92008-11-30 23:51:27 +01002949 /*
2950 * Right now, we know that we have processed the entire headers
2951 * and that unwanted requests have been filtered out. We can do
2952 * whatever we want with the remaining request. Also, now we
2953 * may have separate values for ->fe, ->be.
2954 */
Willy Tarreau06619262006-12-17 08:37:22 +01002955
Willy Tarreau59234e92008-11-30 23:51:27 +01002956 /*
2957 * If HTTP PROXY is set we simply get remote server address
2958 * parsing incoming request.
2959 */
2960 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2961 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2962 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002963
Willy Tarreau59234e92008-11-30 23:51:27 +01002964 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002965 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002966 * Note that doing so might move headers in the request, but
2967 * the fields will stay coherent and the URI will not move.
2968 * This should only be performed in the backend.
2969 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002970 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002971 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2972 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002973
Willy Tarreau59234e92008-11-30 23:51:27 +01002974 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002975 * 8: the appsession cookie was looked up very early in 1.2,
2976 * so let's do the same now.
2977 */
2978
2979 /* It needs to look into the URI */
2980 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002981 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002982 }
2983
2984 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 * 9: add X-Forwarded-For if either the frontend or the backend
2986 * asks for it.
2987 */
2988 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2989 if (s->cli_addr.ss_family == AF_INET) {
2990 /* Add an X-Forwarded-For header unless the source IP is
2991 * in the 'except' network range.
2992 */
2993 if ((!s->fe->except_mask.s_addr ||
2994 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2995 != s->fe->except_net.s_addr) &&
2996 (!s->be->except_mask.s_addr ||
2997 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2998 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002999 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003000 unsigned char *pn;
3001 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003002
3003 /* Note: we rely on the backend to get the header name to be used for
3004 * x-forwarded-for, because the header is really meant for the backends.
3005 * However, if the backend did not specify any option, we have to rely
3006 * on the frontend's header name.
3007 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003008 if (s->be->fwdfor_hdr_len) {
3009 len = s->be->fwdfor_hdr_len;
3010 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003011 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 len = s->fe->fwdfor_hdr_len;
3013 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003014 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003015 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003016
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003017 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003018 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003019 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003020 }
3021 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003022 else if (s->cli_addr.ss_family == AF_INET6) {
3023 /* FIXME: for the sake of completeness, we should also support
3024 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003025 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003026 int len;
3027 char pn[INET6_ADDRSTRLEN];
3028 inet_ntop(AF_INET6,
3029 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3030 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003031
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 /* Note: we rely on the backend to get the header name to be used for
3033 * x-forwarded-for, because the header is really meant for the backends.
3034 * However, if the backend did not specify any option, we have to rely
3035 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003036 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003037 if (s->be->fwdfor_hdr_len) {
3038 len = s->be->fwdfor_hdr_len;
3039 memcpy(trash, s->be->fwdfor_hdr_name, len);
3040 } else {
3041 len = s->fe->fwdfor_hdr_len;
3042 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003043 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003044 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003045
Willy Tarreau59234e92008-11-30 23:51:27 +01003046 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003047 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003048 goto return_bad_req;
3049 }
3050 }
3051
3052 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003053 * 10: add X-Original-To if either the frontend or the backend
3054 * asks for it.
3055 */
3056 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3057
3058 /* FIXME: don't know if IPv6 can handle that case too. */
3059 if (s->cli_addr.ss_family == AF_INET) {
3060 /* Add an X-Original-To header unless the destination IP is
3061 * in the 'except' network range.
3062 */
3063 if (!(s->flags & SN_FRT_ADDR_SET))
3064 get_frt_addr(s);
3065
3066 if ((!s->fe->except_mask_to.s_addr ||
3067 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3068 != s->fe->except_to.s_addr) &&
3069 (!s->be->except_mask_to.s_addr ||
3070 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3071 != s->be->except_to.s_addr)) {
3072 int len;
3073 unsigned char *pn;
3074 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3075
3076 /* Note: we rely on the backend to get the header name to be used for
3077 * x-original-to, because the header is really meant for the backends.
3078 * However, if the backend did not specify any option, we have to rely
3079 * on the frontend's header name.
3080 */
3081 if (s->be->orgto_hdr_len) {
3082 len = s->be->orgto_hdr_len;
3083 memcpy(trash, s->be->orgto_hdr_name, len);
3084 } else {
3085 len = s->fe->orgto_hdr_len;
3086 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003087 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003088 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3089
3090 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003091 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003092 goto return_bad_req;
3093 }
3094 }
3095 }
3096
Willy Tarreau78599912009-10-17 20:12:21 +02003097 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003098 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3099 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3100 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003101 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003102 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003103 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003104 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003105 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003106
3107 /* If we have no server assigned yet and we're balancing on url_param
3108 * with a POST request, we may be interested in checking the body for
3109 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003110 */
3111 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3112 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003113 s->be->url_param_post_limit != 0 &&
3114 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003115 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003116 buffer_dont_connect(req);
3117 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003118 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003119
Willy Tarreaud98cf932009-12-27 22:54:55 +01003120 if (txn->flags & TX_REQ_XFER_LEN)
3121 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003122
Willy Tarreau59234e92008-11-30 23:51:27 +01003123 /*************************************************************
3124 * OK, that's finished for the headers. We have done what we *
3125 * could. Let's switch to the DATA state. *
3126 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003127 req->analyse_exp = TICK_ETERNITY;
3128 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003129
Willy Tarreau59234e92008-11-30 23:51:27 +01003130 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003131 /* OK let's go on with the BODY now */
3132 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003133
Willy Tarreau59234e92008-11-30 23:51:27 +01003134 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003135 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003136 /* we detected a parsing error. We want to archive this request
3137 * in the dedicated proxy area for later troubleshooting.
3138 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003139 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003140 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003141
Willy Tarreau59234e92008-11-30 23:51:27 +01003142 txn->req.msg_state = HTTP_MSG_ERROR;
3143 txn->status = 400;
3144 req->analysers = 0;
3145 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003146
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003147 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003148 if (s->listener->counters)
3149 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003150
Willy Tarreau59234e92008-11-30 23:51:27 +01003151 if (!(s->flags & SN_ERR_MASK))
3152 s->flags |= SN_ERR_PRXCOND;
3153 if (!(s->flags & SN_FINST_MASK))
3154 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003155 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003156}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003157
Willy Tarreau60b85b02008-11-30 23:28:40 +01003158/* This function is an analyser which processes the HTTP tarpit. It always
3159 * returns zero, at the beginning because it prevents any other processing
3160 * from occurring, and at the end because it terminates the request.
3161 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003162int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003163{
3164 struct http_txn *txn = &s->txn;
3165
3166 /* This connection is being tarpitted. The CLIENT side has
3167 * already set the connect expiration date to the right
3168 * timeout. We just have to check that the client is still
3169 * there and that the timeout has not expired.
3170 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003171 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003172 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3173 !tick_is_expired(req->analyse_exp, now_ms))
3174 return 0;
3175
3176 /* We will set the queue timer to the time spent, just for
3177 * logging purposes. We fake a 500 server error, so that the
3178 * attacker will not suspect his connection has been tarpitted.
3179 * It will not cause trouble to the logs because we can exclude
3180 * the tarpitted connections by filtering on the 'PT' status flags.
3181 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003182 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3183
3184 txn->status = 500;
3185 if (req->flags != BF_READ_ERROR)
3186 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3187
3188 req->analysers = 0;
3189 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003190
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003191 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003192 if (s->listener->counters)
3193 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003194
Willy Tarreau60b85b02008-11-30 23:28:40 +01003195 if (!(s->flags & SN_ERR_MASK))
3196 s->flags |= SN_ERR_PRXCOND;
3197 if (!(s->flags & SN_FINST_MASK))
3198 s->flags |= SN_FINST_T;
3199 return 0;
3200}
3201
Willy Tarreaud34af782008-11-30 23:36:37 +01003202/* This function is an analyser which processes the HTTP request body. It looks
3203 * for parameters to be used for the load balancing algorithm (url_param). It
3204 * must only be called after the standard HTTP request processing has occurred,
3205 * because it expects the request to be parsed. It returns zero if it needs to
3206 * read more data, or 1 once it has completed its analysis.
3207 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003208int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003209{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003210 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003211 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003212 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003213
3214 /* We have to parse the HTTP request body to find any required data.
3215 * "balance url_param check_post" should have been the only way to get
3216 * into this. We were brought here after HTTP header analysis, so all
3217 * related structures are ready.
3218 */
3219
Willy Tarreau522d6c02009-12-06 18:49:18 +01003220 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3221 goto missing_data;
3222
3223 if (msg->msg_state < HTTP_MSG_100_SENT) {
3224 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3225 * send an HTTP/1.1 100 Continue intermediate response.
3226 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003227 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003228 struct hdr_ctx ctx;
3229 ctx.idx = 0;
3230 /* Expect is allowed in 1.1, look for it */
3231 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3232 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3233 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3234 }
3235 }
3236 msg->msg_state = HTTP_MSG_100_SENT;
3237 }
3238
3239 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003240 /* we have msg->col and msg->sov which both point to the first
3241 * byte of message body. msg->som still points to the beginning
3242 * of the message. We must save the body in req->lr because it
3243 * survives buffer re-alignments.
3244 */
3245 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003246 if (txn->flags & TX_REQ_TE_CHNK)
3247 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3248 else
3249 msg->msg_state = HTTP_MSG_DATA;
3250 }
3251
3252 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003253 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003254 * set ->sov and ->lr to point to the body and switch to DATA or
3255 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003256 */
3257 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003258
Willy Tarreau115acb92009-12-26 13:56:06 +01003259 if (!ret)
3260 goto missing_data;
3261 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003262 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003263 }
3264
Willy Tarreaud98cf932009-12-27 22:54:55 +01003265 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003266 * We have the first non-header byte in msg->col, which is either the
3267 * beginning of the chunk size or of the data. The first data byte is in
3268 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3269 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003270 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003271
3272 if (msg->hdr_content_len < limit)
3273 limit = msg->hdr_content_len;
3274
Willy Tarreau7c96f672009-12-27 22:47:25 +01003275 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003276 goto http_end;
3277
3278 missing_data:
3279 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003280 if (req->flags & BF_FULL)
3281 goto return_bad_req;
3282
Willy Tarreau522d6c02009-12-06 18:49:18 +01003283 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3284 txn->status = 408;
3285 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3286 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003287 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003288
3289 /* we get here if we need to wait for more data */
3290 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003291 /* Not enough data. We'll re-use the http-request
3292 * timeout here. Ideally, we should set the timeout
3293 * relative to the accept() date. We just set the
3294 * request timeout once at the beginning of the
3295 * request.
3296 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003297 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003298 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003299 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003300 return 0;
3301 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003302
3303 http_end:
3304 /* The situation will not evolve, so let's give up on the analysis. */
3305 s->logs.tv_request = now; /* update the request timer to reflect full request */
3306 req->analysers &= ~an_bit;
3307 req->analyse_exp = TICK_ETERNITY;
3308 return 1;
3309
3310 return_bad_req: /* let's centralize all bad requests */
3311 txn->req.msg_state = HTTP_MSG_ERROR;
3312 txn->status = 400;
3313 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3314
3315 return_err_msg:
3316 req->analysers = 0;
3317 s->fe->counters.failed_req++;
3318 if (s->listener->counters)
3319 s->listener->counters->failed_req++;
3320
3321 if (!(s->flags & SN_ERR_MASK))
3322 s->flags |= SN_ERR_PRXCOND;
3323 if (!(s->flags & SN_FINST_MASK))
3324 s->flags |= SN_FINST_R;
3325 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003326}
3327
Willy Tarreau610ecce2010-01-04 21:15:02 +01003328/* Terminate current transaction and prepare a new one. This is very tricky
3329 * right now but it works.
3330 */
3331void http_end_txn_clean_session(struct session *s)
3332{
3333 /* FIXME: We need a more portable way of releasing a backend's and a
3334 * server's connections. We need a safer way to reinitialize buffer
3335 * flags. We also need a more accurate method for computing per-request
3336 * data.
3337 */
3338 http_silent_debug(__LINE__, s);
3339
3340 s->req->cons->flags |= SI_FL_NOLINGER;
3341 s->req->cons->shutr(s->req->cons);
3342 s->req->cons->shutw(s->req->cons);
3343
3344 http_silent_debug(__LINE__, s);
3345
3346 if (s->flags & SN_BE_ASSIGNED)
3347 s->be->beconn--;
3348
3349 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3350 session_process_counters(s);
3351
3352 if (s->txn.status) {
3353 int n;
3354
3355 n = s->txn.status / 100;
3356 if (n < 1 || n > 5)
3357 n = 0;
3358
3359 if (s->fe->mode == PR_MODE_HTTP)
3360 s->fe->counters.p.http.rsp[n]++;
3361
3362 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3363 (s->be->mode == PR_MODE_HTTP))
3364 s->be->counters.p.http.rsp[n]++;
3365 }
3366
3367 /* don't count other requests' data */
3368 s->logs.bytes_in -= s->req->l - s->req->send_max;
3369 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3370
3371 /* let's do a final log if we need it */
3372 if (s->logs.logwait &&
3373 !(s->flags & SN_MONITOR) &&
3374 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3375 s->do_log(s);
3376 }
3377
3378 s->logs.accept_date = date; /* user-visible date for logging */
3379 s->logs.tv_accept = now; /* corrected date for internal use */
3380 tv_zero(&s->logs.tv_request);
3381 s->logs.t_queue = -1;
3382 s->logs.t_connect = -1;
3383 s->logs.t_data = -1;
3384 s->logs.t_close = 0;
3385 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3386 s->logs.srv_queue_size = 0; /* we will get this number soon */
3387
3388 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3389 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3390
3391 if (s->pend_pos)
3392 pendconn_free(s->pend_pos);
3393
3394 if (s->srv) {
3395 if (s->flags & SN_CURR_SESS) {
3396 s->flags &= ~SN_CURR_SESS;
3397 s->srv->cur_sess--;
3398 }
3399 if (may_dequeue_tasks(s->srv, s->be))
3400 process_srv_queue(s->srv);
3401 }
3402
3403 if (unlikely(s->srv_conn))
3404 sess_change_server(s, NULL);
3405 s->srv = NULL;
3406
3407 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3408 s->req->cons->fd = -1; /* just to help with debugging */
3409 s->req->cons->err_type = SI_ET_NONE;
3410 s->req->cons->err_loc = NULL;
3411 s->req->cons->exp = TICK_ETERNITY;
3412 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003413 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3414 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 +01003415 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3416 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3417 s->txn.meth = 0;
3418 http_reset_txn(s);
3419 s->txn.flags |= TX_NOT_FIRST;
3420 if (s->be->options2 & PR_O2_INDEPSTR)
3421 s->req->cons->flags |= SI_FL_INDEP_STR;
3422
3423 /* if the request buffer is not empty, it means we're
3424 * about to process another request, so send pending
3425 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003426 * Just don't do this if the buffer is close to be full,
3427 * because the request will wait for it to flush a little
3428 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003429 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003430 if (s->req->l > s->req->send_max) {
3431 if (s->rep->send_max &&
3432 !(s->rep->flags & BF_FULL) &&
3433 s->rep->lr <= s->rep->r &&
3434 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3435 s->rep->flags |= BF_EXPECT_MORE;
3436 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003437
3438 /* we're removing the analysers, we MUST re-enable events detection */
3439 buffer_auto_read(s->req);
3440 buffer_auto_close(s->req);
3441 buffer_auto_read(s->rep);
3442 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003443
3444 /* make ->lr point to the first non-forwarded byte */
3445 s->req->lr = s->req->w + s->req->send_max;
3446 if (s->req->lr >= s->req->data + s->req->size)
3447 s->req->lr -= s->req->size;
3448 s->rep->lr = s->rep->w + s->rep->send_max;
3449 if (s->rep->lr >= s->rep->data + s->rep->size)
3450 s->rep->lr -= s->req->size;
3451
3452 s->req->analysers |= s->fe->fe_req_ana;
3453 s->rep->analysers = 0;
3454
3455 http_silent_debug(__LINE__, s);
3456}
3457
3458
3459/* This function updates the request state machine according to the response
3460 * state machine and buffer flags. It returns 1 if it changes anything (flag
3461 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3462 * it is only used to find when a request/response couple is complete. Both
3463 * this function and its equivalent should loop until both return zero. It
3464 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3465 */
3466int http_sync_req_state(struct session *s)
3467{
3468 struct buffer *buf = s->req;
3469 struct http_txn *txn = &s->txn;
3470 unsigned int old_flags = buf->flags;
3471 unsigned int old_state = txn->req.msg_state;
3472
3473 http_silent_debug(__LINE__, s);
3474 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3475 return 0;
3476
3477 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003478 /* No need to read anymore, the request was completely parsed.
3479 * We can shut the read side unless we want to abort_on_close.
3480 */
3481 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3482 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003483
3484 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3485 goto wait_other_side;
3486
3487 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3488 /* The server has not finished to respond, so we
3489 * don't want to move in order not to upset it.
3490 */
3491 goto wait_other_side;
3492 }
3493
3494 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3495 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003496 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003497 txn->req.msg_state = HTTP_MSG_TUNNEL;
3498 goto wait_other_side;
3499 }
3500
3501 /* When we get here, it means that both the request and the
3502 * response have finished receiving. Depending on the connection
3503 * mode, we'll have to wait for the last bytes to leave in either
3504 * direction, and sometimes for a close to be effective.
3505 */
3506
3507 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3508 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3509 /* Server-close mode : queue a connection close to the server */
3510 buffer_shutw_now(buf);
3511 buf->cons->flags |= SI_FL_NOLINGER;
3512 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003513 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003514 /* Option forceclose is set, let's enforce it now
3515 * that we're not expecting any new data to come.
3516 */
3517 buffer_shutr_now(buf);
3518 buffer_shutw_now(buf);
3519 buf->cons->flags |= SI_FL_NOLINGER;
3520 }
3521 /* other modes include httpclose (no action) and keepalive (not implemented) */
3522 }
3523
3524 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3525 /* if we've just closed an output, let's switch */
3526 if (!(buf->flags & BF_OUT_EMPTY)) {
3527 txn->req.msg_state = HTTP_MSG_CLOSING;
3528 goto http_msg_closing;
3529 }
3530 else {
3531 txn->req.msg_state = HTTP_MSG_CLOSED;
3532 goto http_msg_closed;
3533 }
3534 }
3535 else {
3536 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003537 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003538 txn->req.msg_state = HTTP_MSG_TUNNEL;
3539 goto wait_other_side;
3540 }
3541 }
3542
3543 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3544 http_msg_closing:
3545 /* nothing else to forward, just waiting for the output buffer
3546 * to be empty and for the shutw_now to take effect.
3547 */
3548 if (buf->flags & BF_OUT_EMPTY) {
3549 txn->req.msg_state = HTTP_MSG_CLOSED;
3550 goto http_msg_closed;
3551 }
3552 else if (buf->flags & BF_SHUTW) {
3553 txn->req.msg_state = HTTP_MSG_ERROR;
3554 goto wait_other_side;
3555 }
3556 }
3557
3558 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3559 http_msg_closed:
3560 goto wait_other_side;
3561 }
3562
3563 wait_other_side:
3564 http_silent_debug(__LINE__, s);
3565 return txn->req.msg_state != old_state || buf->flags != old_flags;
3566}
3567
3568
3569/* This function updates the response state machine according to the request
3570 * state machine and buffer flags. It returns 1 if it changes anything (flag
3571 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3572 * it is only used to find when a request/response couple is complete. Both
3573 * this function and its equivalent should loop until both return zero. It
3574 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3575 */
3576int http_sync_res_state(struct session *s)
3577{
3578 struct buffer *buf = s->rep;
3579 struct http_txn *txn = &s->txn;
3580 unsigned int old_flags = buf->flags;
3581 unsigned int old_state = txn->rsp.msg_state;
3582
3583 http_silent_debug(__LINE__, s);
3584 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3585 return 0;
3586
3587 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3588 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003589 * still monitor the server connection for a possible close
3590 * while the request is being uploaded, so we don't disable
3591 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003592 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003593 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003594
3595 if (txn->req.msg_state == HTTP_MSG_ERROR)
3596 goto wait_other_side;
3597
3598 if (txn->req.msg_state < HTTP_MSG_DONE) {
3599 /* The client seems to still be sending data, probably
3600 * because we got an error response during an upload.
3601 * We have the choice of either breaking the connection
3602 * or letting it pass through. Let's do the later.
3603 */
3604 goto wait_other_side;
3605 }
3606
3607 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3608 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003609 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003610 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3611 goto wait_other_side;
3612 }
3613
3614 /* When we get here, it means that both the request and the
3615 * response have finished receiving. Depending on the connection
3616 * mode, we'll have to wait for the last bytes to leave in either
3617 * direction, and sometimes for a close to be effective.
3618 */
3619
3620 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3621 /* Server-close mode : shut read and wait for the request
3622 * side to close its output buffer. The caller will detect
3623 * when we're in DONE and the other is in CLOSED and will
3624 * catch that for the final cleanup.
3625 */
3626 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3627 buffer_shutr_now(buf);
3628 goto wait_other_side;
3629 }
3630 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3631 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3632 /* Option forceclose is set, let's enforce it now
3633 * that we're not expecting any new data to come.
3634 * The caller knows the session is complete once
3635 * both states are CLOSED.
3636 */
3637 buffer_shutr_now(buf);
3638 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003639 }
3640 else {
3641 /* other modes include httpclose (no action) and keepalive
3642 * (not implemented). These modes are used as a tunnel right
3643 * now.
3644 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003645 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003646 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3647 goto wait_other_side;
3648 }
3649
3650 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3651 /* if we've just closed an output, let's switch */
3652 if (!(buf->flags & BF_OUT_EMPTY)) {
3653 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3654 goto http_msg_closing;
3655 }
3656 else {
3657 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3658 goto http_msg_closed;
3659 }
3660 }
3661 goto wait_other_side;
3662 }
3663
3664 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3665 http_msg_closing:
3666 /* nothing else to forward, just waiting for the output buffer
3667 * to be empty and for the shutw_now to take effect.
3668 */
3669 if (buf->flags & BF_OUT_EMPTY) {
3670 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3671 goto http_msg_closed;
3672 }
3673 else if (buf->flags & BF_SHUTW) {
3674 txn->rsp.msg_state = HTTP_MSG_ERROR;
3675 goto wait_other_side;
3676 }
3677 }
3678
3679 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3680 http_msg_closed:
3681 /* drop any pending data */
3682 buffer_ignore(buf, buf->l - buf->send_max);
3683 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003684 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003685 goto wait_other_side;
3686 }
3687
3688 wait_other_side:
3689 http_silent_debug(__LINE__, s);
3690 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3691}
3692
3693
3694/* Resync the request and response state machines. Return 1 if either state
3695 * changes.
3696 */
3697int http_resync_states(struct session *s)
3698{
3699 struct http_txn *txn = &s->txn;
3700 int old_req_state = txn->req.msg_state;
3701 int old_res_state = txn->rsp.msg_state;
3702
3703 http_silent_debug(__LINE__, s);
3704 http_sync_req_state(s);
3705 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003706 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003707 if (!http_sync_res_state(s))
3708 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003709 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003710 if (!http_sync_req_state(s))
3711 break;
3712 }
3713 http_silent_debug(__LINE__, s);
3714 /* OK, both state machines agree on a compatible state.
3715 * There are a few cases we're interested in :
3716 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3717 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3718 * directions, so let's simply disable both analysers.
3719 * - HTTP_MSG_CLOSED on the response only means we must abort the
3720 * request.
3721 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3722 * with server-close mode means we've completed one request and we
3723 * must re-initialize the server connection.
3724 */
3725
3726 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3727 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3728 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3729 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3730 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003731 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003732 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003734 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003735 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003737 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3738 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3739 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003740 s->rep->analysers = 0;
3741 buffer_auto_close(s->rep);
3742 buffer_auto_read(s->rep);
3743 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003744 buffer_abort(s->req);
3745 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003746 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003747 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748 }
3749 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3750 txn->rsp.msg_state == HTTP_MSG_DONE &&
3751 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3752 /* server-close: terminate this server connection and
3753 * reinitialize a fresh-new transaction.
3754 */
3755 http_end_txn_clean_session(s);
3756 }
3757
3758 http_silent_debug(__LINE__, s);
3759 return txn->req.msg_state != old_req_state ||
3760 txn->rsp.msg_state != old_res_state;
3761}
3762
Willy Tarreaud98cf932009-12-27 22:54:55 +01003763/* This function is an analyser which forwards request body (including chunk
3764 * sizes if any). It is called as soon as we must forward, even if we forward
3765 * zero byte. The only situation where it must not be called is when we're in
3766 * tunnel mode and we want to forward till the close. It's used both to forward
3767 * remaining data and to resync after end of body. It expects the msg_state to
3768 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3769 * read more data, or 1 once we can go on with next request or end the session.
3770 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3771 * bytes of pending data + the headers if not already done (between som and sov).
3772 * It eventually adjusts som to match sov after the data in between have been sent.
3773 */
3774int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3775{
3776 struct http_txn *txn = &s->txn;
3777 struct http_msg *msg = &s->txn.req;
3778
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003779 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3780 return 0;
3781
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003782 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3783 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3784 /* Output closed while we were sending data. We must abort. */
3785 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003786 buffer_auto_read(req);
3787 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003788 req->analysers &= ~an_bit;
3789 return 1;
3790 }
3791
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003792 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003793
3794 /* Note that we don't have to send 100-continue back because we don't
3795 * need the data to complete our job, and it's up to the server to
3796 * decide whether to return 100, 417 or anything else in return of
3797 * an "Expect: 100-continue" header.
3798 */
3799
3800 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3801 /* we have msg->col and msg->sov which both point to the first
3802 * byte of message body. msg->som still points to the beginning
3803 * of the message. We must save the body in req->lr because it
3804 * survives buffer re-alignments.
3805 */
3806 req->lr = req->data + msg->sov;
3807 if (txn->flags & TX_REQ_TE_CHNK)
3808 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3809 else {
3810 msg->msg_state = HTTP_MSG_DATA;
3811 }
3812 }
3813
Willy Tarreaud98cf932009-12-27 22:54:55 +01003814 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003816 /* we may have some data pending */
3817 if (msg->hdr_content_len || msg->som != msg->sov) {
3818 int bytes = msg->sov - msg->som;
3819 if (bytes < 0) /* sov may have wrapped at the end */
3820 bytes += req->size;
3821 buffer_forward(req, bytes + msg->hdr_content_len);
3822 msg->hdr_content_len = 0; /* don't forward that again */
3823 msg->som = msg->sov;
3824 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003825
Willy Tarreaucaabe412010-01-03 23:08:28 +01003826 if (msg->msg_state == HTTP_MSG_DATA) {
3827 /* must still forward */
3828 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003829 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003830
3831 /* nothing left to forward */
3832 if (txn->flags & TX_REQ_TE_CHNK)
3833 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003834 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003835 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003836 }
3837 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003838 /* read the chunk size and assign it to ->hdr_content_len, then
3839 * set ->sov and ->lr to point to the body and switch to DATA or
3840 * TRAILERS state.
3841 */
3842 int ret = http_parse_chunk_size(req, msg);
3843
3844 if (!ret)
3845 goto missing_data;
3846 else if (ret < 0)
3847 goto return_bad_req;
3848 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003849 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003850 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3851 /* we want the CRLF after the data */
3852 int ret;
3853
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003854 req->lr = req->w + req->send_max;
3855 if (req->lr >= req->data + req->size)
3856 req->lr -= req->size;
3857
Willy Tarreaud98cf932009-12-27 22:54:55 +01003858 ret = http_skip_chunk_crlf(req, msg);
3859
3860 if (ret == 0)
3861 goto missing_data;
3862 else if (ret < 0)
3863 goto return_bad_req;
3864 /* we're in MSG_CHUNK_SIZE now */
3865 }
3866 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3867 int ret = http_forward_trailers(req, msg);
3868
3869 if (ret == 0)
3870 goto missing_data;
3871 else if (ret < 0)
3872 goto return_bad_req;
3873 /* we're in HTTP_MSG_DONE now */
3874 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003875 else {
3876 /* other states, DONE...TUNNEL */
3877 if (http_resync_states(s)) {
3878 /* some state changes occurred, maybe the analyser
3879 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003880 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003881 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3882 goto return_bad_req;
3883 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003884 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003886 }
3887 }
3888
Willy Tarreaud98cf932009-12-27 22:54:55 +01003889 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003890 /* stop waiting for data if the input is closed before the end */
3891 if (req->flags & BF_SHUTR)
3892 goto return_bad_req;
3893
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003894 /* waiting for the last bits to leave the buffer */
3895 if (req->flags & BF_SHUTW)
3896 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003897
3898 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003899 return 0;
3900
Willy Tarreaud98cf932009-12-27 22:54:55 +01003901 return_bad_req: /* let's centralize all bad requests */
3902 txn->req.msg_state = HTTP_MSG_ERROR;
3903 txn->status = 400;
3904 /* Note: we don't send any error if some data were already sent */
3905 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3906
Willy Tarreau90deb182010-01-07 00:20:41 +01003907 buffer_auto_read(req);
3908 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003909 req->analysers = 0;
3910 s->fe->counters.failed_req++;
3911 if (s->listener->counters)
3912 s->listener->counters->failed_req++;
3913
3914 if (!(s->flags & SN_ERR_MASK))
3915 s->flags |= SN_ERR_PRXCOND;
3916 if (!(s->flags & SN_FINST_MASK))
3917 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003919 return 0;
3920}
3921
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003922/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3923 * processing can continue on next analysers, or zero if it either needs more
3924 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3925 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3926 * when it has nothing left to do, and may remove any analyser when it wants to
3927 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003928 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003929int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003930{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003931 struct http_txn *txn = &s->txn;
3932 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003933 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003934 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003935 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003936 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003937
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003938 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 +02003939 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003940 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003941 rep,
3942 rep->rex, rep->wex,
3943 rep->flags,
3944 rep->l,
3945 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003946
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003947 /*
3948 * Now parse the partial (or complete) lines.
3949 * We will check the response syntax, and also join multi-line
3950 * headers. An index of all the lines will be elaborated while
3951 * parsing.
3952 *
3953 * For the parsing, we use a 28 states FSM.
3954 *
3955 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003956 * rep->data + msg->som = beginning of response
3957 * rep->data + msg->eoh = end of processed headers / start of current one
3958 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003959 * rep->lr = first non-visited byte
3960 * rep->r = end of data
3961 */
3962
Willy Tarreau83e3af02009-12-28 17:39:57 +01003963 /* There's a protected area at the end of the buffer for rewriting
3964 * purposes. We don't want to start to parse the request if the
3965 * protected area is affected, because we may have to move processed
3966 * data later, which is much more complicated.
3967 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003968 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3969 if (unlikely((rep->flags & BF_FULL) ||
3970 rep->r < rep->lr ||
3971 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3972 if (rep->send_max) {
3973 /* some data has still not left the buffer, wake us once that's done */
3974 buffer_dont_close(rep);
3975 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3976 return 0;
3977 }
3978 if (rep->l <= rep->size - global.tune.maxrewrite)
3979 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003980 }
3981
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003982 if (likely(rep->lr < rep->r))
3983 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003984 }
3985
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003986 /* 1: we might have to print this header in debug mode */
3987 if (unlikely((global.mode & MODE_DEBUG) &&
3988 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003989 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003990 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003991
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003992 sol = rep->data + msg->som;
3993 eol = sol + msg->sl.rq.l;
3994 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003995
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003996 sol += hdr_idx_first_pos(&txn->hdr_idx);
3997 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003998
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003999 while (cur_idx) {
4000 eol = sol + txn->hdr_idx.v[cur_idx].len;
4001 debug_hdr("srvhdr", s, sol, eol);
4002 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4003 cur_idx = txn->hdr_idx.v[cur_idx].next;
4004 }
4005 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004006
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004007 /*
4008 * Now we quickly check if we have found a full valid response.
4009 * If not so, we check the FD and buffer states before leaving.
4010 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004011 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004012 * responses are checked first.
4013 *
4014 * Depending on whether the client is still there or not, we
4015 * may send an error response back or not. Note that normally
4016 * we should only check for HTTP status there, and check I/O
4017 * errors somewhere else.
4018 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004019
Willy Tarreau655dce92009-11-08 13:10:58 +01004020 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004021 /* Invalid response */
4022 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4023 /* we detected a parsing error. We want to archive this response
4024 * in the dedicated proxy area for later troubleshooting.
4025 */
4026 hdr_response_bad:
4027 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4028 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4029
4030 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004031 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004032 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004033 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4034 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004035
Willy Tarreau90deb182010-01-07 00:20:41 +01004036 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004037 rep->analysers = 0;
4038 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004039 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004040 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4041
4042 if (!(s->flags & SN_ERR_MASK))
4043 s->flags |= SN_ERR_PRXCOND;
4044 if (!(s->flags & SN_FINST_MASK))
4045 s->flags |= SN_FINST_H;
4046
4047 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004048 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004049
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004050 /* too large response does not fit in buffer. */
4051 else if (rep->flags & BF_FULL) {
4052 goto hdr_response_bad;
4053 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004054
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004055 /* read error */
4056 else if (rep->flags & BF_READ_ERROR) {
4057 if (msg->err_pos >= 0)
4058 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004059
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004060 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004061 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004062 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004063 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4064 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004065
Willy Tarreau90deb182010-01-07 00:20:41 +01004066 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004067 rep->analysers = 0;
4068 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004069 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004070 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004071
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004072 if (!(s->flags & SN_ERR_MASK))
4073 s->flags |= SN_ERR_SRVCL;
4074 if (!(s->flags & SN_FINST_MASK))
4075 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004076 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004077 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004078
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004079 /* read timeout : return a 504 to the client. */
4080 else if (rep->flags & BF_READ_TIMEOUT) {
4081 if (msg->err_pos >= 0)
4082 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004083
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004084 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004085 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004086 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004087 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4088 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004089
Willy Tarreau90deb182010-01-07 00:20:41 +01004090 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004091 rep->analysers = 0;
4092 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004093 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004094 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004095
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004096 if (!(s->flags & SN_ERR_MASK))
4097 s->flags |= SN_ERR_SRVTO;
4098 if (!(s->flags & SN_FINST_MASK))
4099 s->flags |= SN_FINST_H;
4100 return 0;
4101 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004102
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004103 /* close from server */
4104 else if (rep->flags & BF_SHUTR) {
4105 if (msg->err_pos >= 0)
4106 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004107
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004108 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004109 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004110 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004111 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4112 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004113
Willy Tarreau90deb182010-01-07 00:20:41 +01004114 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004115 rep->analysers = 0;
4116 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004117 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004118 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004119
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004120 if (!(s->flags & SN_ERR_MASK))
4121 s->flags |= SN_ERR_SRVCL;
4122 if (!(s->flags & SN_FINST_MASK))
4123 s->flags |= SN_FINST_H;
4124 return 0;
4125 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004126
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004127 /* write error to client (we don't send any message then) */
4128 else if (rep->flags & BF_WRITE_ERROR) {
4129 if (msg->err_pos >= 0)
4130 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004131
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004132 s->be->counters.failed_resp++;
4133 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004134 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004135
4136 if (!(s->flags & SN_ERR_MASK))
4137 s->flags |= SN_ERR_CLICL;
4138 if (!(s->flags & SN_FINST_MASK))
4139 s->flags |= SN_FINST_H;
4140
4141 /* process_session() will take care of the error */
4142 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004143 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004144
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004145 buffer_dont_close(rep);
4146 return 0;
4147 }
4148
4149 /* More interesting part now : we know that we have a complete
4150 * response which at least looks like HTTP. We have an indicator
4151 * of each header's length, so we can parse them quickly.
4152 */
4153
4154 if (unlikely(msg->err_pos >= 0))
4155 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4156
4157 /* ensure we keep this pointer to the beginning of the message */
4158 msg->sol = rep->data + msg->som;
4159
4160 /*
4161 * 1: get the status code
4162 */
4163 n = rep->data[msg->sl.st.c] - '0';
4164 if (n < 1 || n > 5)
4165 n = 0;
4166 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004167
Willy Tarreau5b154472009-12-21 20:11:07 +01004168 /* check if the response is HTTP/1.1 or above */
4169 if ((msg->sl.st.v_l == 8) &&
4170 ((rep->data[msg->som + 5] > '1') ||
4171 ((rep->data[msg->som + 5] == '1') &&
4172 (rep->data[msg->som + 7] >= '1'))))
4173 txn->flags |= TX_RES_VER_11;
4174
4175 /* "connection" has not been parsed yet */
4176 txn->flags &= ~TX_CON_HDR_PARS;
4177
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004178 /* transfer length unknown*/
4179 txn->flags &= ~TX_RES_XFER_LEN;
4180
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004181 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
4182
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004183 if (txn->status >= 100 && txn->status < 500)
4184 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4185 else
4186 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4187
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004188 /*
4189 * 2: check for cacheability.
4190 */
4191
4192 switch (txn->status) {
4193 case 200:
4194 case 203:
4195 case 206:
4196 case 300:
4197 case 301:
4198 case 410:
4199 /* RFC2616 @13.4:
4200 * "A response received with a status code of
4201 * 200, 203, 206, 300, 301 or 410 MAY be stored
4202 * by a cache (...) unless a cache-control
4203 * directive prohibits caching."
4204 *
4205 * RFC2616 @9.5: POST method :
4206 * "Responses to this method are not cacheable,
4207 * unless the response includes appropriate
4208 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004209 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004210 if (likely(txn->meth != HTTP_METH_POST) &&
4211 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4212 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4213 break;
4214 default:
4215 break;
4216 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004217
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004218 /*
4219 * 3: we may need to capture headers
4220 */
4221 s->logs.logwait &= ~LW_RESP;
4222 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
4223 capture_headers(rep->data + msg->som, &txn->hdr_idx,
4224 txn->rsp.cap, s->fe->rsp_cap);
4225
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004226 /* 4: determine the transfer-length.
4227 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4228 * the presence of a message-body in a RESPONSE and its transfer length
4229 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004230 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004231 * All responses to the HEAD request method MUST NOT include a
4232 * message-body, even though the presence of entity-header fields
4233 * might lead one to believe they do. All 1xx (informational), 204
4234 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4235 * message-body. All other responses do include a message-body,
4236 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004237 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004238 * 1. Any response which "MUST NOT" include a message-body (such as the
4239 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4240 * always terminated by the first empty line after the header fields,
4241 * regardless of the entity-header fields present in the message.
4242 *
4243 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4244 * the "chunked" transfer-coding (Section 6.2) is used, the
4245 * transfer-length is defined by the use of this transfer-coding.
4246 * If a Transfer-Encoding header field is present and the "chunked"
4247 * transfer-coding is not present, the transfer-length is defined by
4248 * the sender closing the connection.
4249 *
4250 * 3. If a Content-Length header field is present, its decimal value in
4251 * OCTETs represents both the entity-length and the transfer-length.
4252 * If a message is received with both a Transfer-Encoding header
4253 * field and a Content-Length header field, the latter MUST be ignored.
4254 *
4255 * 4. If the message uses the media type "multipart/byteranges", and
4256 * the transfer-length is not otherwise specified, then this self-
4257 * delimiting media type defines the transfer-length. This media
4258 * type MUST NOT be used unless the sender knows that the recipient
4259 * can parse it; the presence in a request of a Range header with
4260 * multiple byte-range specifiers from a 1.1 client implies that the
4261 * client can parse multipart/byteranges responses.
4262 *
4263 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004264 */
4265
4266 /* Skip parsing if no content length is possible. The response flags
4267 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004268 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004269 * FIXME: should we parse anyway and return an error on chunked encoding ?
4270 */
4271 if (txn->meth == HTTP_METH_HEAD ||
4272 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004273 txn->status == 204 || txn->status == 304) {
4274 txn->flags |= TX_RES_XFER_LEN;
4275 goto skip_content_length;
4276 }
4277
4278 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004279 goto skip_content_length;
4280
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004281 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004282 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004283 while ((txn->flags & TX_RES_VER_11) &&
4284 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004285 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4286 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4287 else if (txn->flags & TX_RES_TE_CHNK) {
4288 /* bad transfer-encoding (chunked followed by something else) */
4289 use_close_only = 1;
4290 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4291 break;
4292 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004293 }
4294
4295 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4296 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004297 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004298 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4299 signed long long cl;
4300
4301 if (!ctx.vlen)
4302 goto hdr_response_bad;
4303
4304 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4305 goto hdr_response_bad; /* parse failure */
4306
4307 if (cl < 0)
4308 goto hdr_response_bad;
4309
4310 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4311 goto hdr_response_bad; /* already specified, was different */
4312
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004313 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004314 msg->hdr_content_len = cl;
4315 }
4316
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004317 /* FIXME: we should also implement the multipart/byterange method.
4318 * For now on, we resort to close mode in this case (unknown length).
4319 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004320skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004321
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004322 /* end of job, return OK */
4323 rep->analysers &= ~an_bit;
4324 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004325 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004326 return 1;
4327}
4328
4329/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004330 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4331 * and updates t->rep->analysers. It might make sense to explode it into several
4332 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004333 */
4334int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4335{
4336 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004337 struct http_msg *msg = &txn->rsp;
4338 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004339 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004340 int conn_ka = 0, conn_cl = 0;
4341 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004342 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004343
4344 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4345 now_ms, __FUNCTION__,
4346 t,
4347 rep,
4348 rep->rex, rep->wex,
4349 rep->flags,
4350 rep->l,
4351 rep->analysers);
4352
Willy Tarreau655dce92009-11-08 13:10:58 +01004353 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004354 return 0;
4355
4356 rep->analysers &= ~an_bit;
4357 rep->analyse_exp = TICK_ETERNITY;
4358
Willy Tarreau5b154472009-12-21 20:11:07 +01004359 /* Now we have to check if we need to modify the Connection header.
4360 * This is more difficult on the response than it is on the request,
4361 * because we can have two different HTTP versions and we don't know
4362 * how the client will interprete a response. For instance, let's say
4363 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4364 * HTTP/1.1 response without any header. Maybe it will bound itself to
4365 * HTTP/1.0 because it only knows about it, and will consider the lack
4366 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4367 * the lack of header as a keep-alive. Thus we will use two flags
4368 * indicating how a request MAY be understood by the client. In case
4369 * of multiple possibilities, we'll fix the header to be explicit. If
4370 * ambiguous cases such as both close and keepalive are seen, then we
4371 * will fall back to explicit close. Note that we won't take risks with
4372 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4373 */
4374
4375 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004376 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4377 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4378 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004379 int may_keep = 0, may_close = 0; /* how it may be understood */
4380 struct hdr_ctx ctx;
4381
4382 ctx.idx = 0;
4383 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4384 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4385 conn_cl = 1;
4386 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4387 conn_ka = 1;
4388 }
4389
4390 if (conn_cl) {
4391 /* close header present */
4392 may_close = 1;
4393 if (conn_ka) /* we have both close and keep-alive */
4394 may_keep = 1;
4395 }
4396 else if (conn_ka) {
4397 /* keep-alive alone */
4398 may_keep = 1;
4399 }
4400 else {
4401 /* no close nor keep-alive header */
4402 if (txn->flags & TX_RES_VER_11)
4403 may_keep = 1;
4404 else
4405 may_close = 1;
4406
4407 if (txn->flags & TX_REQ_VER_11)
4408 may_keep = 1;
4409 else
4410 may_close = 1;
4411 }
4412
4413 /* let's update the transaction status to reflect any close.
4414 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004415 * handled. We also explicitly state that we will close in
4416 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004417 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004418 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004419 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4420 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004421 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4422
4423 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004424 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004425 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4426 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004427 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004428 if (may_keep &&
4429 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4430 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004431 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004432 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4433 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4434 must_del_close = 1;
4435 if (!(txn->flags & TX_REQ_VER_11))
4436 must_keep = 1;
4437 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004438
4439 txn->flags |= TX_CON_HDR_PARS;
4440 }
4441
4442 /* We might have to check for "Connection:" if the server
4443 * returns a connection status that is not compatible with
4444 * the client's or with the config.
4445 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004446 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004447 char *cur_ptr, *cur_end, *cur_next;
4448 int cur_idx, old_idx, delta, val;
4449 int must_delete;
4450 struct hdr_idx_elem *cur_hdr;
4451
4452 /* we just have to remove the headers if both sides are 1.0 */
4453 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004454
4455 /* same if we want to re-enable keep-alive on 1.1 */
4456 must_delete |= must_del_close;
4457
Willy Tarreau5b154472009-12-21 20:11:07 +01004458 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4459
4460 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4461 cur_hdr = &txn->hdr_idx.v[cur_idx];
4462 cur_ptr = cur_next;
4463 cur_end = cur_ptr + cur_hdr->len;
4464 cur_next = cur_end + cur_hdr->cr + 1;
4465
4466 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4467 if (!val)
4468 continue;
4469
4470 /* 3 possibilities :
4471 * - we have already set "Connection: close" or we're in
4472 * HTTP/1.0, so we remove this line.
4473 * - we have not yet set "Connection: close", but this line
4474 * indicates close. We leave it untouched and set the flag.
4475 * - we have not yet set "Connection: close", and this line
4476 * indicates non-close. We replace it and set the flag.
4477 */
4478 if (must_delete) {
4479 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4480 http_msg_move_end(&txn->rsp, delta);
4481 cur_next += delta;
4482 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4483 txn->hdr_idx.used--;
4484 cur_hdr->len = 0;
4485 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004486 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004487 } else {
4488 if (cur_end - cur_ptr - val != 5 ||
4489 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4490 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4491 "close", 5);
4492 cur_next += delta;
4493 cur_hdr->len += delta;
4494 http_msg_move_end(&txn->rsp, delta);
4495 }
4496 must_delete = 1;
4497 must_close = 0;
4498 }
4499 } /* for loop */
4500 } /* if must close keep-alive */
4501
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004503 /*
4504 * 3: we will have to evaluate the filters.
4505 * As opposed to version 1.2, now they will be evaluated in the
4506 * filters order and not in the header order. This means that
4507 * each filter has to be validated among all headers.
4508 *
4509 * Filters are tried with ->be first, then with ->fe if it is
4510 * different from ->be.
4511 */
4512
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004513 cur_proxy = t->be;
4514 while (1) {
4515 struct proxy *rule_set = cur_proxy;
4516
4517 /* try headers filters */
4518 if (rule_set->rsp_exp != NULL) {
4519 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4520 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004521 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004522 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004523 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4524 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004525 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004526 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004527 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004528 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004529 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004530 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004531 if (!(t->flags & SN_ERR_MASK))
4532 t->flags |= SN_ERR_PRXCOND;
4533 if (!(t->flags & SN_FINST_MASK))
4534 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004535 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004536 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004537 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004538
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004539 /* has the response been denied ? */
4540 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004541 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004542 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004543
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004544 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004545 if (t->listener->counters)
4546 t->listener->counters->denied_resp++;
4547
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004548 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004549 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004550
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004551 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004552 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004553 if (txn->status < 200)
4554 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004555 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004556 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004557 }
4558
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004559 /* check whether we're already working on the frontend */
4560 if (cur_proxy == t->fe)
4561 break;
4562 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004563 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004564
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004565 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004566 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4567 * in which case this is not the right response, and we're waiting for the
4568 * next one. Let's allow this response to go to the client and wait for the
4569 * next one.
4570 */
4571 if (txn->status < 200) {
4572 hdr_idx_init(&txn->hdr_idx);
4573 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4574 msg->msg_state = HTTP_MSG_RPBEFORE;
4575 txn->status = 0;
4576 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4577 return 1;
4578 }
4579
4580 /* we don't have any 1xx status code now */
4581
4582 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004583 * 4: check for server cookie.
4584 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004585 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4586 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004587 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004588
Willy Tarreaubaaee002006-06-26 02:48:02 +02004589
Willy Tarreaua15645d2007-03-18 16:22:39 +01004590 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004591 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004593 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004594 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004595
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596 /*
4597 * 6: add server cookie in the response if needed
4598 */
4599 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004600 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004601 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004602
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004603 /* the server is known, it's not the one the client requested, we have to
4604 * insert a set-cookie here, except if we want to insert only on POST
4605 * requests and this one isn't. Note that servers which don't have cookies
4606 * (eg: some backup servers) will return a full cookie removal request.
4607 */
4608 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4609 t->be->cookie_name,
4610 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004611
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004612 if (t->be->cookie_domain)
4613 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004614
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004615 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004616 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004617 goto return_bad_resp;
4618 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004619
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004620 /* Here, we will tell an eventual cache on the client side that we don't
4621 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4622 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4623 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4624 */
4625 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004626
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004627 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4628
4629 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004630 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004631 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004632 }
4633 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004634
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004635 /*
4636 * 7: check if result will be cacheable with a cookie.
4637 * We'll block the response if security checks have caught
4638 * nasty things such as a cacheable cookie.
4639 */
4640 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4641 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004642 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004643
4644 /* we're in presence of a cacheable response containing
4645 * a set-cookie header. We'll block it as requested by
4646 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004647 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004648 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004649 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004650
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004651 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004652 if (t->listener->counters)
4653 t->listener->counters->denied_resp++;
4654
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004655 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4656 t->be->id, t->srv?t->srv->id:"<dispatch>");
4657 send_log(t->be, LOG_ALERT,
4658 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4659 t->be->id, t->srv?t->srv->id:"<dispatch>");
4660 goto return_srv_prx_502;
4661 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004662
4663 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004664 * 8: add "Connection: close" if needed and not yet set. This is
4665 * only needed for 1.1 responses since we know there is no other
4666 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004667 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004668 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004669 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004670 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004671 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004672 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004673 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004674 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4675 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4676 "Connection: keep-alive", 22) < 0))
4677 goto return_bad_resp;
4678 must_keep = 0;
4679 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004680
Willy Tarreaud98cf932009-12-27 22:54:55 +01004681 if (txn->flags & TX_RES_XFER_LEN)
4682 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004683
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004684 /*************************************************************
4685 * OK, that's finished for the headers. We have done what we *
4686 * could. Let's switch to the DATA state. *
4687 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004688
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004689 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004690
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004691 /* if the user wants to log as soon as possible, without counting
4692 * bytes from the server, then this is the right moment. We have
4693 * to temporarily assign bytes_out to log what we currently have.
4694 */
4695 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4696 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4697 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004698 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004699 t->logs.bytes_out = 0;
4700 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004701
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004702 /* Note: we must not try to cheat by jumping directly to DATA,
4703 * otherwise we would not let the client side wake up.
4704 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004705
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004706 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004707 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004708 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004709}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004710
Willy Tarreaud98cf932009-12-27 22:54:55 +01004711/* This function is an analyser which forwards response body (including chunk
4712 * sizes if any). It is called as soon as we must forward, even if we forward
4713 * zero byte. The only situation where it must not be called is when we're in
4714 * tunnel mode and we want to forward till the close. It's used both to forward
4715 * remaining data and to resync after end of body. It expects the msg_state to
4716 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4717 * read more data, or 1 once we can go on with next request or end the session.
4718 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4719 * bytes of pending data + the headers if not already done (between som and sov).
4720 * It eventually adjusts som to match sov after the data in between have been sent.
4721 */
4722int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4723{
4724 struct http_txn *txn = &s->txn;
4725 struct http_msg *msg = &s->txn.rsp;
4726
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004727 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4728 return 0;
4729
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004730 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004731 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004732 !s->req->analysers) {
4733 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4734 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004735 buffer_auto_read(res);
4736 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004737 res->analysers &= ~an_bit;
4738 return 1;
4739 }
4740
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004741 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004742
Willy Tarreaud98cf932009-12-27 22:54:55 +01004743 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4744 /* we have msg->col and msg->sov which both point to the first
4745 * byte of message body. msg->som still points to the beginning
4746 * of the message. We must save the body in req->lr because it
4747 * survives buffer re-alignments.
4748 */
4749 res->lr = res->data + msg->sov;
4750 if (txn->flags & TX_RES_TE_CHNK)
4751 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4752 else {
4753 msg->msg_state = HTTP_MSG_DATA;
4754 }
4755 }
4756
Willy Tarreaud98cf932009-12-27 22:54:55 +01004757 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004758 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004759 /* we may have some data pending */
4760 if (msg->hdr_content_len || msg->som != msg->sov) {
4761 int bytes = msg->sov - msg->som;
4762 if (bytes < 0) /* sov may have wrapped at the end */
4763 bytes += res->size;
4764 buffer_forward(res, bytes + msg->hdr_content_len);
4765 msg->hdr_content_len = 0; /* don't forward that again */
4766 msg->som = msg->sov;
4767 }
4768
Willy Tarreaucaabe412010-01-03 23:08:28 +01004769 if (msg->msg_state == HTTP_MSG_DATA) {
4770 /* must still forward */
4771 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004772 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004773
4774 /* nothing left to forward */
4775 if (txn->flags & TX_RES_TE_CHNK)
4776 msg->msg_state = HTTP_MSG_DATA_CRLF;
4777 else
4778 msg->msg_state = HTTP_MSG_DONE;
4779 }
4780 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004781 /* read the chunk size and assign it to ->hdr_content_len, then
4782 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4783 */
4784 int ret = http_parse_chunk_size(res, msg);
4785
4786 if (!ret)
4787 goto missing_data;
4788 else if (ret < 0)
4789 goto return_bad_res;
4790 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004791 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004792 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4793 /* we want the CRLF after the data */
4794 int ret;
4795
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004796 res->lr = res->w + res->send_max;
4797 if (res->lr >= res->data + res->size)
4798 res->lr -= res->size;
4799
Willy Tarreaud98cf932009-12-27 22:54:55 +01004800 ret = http_skip_chunk_crlf(res, msg);
4801
4802 if (!ret)
4803 goto missing_data;
4804 else if (ret < 0)
4805 goto return_bad_res;
4806 /* we're in MSG_CHUNK_SIZE now */
4807 }
4808 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4809 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004810
Willy Tarreaud98cf932009-12-27 22:54:55 +01004811 if (ret == 0)
4812 goto missing_data;
4813 else if (ret < 0)
4814 goto return_bad_res;
4815 /* we're in HTTP_MSG_DONE now */
4816 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004817 else {
4818 /* other states, DONE...TUNNEL */
4819 if (http_resync_states(s)) {
4820 http_silent_debug(__LINE__, s);
4821 /* some state changes occurred, maybe the analyser
4822 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004823 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004824 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4825 goto return_bad_res;
4826 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004827 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004828 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004829 }
4830 }
4831
Willy Tarreaud98cf932009-12-27 22:54:55 +01004832 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004833 /* stop waiting for data if the input is closed before the end */
4834 if (res->flags & BF_SHUTR)
4835 goto return_bad_res;
4836
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837 if (!s->req->analysers)
4838 goto return_bad_res;
4839
Willy Tarreaud98cf932009-12-27 22:54:55 +01004840 /* forward the chunk size as well as any pending data */
4841 if (msg->hdr_content_len || msg->som != msg->sov) {
4842 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4843 msg->hdr_content_len = 0; /* don't forward that again */
4844 msg->som = msg->sov;
4845 }
4846
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004848 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004849 return 0;
4850
4851 return_bad_res: /* let's centralize all bad resuests */
4852 txn->rsp.msg_state = HTTP_MSG_ERROR;
4853 txn->status = 502;
4854 stream_int_cond_close(res->cons, NULL);
4855
Willy Tarreau90deb182010-01-07 00:20:41 +01004856 buffer_auto_close(res);
4857 buffer_auto_read(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004858 res->analysers = 0;
4859 s->be->counters.failed_resp++;
4860 if (s->srv) {
4861 s->srv->counters.failed_resp++;
4862 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4863 }
4864
4865 if (!(s->flags & SN_ERR_MASK))
4866 s->flags |= SN_ERR_PRXCOND;
4867 if (!(s->flags & SN_FINST_MASK))
4868 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004870 return 0;
4871}
4872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004873/* Iterate the same filter through all request headers.
4874 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004875 * Since it can manage the switch to another backend, it updates the per-proxy
4876 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004877 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004878int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004879{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004880 char term;
4881 char *cur_ptr, *cur_end, *cur_next;
4882 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004883 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004884 struct hdr_idx_elem *cur_hdr;
4885 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004887 last_hdr = 0;
4888
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004889 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004890 old_idx = 0;
4891
4892 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004893 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004894 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004895 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004896 (exp->action == ACT_ALLOW ||
4897 exp->action == ACT_DENY ||
4898 exp->action == ACT_TARPIT))
4899 return 0;
4900
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004901 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004902 if (!cur_idx)
4903 break;
4904
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004905 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004906 cur_ptr = cur_next;
4907 cur_end = cur_ptr + cur_hdr->len;
4908 cur_next = cur_end + cur_hdr->cr + 1;
4909
4910 /* Now we have one header between cur_ptr and cur_end,
4911 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004912 */
4913
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004914 /* The annoying part is that pattern matching needs
4915 * that we modify the contents to null-terminate all
4916 * strings before testing them.
4917 */
4918
4919 term = *cur_end;
4920 *cur_end = '\0';
4921
4922 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4923 switch (exp->action) {
4924 case ACT_SETBE:
4925 /* It is not possible to jump a second time.
4926 * FIXME: should we return an HTTP/500 here so that
4927 * the admin knows there's a problem ?
4928 */
4929 if (t->be != t->fe)
4930 break;
4931
4932 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004933 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004934 last_hdr = 1;
4935 break;
4936
4937 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004938 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004939 last_hdr = 1;
4940 break;
4941
4942 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004943 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004944 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004945
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004946 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004947 if (t->listener->counters)
4948 t->listener->counters->denied_resp++;
4949
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004950 break;
4951
4952 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004953 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004955
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004956 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004957 if (t->listener->counters)
4958 t->listener->counters->denied_resp++;
4959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004960 break;
4961
4962 case ACT_REPLACE:
4963 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4964 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4965 /* FIXME: if the user adds a newline in the replacement, the
4966 * index will not be recalculated for now, and the new line
4967 * will not be counted as a new header.
4968 */
4969
4970 cur_end += delta;
4971 cur_next += delta;
4972 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004973 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004974 break;
4975
4976 case ACT_REMOVE:
4977 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4978 cur_next += delta;
4979
Willy Tarreaufa355d42009-11-29 18:12:29 +01004980 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004981 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4982 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004983 cur_hdr->len = 0;
4984 cur_end = NULL; /* null-term has been rewritten */
4985 break;
4986
4987 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004988 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004989 if (cur_end)
4990 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004992 /* keep the link from this header to next one in case of later
4993 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004994 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004995 old_idx = cur_idx;
4996 }
4997 return 0;
4998}
4999
5000
5001/* Apply the filter to the request line.
5002 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5003 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005004 * Since it can manage the switch to another backend, it updates the per-proxy
5005 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005006 */
5007int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5008{
5009 char term;
5010 char *cur_ptr, *cur_end;
5011 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005012 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005013 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005014
Willy Tarreau58f10d72006-12-04 02:26:12 +01005015
Willy Tarreau3d300592007-03-18 18:34:41 +01005016 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005017 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005018 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005019 (exp->action == ACT_ALLOW ||
5020 exp->action == ACT_DENY ||
5021 exp->action == ACT_TARPIT))
5022 return 0;
5023 else if (exp->action == ACT_REMOVE)
5024 return 0;
5025
5026 done = 0;
5027
Willy Tarreau9cdde232007-05-02 20:58:19 +02005028 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005029 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005030
5031 /* Now we have the request line between cur_ptr and cur_end */
5032
5033 /* The annoying part is that pattern matching needs
5034 * that we modify the contents to null-terminate all
5035 * strings before testing them.
5036 */
5037
5038 term = *cur_end;
5039 *cur_end = '\0';
5040
5041 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5042 switch (exp->action) {
5043 case ACT_SETBE:
5044 /* It is not possible to jump a second time.
5045 * FIXME: should we return an HTTP/500 here so that
5046 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005047 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005048 if (t->be != t->fe)
5049 break;
5050
5051 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005052 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005053 done = 1;
5054 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005056 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005057 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005058 done = 1;
5059 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005061 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005062 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005063
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005064 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005065 if (t->listener->counters)
5066 t->listener->counters->denied_resp++;
5067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005068 done = 1;
5069 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005071 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005072 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005073
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005074 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005075 if (t->listener->counters)
5076 t->listener->counters->denied_resp++;
5077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005078 done = 1;
5079 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005081 case ACT_REPLACE:
5082 *cur_end = term; /* restore the string terminator */
5083 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5084 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5085 /* FIXME: if the user adds a newline in the replacement, the
5086 * index will not be recalculated for now, and the new line
5087 * will not be counted as a new header.
5088 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005089
Willy Tarreaufa355d42009-11-29 18:12:29 +01005090 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005091 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005092
Willy Tarreau9cdde232007-05-02 20:58:19 +02005093 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005094 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005095 HTTP_MSG_RQMETH,
5096 cur_ptr, cur_end + 1,
5097 NULL, NULL);
5098 if (unlikely(!cur_end))
5099 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005100
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005101 /* we have a full request and we know that we have either a CR
5102 * or an LF at <ptr>.
5103 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005104 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5105 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005106 /* there is no point trying this regex on headers */
5107 return 1;
5108 }
5109 }
5110 *cur_end = term; /* restore the string terminator */
5111 return done;
5112}
Willy Tarreau97de6242006-12-27 17:18:38 +01005113
Willy Tarreau58f10d72006-12-04 02:26:12 +01005114
Willy Tarreau58f10d72006-12-04 02:26:12 +01005115
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005116/*
5117 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5118 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005119 * unparsable request. Since it can manage the switch to another backend, it
5120 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005121 */
5122int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5123{
Willy Tarreau3d300592007-03-18 18:34:41 +01005124 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005125 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005126 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005129 /*
5130 * The interleaving of transformations and verdicts
5131 * makes it difficult to decide to continue or stop
5132 * the evaluation.
5133 */
5134
Willy Tarreau3d300592007-03-18 18:34:41 +01005135 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005136 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5137 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5138 exp = exp->next;
5139 continue;
5140 }
5141
5142 /* Apply the filter to the request line. */
5143 ret = apply_filter_to_req_line(t, req, exp);
5144 if (unlikely(ret < 0))
5145 return -1;
5146
5147 if (likely(ret == 0)) {
5148 /* The filter did not match the request, it can be
5149 * iterated through all headers.
5150 */
5151 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005152 }
5153 exp = exp->next;
5154 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005155 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005156}
5157
5158
Willy Tarreaua15645d2007-03-18 16:22:39 +01005159
Willy Tarreau58f10d72006-12-04 02:26:12 +01005160/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005161 * Try to retrieve the server associated to the appsession.
5162 * If the server is found, it's assigned to the session.
5163 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005164void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005165 struct http_txn *txn = &t->txn;
5166 appsess *asession = NULL;
5167 char *sessid_temp = NULL;
5168
Cyril Bontéb21570a2009-11-29 20:04:48 +01005169 if (len > t->be->appsession_len) {
5170 len = t->be->appsession_len;
5171 }
5172
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005173 if (t->be->options2 & PR_O2_AS_REQL) {
5174 /* request-learn option is enabled : store the sessid in the session for future use */
5175 if (t->sessid != NULL) {
5176 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5177 pool_free2(apools.sessid, t->sessid);
5178 }
5179
5180 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5181 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5182 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5183 return;
5184 }
5185
Cyril Bontéb21570a2009-11-29 20:04:48 +01005186 memcpy(t->sessid, buf, len);
5187 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005188 }
5189
5190 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5191 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5192 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5193 return;
5194 }
5195
Cyril Bontéb21570a2009-11-29 20:04:48 +01005196 memcpy(sessid_temp, buf, len);
5197 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005198
5199 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5200 /* free previously allocated memory */
5201 pool_free2(apools.sessid, sessid_temp);
5202
5203 if (asession != NULL) {
5204 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5205 if (!(t->be->options2 & PR_O2_AS_REQL))
5206 asession->request_count++;
5207
5208 if (asession->serverid != NULL) {
5209 struct server *srv = t->be->srv;
5210 while (srv) {
5211 if (strcmp(srv->id, asession->serverid) == 0) {
5212 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5213 /* we found the server and it's usable */
5214 txn->flags &= ~TX_CK_MASK;
5215 txn->flags |= TX_CK_VALID;
5216 t->flags |= SN_DIRECT | SN_ASSIGNED;
5217 t->srv = srv;
5218 break;
5219 } else {
5220 txn->flags &= ~TX_CK_MASK;
5221 txn->flags |= TX_CK_DOWN;
5222 }
5223 }
5224 srv = srv->next;
5225 }
5226 }
5227 }
5228}
5229
5230/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005231 * Manage client-side cookie. It can impact performance by about 2% so it is
5232 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005233 */
5234void manage_client_side_cookies(struct session *t, struct buffer *req)
5235{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005236 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005237 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005238 char *del_colon, *del_cookie, *colon;
5239 int app_cookies;
5240
Willy Tarreau58f10d72006-12-04 02:26:12 +01005241 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005242 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005243
Willy Tarreau2a324282006-12-05 00:05:46 +01005244 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005245 * we start with the start line.
5246 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005247 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005248 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005250 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005251 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005252 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005253
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005254 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005255 cur_ptr = cur_next;
5256 cur_end = cur_ptr + cur_hdr->len;
5257 cur_next = cur_end + cur_hdr->cr + 1;
5258
5259 /* We have one full header between cur_ptr and cur_end, and the
5260 * next header starts at cur_next. We're only interested in
5261 * "Cookie:" headers.
5262 */
5263
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005264 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5265 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005266 old_idx = cur_idx;
5267 continue;
5268 }
5269
5270 /* Now look for cookies. Conforming to RFC2109, we have to support
5271 * attributes whose name begin with a '$', and associate them with
5272 * the right cookie, if we want to delete this cookie.
5273 * So there are 3 cases for each cookie read :
5274 * 1) it's a special attribute, beginning with a '$' : ignore it.
5275 * 2) it's a server id cookie that we *MAY* want to delete : save
5276 * some pointers on it (last semi-colon, beginning of cookie...)
5277 * 3) it's an application cookie : we *MAY* have to delete a previous
5278 * "special" cookie.
5279 * At the end of loop, if a "special" cookie remains, we may have to
5280 * remove it. If no application cookie persists in the header, we
5281 * *MUST* delete it
5282 */
5283
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005284 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005285
Willy Tarreau58f10d72006-12-04 02:26:12 +01005286 /* del_cookie == NULL => nothing to be deleted */
5287 del_colon = del_cookie = NULL;
5288 app_cookies = 0;
5289
5290 while (p1 < cur_end) {
5291 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005292 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005293 while (p1 < cur_end) {
5294 if (*p1 == ';' || *p1 == ',')
5295 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005296 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005297 break;
5298 p1++;
5299 }
5300
5301 if (p1 == cur_end)
5302 break;
5303
5304 /* p1 is at the beginning of the cookie name */
5305 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005306 while (p2 < cur_end && *p2 != '=') {
5307 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5308 /* oops, the cookie name was truncated, resync */
5309 p1 = p2;
5310 goto resync_name;
5311 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005312 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005313 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005314
5315 if (p2 == cur_end)
5316 break;
5317
5318 p3 = p2 + 1; /* skips the '=' sign */
5319 if (p3 == cur_end)
5320 break;
5321
Willy Tarreau305ae852010-01-03 19:45:54 +01005322 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5323 p5 = p4 = p3;
5324 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5325 if (!isspace((unsigned char)*p5))
5326 p4 = p5 + 1;
5327 p5++;
5328 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005329
5330 /* here, we have the cookie name between p1 and p2,
5331 * and its value between p3 and p4.
5332 * we can process it :
5333 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005334 * Cookie: NAME=VALUE ;
5335 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005336 * | || || +--> p4
5337 * | || |+-------> p3
5338 * | || +--------> p2
5339 * | |+------------> p1
5340 * | +-------------> colon
5341 * +--------------------> cur_ptr
5342 */
5343
5344 if (*p1 == '$') {
5345 /* skip this one */
5346 }
5347 else {
5348 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005349 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005350 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005351 (p4 - p1 >= t->fe->capture_namelen) &&
5352 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005353 int log_len = p4 - p1;
5354
Willy Tarreau086b3b42007-05-13 21:45:51 +02005355 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005356 Alert("HTTP logging : out of memory.\n");
5357 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005358 if (log_len > t->fe->capture_len)
5359 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005360 memcpy(txn->cli_cookie, p1, log_len);
5361 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005362 }
5363 }
5364
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005365 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5366 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005367 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005368 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005369 char *delim;
5370
5371 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5372 * have the server ID betweek p3 and delim, and the original cookie between
5373 * delim+1 and p4. Otherwise, delim==p4 :
5374 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005375 * Cookie: NAME=SRV~VALUE ;
5376 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005377 * | || || | +--> p4
5378 * | || || +--------> delim
5379 * | || |+-----------> p3
5380 * | || +------------> p2
5381 * | |+----------------> p1
5382 * | +-----------------> colon
5383 * +------------------------> cur_ptr
5384 */
5385
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005386 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005387 for (delim = p3; delim < p4; delim++)
5388 if (*delim == COOKIE_DELIM)
5389 break;
5390 }
5391 else
5392 delim = p4;
5393
5394
5395 /* Here, we'll look for the first running server which supports the cookie.
5396 * This allows to share a same cookie between several servers, for example
5397 * to dedicate backup servers to specific servers only.
5398 * However, to prevent clients from sticking to cookie-less backup server
5399 * when they have incidentely learned an empty cookie, we simply ignore
5400 * empty cookies and mark them as invalid.
5401 */
5402 if (delim == p3)
5403 srv = NULL;
5404
5405 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005406 if (srv->cookie && (srv->cklen == delim - p3) &&
5407 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005408 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005409 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005410 txn->flags &= ~TX_CK_MASK;
5411 txn->flags |= TX_CK_VALID;
5412 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005413 t->srv = srv;
5414 break;
5415 } else {
5416 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005417 txn->flags &= ~TX_CK_MASK;
5418 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005419 }
5420 }
5421 srv = srv->next;
5422 }
5423
Willy Tarreau3d300592007-03-18 18:34:41 +01005424 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005425 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005426 txn->flags &= ~TX_CK_MASK;
5427 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005428 }
5429
5430 /* depending on the cookie mode, we may have to either :
5431 * - delete the complete cookie if we're in insert+indirect mode, so that
5432 * the server never sees it ;
5433 * - remove the server id from the cookie value, and tag the cookie as an
5434 * application cookie so that it does not get accidentely removed later,
5435 * if we're in cookie prefix mode
5436 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005437 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005438 int delta; /* negative */
5439
5440 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5441 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005442 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005443 cur_end += delta;
5444 cur_next += delta;
5445 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005446 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005447
5448 del_cookie = del_colon = NULL;
5449 app_cookies++; /* protect the header from deletion */
5450 }
5451 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005452 (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 +01005453 del_cookie = p1;
5454 del_colon = colon;
5455 }
5456 } else {
5457 /* now we know that we must keep this cookie since it's
5458 * not ours. But if we wanted to delete our cookie
5459 * earlier, we cannot remove the complete header, but we
5460 * can remove the previous block itself.
5461 */
5462 app_cookies++;
5463
5464 if (del_cookie != NULL) {
5465 int delta; /* negative */
5466
5467 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5468 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005469 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005470 cur_end += delta;
5471 cur_next += delta;
5472 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005473 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005474 del_cookie = del_colon = NULL;
5475 }
5476 }
5477
Cyril Bontéb21570a2009-11-29 20:04:48 +01005478 if (t->be->appsession_name != NULL) {
5479 int cmp_len, value_len;
5480 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005481
Cyril Bontéb21570a2009-11-29 20:04:48 +01005482 if (t->be->options2 & PR_O2_AS_PFX) {
5483 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5484 value_begin = p1 + t->be->appsession_name_len;
5485 value_len = p4 - p1 - t->be->appsession_name_len;
5486 } else {
5487 cmp_len = p2 - p1;
5488 value_begin = p3;
5489 value_len = p4 - p3;
5490 }
5491
5492 /* let's see if the cookie is our appcookie */
5493 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5494 /* Cool... it's the right one */
5495 manage_client_side_appsession(t, value_begin, value_len);
5496 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005497#if defined(DEBUG_HASH)
5498 Alert("manage_client_side_cookies\n");
5499 appsession_hash_dump(&(t->be->htbl_proxy));
5500#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005501 }/* end if ((t->proxy->appsession_name != NULL) ... */
5502 }
5503
5504 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005505 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005506 } /* while (p1 < cur_end) */
5507
5508 /* There's no more cookie on this line.
5509 * We may have marked the last one(s) for deletion.
5510 * We must do this now in two ways :
5511 * - if there is no app cookie, we simply delete the header ;
5512 * - if there are app cookies, we must delete the end of the
5513 * string properly, including the colon/semi-colon before
5514 * the cookie name.
5515 */
5516 if (del_cookie != NULL) {
5517 int delta;
5518 if (app_cookies) {
5519 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5520 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005521 cur_hdr->len += delta;
5522 } else {
5523 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005524
5525 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005526 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5527 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005528 cur_hdr->len = 0;
5529 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005530 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005531 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005532 }
5533
5534 /* keep the link from this header to next one */
5535 old_idx = cur_idx;
5536 } /* end of cookie processing on this header */
5537}
5538
5539
Willy Tarreaua15645d2007-03-18 16:22:39 +01005540/* Iterate the same filter through all response headers contained in <rtr>.
5541 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5542 */
5543int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5544{
5545 char term;
5546 char *cur_ptr, *cur_end, *cur_next;
5547 int cur_idx, old_idx, last_hdr;
5548 struct http_txn *txn = &t->txn;
5549 struct hdr_idx_elem *cur_hdr;
5550 int len, delta;
5551
5552 last_hdr = 0;
5553
5554 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5555 old_idx = 0;
5556
5557 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005558 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005559 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005560 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005561 (exp->action == ACT_ALLOW ||
5562 exp->action == ACT_DENY))
5563 return 0;
5564
5565 cur_idx = txn->hdr_idx.v[old_idx].next;
5566 if (!cur_idx)
5567 break;
5568
5569 cur_hdr = &txn->hdr_idx.v[cur_idx];
5570 cur_ptr = cur_next;
5571 cur_end = cur_ptr + cur_hdr->len;
5572 cur_next = cur_end + cur_hdr->cr + 1;
5573
5574 /* Now we have one header between cur_ptr and cur_end,
5575 * and the next header starts at cur_next.
5576 */
5577
5578 /* The annoying part is that pattern matching needs
5579 * that we modify the contents to null-terminate all
5580 * strings before testing them.
5581 */
5582
5583 term = *cur_end;
5584 *cur_end = '\0';
5585
5586 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5587 switch (exp->action) {
5588 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005589 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005590 last_hdr = 1;
5591 break;
5592
5593 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005594 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005595 last_hdr = 1;
5596 break;
5597
5598 case ACT_REPLACE:
5599 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5600 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5601 /* FIXME: if the user adds a newline in the replacement, the
5602 * index will not be recalculated for now, and the new line
5603 * will not be counted as a new header.
5604 */
5605
5606 cur_end += delta;
5607 cur_next += delta;
5608 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005609 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005610 break;
5611
5612 case ACT_REMOVE:
5613 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5614 cur_next += delta;
5615
Willy Tarreaufa355d42009-11-29 18:12:29 +01005616 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005617 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5618 txn->hdr_idx.used--;
5619 cur_hdr->len = 0;
5620 cur_end = NULL; /* null-term has been rewritten */
5621 break;
5622
5623 }
5624 }
5625 if (cur_end)
5626 *cur_end = term; /* restore the string terminator */
5627
5628 /* keep the link from this header to next one in case of later
5629 * removal of next header.
5630 */
5631 old_idx = cur_idx;
5632 }
5633 return 0;
5634}
5635
5636
5637/* Apply the filter to the status line in the response buffer <rtr>.
5638 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5639 * or -1 if a replacement resulted in an invalid status line.
5640 */
5641int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5642{
5643 char term;
5644 char *cur_ptr, *cur_end;
5645 int done;
5646 struct http_txn *txn = &t->txn;
5647 int len, delta;
5648
5649
Willy Tarreau3d300592007-03-18 18:34:41 +01005650 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005651 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005652 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005653 (exp->action == ACT_ALLOW ||
5654 exp->action == ACT_DENY))
5655 return 0;
5656 else if (exp->action == ACT_REMOVE)
5657 return 0;
5658
5659 done = 0;
5660
Willy Tarreau9cdde232007-05-02 20:58:19 +02005661 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005662 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5663
5664 /* Now we have the status line between cur_ptr and cur_end */
5665
5666 /* The annoying part is that pattern matching needs
5667 * that we modify the contents to null-terminate all
5668 * strings before testing them.
5669 */
5670
5671 term = *cur_end;
5672 *cur_end = '\0';
5673
5674 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5675 switch (exp->action) {
5676 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005677 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005678 done = 1;
5679 break;
5680
5681 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005682 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005683 done = 1;
5684 break;
5685
5686 case ACT_REPLACE:
5687 *cur_end = term; /* restore the string terminator */
5688 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5689 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5690 /* FIXME: if the user adds a newline in the replacement, the
5691 * index will not be recalculated for now, and the new line
5692 * will not be counted as a new header.
5693 */
5694
Willy Tarreaufa355d42009-11-29 18:12:29 +01005695 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005696 cur_end += delta;
5697
Willy Tarreau9cdde232007-05-02 20:58:19 +02005698 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005699 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005700 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005701 cur_ptr, cur_end + 1,
5702 NULL, NULL);
5703 if (unlikely(!cur_end))
5704 return -1;
5705
5706 /* we have a full respnse and we know that we have either a CR
5707 * or an LF at <ptr>.
5708 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005709 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005710 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5711 /* there is no point trying this regex on headers */
5712 return 1;
5713 }
5714 }
5715 *cur_end = term; /* restore the string terminator */
5716 return done;
5717}
5718
5719
5720
5721/*
5722 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5723 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5724 * unparsable response.
5725 */
5726int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5727{
Willy Tarreau3d300592007-03-18 18:34:41 +01005728 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005729 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005730 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005731 int ret;
5732
5733 /*
5734 * The interleaving of transformations and verdicts
5735 * makes it difficult to decide to continue or stop
5736 * the evaluation.
5737 */
5738
Willy Tarreau3d300592007-03-18 18:34:41 +01005739 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005740 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5741 exp->action == ACT_PASS)) {
5742 exp = exp->next;
5743 continue;
5744 }
5745
5746 /* Apply the filter to the status line. */
5747 ret = apply_filter_to_sts_line(t, rtr, exp);
5748 if (unlikely(ret < 0))
5749 return -1;
5750
5751 if (likely(ret == 0)) {
5752 /* The filter did not match the response, it can be
5753 * iterated through all headers.
5754 */
5755 apply_filter_to_resp_headers(t, rtr, exp);
5756 }
5757 exp = exp->next;
5758 }
5759 return 0;
5760}
5761
5762
5763
5764/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005765 * Manage server-side cookies. It can impact performance by about 2% so it is
5766 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005767 */
5768void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5769{
5770 struct http_txn *txn = &t->txn;
5771 char *p1, *p2, *p3, *p4;
5772
Willy Tarreaua15645d2007-03-18 16:22:39 +01005773 char *cur_ptr, *cur_end, *cur_next;
5774 int cur_idx, old_idx, delta;
5775
Willy Tarreaua15645d2007-03-18 16:22:39 +01005776 /* Iterate through the headers.
5777 * we start with the start line.
5778 */
5779 old_idx = 0;
5780 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5781
5782 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5783 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005784 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005785
5786 cur_hdr = &txn->hdr_idx.v[cur_idx];
5787 cur_ptr = cur_next;
5788 cur_end = cur_ptr + cur_hdr->len;
5789 cur_next = cur_end + cur_hdr->cr + 1;
5790
5791 /* We have one full header between cur_ptr and cur_end, and the
5792 * next header starts at cur_next. We're only interested in
5793 * "Cookie:" headers.
5794 */
5795
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005796 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5797 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005798 old_idx = cur_idx;
5799 continue;
5800 }
5801
5802 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005803 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005804
5805
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005806 /* maybe we only wanted to see if there was a set-cookie. Note that
5807 * the cookie capture is declared in the fronend.
5808 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005809 if (t->be->cookie_name == NULL &&
5810 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005811 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005812 return;
5813
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005814 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005815
5816 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005817 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5818 break;
5819
5820 /* p1 is at the beginning of the cookie name */
5821 p2 = p1;
5822
5823 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5824 p2++;
5825
5826 if (p2 == cur_end || *p2 == ';') /* next cookie */
5827 break;
5828
5829 p3 = p2 + 1; /* skip the '=' sign */
5830 if (p3 == cur_end)
5831 break;
5832
5833 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005834 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005835 p4++;
5836
5837 /* here, we have the cookie name between p1 and p2,
5838 * and its value between p3 and p4.
5839 * we can process it.
5840 */
5841
5842 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005843 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005844 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005845 (p4 - p1 >= t->fe->capture_namelen) &&
5846 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005847 int log_len = p4 - p1;
5848
Willy Tarreau086b3b42007-05-13 21:45:51 +02005849 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005850 Alert("HTTP logging : out of memory.\n");
5851 }
5852
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005853 if (log_len > t->fe->capture_len)
5854 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005855 memcpy(txn->srv_cookie, p1, log_len);
5856 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005857 }
5858
5859 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005860 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5861 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005862 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005863 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005864
5865 /* If the cookie is in insert mode on a known server, we'll delete
5866 * this occurrence because we'll insert another one later.
5867 * We'll delete it too if the "indirect" option is set and we're in
5868 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005869 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5870 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005871 /* this header must be deleted */
5872 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5873 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5874 txn->hdr_idx.used--;
5875 cur_hdr->len = 0;
5876 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005877 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005878
Willy Tarreau3d300592007-03-18 18:34:41 +01005879 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005880 }
5881 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005882 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005883 /* replace bytes p3->p4 with the cookie name associated
5884 * with this server since we know it.
5885 */
5886 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5887 cur_hdr->len += delta;
5888 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005889 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005890
Willy Tarreau3d300592007-03-18 18:34:41 +01005891 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005892 }
5893 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005894 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005895 /* insert the cookie name associated with this server
5896 * before existing cookie, and insert a delimitor between them..
5897 */
5898 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5899 cur_hdr->len += delta;
5900 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005901 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005902
5903 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005904 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005905 }
5906 }
5907 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005908 else if (t->be->appsession_name != NULL) {
5909 int cmp_len, value_len;
5910 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005911
Cyril Bontéb21570a2009-11-29 20:04:48 +01005912 if (t->be->options2 & PR_O2_AS_PFX) {
5913 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5914 value_begin = p1 + t->be->appsession_name_len;
5915 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5916 } else {
5917 cmp_len = p2 - p1;
5918 value_begin = p3;
5919 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005920 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005921
5922 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5923 /* Cool... it's the right one */
5924 if (t->sessid != NULL) {
5925 /* free previously allocated memory as we don't need it anymore */
5926 pool_free2(apools.sessid, t->sessid);
5927 }
5928 /* Store the sessid in the session for future use */
5929 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5930 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5931 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5932 return;
5933 }
5934 memcpy(t->sessid, value_begin, value_len);
5935 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005936 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005937 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005938 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5939 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005940 /* keep the link from this header to next one */
5941 old_idx = cur_idx;
5942 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005943
5944 if (t->sessid != NULL) {
5945 appsess *asession = NULL;
5946 /* only do insert, if lookup fails */
5947 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5948 if (asession == NULL) {
5949 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5950 Alert("Not enough Memory process_srv():asession:calloc().\n");
5951 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5952 return;
5953 }
5954 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5955 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5956 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5957 return;
5958 }
5959 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5960 asession->sessid[t->be->appsession_len] = 0;
5961
5962 size_t server_id_len = strlen(t->srv->id) + 1;
5963 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5964 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5965 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5966 return;
5967 }
5968 asession->serverid[0] = '\0';
5969 memcpy(asession->serverid, t->srv->id, server_id_len);
5970
5971 asession->request_count = 0;
5972 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5973 }
5974
5975 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5976 asession->request_count++;
5977 }
5978
5979#if defined(DEBUG_HASH)
5980 Alert("manage_server_side_cookies\n");
5981 appsession_hash_dump(&(t->be->htbl_proxy));
5982#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005983}
5984
5985
5986
5987/*
5988 * Check if response is cacheable or not. Updates t->flags.
5989 */
5990void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5991{
5992 struct http_txn *txn = &t->txn;
5993 char *p1, *p2;
5994
5995 char *cur_ptr, *cur_end, *cur_next;
5996 int cur_idx;
5997
Willy Tarreau5df51872007-11-25 16:20:08 +01005998 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005999 return;
6000
6001 /* Iterate through the headers.
6002 * we start with the start line.
6003 */
6004 cur_idx = 0;
6005 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
6006
6007 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6008 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006009 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006010
6011 cur_hdr = &txn->hdr_idx.v[cur_idx];
6012 cur_ptr = cur_next;
6013 cur_end = cur_ptr + cur_hdr->len;
6014 cur_next = cur_end + cur_hdr->cr + 1;
6015
6016 /* We have one full header between cur_ptr and cur_end, and the
6017 * next header starts at cur_next. We're only interested in
6018 * "Cookie:" headers.
6019 */
6020
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006021 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6022 if (val) {
6023 if ((cur_end - (cur_ptr + val) >= 8) &&
6024 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6025 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6026 return;
6027 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006028 }
6029
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006030 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6031 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006032 continue;
6033
6034 /* OK, right now we know we have a cache-control header at cur_ptr */
6035
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006036 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006037
6038 if (p1 >= cur_end) /* no more info */
6039 continue;
6040
6041 /* p1 is at the beginning of the value */
6042 p2 = p1;
6043
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006044 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006045 p2++;
6046
6047 /* we have a complete value between p1 and p2 */
6048 if (p2 < cur_end && *p2 == '=') {
6049 /* we have something of the form no-cache="set-cookie" */
6050 if ((cur_end - p1 >= 21) &&
6051 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6052 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006053 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006054 continue;
6055 }
6056
6057 /* OK, so we know that either p2 points to the end of string or to a comma */
6058 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6059 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6060 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6061 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006062 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006063 return;
6064 }
6065
6066 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006067 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006068 continue;
6069 }
6070 }
6071}
6072
6073
Willy Tarreau58f10d72006-12-04 02:26:12 +01006074/*
6075 * Try to retrieve a known appsession in the URI, then the associated server.
6076 * If the server is found, it's assigned to the session.
6077 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006078void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006079{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006080 char *end_params, *first_param, *cur_param, *next_param;
6081 char separator;
6082 int value_len;
6083
6084 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006085
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006086 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006087 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Cyril Bontéb21570a2009-11-29 20:04:48 +01006091 first_param = NULL;
6092 switch (mode) {
6093 case PR_O2_AS_M_PP:
6094 first_param = memchr(begin, ';', len);
6095 break;
6096 case PR_O2_AS_M_QS:
6097 first_param = memchr(begin, '?', len);
6098 break;
6099 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100
Cyril Bontéb21570a2009-11-29 20:04:48 +01006101 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006102 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006103 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006104
Cyril Bontéb21570a2009-11-29 20:04:48 +01006105 switch (mode) {
6106 case PR_O2_AS_M_PP:
6107 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6108 end_params = (char *) begin + len;
6109 }
6110 separator = ';';
6111 break;
6112 case PR_O2_AS_M_QS:
6113 end_params = (char *) begin + len;
6114 separator = '&';
6115 break;
6116 default:
6117 /* unknown mode, shouldn't happen */
6118 return;
6119 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120
Cyril Bontéb21570a2009-11-29 20:04:48 +01006121 cur_param = next_param = end_params;
6122 while (cur_param > first_param) {
6123 cur_param--;
6124 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6125 /* let's see if this is the appsession parameter */
6126 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6127 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6128 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6129 /* Cool... it's the right one */
6130 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6131 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6132 if (value_len > 0) {
6133 manage_client_side_appsession(t, cur_param, value_len);
6134 }
6135 break;
6136 }
6137 next_param = cur_param;
6138 }
6139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006140#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006141 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006142 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006143#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144}
6145
6146
Willy Tarreaub2513902006-12-17 14:52:38 +01006147/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006148 * In a GET or HEAD request, check if the requested URI matches the stats uri
6149 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006150 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006151 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006152 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006153 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006154 *
6155 * Returns 1 if the session's state changes, otherwise 0.
6156 */
6157int stats_check_uri_auth(struct session *t, struct proxy *backend)
6158{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006159 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006160 struct uri_auth *uri_auth = backend->uri_auth;
6161 struct user_auth *user;
6162 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006163 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006164
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006165 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006167 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006168 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006169 return 0;
6170
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006171 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006172
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006173 /* the URI is in h */
6174 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006175 return 0;
6176
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006177 h += uri_auth->uri_len;
6178 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6179 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006180 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006181 break;
6182 }
6183 h++;
6184 }
6185
6186 if (uri_auth->refresh) {
6187 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6188 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6189 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006190 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006191 break;
6192 }
6193 h++;
6194 }
6195 }
6196
Willy Tarreau55bb8452007-10-17 18:44:57 +02006197 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6198 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6199 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006200 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006201 break;
6202 }
6203 h++;
6204 }
6205
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006206 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6207
Willy Tarreaub2513902006-12-17 14:52:38 +01006208 /* we are in front of a interceptable URI. Let's check
6209 * if there's an authentication and if it's valid.
6210 */
6211 user = uri_auth->users;
6212 if (!user) {
6213 /* no user auth required, it's OK */
6214 authenticated = 1;
6215 } else {
6216 authenticated = 0;
6217
6218 /* a user list is defined, we have to check.
6219 * skip 21 chars for "Authorization: Basic ".
6220 */
6221
6222 /* FIXME: this should move to an earlier place */
6223 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006224 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6225 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6226 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006227 if (len > 14 &&
6228 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006229 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006230 break;
6231 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006232 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006233 }
6234
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006235 if (txn->auth_hdr.len < 21 ||
6236 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006237 user = NULL;
6238
6239 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006240 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6241 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006242 user->user_pwd, user->user_len)) {
6243 authenticated = 1;
6244 break;
6245 }
6246 user = user->next;
6247 }
6248 }
6249
6250 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006251 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006252
6253 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006254 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6255 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006256 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006257 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006258 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006259 if (!(t->flags & SN_ERR_MASK))
6260 t->flags |= SN_ERR_PRXCOND;
6261 if (!(t->flags & SN_FINST_MASK))
6262 t->flags |= SN_FINST_R;
6263 return 1;
6264 }
6265
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006266 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006267 * data.
6268 */
Willy Tarreau70089872008-06-13 21:12:51 +02006269 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006270 t->data_source = DATA_SRC_STATS;
6271 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006272 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006273 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6274 t->rep->prod->private = t;
6275 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006276 return 1;
6277}
6278
Willy Tarreau4076a152009-04-02 15:18:36 +02006279/*
6280 * Capture a bad request or response and archive it in the proxy's structure.
6281 */
6282void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6283 struct buffer *buf, struct http_msg *msg,
6284 struct proxy *other_end)
6285{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006286 es->len = buf->r - (buf->data + msg->som);
6287 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006288 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006289 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006290 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006291 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006292 es->when = date; // user-visible date
6293 es->sid = s->uniq_id;
6294 es->srv = s->srv;
6295 es->oe = other_end;
6296 es->src = s->cli_addr;
6297}
Willy Tarreaub2513902006-12-17 14:52:38 +01006298
Willy Tarreaubaaee002006-06-26 02:48:02 +02006299/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006300 * Print a debug line with a header
6301 */
6302void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6303{
6304 int len, max;
6305 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006306 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307 max = end - start;
6308 UBOUND(max, sizeof(trash) - len - 1);
6309 len += strlcpy2(trash + len, start, max + 1);
6310 trash[len++] = '\n';
6311 write(1, trash, len);
6312}
6313
Willy Tarreau0937bc42009-12-22 15:03:09 +01006314/*
6315 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6316 * the required fields are properly allocated and that we only need to (re)init
6317 * them. This should be used before processing any new request.
6318 */
6319void http_init_txn(struct session *s)
6320{
6321 struct http_txn *txn = &s->txn;
6322 struct proxy *fe = s->fe;
6323
6324 txn->flags = 0;
6325 txn->status = -1;
6326
6327 txn->req.sol = txn->req.eol = NULL;
6328 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6329 txn->rsp.sol = txn->rsp.eol = NULL;
6330 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6331 txn->req.hdr_content_len = 0LL;
6332 txn->rsp.hdr_content_len = 0LL;
6333 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6334 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6335 chunk_reset(&txn->auth_hdr);
6336
6337 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6338 if (fe->options2 & PR_O2_REQBUG_OK)
6339 txn->req.err_pos = -1; /* let buggy requests pass */
6340
Willy Tarreau46023632010-01-07 22:51:47 +01006341 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006342 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6343
Willy Tarreau46023632010-01-07 22:51:47 +01006344 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006345 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6346
6347 if (txn->hdr_idx.v)
6348 hdr_idx_init(&txn->hdr_idx);
6349}
6350
6351/* to be used at the end of a transaction */
6352void http_end_txn(struct session *s)
6353{
6354 struct http_txn *txn = &s->txn;
6355
6356 /* these ones will have been dynamically allocated */
6357 pool_free2(pool2_requri, txn->uri);
6358 pool_free2(pool2_capture, txn->cli_cookie);
6359 pool_free2(pool2_capture, txn->srv_cookie);
6360 txn->uri = NULL;
6361 txn->srv_cookie = NULL;
6362 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006363
6364 if (txn->req.cap) {
6365 struct cap_hdr *h;
6366 for (h = s->fe->req_cap; h; h = h->next)
6367 pool_free2(h->pool, txn->req.cap[h->index]);
6368 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6369 }
6370
6371 if (txn->rsp.cap) {
6372 struct cap_hdr *h;
6373 for (h = s->fe->rsp_cap; h; h = h->next)
6374 pool_free2(h->pool, txn->rsp.cap[h->index]);
6375 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6376 }
6377
Willy Tarreau0937bc42009-12-22 15:03:09 +01006378}
6379
6380/* to be used at the end of a transaction to prepare a new one */
6381void http_reset_txn(struct session *s)
6382{
6383 http_end_txn(s);
6384 http_init_txn(s);
6385
6386 s->be = s->fe;
6387 s->req->analysers = s->listener->analysers;
6388 s->logs.logwait = s->fe->to_log;
6389 s->srv = s->prev_srv = s->srv_conn = NULL;
6390 s->pend_pos = NULL;
6391 s->conn_retries = s->be->conn_retries;
6392
6393 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6394
6395 s->req->rto = s->fe->timeout.client;
6396 s->req->wto = s->be->timeout.server;
6397 s->req->cto = s->be->timeout.connect;
6398
6399 s->rep->rto = s->be->timeout.server;
6400 s->rep->wto = s->fe->timeout.client;
6401 s->rep->cto = TICK_ETERNITY;
6402
6403 s->req->rex = TICK_ETERNITY;
6404 s->req->wex = TICK_ETERNITY;
6405 s->req->analyse_exp = TICK_ETERNITY;
6406 s->rep->rex = TICK_ETERNITY;
6407 s->rep->wex = TICK_ETERNITY;
6408 s->rep->analyse_exp = TICK_ETERNITY;
6409}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006410
Willy Tarreau8797c062007-05-07 00:55:35 +02006411/************************************************************************/
6412/* The code below is dedicated to ACL parsing and matching */
6413/************************************************************************/
6414
6415
6416
6417
6418/* 1. Check on METHOD
6419 * We use the pre-parsed method if it is known, and store its number as an
6420 * integer. If it is unknown, we use the pointer and the length.
6421 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006422static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006423{
6424 int len, meth;
6425
Willy Tarreauae8b7962007-06-09 23:10:04 +02006426 len = strlen(*text);
6427 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006428
6429 pattern->val.i = meth;
6430 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006431 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006432 if (!pattern->ptr.str)
6433 return 0;
6434 pattern->len = len;
6435 }
6436 return 1;
6437}
6438
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006439static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006440acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6441 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006442{
6443 int meth;
6444 struct http_txn *txn = l7;
6445
Willy Tarreaub6866442008-07-14 23:54:42 +02006446 if (!txn)
6447 return 0;
6448
Willy Tarreau655dce92009-11-08 13:10:58 +01006449 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006450 return 0;
6451
Willy Tarreau8797c062007-05-07 00:55:35 +02006452 meth = txn->meth;
6453 test->i = meth;
6454 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006455 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6456 /* ensure the indexes are not affected */
6457 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006458 test->len = txn->req.sl.rq.m_l;
6459 test->ptr = txn->req.sol;
6460 }
6461 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6462 return 1;
6463}
6464
6465static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6466{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006467 int icase;
6468
Willy Tarreau8797c062007-05-07 00:55:35 +02006469 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006470 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006471
6472 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006473 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006474
6475 /* Other method, we must compare the strings */
6476 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006477 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006478
6479 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6480 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6481 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006482 return ACL_PAT_FAIL;
6483 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006484}
6485
6486/* 2. Check on Request/Status Version
6487 * We simply compare strings here.
6488 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006489static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006490{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006491 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006492 if (!pattern->ptr.str)
6493 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006494 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006495 return 1;
6496}
6497
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006498static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006499acl_fetch_rqver(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->req.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->req.sl.rq.v_l;
6513 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
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
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006526static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006527acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6528 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006529{
6530 struct http_txn *txn = l7;
6531 char *ptr;
6532 int len;
6533
Willy Tarreaub6866442008-07-14 23:54:42 +02006534 if (!txn)
6535 return 0;
6536
Willy Tarreau655dce92009-11-08 13:10:58 +01006537 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006538 return 0;
6539
Willy Tarreau8797c062007-05-07 00:55:35 +02006540 len = txn->rsp.sl.st.v_l;
6541 ptr = txn->rsp.sol;
6542
6543 while ((len-- > 0) && (*ptr++ != '/'));
6544 if (len <= 0)
6545 return 0;
6546
6547 test->ptr = ptr;
6548 test->len = len;
6549
6550 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6551 return 1;
6552}
6553
6554/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006555static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006556acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6557 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006558{
6559 struct http_txn *txn = l7;
6560 char *ptr;
6561 int len;
6562
Willy Tarreaub6866442008-07-14 23:54:42 +02006563 if (!txn)
6564 return 0;
6565
Willy Tarreau655dce92009-11-08 13:10:58 +01006566 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006567 return 0;
6568
Willy Tarreau8797c062007-05-07 00:55:35 +02006569 len = txn->rsp.sl.st.c_l;
6570 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6571
6572 test->i = __strl2ui(ptr, len);
6573 test->flags = ACL_TEST_F_VOL_1ST;
6574 return 1;
6575}
6576
6577/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006578static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006579acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6580 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006581{
6582 struct http_txn *txn = l7;
6583
Willy Tarreaub6866442008-07-14 23:54:42 +02006584 if (!txn)
6585 return 0;
6586
Willy Tarreau655dce92009-11-08 13:10:58 +01006587 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006588 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006589
Willy Tarreauc11416f2007-06-17 16:58:38 +02006590 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6591 /* ensure the indexes are not affected */
6592 return 0;
6593
Willy Tarreau8797c062007-05-07 00:55:35 +02006594 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006595 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006596
Willy Tarreauf3d25982007-05-08 22:45:09 +02006597 /* we do not need to set READ_ONLY because the data is in a buffer */
6598 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006599 return 1;
6600}
6601
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006602static int
6603acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6604 struct acl_expr *expr, struct acl_test *test)
6605{
6606 struct http_txn *txn = l7;
6607
Willy Tarreaub6866442008-07-14 23:54:42 +02006608 if (!txn)
6609 return 0;
6610
Willy Tarreau655dce92009-11-08 13:10:58 +01006611 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006612 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006613
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006614 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6615 /* ensure the indexes are not affected */
6616 return 0;
6617
6618 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006619 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 +01006620 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6621 test->i = AF_INET;
6622
6623 /*
6624 * If we are parsing url in frontend space, we prepare backend stage
6625 * to not parse again the same url ! optimization lazyness...
6626 */
6627 if (px->options & PR_O_HTTP_PROXY)
6628 l4->flags |= SN_ADDR_SET;
6629
6630 test->flags = ACL_TEST_F_READ_ONLY;
6631 return 1;
6632}
6633
6634static int
6635acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6636 struct acl_expr *expr, struct acl_test *test)
6637{
6638 struct http_txn *txn = l7;
6639
Willy Tarreaub6866442008-07-14 23:54:42 +02006640 if (!txn)
6641 return 0;
6642
Willy Tarreau655dce92009-11-08 13:10:58 +01006643 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006644 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006645
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006646 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6647 /* ensure the indexes are not affected */
6648 return 0;
6649
6650 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006651 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 +01006652 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6653
6654 if (px->options & PR_O_HTTP_PROXY)
6655 l4->flags |= SN_ADDR_SET;
6656
6657 test->flags = ACL_TEST_F_READ_ONLY;
6658 return 1;
6659}
6660
Willy Tarreauc11416f2007-06-17 16:58:38 +02006661/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6662 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6663 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006664static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006665acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006666 struct acl_expr *expr, struct acl_test *test)
6667{
6668 struct http_txn *txn = l7;
6669 struct hdr_idx *idx = &txn->hdr_idx;
6670 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006671
Willy Tarreaub6866442008-07-14 23:54:42 +02006672 if (!txn)
6673 return 0;
6674
Willy Tarreau33a7e692007-06-10 19:45:56 +02006675 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6676 /* search for header from the beginning */
6677 ctx->idx = 0;
6678
Willy Tarreau33a7e692007-06-10 19:45:56 +02006679 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6680 test->flags |= ACL_TEST_F_FETCH_MORE;
6681 test->flags |= ACL_TEST_F_VOL_HDR;
6682 test->len = ctx->vlen;
6683 test->ptr = (char *)ctx->line + ctx->val;
6684 return 1;
6685 }
6686
6687 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6688 test->flags |= ACL_TEST_F_VOL_HDR;
6689 return 0;
6690}
6691
Willy Tarreau33a7e692007-06-10 19:45:56 +02006692static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006693acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6694 struct acl_expr *expr, struct acl_test *test)
6695{
6696 struct http_txn *txn = l7;
6697
Willy Tarreaub6866442008-07-14 23:54:42 +02006698 if (!txn)
6699 return 0;
6700
Willy Tarreau655dce92009-11-08 13:10:58 +01006701 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006702 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006703
Willy Tarreauc11416f2007-06-17 16:58:38 +02006704 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6705 /* ensure the indexes are not affected */
6706 return 0;
6707
6708 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6709}
6710
6711static int
6712acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6713 struct acl_expr *expr, struct acl_test *test)
6714{
6715 struct http_txn *txn = l7;
6716
Willy Tarreaub6866442008-07-14 23:54:42 +02006717 if (!txn)
6718 return 0;
6719
Willy Tarreau655dce92009-11-08 13:10:58 +01006720 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006721 return 0;
6722
6723 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6724}
6725
6726/* 6. Check on HTTP header count. The number of occurrences is returned.
6727 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6728 */
6729static int
6730acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006731 struct acl_expr *expr, struct acl_test *test)
6732{
6733 struct http_txn *txn = l7;
6734 struct hdr_idx *idx = &txn->hdr_idx;
6735 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006736 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006737
Willy Tarreaub6866442008-07-14 23:54:42 +02006738 if (!txn)
6739 return 0;
6740
Willy Tarreau33a7e692007-06-10 19:45:56 +02006741 ctx.idx = 0;
6742 cnt = 0;
6743 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6744 cnt++;
6745
6746 test->i = cnt;
6747 test->flags = ACL_TEST_F_VOL_HDR;
6748 return 1;
6749}
6750
Willy Tarreauc11416f2007-06-17 16:58:38 +02006751static int
6752acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6753 struct acl_expr *expr, struct acl_test *test)
6754{
6755 struct http_txn *txn = l7;
6756
Willy Tarreaub6866442008-07-14 23:54:42 +02006757 if (!txn)
6758 return 0;
6759
Willy Tarreau655dce92009-11-08 13:10:58 +01006760 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006761 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006762
Willy Tarreauc11416f2007-06-17 16:58:38 +02006763 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6764 /* ensure the indexes are not affected */
6765 return 0;
6766
6767 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6768}
6769
6770static int
6771acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6772 struct acl_expr *expr, struct acl_test *test)
6773{
6774 struct http_txn *txn = l7;
6775
Willy Tarreaub6866442008-07-14 23:54:42 +02006776 if (!txn)
6777 return 0;
6778
Willy Tarreau655dce92009-11-08 13:10:58 +01006779 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006780 return 0;
6781
6782 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6783}
6784
Willy Tarreau33a7e692007-06-10 19:45:56 +02006785/* 7. Check on HTTP header's integer value. The integer value is returned.
6786 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006787 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006788 */
6789static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006790acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006791 struct acl_expr *expr, struct acl_test *test)
6792{
6793 struct http_txn *txn = l7;
6794 struct hdr_idx *idx = &txn->hdr_idx;
6795 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006796
Willy Tarreaub6866442008-07-14 23:54:42 +02006797 if (!txn)
6798 return 0;
6799
Willy Tarreau33a7e692007-06-10 19:45:56 +02006800 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6801 /* search for header from the beginning */
6802 ctx->idx = 0;
6803
Willy Tarreau33a7e692007-06-10 19:45:56 +02006804 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6805 test->flags |= ACL_TEST_F_FETCH_MORE;
6806 test->flags |= ACL_TEST_F_VOL_HDR;
6807 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6808 return 1;
6809 }
6810
6811 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6812 test->flags |= ACL_TEST_F_VOL_HDR;
6813 return 0;
6814}
6815
Willy Tarreauc11416f2007-06-17 16:58:38 +02006816static int
6817acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6818 struct acl_expr *expr, struct acl_test *test)
6819{
6820 struct http_txn *txn = l7;
6821
Willy Tarreaub6866442008-07-14 23:54:42 +02006822 if (!txn)
6823 return 0;
6824
Willy Tarreau655dce92009-11-08 13:10:58 +01006825 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006826 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006827
Willy Tarreauc11416f2007-06-17 16:58:38 +02006828 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6829 /* ensure the indexes are not affected */
6830 return 0;
6831
6832 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6833}
6834
6835static int
6836acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6837 struct acl_expr *expr, struct acl_test *test)
6838{
6839 struct http_txn *txn = l7;
6840
Willy Tarreaub6866442008-07-14 23:54:42 +02006841 if (!txn)
6842 return 0;
6843
Willy Tarreau655dce92009-11-08 13:10:58 +01006844 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006845 return 0;
6846
6847 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6848}
6849
Willy Tarreau106f9792009-09-19 07:54:16 +02006850/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6851 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6852 */
6853static int
6854acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6855 struct acl_expr *expr, struct acl_test *test)
6856{
6857 struct http_txn *txn = l7;
6858 struct hdr_idx *idx = &txn->hdr_idx;
6859 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6860
6861 if (!txn)
6862 return 0;
6863
6864 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6865 /* search for header from the beginning */
6866 ctx->idx = 0;
6867
6868 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6869 test->flags |= ACL_TEST_F_FETCH_MORE;
6870 test->flags |= ACL_TEST_F_VOL_HDR;
6871 /* Same optimization as url_ip */
6872 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6873 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6874 test->ptr = (void *)&l4->srv_addr.sin_addr;
6875 test->i = AF_INET;
6876 return 1;
6877 }
6878
6879 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6880 test->flags |= ACL_TEST_F_VOL_HDR;
6881 return 0;
6882}
6883
6884static int
6885acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6886 struct acl_expr *expr, struct acl_test *test)
6887{
6888 struct http_txn *txn = l7;
6889
6890 if (!txn)
6891 return 0;
6892
Willy Tarreau655dce92009-11-08 13:10:58 +01006893 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006894 return 0;
6895
6896 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6897 /* ensure the indexes are not affected */
6898 return 0;
6899
6900 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6901}
6902
6903static int
6904acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6905 struct acl_expr *expr, struct acl_test *test)
6906{
6907 struct http_txn *txn = l7;
6908
6909 if (!txn)
6910 return 0;
6911
Willy Tarreau655dce92009-11-08 13:10:58 +01006912 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006913 return 0;
6914
6915 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6916}
6917
Willy Tarreau737b0c12007-06-10 21:28:46 +02006918/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6919 * the first '/' after the possible hostname, and ends before the possible '?'.
6920 */
6921static int
6922acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6923 struct acl_expr *expr, struct acl_test *test)
6924{
6925 struct http_txn *txn = l7;
6926 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006927
Willy Tarreaub6866442008-07-14 23:54:42 +02006928 if (!txn)
6929 return 0;
6930
Willy Tarreau655dce92009-11-08 13:10:58 +01006931 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006932 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006933
Willy Tarreauc11416f2007-06-17 16:58:38 +02006934 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6935 /* ensure the indexes are not affected */
6936 return 0;
6937
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006938 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 +01006939 ptr = http_get_path(txn);
6940 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006941 return 0;
6942
6943 /* OK, we got the '/' ! */
6944 test->ptr = ptr;
6945
6946 while (ptr < end && *ptr != '?')
6947 ptr++;
6948
6949 test->len = ptr - test->ptr;
6950
6951 /* we do not need to set READ_ONLY because the data is in a buffer */
6952 test->flags = ACL_TEST_F_VOL_1ST;
6953 return 1;
6954}
6955
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006956static int
6957acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6958 struct acl_expr *expr, struct acl_test *test)
6959{
6960 struct buffer *req = s->req;
6961 struct http_txn *txn = &s->txn;
6962 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006963
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006964 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6965 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6966 */
6967
6968 if (!s || !req)
6969 return 0;
6970
Willy Tarreau655dce92009-11-08 13:10:58 +01006971 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006972 /* Already decoded as OK */
6973 test->flags |= ACL_TEST_F_SET_RES_PASS;
6974 return 1;
6975 }
6976
6977 /* Try to decode HTTP request */
6978 if (likely(req->lr < req->r))
6979 http_msg_analyzer(req, msg, &txn->hdr_idx);
6980
Willy Tarreau655dce92009-11-08 13:10:58 +01006981 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006982 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6983 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6984 return 1;
6985 }
6986 /* wait for final state */
6987 test->flags |= ACL_TEST_F_MAY_CHANGE;
6988 return 0;
6989 }
6990
6991 /* OK we got a valid HTTP request. We have some minor preparation to
6992 * perform so that further checks can rely on HTTP tests.
6993 */
6994 msg->sol = req->data + msg->som;
6995 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6996 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6997 s->flags |= SN_REDIRECTABLE;
6998
6999 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7000 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7001 return 1;
7002 }
7003
7004 test->flags |= ACL_TEST_F_SET_RES_PASS;
7005 return 1;
7006}
7007
Willy Tarreau8797c062007-05-07 00:55:35 +02007008
7009/************************************************************************/
7010/* All supported keywords must be declared here. */
7011/************************************************************************/
7012
7013/* Note: must not be declared <const> as its list will be overwritten */
7014static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007015 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7016
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007017 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7018 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7019 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7020 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007021
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007022 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7023 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7024 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7025 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7026 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7027 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7028 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7029 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7030 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007031
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007032 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7033 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7034 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7035 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7036 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7037 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7038 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7039 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7040 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7041 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007042 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007043
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007044 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7045 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7046 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7047 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7048 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7049 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7050 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7051 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7052 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007053 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007054
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007055 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7056 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7057 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7058 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7059 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7060 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7061 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007062
Willy Tarreauf3d25982007-05-08 22:45:09 +02007063 { NULL, NULL, NULL, NULL },
7064
7065#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007066 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7067 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7068 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7069 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7070 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7071 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7072 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7073
Willy Tarreau8797c062007-05-07 00:55:35 +02007074 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7075 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7076 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7077 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7078 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7079 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7080 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7081 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7082
7083 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7084 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7085 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7086 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7087 { NULL, NULL, NULL, NULL },
7088#endif
7089}};
7090
7091
7092__attribute__((constructor))
7093static void __http_protocol_init(void)
7094{
7095 acl_register_keywords(&acl_kws);
7096}
7097
7098
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007100 * Local variables:
7101 * c-indent-level: 8
7102 * c-basic-offset: 8
7103 * End:
7104 */