blob: 92c58691a821a1da1b9604e2890eee42925ab912 [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 Tarreau962c3f42010-01-10 00:15:35 +0100624 ptr = txn->req.sol + 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;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100677 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100678 memcpy(rdr.str, HTTP_302, rdr.len);
679
680 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200681 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100682 return;
683
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100684 /* special prefix "/" means don't change URL */
685 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
686 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
687 rdr.len += s->srv->rdr_len;
688 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689
690 /* 3: add the request URI */
691 txn = &s->txn;
692 path = http_get_path(txn);
693 if (!path)
694 return;
695
Willy Tarreau962c3f42010-01-10 00:15:35 +0100696 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200697 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100698 return;
699
700 memcpy(rdr.str + rdr.len, path, len);
701 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100702 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
703 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100704
705 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100706 si->shutr(si);
707 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100708 si->err_type = SI_ET_NONE;
709 si->err_loc = NULL;
710 si->state = SI_ST_CLO;
711
712 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100713 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100714
715 /* FIXME: we should increase a counter of redirects per server and per backend. */
716 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100717 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100718}
719
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100720/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100721 * that the server side is closed. Note that err_type is actually a
722 * bitmask, where almost only aborts may be cumulated with other
723 * values. We consider that aborted operations are more important
724 * than timeouts or errors due to the fact that nobody else in the
725 * logs might explain incomplete retries. All others should avoid
726 * being cumulated. It should normally not be possible to have multiple
727 * aborts at once, but just in case, the first one in sequence is reported.
728 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100729void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100730{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100731 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100732
733 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100734 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
735 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100736 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100737 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
738 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100739 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100740 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
741 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100742 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100743 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
744 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100746 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
747 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100748 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100749 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
750 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100752 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
753 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754}
755
Willy Tarreau42250582007-04-01 01:30:43 +0200756extern const char sess_term_cond[8];
757extern const char sess_fin_state[8];
758extern const char *monthname[12];
759const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
760const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
761 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
762 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200763struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200764struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100765
Emeric Brun3a058f32009-06-30 18:26:00 +0200766void http_sess_clflog(struct session *s)
767{
768 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
769 struct proxy *fe = s->fe;
770 struct proxy *be = s->be;
771 struct proxy *prx_log;
772 struct http_txn *txn = &s->txn;
773 int tolog, level, err;
774 char *uri, *h;
775 char *svid;
776 struct tm tm;
777 static char tmpline[MAX_SYSLOG_LEN];
778 int hdr;
779 size_t w;
780 int t_request;
781
782 prx_log = fe;
783 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
784 (s->conn_retries != be->conn_retries) ||
785 txn->status >= 500;
786
787 if (s->cli_addr.ss_family == AF_INET)
788 inet_ntop(AF_INET,
789 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
790 pn, sizeof(pn));
791 else
792 inet_ntop(AF_INET6,
793 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
794 pn, sizeof(pn));
795
796 get_gmtime(s->logs.accept_date.tv_sec, &tm);
797
798 /* FIXME: let's limit ourselves to frontend logging for now. */
799 tolog = fe->to_log;
800
801 h = tmpline;
802
803 w = snprintf(h, sizeof(tmpline),
804 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
805 pn,
806 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
807 tm.tm_hour, tm.tm_min, tm.tm_sec);
808 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
809 goto trunc;
810 h += w;
811
812 if (h >= tmpline + sizeof(tmpline) - 4)
813 goto trunc;
814
815 *(h++) = ' ';
816 *(h++) = '\"';
817 uri = txn->uri ? txn->uri : "<BADREQ>";
818 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
819 '#', url_encode_map, uri);
820 *(h++) = '\"';
821
822 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
823 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
824 goto trunc;
825 h += w;
826
827 if (h >= tmpline + sizeof(tmpline) - 9)
828 goto trunc;
829 memcpy(h, " \"-\" \"-\"", 8);
830 h += 8;
831
832 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
833 " %d %03d",
834 (s->cli_addr.ss_family == AF_INET) ?
835 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
836 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
837 (int)s->logs.accept_date.tv_usec/1000);
838 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
839 goto trunc;
840 h += w;
841
842 w = strlen(fe->id);
843 if (h >= tmpline + sizeof(tmpline) - 4 - w)
844 goto trunc;
845 *(h++) = ' ';
846 *(h++) = '\"';
847 memcpy(h, fe->id, w);
848 h += w;
849 *(h++) = '\"';
850
851 w = strlen(be->id);
852 if (h >= tmpline + sizeof(tmpline) - 4 - w)
853 goto trunc;
854 *(h++) = ' ';
855 *(h++) = '\"';
856 memcpy(h, be->id, w);
857 h += w;
858 *(h++) = '\"';
859
860 svid = (tolog & LW_SVID) ?
861 (s->data_source != DATA_SRC_STATS) ?
862 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
863
864 w = strlen(svid);
865 if (h >= tmpline + sizeof(tmpline) - 4 - w)
866 goto trunc;
867 *(h++) = ' ';
868 *(h++) = '\"';
869 memcpy(h, svid, w);
870 h += w;
871 *(h++) = '\"';
872
873 t_request = -1;
874 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
875 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
876 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
877 " %d %ld %ld %ld %ld",
878 t_request,
879 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
880 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
881 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
882 s->logs.t_close);
883 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
884 goto trunc;
885 h += w;
886
887 if (h >= tmpline + sizeof(tmpline) - 8)
888 goto trunc;
889 *(h++) = ' ';
890 *(h++) = '\"';
891 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
892 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
893 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
894 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
895 *(h++) = '\"';
896
897 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
898 " %d %d %d %d %d %ld %ld",
899 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
900 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
901 s->logs.srv_queue_size, s->logs.prx_queue_size);
902
903 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
904 goto trunc;
905 h += w;
906
907 if (txn->cli_cookie) {
908 w = strlen(txn->cli_cookie);
909 if (h >= tmpline + sizeof(tmpline) - 4 - w)
910 goto trunc;
911 *(h++) = ' ';
912 *(h++) = '\"';
913 memcpy(h, txn->cli_cookie, w);
914 h += w;
915 *(h++) = '\"';
916 } else {
917 if (h >= tmpline + sizeof(tmpline) - 5)
918 goto trunc;
919 memcpy(h, " \"-\"", 4);
920 h += 4;
921 }
922
923 if (txn->srv_cookie) {
924 w = strlen(txn->srv_cookie);
925 if (h >= tmpline + sizeof(tmpline) - 4 - w)
926 goto trunc;
927 *(h++) = ' ';
928 *(h++) = '\"';
929 memcpy(h, txn->srv_cookie, w);
930 h += w;
931 *(h++) = '\"';
932 } else {
933 if (h >= tmpline + sizeof(tmpline) - 5)
934 goto trunc;
935 memcpy(h, " \"-\"", 4);
936 h += 4;
937 }
938
939 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
940 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
941 if (h >= sizeof (tmpline) + tmpline - 4)
942 goto trunc;
943 *(h++) = ' ';
944 *(h++) = '\"';
945 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
946 '#', hdr_encode_map, txn->req.cap[hdr]);
947 *(h++) = '\"';
948 }
949 }
950
951 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
952 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
953 if (h >= sizeof (tmpline) + tmpline - 4)
954 goto trunc;
955 *(h++) = ' ';
956 *(h++) = '\"';
957 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
958 '#', hdr_encode_map, txn->rsp.cap[hdr]);
959 *(h++) = '\"';
960 }
961 }
962
963trunc:
964 *h = '\0';
965
966 level = LOG_INFO;
967 if (err && (fe->options2 & PR_O2_LOGERRORS))
968 level = LOG_ERR;
969
970 send_log(prx_log, level, "%s\n", tmpline);
971
972 s->logs.logwait = 0;
973}
974
Willy Tarreau42250582007-04-01 01:30:43 +0200975/*
976 * send a log for the session when we have enough info about it.
977 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100979void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200980{
981 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
982 struct proxy *fe = s->fe;
983 struct proxy *be = s->be;
984 struct proxy *prx_log;
985 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200986 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200987 char *uri, *h;
988 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200989 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200990 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200991 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200992 int hdr;
993
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200994 /* if we don't want to log normal traffic, return now */
995 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
996 (s->conn_retries != be->conn_retries) ||
997 txn->status >= 500;
998 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
999 return;
1000
Willy Tarreau42250582007-04-01 01:30:43 +02001001 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1002 return;
1003 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001004
Emeric Brun3a058f32009-06-30 18:26:00 +02001005 if (prx_log->options2 & PR_O2_CLFLOG)
1006 return http_sess_clflog(s);
1007
Willy Tarreau42250582007-04-01 01:30:43 +02001008 if (s->cli_addr.ss_family == AF_INET)
1009 inet_ntop(AF_INET,
1010 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1011 pn, sizeof(pn));
1012 else
1013 inet_ntop(AF_INET6,
1014 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1015 pn, sizeof(pn));
1016
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001017 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001018
1019 /* FIXME: let's limit ourselves to frontend logging for now. */
1020 tolog = fe->to_log;
1021
1022 h = tmpline;
1023 if (fe->to_log & LW_REQHDR &&
1024 txn->req.cap &&
1025 (h < tmpline + sizeof(tmpline) - 10)) {
1026 *(h++) = ' ';
1027 *(h++) = '{';
1028 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1029 if (hdr)
1030 *(h++) = '|';
1031 if (txn->req.cap[hdr] != NULL)
1032 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1033 '#', hdr_encode_map, txn->req.cap[hdr]);
1034 }
1035 *(h++) = '}';
1036 }
1037
1038 if (fe->to_log & LW_RSPHDR &&
1039 txn->rsp.cap &&
1040 (h < tmpline + sizeof(tmpline) - 7)) {
1041 *(h++) = ' ';
1042 *(h++) = '{';
1043 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1044 if (hdr)
1045 *(h++) = '|';
1046 if (txn->rsp.cap[hdr] != NULL)
1047 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1048 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1049 }
1050 *(h++) = '}';
1051 }
1052
1053 if (h < tmpline + sizeof(tmpline) - 4) {
1054 *(h++) = ' ';
1055 *(h++) = '"';
1056 uri = txn->uri ? txn->uri : "<BADREQ>";
1057 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1058 '#', url_encode_map, uri);
1059 *(h++) = '"';
1060 }
1061 *h = '\0';
1062
1063 svid = (tolog & LW_SVID) ?
1064 (s->data_source != DATA_SRC_STATS) ?
1065 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1066
Willy Tarreau70089872008-06-13 21:12:51 +02001067 t_request = -1;
1068 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1069 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1070
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001071 level = LOG_INFO;
1072 if (err && (fe->options2 & PR_O2_LOGERRORS))
1073 level = LOG_ERR;
1074
1075 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001076 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001077 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1078 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001079 pn,
1080 (s->cli_addr.ss_family == AF_INET) ?
1081 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1082 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001083 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001084 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001085 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001086 t_request,
1087 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001088 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1089 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1090 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1091 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001092 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001093 txn->cli_cookie ? txn->cli_cookie : "-",
1094 txn->srv_cookie ? txn->srv_cookie : "-",
1095 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1096 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1097 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1098 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1099 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001100 (s->flags & SN_REDISP)?"+":"",
1101 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001102 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1103
1104 s->logs.logwait = 0;
1105}
1106
Willy Tarreau117f59e2007-03-04 18:17:17 +01001107
1108/*
1109 * Capture headers from message starting at <som> according to header list
1110 * <cap_hdr>, and fill the <idx> structure appropriately.
1111 */
1112void capture_headers(char *som, struct hdr_idx *idx,
1113 char **cap, struct cap_hdr *cap_hdr)
1114{
1115 char *eol, *sol, *col, *sov;
1116 int cur_idx;
1117 struct cap_hdr *h;
1118 int len;
1119
1120 sol = som + hdr_idx_first_pos(idx);
1121 cur_idx = hdr_idx_first_idx(idx);
1122
1123 while (cur_idx) {
1124 eol = sol + idx->v[cur_idx].len;
1125
1126 col = sol;
1127 while (col < eol && *col != ':')
1128 col++;
1129
1130 sov = col + 1;
1131 while (sov < eol && http_is_lws[(unsigned char)*sov])
1132 sov++;
1133
1134 for (h = cap_hdr; h; h = h->next) {
1135 if ((h->namelen == col - sol) &&
1136 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1137 if (cap[h->index] == NULL)
1138 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001139 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001140
1141 if (cap[h->index] == NULL) {
1142 Alert("HTTP capture : out of memory.\n");
1143 continue;
1144 }
1145
1146 len = eol - sov;
1147 if (len > h->len)
1148 len = h->len;
1149
1150 memcpy(cap[h->index], sov, len);
1151 cap[h->index][len]=0;
1152 }
1153 }
1154 sol = eol + idx->v[cur_idx].cr + 1;
1155 cur_idx = idx->v[cur_idx].next;
1156 }
1157}
1158
1159
Willy Tarreau42250582007-04-01 01:30:43 +02001160/* either we find an LF at <ptr> or we jump to <bad>.
1161 */
1162#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1163
1164/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1165 * otherwise to <http_msg_ood> with <state> set to <st>.
1166 */
1167#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1168 ptr++; \
1169 if (likely(ptr < end)) \
1170 goto good; \
1171 else { \
1172 state = (st); \
1173 goto http_msg_ood; \
1174 } \
1175 } while (0)
1176
1177
Willy Tarreaubaaee002006-06-26 02:48:02 +02001178/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001179 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1181 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1182 * will give undefined results.
1183 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1184 * and that msg->sol points to the beginning of the response.
1185 * If a complete line is found (which implies that at least one CR or LF is
1186 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1187 * returned indicating an incomplete line (which does not mean that parts have
1188 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1189 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1190 * upon next call.
1191 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001192 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1194 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001195 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001197const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1198 unsigned int state, const char *ptr, const char *end,
1199 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001200{
Willy Tarreau8973c702007-01-21 23:58:29 +01001201 switch (state) {
1202 http_msg_rpver:
1203 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001204 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1206
1207 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001208 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1210 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001211 state = HTTP_MSG_ERROR;
1212 break;
1213
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 http_msg_rpver_sp:
1215 case HTTP_MSG_RPVER_SP:
1216 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001217 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001218 goto http_msg_rpcode;
1219 }
1220 if (likely(HTTP_IS_SPHT(*ptr)))
1221 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1222 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001223 state = HTTP_MSG_ERROR;
1224 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001225
1226 http_msg_rpcode:
1227 case HTTP_MSG_RPCODE:
1228 if (likely(!HTTP_IS_LWS(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1230
1231 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001232 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001233 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1234 }
1235
1236 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001237 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 http_msg_rsp_reason:
1239 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001240 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 msg->sl.st.r_l = 0;
1242 goto http_msg_rpline_eol;
1243
1244 http_msg_rpcode_sp:
1245 case HTTP_MSG_RPCODE_SP:
1246 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001247 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 goto http_msg_rpreason;
1249 }
1250 if (likely(HTTP_IS_SPHT(*ptr)))
1251 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1252 /* so it's a CR/LF, so there is no reason phrase */
1253 goto http_msg_rsp_reason;
1254
1255 http_msg_rpreason:
1256 case HTTP_MSG_RPREASON:
1257 if (likely(!HTTP_IS_CRLF(*ptr)))
1258 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001259 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 http_msg_rpline_eol:
1261 /* We have seen the end of line. Note that we do not
1262 * necessarily have the \n yet, but at least we know that we
1263 * have EITHER \r OR \n, otherwise the response would not be
1264 * complete. We can then record the response length and return
1265 * to the caller which will be able to register it.
1266 */
1267 msg->sl.st.l = ptr - msg->sol;
1268 return ptr;
1269
1270#ifdef DEBUG_FULL
1271 default:
1272 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1273 exit(1);
1274#endif
1275 }
1276
1277 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001278 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 if (ret_state)
1280 *ret_state = state;
1281 if (ret_ptr)
1282 *ret_ptr = (char *)ptr;
1283 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001284}
1285
1286
1287/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 * This function parses a request line between <ptr> and <end>, starting with
1289 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1290 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1291 * will give undefined results.
1292 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1293 * and that msg->sol points to the beginning of the request.
1294 * If a complete line is found (which implies that at least one CR or LF is
1295 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1296 * returned indicating an incomplete line (which does not mean that parts have
1297 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1298 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1299 * upon next call.
1300 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001301 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1303 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001304 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001305 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001306const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1307 unsigned int state, const char *ptr, const char *end,
1308 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001309{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 switch (state) {
1311 http_msg_rqmeth:
1312 case HTTP_MSG_RQMETH:
1313 if (likely(HTTP_IS_TOKEN(*ptr)))
1314 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001317 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 if (likely(HTTP_IS_CRLF(*ptr))) {
1322 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001323 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001325 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001327 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001329 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 msg->sl.rq.v_l = 0;
1331 goto http_msg_rqline_eol;
1332 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001333 state = HTTP_MSG_ERROR;
1334 break;
1335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 http_msg_rqmeth_sp:
1337 case HTTP_MSG_RQMETH_SP:
1338 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001339 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 goto http_msg_rquri;
1341 }
1342 if (likely(HTTP_IS_SPHT(*ptr)))
1343 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1344 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1345 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_rquri:
1348 case HTTP_MSG_RQURI:
1349 if (likely(!HTTP_IS_LWS(*ptr)))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001353 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1355 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1358 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 http_msg_rquri_sp:
1361 case HTTP_MSG_RQURI_SP:
1362 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001363 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 goto http_msg_rqver;
1365 }
1366 if (likely(HTTP_IS_SPHT(*ptr)))
1367 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1368 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1369 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 http_msg_rqver:
1372 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001373 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001375
1376 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001377 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001378 http_msg_rqline_eol:
1379 /* We have seen the end of line. Note that we do not
1380 * necessarily have the \n yet, but at least we know that we
1381 * have EITHER \r OR \n, otherwise the request would not be
1382 * complete. We can then record the request length and return
1383 * to the caller which will be able to register it.
1384 */
1385 msg->sl.rq.l = ptr - msg->sol;
1386 return ptr;
1387 }
1388
1389 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001390 state = HTTP_MSG_ERROR;
1391 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393#ifdef DEBUG_FULL
1394 default:
1395 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1396 exit(1);
1397#endif
1398 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001401 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 if (ret_state)
1403 *ret_state = state;
1404 if (ret_ptr)
1405 *ret_ptr = (char *)ptr;
1406 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001408
1409
Willy Tarreau8973c702007-01-21 23:58:29 +01001410/*
1411 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001412 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 * when data are missing and recalled at the exact same location with no
1414 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001415 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001416 * fields. Note that msg->som and msg->sol will be initialized after completing
1417 * the first state, so that none of the msg pointers has to be initialized
1418 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1421{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001422 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001425 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 ptr = buf->lr;
1427 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 if (unlikely(ptr >= end))
1430 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /*
1434 * First, states that are specific to the response only.
1435 * We check them first so that request and headers are
1436 * closer to each other (accessed more often).
1437 */
1438 http_msg_rpbefore:
1439 case HTTP_MSG_RPBEFORE:
1440 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001441 /* we have a start of message, but we have to check
1442 * first if we need to remove some CRLF. We can only
1443 * do this when send_max=0.
1444 */
1445 char *beg = buf->w + buf->send_max;
1446 if (beg >= buf->data + buf->size)
1447 beg -= buf->size;
1448 if (unlikely(ptr != beg)) {
1449 if (buf->send_max)
1450 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001451 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001452 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001454 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001455 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001456 hdr_idx_init(idx);
1457 state = HTTP_MSG_RPVER;
1458 goto http_msg_rpver;
1459 }
1460
1461 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1462 goto http_msg_invalid;
1463
1464 if (unlikely(*ptr == '\n'))
1465 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1466 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1467 /* stop here */
1468
1469 http_msg_rpbefore_cr:
1470 case HTTP_MSG_RPBEFORE_CR:
1471 EXPECT_LF_HERE(ptr, http_msg_invalid);
1472 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1473 /* stop here */
1474
1475 http_msg_rpver:
1476 case HTTP_MSG_RPVER:
1477 case HTTP_MSG_RPVER_SP:
1478 case HTTP_MSG_RPCODE:
1479 case HTTP_MSG_RPCODE_SP:
1480 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001481 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001482 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 if (unlikely(!ptr))
1484 return;
1485
1486 /* we have a full response and we know that we have either a CR
1487 * or an LF at <ptr>.
1488 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001489 //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 +01001490 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1491
1492 msg->sol = ptr;
1493 if (likely(*ptr == '\r'))
1494 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1495 goto http_msg_rpline_end;
1496
1497 http_msg_rpline_end:
1498 case HTTP_MSG_RPLINE_END:
1499 /* msg->sol must point to the first of CR or LF. */
1500 EXPECT_LF_HERE(ptr, http_msg_invalid);
1501 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1502 /* stop here */
1503
1504 /*
1505 * Second, states that are specific to the request only
1506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 http_msg_rqbefore:
1508 case HTTP_MSG_RQBEFORE:
1509 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001510 /* we have a start of message, but we have to check
1511 * first if we need to remove some CRLF. We can only
1512 * do this when send_max=0.
1513 */
1514 char *beg = buf->w + buf->send_max;
1515 if (beg >= buf->data + buf->size)
1516 beg -= buf->size;
1517 if (likely(ptr != beg)) {
1518 if (buf->send_max)
1519 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001520 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001521 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001523 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001524 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001525 /* we will need this when keep-alive will be supported
1526 hdr_idx_init(idx);
1527 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001528 state = HTTP_MSG_RQMETH;
1529 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1533 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 if (unlikely(*ptr == '\n'))
1536 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1537 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001538 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 http_msg_rqbefore_cr:
1541 case HTTP_MSG_RQBEFORE_CR:
1542 EXPECT_LF_HERE(ptr, http_msg_invalid);
1543 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001544 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 http_msg_rqmeth:
1547 case HTTP_MSG_RQMETH:
1548 case HTTP_MSG_RQMETH_SP:
1549 case HTTP_MSG_RQURI:
1550 case HTTP_MSG_RQURI_SP:
1551 case HTTP_MSG_RQVER:
1552 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001553 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 if (unlikely(!ptr))
1555 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 /* we have a full request and we know that we have either a CR
1558 * or an LF at <ptr>.
1559 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001560 //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 +01001561 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 msg->sol = ptr;
1564 if (likely(*ptr == '\r'))
1565 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 http_msg_rqline_end:
1569 case HTTP_MSG_RQLINE_END:
1570 /* check for HTTP/0.9 request : no version information available.
1571 * msg->sol must point to the first of CR or LF.
1572 */
1573 if (unlikely(msg->sl.rq.v_l == 0))
1574 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 EXPECT_LF_HERE(ptr, http_msg_invalid);
1577 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001578 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8973c702007-01-21 23:58:29 +01001580 /*
1581 * Common states below
1582 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 http_msg_hdr_first:
1584 case HTTP_MSG_HDR_FIRST:
1585 msg->sol = ptr;
1586 if (likely(!HTTP_IS_CRLF(*ptr))) {
1587 goto http_msg_hdr_name;
1588 }
1589
1590 if (likely(*ptr == '\r'))
1591 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1592 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 http_msg_hdr_name:
1595 case HTTP_MSG_HDR_NAME:
1596 /* assumes msg->sol points to the first char */
1597 if (likely(HTTP_IS_TOKEN(*ptr)))
1598 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 if (likely(*ptr == ':')) {
1601 msg->col = ptr - buf->data;
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1603 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001604
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001605 if (likely(msg->err_pos < -1) || *ptr == '\n')
1606 goto http_msg_invalid;
1607
1608 if (msg->err_pos == -1) /* capture error pointer */
1609 msg->err_pos = ptr - buf->data; /* >= 0 now */
1610
1611 /* and we still accept this non-token character */
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 http_msg_hdr_l1_sp:
1615 case HTTP_MSG_HDR_L1_SP:
1616 /* assumes msg->sol points to the first char and msg->col to the colon */
1617 if (likely(HTTP_IS_SPHT(*ptr)))
1618 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* header value can be basically anything except CR/LF */
1621 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (likely(!HTTP_IS_CRLF(*ptr))) {
1624 goto http_msg_hdr_val;
1625 }
1626
1627 if (likely(*ptr == '\r'))
1628 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1629 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 http_msg_hdr_l1_lf:
1632 case HTTP_MSG_HDR_L1_LF:
1633 EXPECT_LF_HERE(ptr, http_msg_invalid);
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 http_msg_hdr_l1_lws:
1637 case HTTP_MSG_HDR_L1_LWS:
1638 if (likely(HTTP_IS_SPHT(*ptr))) {
1639 /* replace HT,CR,LF with spaces */
1640 for (; buf->data+msg->sov < ptr; msg->sov++)
1641 buf->data[msg->sov] = ' ';
1642 goto http_msg_hdr_l1_sp;
1643 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001644 /* we had a header consisting only in spaces ! */
1645 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 goto http_msg_complete_header;
1647
1648 http_msg_hdr_val:
1649 case HTTP_MSG_HDR_VAL:
1650 /* assumes msg->sol points to the first char, msg->col to the
1651 * colon, and msg->sov points to the first character of the
1652 * value.
1653 */
1654 if (likely(!HTTP_IS_CRLF(*ptr)))
1655 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 msg->eol = ptr;
1658 /* Note: we could also copy eol into ->eoh so that we have the
1659 * real header end in case it ends with lots of LWS, but is this
1660 * really needed ?
1661 */
1662 if (likely(*ptr == '\r'))
1663 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1664 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 http_msg_hdr_l2_lf:
1667 case HTTP_MSG_HDR_L2_LF:
1668 EXPECT_LF_HERE(ptr, http_msg_invalid);
1669 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 http_msg_hdr_l2_lws:
1672 case HTTP_MSG_HDR_L2_LWS:
1673 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1674 /* LWS: replace HT,CR,LF with spaces */
1675 for (; msg->eol < ptr; msg->eol++)
1676 *msg->eol = ' ';
1677 goto http_msg_hdr_val;
1678 }
1679 http_msg_complete_header:
1680 /*
1681 * It was a new header, so the last one is finished.
1682 * Assumes msg->sol points to the first char, msg->col to the
1683 * colon, msg->sov points to the first character of the value
1684 * and msg->eol to the first CR or LF so we know how the line
1685 * ends. We insert last header into the index.
1686 */
1687 /*
1688 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1689 write(2, msg->sol, msg->eol-msg->sol);
1690 fprintf(stderr,"\n");
1691 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1694 idx, idx->tail) < 0))
1695 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 msg->sol = ptr;
1698 if (likely(!HTTP_IS_CRLF(*ptr))) {
1699 goto http_msg_hdr_name;
1700 }
1701
1702 if (likely(*ptr == '\r'))
1703 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1704 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 http_msg_last_lf:
1707 case HTTP_MSG_LAST_LF:
1708 /* Assumes msg->sol points to the first of either CR or LF */
1709 EXPECT_LF_HERE(ptr, http_msg_invalid);
1710 ptr++;
1711 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001712 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001714 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001715 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 return;
1717#ifdef DEBUG_FULL
1718 default:
1719 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1720 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001721#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 }
1723 http_msg_ood:
1724 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001725 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 buf->lr = ptr;
1727 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 http_msg_invalid:
1730 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001731 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001732 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 return;
1734}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001735
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001736/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1737 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1738 * nothing is done and 1 is returned.
1739 */
1740static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1741{
1742 int delta;
1743 char *cur_end;
1744
1745 if (msg->sl.rq.v_l != 0)
1746 return 1;
1747
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001748 cur_end = msg->sol + msg->sl.rq.l;
1749 delta = 0;
1750
1751 if (msg->sl.rq.u_l == 0) {
1752 /* if no URI was set, add "/" */
1753 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1754 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001755 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001756 }
1757 /* add HTTP version */
1758 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001759 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760 cur_end += delta;
1761 cur_end = (char *)http_parse_reqline(msg, req->data,
1762 HTTP_MSG_RQMETH,
1763 msg->sol, cur_end + 1,
1764 NULL, NULL);
1765 if (unlikely(!cur_end))
1766 return 0;
1767
1768 /* we have a full HTTP/1.0 request now and we know that
1769 * we have either a CR or an LF at <ptr>.
1770 */
1771 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1772 return 1;
1773}
1774
Willy Tarreau5b154472009-12-21 20:11:07 +01001775/* Parse the Connection: headaer of an HTTP request, and set the transaction
1776 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1777 * is also set so that we don't parse a second time. If some dangerous values
1778 * are encountered, we leave the status to indicate that the request might be
1779 * interpreted as keep-alive, but we also set the connection flags to indicate
1780 * that we WANT it to be a close, so that the header will be fixed. This
1781 * function should only be called when we know we're interested in checking
1782 * the request (not a CONNECT, and FE or BE mangles the header).
1783 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001784void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001785{
1786 struct http_msg *msg = &txn->req;
1787 struct hdr_ctx ctx;
1788 int conn_cl, conn_ka;
1789
1790 if (txn->flags & TX_CON_HDR_PARS)
1791 return;
1792
1793 conn_cl = 0;
1794 conn_ka = 0;
1795 ctx.idx = 0;
1796
1797 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1798 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1799 conn_cl = 1;
1800 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1801 conn_ka = 1;
1802 }
1803
1804 /* Determine if the client wishes keep-alive or close.
1805 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1806 * are persistent unless "Connection: close" is explicitly specified.
1807 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1808 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1809 * they explicitly specify "Connection: Keep-Alive", regardless of any
1810 * optional "Keep-Alive" header.
1811 * Note that if we find a request with both "Connection: close" and
1812 * "Connection: Keep-Alive", we indicate we want a close but don't have
1813 * it, so that it can be enforced later.
1814 */
1815
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001816 if (conn_cl && conn_ka) {
1817 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1818 }
1819 else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 if (conn_cl) {
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001821 txn->flags |= TX_REQ_CONN_CLO;
1822 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1823 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01001824 }
1825 } else { /* HTTP/1.0 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001826 if (!conn_ka) {
1827 txn->flags |= TX_REQ_CONN_CLO;
1828 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1829 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1830 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001831 }
1832 txn->flags |= TX_CON_HDR_PARS;
1833}
1834
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1836 * first byte of body, and increments msg->sov by the number of bytes parsed,
1837 * so that we know we can forward between ->som and ->sov. Note that due to
1838 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1839 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001840 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001841 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001842 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001843int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001844{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001845 char *ptr = buf->lr;
1846 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 unsigned int chunk = 0;
1848
1849 /* The chunk size is in the following form, though we are only
1850 * interested in the size and CRLF :
1851 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1852 */
1853 while (1) {
1854 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001856 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001858 if (c < 0) /* not a hex digit anymore */
1859 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860 if (++ptr >= end)
1861 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001862 if (chunk & 0xF000000) /* overflow will occur */
1863 return -1;
1864 chunk = (chunk << 4) + c;
1865 }
1866
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867 /* empty size not allowed */
1868 if (ptr == buf->lr)
1869 return -1;
1870
1871 while (http_is_spht[(unsigned char)*ptr]) {
1872 if (++ptr >= end)
1873 ptr = buf->data;
1874 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001875 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001876 }
1877
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878 /* Up to there, we know that at least one byte is present at *ptr. Check
1879 * for the end of chunk size.
1880 */
1881 while (1) {
1882 if (likely(HTTP_IS_CRLF(*ptr))) {
1883 /* we now have a CR or an LF at ptr */
1884 if (likely(*ptr == '\r')) {
1885 if (++ptr >= end)
1886 ptr = buf->data;
1887 if (ptr == buf->r)
1888 return 0;
1889 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001890
Willy Tarreaud98cf932009-12-27 22:54:55 +01001891 if (*ptr != '\n')
1892 return -1;
1893 if (++ptr >= end)
1894 ptr = buf->data;
1895 /* done */
1896 break;
1897 }
1898 else if (*ptr == ';') {
1899 /* chunk extension, ends at next CRLF */
1900 if (++ptr >= end)
1901 ptr = buf->data;
1902 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001903 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001904
1905 while (!HTTP_IS_CRLF(*ptr)) {
1906 if (++ptr >= end)
1907 ptr = buf->data;
1908 if (ptr == buf->r)
1909 return 0;
1910 }
1911 /* we have a CRLF now, loop above */
1912 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001915 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001916 }
1917
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 /* OK we found our CRLF and now <ptr> points to the next byte,
1919 * which may or may not be present. We save that into ->lr and
1920 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 msg->sov += ptr - buf->lr;
1923 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001924 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 return 1;
1927}
1928
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929/* This function skips trailers in the buffer <buf> associated with HTTP
1930 * message <msg>. The first visited position is buf->lr. If the end of
1931 * the trailers is found, it is automatically scheduled to be forwarded,
1932 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1933 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001934 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1935 * zero. If a parse error is encountered, the function returns < 0 and does not
1936 * change anything except maybe buf->lr and msg->sov. Note that the message
1937 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1938 * which implies that all non-trailers data have already been scheduled for
1939 * forwarding, and that the difference between msg->som and msg->sov exactly
1940 * matches the length of trailers already parsed and not forwarded. It is also
1941 * important to note that this function is designed to be able to parse wrapped
1942 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 */
1944int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1945{
1946 /* we have buf->lr which points to next line. Look for CRLF. */
1947 while (1) {
1948 char *p1 = NULL, *p2 = NULL;
1949 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001950 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951
1952 /* scan current line and stop at LF or CRLF */
1953 while (1) {
1954 if (ptr == buf->r)
1955 return 0;
1956
1957 if (*ptr == '\n') {
1958 if (!p1)
1959 p1 = ptr;
1960 p2 = ptr;
1961 break;
1962 }
1963
1964 if (*ptr == '\r') {
1965 if (p1)
1966 return -1;
1967 p1 = ptr;
1968 }
1969
1970 ptr++;
1971 if (ptr >= buf->data + buf->size)
1972 ptr = buf->data;
1973 }
1974
1975 /* after LF; point to beginning of next line */
1976 p2++;
1977 if (p2 >= buf->data + buf->size)
1978 p2 = buf->data;
1979
Willy Tarreau638cd022010-01-03 07:42:04 +01001980 bytes = p2 - buf->lr;
1981 if (bytes < 0)
1982 bytes += buf->size;
1983
1984 /* schedule this line for forwarding */
1985 msg->sov += bytes;
1986 if (msg->sov >= buf->size)
1987 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001988
Willy Tarreau638cd022010-01-03 07:42:04 +01001989 if (p1 == buf->lr) {
1990 /* LF/CRLF at beginning of line => end of trailers at p2.
1991 * Everything was scheduled for forwarding, there's nothing
1992 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001993 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001994 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001995 msg->msg_state = HTTP_MSG_DONE;
1996 return 1;
1997 }
1998 /* OK, next line then */
1999 buf->lr = p2;
2000 }
2001}
2002
2003/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2004 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2005 * ->som, buf->lr in order to include this part into the next forwarding phase.
2006 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2007 * not enough data are available, the function does not change anything and
2008 * returns zero. If a parse error is encountered, the function returns < 0 and
2009 * does not change anything. Note: this function is designed to parse wrapped
2010 * CRLF at the end of the buffer.
2011 */
2012int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2013{
2014 char *ptr;
2015 int bytes;
2016
2017 /* NB: we'll check data availabilty at the end. It's not a
2018 * problem because whatever we match first will be checked
2019 * against the correct length.
2020 */
2021 bytes = 1;
2022 ptr = buf->lr;
2023 if (*ptr == '\r') {
2024 bytes++;
2025 ptr++;
2026 if (ptr >= buf->data + buf->size)
2027 ptr = buf->data;
2028 }
2029
2030 if (buf->l < bytes)
2031 return 0;
2032
2033 if (*ptr != '\n')
2034 return -1;
2035
2036 ptr++;
2037 if (ptr >= buf->data + buf->size)
2038 ptr = buf->data;
2039 buf->lr = ptr;
2040 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2041 msg->sov = ptr - buf->data;
2042 msg->som = msg->sov - bytes;
2043 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2044 return 1;
2045}
Willy Tarreau5b154472009-12-21 20:11:07 +01002046
Willy Tarreau83e3af02009-12-28 17:39:57 +01002047void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2048{
2049 char *end = buf->data + buf->size;
2050 int off = buf->data + buf->size - buf->w;
2051
2052 /* two possible cases :
2053 * - the buffer is in one contiguous block, we move it in-place
2054 * - the buffer is in two blocks, we move it via the trash
2055 */
2056 if (buf->l) {
2057 int block1 = buf->l;
2058 int block2 = 0;
2059 if (buf->r <= buf->w) {
2060 /* non-contiguous block */
2061 block1 = buf->data + buf->size - buf->w;
2062 block2 = buf->r - buf->data;
2063 }
2064 if (block2)
2065 memcpy(trash, buf->data, block2);
2066 memmove(buf->data, buf->w, block1);
2067 if (block2)
2068 memcpy(buf->data + block1, trash, block2);
2069 }
2070
2071 /* adjust all known pointers */
2072 buf->w = buf->data;
2073 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2074 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2075 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2076 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2077
2078 /* adjust relative pointers */
2079 msg->som = 0;
2080 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2081 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2082 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2083
Willy Tarreau83e3af02009-12-28 17:39:57 +01002084 if (msg->err_pos >= 0) {
2085 msg->err_pos += off;
2086 if (msg->err_pos >= buf->size)
2087 msg->err_pos -= buf->size;
2088 }
2089
2090 buf->flags &= ~BF_FULL;
2091 if (buf->l >= buffer_max_len(buf))
2092 buf->flags |= BF_FULL;
2093}
2094
Willy Tarreaud787e662009-07-07 10:14:51 +02002095/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2096 * processing can continue on next analysers, or zero if it either needs more
2097 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2098 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2099 * when it has nothing left to do, and may remove any analyser when it wants to
2100 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002101 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002102int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002103{
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 /*
2105 * We will parse the partial (or complete) lines.
2106 * We will check the request syntax, and also join multi-line
2107 * headers. An index of all the lines will be elaborated while
2108 * parsing.
2109 *
2110 * For the parsing, we use a 28 states FSM.
2111 *
2112 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002113 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002114 * req->data + msg->eoh = end of processed headers / start of current one
2115 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 * req->lr = first non-visited byte
2117 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002118 *
2119 * At end of parsing, we may perform a capture of the error (if any), and
2120 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2121 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2122 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002124
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002126 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 struct http_txn *txn = &s->txn;
2128 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002129 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002130
Willy Tarreau6bf17362009-02-24 10:48:35 +01002131 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2132 now_ms, __FUNCTION__,
2133 s,
2134 req,
2135 req->rex, req->wex,
2136 req->flags,
2137 req->l,
2138 req->analysers);
2139
Willy Tarreau52a0c602009-08-16 22:45:38 +02002140 /* we're speaking HTTP here, so let's speak HTTP to the client */
2141 s->srv_error = http_return_srv_error;
2142
Willy Tarreau83e3af02009-12-28 17:39:57 +01002143 /* There's a protected area at the end of the buffer for rewriting
2144 * purposes. We don't want to start to parse the request if the
2145 * protected area is affected, because we may have to move processed
2146 * data later, which is much more complicated.
2147 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002148 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002149 if ((txn->flags & TX_NOT_FIRST) &&
2150 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002151 req->r < req->lr ||
2152 req->r > req->data + req->size - global.tune.maxrewrite)) {
2153 if (req->send_max) {
2154 /* some data has still not left the buffer, wake us once that's done */
2155 buffer_dont_connect(req);
2156 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2157 return 0;
2158 }
2159 if (req->l <= req->size - global.tune.maxrewrite)
2160 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002161 }
2162
Willy Tarreau065e8332010-01-08 00:30:20 +01002163 /* Note that we have the same problem with the response ; we
2164 * may want to send a redirect, error or anything which requires
2165 * some spare space. So we'll ensure that we have at least
2166 * maxrewrite bytes available in the response buffer before
2167 * processing that one. This will only affect pipelined
2168 * keep-alive requests.
2169 */
2170 if ((txn->flags & TX_NOT_FIRST) &&
2171 unlikely((s->rep->flags & BF_FULL) ||
2172 s->rep->r < s->rep->lr ||
2173 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2174 if (s->rep->send_max) {
2175 /* don't let a connection request be initiated */
2176 buffer_dont_connect(req);
2177 return 0;
2178 }
2179 }
2180
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002181 if (likely(req->lr < req->r))
2182 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002183 }
2184
Willy Tarreau59234e92008-11-30 23:51:27 +01002185 /* 1: we might have to print this header in debug mode */
2186 if (unlikely((global.mode & MODE_DEBUG) &&
2187 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002188 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002190
Willy Tarreau962c3f42010-01-10 00:15:35 +01002191 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 eol = sol + msg->sl.rq.l;
2193 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002194
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 sol += hdr_idx_first_pos(&txn->hdr_idx);
2196 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 while (cur_idx) {
2199 eol = sol + txn->hdr_idx.v[cur_idx].len;
2200 debug_hdr("clihdr", s, sol, eol);
2201 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2202 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002203 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 }
2205
Willy Tarreau58f10d72006-12-04 02:26:12 +01002206
Willy Tarreau59234e92008-11-30 23:51:27 +01002207 /*
2208 * Now we quickly check if we have found a full valid request.
2209 * If not so, we check the FD and buffer states before leaving.
2210 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002211 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002212 * requests are checked first.
2213 *
2214 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002215
Willy Tarreau655dce92009-11-08 13:10:58 +01002216 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002217 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002218 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002219 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2221 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002222
Willy Tarreau59234e92008-11-30 23:51:27 +01002223 /* 1: Since we are in header mode, if there's no space
2224 * left for headers, we won't be able to free more
2225 * later, so the session will never terminate. We
2226 * must terminate it now.
2227 */
2228 if (unlikely(req->flags & BF_FULL)) {
2229 /* FIXME: check if URI is set and return Status
2230 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002231 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 goto return_bad_req;
2233 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002234
Willy Tarreau59234e92008-11-30 23:51:27 +01002235 /* 2: have we encountered a read error ? */
2236 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002237 if (txn->flags & TX_NOT_FIRST)
2238 goto failed_keep_alive;
2239
Willy Tarreau59234e92008-11-30 23:51:27 +01002240 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002241 if (msg->err_pos >= 0)
2242 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002243 msg->msg_state = HTTP_MSG_ERROR;
2244 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002245
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002246 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002247 if (s->listener->counters)
2248 s->listener->counters->failed_req++;
2249
Willy Tarreau59234e92008-11-30 23:51:27 +01002250 if (!(s->flags & SN_ERR_MASK))
2251 s->flags |= SN_ERR_CLICL;
2252 if (!(s->flags & SN_FINST_MASK))
2253 s->flags |= SN_FINST_R;
2254 return 0;
2255 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002256
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 /* 3: has the read timeout expired ? */
2258 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002259 if (txn->flags & TX_NOT_FIRST)
2260 goto failed_keep_alive;
2261
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002263 if (msg->err_pos >= 0)
2264 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 txn->status = 408;
2266 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2267 msg->msg_state = HTTP_MSG_ERROR;
2268 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002269
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002270 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002271 if (s->listener->counters)
2272 s->listener->counters->failed_req++;
2273
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 if (!(s->flags & SN_ERR_MASK))
2275 s->flags |= SN_ERR_CLITO;
2276 if (!(s->flags & SN_FINST_MASK))
2277 s->flags |= SN_FINST_R;
2278 return 0;
2279 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002280
Willy Tarreau59234e92008-11-30 23:51:27 +01002281 /* 4: have we encountered a close ? */
2282 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002283 if (txn->flags & TX_NOT_FIRST)
2284 goto failed_keep_alive;
2285
Willy Tarreau4076a152009-04-02 15:18:36 +02002286 if (msg->err_pos >= 0)
2287 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 txn->status = 400;
2289 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2290 msg->msg_state = HTTP_MSG_ERROR;
2291 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002292
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002293 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002294 if (s->listener->counters)
2295 s->listener->counters->failed_req++;
2296
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 if (!(s->flags & SN_ERR_MASK))
2298 s->flags |= SN_ERR_CLICL;
2299 if (!(s->flags & SN_FINST_MASK))
2300 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002301 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002302 }
2303
Willy Tarreau520d95e2009-09-19 21:04:57 +02002304 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002305 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2306
Willy Tarreau59234e92008-11-30 23:51:27 +01002307 /* just set the request timeout once at the beginning of the request */
2308 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002309 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 /* we're not ready yet */
2312 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002313
2314 failed_keep_alive:
2315 /* Here we process low-level errors for keep-alive requests. In
2316 * short, if the request is not the first one and it experiences
2317 * a timeout, read error or shutdown, we just silently close so
2318 * that the client can try again.
2319 */
2320 txn->status = 0;
2321 msg->msg_state = HTTP_MSG_RQBEFORE;
2322 req->analysers = 0;
2323 s->logs.logwait = 0;
Willy Tarreau148d0992010-01-10 10:21:21 +01002324 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002325 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002327
Willy Tarreaud787e662009-07-07 10:14:51 +02002328 /* OK now we have a complete HTTP request with indexed headers. Let's
2329 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002330 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2331 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2332 * points to the CRLF of the request line. req->lr points to the first
2333 * byte after the last LF. msg->col and msg->sov point to the first
2334 * byte of data. msg->eol cannot be trusted because it may have been
2335 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002336 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002337
Willy Tarreaud787e662009-07-07 10:14:51 +02002338 /* Maybe we found in invalid header name while we were configured not
2339 * to block on that, so we have to capture it now.
2340 */
2341 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002342 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /*
2345 * 1: identify the method
2346 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002347 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002348
2349 /* we can make use of server redirect on GET and HEAD */
2350 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2351 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /*
2354 * 2: check if the URI matches the monitor_uri.
2355 * We have to do this for every request which gets in, because
2356 * the monitor-uri is defined by the frontend.
2357 */
2358 if (unlikely((s->fe->monitor_uri_len != 0) &&
2359 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002360 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 s->fe->monitor_uri,
2362 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002363 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002365 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002367
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002369
Willy Tarreau59234e92008-11-30 23:51:27 +01002370 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002371 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2372 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002373
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 ret = acl_pass(ret);
2375 if (cond->pol == ACL_COND_UNLESS)
2376 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002377
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 if (ret) {
2379 /* we fail this request, let's return 503 service unavail */
2380 txn->status = 503;
2381 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2382 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002383 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002385
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 /* nothing to fail, let's reply normaly */
2387 txn->status = 200;
2388 stream_int_retnclose(req->prod, &http_200_chunk);
2389 goto return_prx_cond;
2390 }
2391
2392 /*
2393 * 3: Maybe we have to copy the original REQURI for the logs ?
2394 * Note: we cannot log anymore if the request has been
2395 * classified as invalid.
2396 */
2397 if (unlikely(s->logs.logwait & LW_REQ)) {
2398 /* we have a complete HTTP request that we must log */
2399 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2400 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002401
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 if (urilen >= REQURI_LEN)
2403 urilen = REQURI_LEN - 1;
2404 memcpy(txn->uri, &req->data[msg->som], urilen);
2405 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 if (!(s->logs.logwait &= ~LW_REQ))
2408 s->do_log(s);
2409 } else {
2410 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002411 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002412 }
Willy Tarreau06619262006-12-17 08:37:22 +01002413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002415 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2416 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002417
Willy Tarreau5b154472009-12-21 20:11:07 +01002418 /* ... and check if the request is HTTP/1.1 or above */
2419 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002420 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2421 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2422 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002423 txn->flags |= TX_REQ_VER_11;
2424
2425 /* "connection" has not been parsed yet */
2426 txn->flags &= ~TX_CON_HDR_PARS;
2427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 /* transfer length unknown*/
2429 txn->flags &= ~TX_REQ_XFER_LEN;
2430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /* 5: we may need to capture headers */
2432 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002433 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002435
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436 /* 6: determine the transfer-length.
2437 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2438 * the presence of a message-body in a REQUEST and its transfer length
2439 * must be determined that way (in order of precedence) :
2440 * 1. The presence of a message-body in a request is signaled by the
2441 * inclusion of a Content-Length or Transfer-Encoding header field
2442 * in the request's header fields. When a request message contains
2443 * both a message-body of non-zero length and a method that does
2444 * not define any semantics for that request message-body, then an
2445 * origin server SHOULD either ignore the message-body or respond
2446 * with an appropriate error message (e.g., 413). A proxy or
2447 * gateway, when presented the same request, SHOULD either forward
2448 * the request inbound with the message- body or ignore the
2449 * message-body when determining a response.
2450 *
2451 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2452 * and the "chunked" transfer-coding (Section 6.2) is used, the
2453 * transfer-length is defined by the use of this transfer-coding.
2454 * If a Transfer-Encoding header field is present and the "chunked"
2455 * transfer-coding is not present, the transfer-length is defined
2456 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002457 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002458 * 3. If a Content-Length header field is present, its decimal value in
2459 * OCTETs represents both the entity-length and the transfer-length.
2460 * If a message is received with both a Transfer-Encoding header
2461 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 * 4. By the server closing the connection. (Closing the connection
2464 * cannot be used to indicate the end of a request body, since that
2465 * would leave no possibility for the server to send back a response.)
2466 *
2467 * Whenever a transfer-coding is applied to a message-body, the set of
2468 * transfer-codings MUST include "chunked", unless the message indicates
2469 * it is terminated by closing the connection. When the "chunked"
2470 * transfer-coding is used, it MUST be the last transfer-coding applied
2471 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472 */
2473
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002474 /* CONNECT sets a tunnel and ignores everything else */
2475 if (txn->meth == HTTP_METH_CONNECT)
2476 goto skip_xfer_len;
2477
2478 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002479 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002480 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002481 while ((txn->flags & TX_REQ_VER_11) &&
2482 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002483 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2484 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2485 else if (txn->flags & TX_REQ_TE_CHNK) {
2486 /* bad transfer-encoding (chunked followed by something else) */
2487 use_close_only = 1;
2488 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2489 break;
2490 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002491 }
2492
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002494 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2496 signed long long cl;
2497
2498 if (!ctx.vlen)
2499 goto return_bad_req;
2500
2501 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2502 goto return_bad_req; /* parse failure */
2503
2504 if (cl < 0)
2505 goto return_bad_req;
2506
2507 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2508 goto return_bad_req; /* already specified, was different */
2509
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002510 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002511 msg->hdr_content_len = cl;
2512 }
2513
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002514 /* bodyless requests have a known length */
2515 if (!use_close_only)
2516 txn->flags |= TX_REQ_XFER_LEN;
2517
2518 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002519 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002520 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 req->analyse_exp = TICK_ETERNITY;
2522 return 1;
2523
2524 return_bad_req:
2525 /* We centralize bad requests processing here */
2526 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2527 /* we detected a parsing error. We want to archive this request
2528 * in the dedicated proxy area for later troubleshooting.
2529 */
2530 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2531 }
2532
2533 txn->req.msg_state = HTTP_MSG_ERROR;
2534 txn->status = 400;
2535 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002536
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002537 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002538 if (s->listener->counters)
2539 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002540
2541 return_prx_cond:
2542 if (!(s->flags & SN_ERR_MASK))
2543 s->flags |= SN_ERR_PRXCOND;
2544 if (!(s->flags & SN_FINST_MASK))
2545 s->flags |= SN_FINST_R;
2546
2547 req->analysers = 0;
2548 req->analyse_exp = TICK_ETERNITY;
2549 return 0;
2550}
2551
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002552/* This stream analyser runs all HTTP request processing which is common to
2553 * frontends and backends, which means blocking ACLs, filters, connection-close,
2554 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002555 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002556 * either needs more data or wants to immediately abort the request (eg: deny,
2557 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002559int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002560{
Willy Tarreaud787e662009-07-07 10:14:51 +02002561 struct http_txn *txn = &s->txn;
2562 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002563 struct acl_cond *cond;
2564 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002565 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002566 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002567
Willy Tarreau655dce92009-11-08 13:10:58 +01002568 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002569 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002570 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002571 return 0;
2572 }
2573
Willy Tarreau3a816292009-07-07 10:55:49 +02002574 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002575 req->analyse_exp = TICK_ETERNITY;
2576
2577 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2578 now_ms, __FUNCTION__,
2579 s,
2580 req,
2581 req->rex, req->wex,
2582 req->flags,
2583 req->l,
2584 req->analysers);
2585
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002586 /* first check whether we have some ACLs set to block this request */
2587 list_for_each_entry(cond, &px->block_cond, list) {
2588 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002589
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002590 ret = acl_pass(ret);
2591 if (cond->pol == ACL_COND_UNLESS)
2592 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002593
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002594 if (ret) {
2595 txn->status = 403;
2596 /* let's log the request time */
2597 s->logs.tv_request = now;
2598 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2599 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002601 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002602
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002603 /* try headers filters */
2604 if (px->req_exp != NULL) {
2605 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2606 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 /* has the request been denied ? */
2609 if (txn->flags & TX_CLDENY) {
2610 /* no need to go further */
2611 txn->status = 403;
2612 /* let's log the request time */
2613 s->logs.tv_request = now;
2614 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2615 goto return_prx_cond;
2616 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002617
2618 /* When a connection is tarpitted, we use the tarpit timeout,
2619 * which may be the same as the connect timeout if unspecified.
2620 * If unset, then set it to zero because we really want it to
2621 * eventually expire. We build the tarpit as an analyser.
2622 */
2623 if (txn->flags & TX_CLTARPIT) {
2624 buffer_erase(s->req);
2625 /* wipe the request out so that we can drop the connection early
2626 * if the client closes first.
2627 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002628 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002629 req->analysers = 0; /* remove switching rules etc... */
2630 req->analysers |= AN_REQ_HTTP_TARPIT;
2631 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2632 if (!req->analyse_exp)
2633 req->analyse_exp = tick_add(now_ms, 0);
2634 return 1;
2635 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002636 }
Willy Tarreau06619262006-12-17 08:37:22 +01002637
Willy Tarreau5b154472009-12-21 20:11:07 +01002638 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2639 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002640 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2641 * in which headers are mangled. However, if another mode is set, it will
2642 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002643 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002644
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002645 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002646 ((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 +01002647 int tmp = TX_CON_WANT_TUN;
2648 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2649 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002650 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2651 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002652 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002653 tmp = TX_CON_WANT_CLO;
2654
Willy Tarreau5b154472009-12-21 20:11:07 +01002655 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2656 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002657
Willy Tarreau8db1c172010-01-05 23:12:12 +01002658 if (!(txn->flags & TX_CON_HDR_PARS))
2659 http_req_parse_connection_header(txn);
2660
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002661 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2662 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2663 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2664 if (!(txn->flags & TX_REQ_XFER_LEN))
2665 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2666 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002667 }
2668
2669 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2670 * once we know the backend, because the tunnel mode can be implied by the
2671 * lack of any close/keepalive options in both the FE and the BE. Since
2672 * this information can evolve with time, we proceed by trying to make the
2673 * header status match the desired status. For this, we'll have to adjust
2674 * the "Connection" header. The test for persistent connections has already
2675 * been performed, so we only enter here if there is a risk the connection
2676 * is considered as persistent and we want it to be closed on the server
2677 * side. It would be nice if we could enter this place only when a
2678 * Connection header exists. Note that a CONNECT method will not enter
2679 * here.
2680 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002681 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2682 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2683 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002684 char *cur_ptr, *cur_end, *cur_next;
2685 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002686 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002687 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002688
Willy Tarreau5b154472009-12-21 20:11:07 +01002689 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002690 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002691
Willy Tarreau5b154472009-12-21 20:11:07 +01002692 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002693 cur_hdr = &txn->hdr_idx.v[cur_idx];
2694 cur_ptr = cur_next;
2695 cur_end = cur_ptr + cur_hdr->len;
2696 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002697
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002698 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002699 if (!val)
2700 continue;
2701
2702 /* 3 possibilities :
2703 * - we have already set "Connection: close" or we're in
2704 * HTTP/1.0, so we remove this line.
2705 * - we have not yet set "Connection: close", but this line
2706 * indicates close. We leave it untouched and set the flag.
2707 * - we have not yet set "Connection: close", and this line
2708 * indicates non-close. We replace it and set the flag.
2709 */
2710 if (must_delete) {
2711 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2712 http_msg_move_end(&txn->req, delta);
2713 cur_next += delta;
2714 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2715 txn->hdr_idx.used--;
2716 cur_hdr->len = 0;
2717 txn->flags |= TX_REQ_CONN_CLO;
2718 } else {
2719 if (cur_end - cur_ptr - val != 5 ||
2720 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2721 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2722 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002723 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002724 cur_hdr->len += delta;
2725 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002726 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002727 txn->flags |= TX_REQ_CONN_CLO;
2728 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002729 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002730 } /* for loop */
2731 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002732
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002734 list_for_each_entry(wl, &px->req_add, list) {
2735 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002736 goto return_bad_req;
2737 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002738
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002739 /* check if stats URI was requested, and if an auth is needed */
2740 if (px->uri_auth != NULL &&
2741 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2742 /* we have to check the URI and auth for this request.
2743 * FIXME!!! that one is rather dangerous, we want to
2744 * make it follow standard rules (eg: clear req->analysers).
2745 */
2746 if (stats_check_uri_auth(s, px)) {
2747 req->analysers = 0;
2748 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 /* check whether we have some ACLs set to redirect this request */
2753 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002754 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002755
Willy Tarreauf285f542010-01-03 20:03:03 +01002756 if (rule->cond) {
2757 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2758 ret = acl_pass(ret);
2759 if (rule->cond->pol == ACL_COND_UNLESS)
2760 ret = !ret;
2761 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002762
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002764 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002766
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 /* build redirect message */
2768 switch(rule->code) {
2769 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770 msg_fmt = HTTP_303;
2771 break;
2772 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 msg_fmt = HTTP_301;
2774 break;
2775 case 302:
2776 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002777 msg_fmt = HTTP_302;
2778 break;
2779 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002780
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002781 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002782 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002783
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002784 switch(rule->type) {
2785 case REDIRECT_TYPE_PREFIX: {
2786 const char *path;
2787 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002788
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002789 path = http_get_path(txn);
2790 /* build message using path */
2791 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002792 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002793 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2794 int qs = 0;
2795 while (qs < pathlen) {
2796 if (path[qs] == '?') {
2797 pathlen = qs;
2798 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002799 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002800 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002801 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002802 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 } else {
2804 path = "/";
2805 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002806 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002807
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002808 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002809 goto return_bad_req;
2810
2811 /* add prefix. Note that if prefix == "/", we don't want to
2812 * add anything, otherwise it makes it hard for the user to
2813 * configure a self-redirection.
2814 */
2815 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002816 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2817 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002818 }
2819
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820 /* add path */
2821 memcpy(rdr.str + rdr.len, path, pathlen);
2822 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01002823
2824 /* append a slash at the end of the location is needed and missing */
2825 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
2826 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2827 if (rdr.len > rdr.size - 5)
2828 goto return_bad_req;
2829 rdr.str[rdr.len] = '/';
2830 rdr.len++;
2831 }
2832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 break;
2834 }
2835 case REDIRECT_TYPE_LOCATION:
2836 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002837 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002838 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002839
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002840 /* add location */
2841 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2842 rdr.len += rule->rdr_len;
2843 break;
2844 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002845
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002846 if (rule->cookie_len) {
2847 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2848 rdr.len += 14;
2849 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2850 rdr.len += rule->cookie_len;
2851 memcpy(rdr.str + rdr.len, "\r\n", 2);
2852 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002853 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002854
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002855 /* add end of headers and the keep-alive/close status.
2856 * We may choose to set keep-alive if the Location begins
2857 * with a slash, because the client will come back to the
2858 * same server.
2859 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002860 txn->status = rule->code;
2861 /* let's log the request time */
2862 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002863
2864 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2865 (txn->flags & TX_REQ_XFER_LEN) &&
2866 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2867 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2868 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2869 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01002870 if (!(txn->flags & TX_REQ_VER_11)) {
2871 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
2872 rdr.len += 24;
2873 }
2874 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2875 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002876 buffer_write(req->prod->ob, rdr.str, rdr.len);
2877 /* "eat" the request */
2878 buffer_ignore(req, msg->sov - msg->som);
2879 msg->som = msg->sov;
2880 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002881 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2882 txn->req.msg_state = HTTP_MSG_CLOSED;
2883 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002884 break;
2885 } else {
2886 /* keep-alive not possible */
2887 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2888 rdr.len += 23;
Willy Tarreau148d0992010-01-10 10:21:21 +01002889 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002890 goto return_prx_cond;
2891 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002892 }
2893 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002894
Willy Tarreau2be39392010-01-03 17:24:51 +01002895 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2896 * If this happens, then the data will not come immediately, so we must
2897 * send all what we have without waiting. Note that due to the small gain
2898 * in waiting for the body of the request, it's easier to simply put the
2899 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2900 * itself once used.
2901 */
2902 req->flags |= BF_SEND_DONTWAIT;
2903
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002904 /* that's OK for us now, let's move on to next analysers */
2905 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002906
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002907 return_bad_req:
2908 /* We centralize bad requests processing here */
2909 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2910 /* we detected a parsing error. We want to archive this request
2911 * in the dedicated proxy area for later troubleshooting.
2912 */
2913 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2914 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002915
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002916 txn->req.msg_state = HTTP_MSG_ERROR;
2917 txn->status = 400;
2918 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002919
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002920 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002921 if (s->listener->counters)
2922 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002923
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002924 return_prx_cond:
2925 if (!(s->flags & SN_ERR_MASK))
2926 s->flags |= SN_ERR_PRXCOND;
2927 if (!(s->flags & SN_FINST_MASK))
2928 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002929
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002930 req->analysers = 0;
2931 req->analyse_exp = TICK_ETERNITY;
2932 return 0;
2933}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002934
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002935/* This function performs all the processing enabled for the current request.
2936 * It returns 1 if the processing can continue on next analysers, or zero if it
2937 * needs more data, encounters an error, or wants to immediately abort the
2938 * request. It relies on buffers flags, and updates s->req->analysers.
2939 */
2940int http_process_request(struct session *s, struct buffer *req, int an_bit)
2941{
2942 struct http_txn *txn = &s->txn;
2943 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002944
Willy Tarreau655dce92009-11-08 13:10:58 +01002945 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002946 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002947 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002948 return 0;
2949 }
2950
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002951 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2952 now_ms, __FUNCTION__,
2953 s,
2954 req,
2955 req->rex, req->wex,
2956 req->flags,
2957 req->l,
2958 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002959
Willy Tarreau59234e92008-11-30 23:51:27 +01002960 /*
2961 * Right now, we know that we have processed the entire headers
2962 * and that unwanted requests have been filtered out. We can do
2963 * whatever we want with the remaining request. Also, now we
2964 * may have separate values for ->fe, ->be.
2965 */
Willy Tarreau06619262006-12-17 08:37:22 +01002966
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 /*
2968 * If HTTP PROXY is set we simply get remote server address
2969 * parsing incoming request.
2970 */
2971 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002972 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002974
Willy Tarreau59234e92008-11-30 23:51:27 +01002975 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002976 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002977 * Note that doing so might move headers in the request, but
2978 * the fields will stay coherent and the URI will not move.
2979 * This should only be performed in the backend.
2980 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002981 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002982 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2983 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002984
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002986 * 8: the appsession cookie was looked up very early in 1.2,
2987 * so let's do the same now.
2988 */
2989
2990 /* It needs to look into the URI */
2991 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002992 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002993 }
2994
2995 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 * 9: add X-Forwarded-For if either the frontend or the backend
2997 * asks for it.
2998 */
2999 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3000 if (s->cli_addr.ss_family == AF_INET) {
3001 /* Add an X-Forwarded-For header unless the source IP is
3002 * in the 'except' network range.
3003 */
3004 if ((!s->fe->except_mask.s_addr ||
3005 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3006 != s->fe->except_net.s_addr) &&
3007 (!s->be->except_mask.s_addr ||
3008 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3009 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003010 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003011 unsigned char *pn;
3012 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003013
3014 /* Note: we rely on the backend to get the header name to be used for
3015 * x-forwarded-for, because the header is really meant for the backends.
3016 * However, if the backend did not specify any option, we have to rely
3017 * on the frontend's header name.
3018 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003019 if (s->be->fwdfor_hdr_len) {
3020 len = s->be->fwdfor_hdr_len;
3021 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003022 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003023 len = s->fe->fwdfor_hdr_len;
3024 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003025 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003026 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003027
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003028 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003029 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003030 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003031 }
3032 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 else if (s->cli_addr.ss_family == AF_INET6) {
3034 /* FIXME: for the sake of completeness, we should also support
3035 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003036 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003037 int len;
3038 char pn[INET6_ADDRSTRLEN];
3039 inet_ntop(AF_INET6,
3040 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3041 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003042
Willy Tarreau59234e92008-11-30 23:51:27 +01003043 /* Note: we rely on the backend to get the header name to be used for
3044 * x-forwarded-for, because the header is really meant for the backends.
3045 * However, if the backend did not specify any option, we have to rely
3046 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003047 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003048 if (s->be->fwdfor_hdr_len) {
3049 len = s->be->fwdfor_hdr_len;
3050 memcpy(trash, s->be->fwdfor_hdr_name, len);
3051 } else {
3052 len = s->fe->fwdfor_hdr_len;
3053 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003054 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003055 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003056
Willy Tarreau59234e92008-11-30 23:51:27 +01003057 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003058 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003059 goto return_bad_req;
3060 }
3061 }
3062
3063 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003064 * 10: add X-Original-To if either the frontend or the backend
3065 * asks for it.
3066 */
3067 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3068
3069 /* FIXME: don't know if IPv6 can handle that case too. */
3070 if (s->cli_addr.ss_family == AF_INET) {
3071 /* Add an X-Original-To header unless the destination IP is
3072 * in the 'except' network range.
3073 */
3074 if (!(s->flags & SN_FRT_ADDR_SET))
3075 get_frt_addr(s);
3076
3077 if ((!s->fe->except_mask_to.s_addr ||
3078 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3079 != s->fe->except_to.s_addr) &&
3080 (!s->be->except_mask_to.s_addr ||
3081 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3082 != s->be->except_to.s_addr)) {
3083 int len;
3084 unsigned char *pn;
3085 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3086
3087 /* Note: we rely on the backend to get the header name to be used for
3088 * x-original-to, because the header is really meant for the backends.
3089 * However, if the backend did not specify any option, we have to rely
3090 * on the frontend's header name.
3091 */
3092 if (s->be->orgto_hdr_len) {
3093 len = s->be->orgto_hdr_len;
3094 memcpy(trash, s->be->orgto_hdr_name, len);
3095 } else {
3096 len = s->fe->orgto_hdr_len;
3097 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003098 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003099 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3100
3101 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003102 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003103 goto return_bad_req;
3104 }
3105 }
3106 }
3107
Willy Tarreau78599912009-10-17 20:12:21 +02003108 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003109 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3110 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3111 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003112 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003113 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003114 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003115 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003116 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003117
3118 /* If we have no server assigned yet and we're balancing on url_param
3119 * with a POST request, we may be interested in checking the body for
3120 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003121 */
3122 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3123 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003124 s->be->url_param_post_limit != 0 &&
3125 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003126 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003127 buffer_dont_connect(req);
3128 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003129 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003130
Willy Tarreaud98cf932009-12-27 22:54:55 +01003131 if (txn->flags & TX_REQ_XFER_LEN)
3132 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003133
Willy Tarreau59234e92008-11-30 23:51:27 +01003134 /*************************************************************
3135 * OK, that's finished for the headers. We have done what we *
3136 * could. Let's switch to the DATA state. *
3137 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003138 req->analyse_exp = TICK_ETERNITY;
3139 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003140
Willy Tarreau59234e92008-11-30 23:51:27 +01003141 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003142 /* OK let's go on with the BODY now */
3143 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003144
Willy Tarreau59234e92008-11-30 23:51:27 +01003145 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003146 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003147 /* we detected a parsing error. We want to archive this request
3148 * in the dedicated proxy area for later troubleshooting.
3149 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003150 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003151 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003152
Willy Tarreau59234e92008-11-30 23:51:27 +01003153 txn->req.msg_state = HTTP_MSG_ERROR;
3154 txn->status = 400;
3155 req->analysers = 0;
3156 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003157
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003158 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003159 if (s->listener->counters)
3160 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003161
Willy Tarreau59234e92008-11-30 23:51:27 +01003162 if (!(s->flags & SN_ERR_MASK))
3163 s->flags |= SN_ERR_PRXCOND;
3164 if (!(s->flags & SN_FINST_MASK))
3165 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003166 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003167}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003168
Willy Tarreau60b85b02008-11-30 23:28:40 +01003169/* This function is an analyser which processes the HTTP tarpit. It always
3170 * returns zero, at the beginning because it prevents any other processing
3171 * from occurring, and at the end because it terminates the request.
3172 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003173int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003174{
3175 struct http_txn *txn = &s->txn;
3176
3177 /* This connection is being tarpitted. The CLIENT side has
3178 * already set the connect expiration date to the right
3179 * timeout. We just have to check that the client is still
3180 * there and that the timeout has not expired.
3181 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003182 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003183 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3184 !tick_is_expired(req->analyse_exp, now_ms))
3185 return 0;
3186
3187 /* We will set the queue timer to the time spent, just for
3188 * logging purposes. We fake a 500 server error, so that the
3189 * attacker will not suspect his connection has been tarpitted.
3190 * It will not cause trouble to the logs because we can exclude
3191 * the tarpitted connections by filtering on the 'PT' status flags.
3192 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003193 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3194
3195 txn->status = 500;
3196 if (req->flags != BF_READ_ERROR)
3197 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3198
3199 req->analysers = 0;
3200 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003201
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003202 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003203 if (s->listener->counters)
3204 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003205
Willy Tarreau60b85b02008-11-30 23:28:40 +01003206 if (!(s->flags & SN_ERR_MASK))
3207 s->flags |= SN_ERR_PRXCOND;
3208 if (!(s->flags & SN_FINST_MASK))
3209 s->flags |= SN_FINST_T;
3210 return 0;
3211}
3212
Willy Tarreaud34af782008-11-30 23:36:37 +01003213/* This function is an analyser which processes the HTTP request body. It looks
3214 * for parameters to be used for the load balancing algorithm (url_param). It
3215 * must only be called after the standard HTTP request processing has occurred,
3216 * because it expects the request to be parsed. It returns zero if it needs to
3217 * read more data, or 1 once it has completed its analysis.
3218 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003219int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003220{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003221 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003222 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003223 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003224
3225 /* We have to parse the HTTP request body to find any required data.
3226 * "balance url_param check_post" should have been the only way to get
3227 * into this. We were brought here after HTTP header analysis, so all
3228 * related structures are ready.
3229 */
3230
Willy Tarreau522d6c02009-12-06 18:49:18 +01003231 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3232 goto missing_data;
3233
3234 if (msg->msg_state < HTTP_MSG_100_SENT) {
3235 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3236 * send an HTTP/1.1 100 Continue intermediate response.
3237 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003238 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003239 struct hdr_ctx ctx;
3240 ctx.idx = 0;
3241 /* Expect is allowed in 1.1, look for it */
3242 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3243 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3244 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3245 }
3246 }
3247 msg->msg_state = HTTP_MSG_100_SENT;
3248 }
3249
3250 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003251 /* we have msg->col and msg->sov which both point to the first
3252 * byte of message body. msg->som still points to the beginning
3253 * of the message. We must save the body in req->lr because it
3254 * survives buffer re-alignments.
3255 */
3256 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003257 if (txn->flags & TX_REQ_TE_CHNK)
3258 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3259 else
3260 msg->msg_state = HTTP_MSG_DATA;
3261 }
3262
3263 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003264 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003265 * set ->sov and ->lr to point to the body and switch to DATA or
3266 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003267 */
3268 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003269
Willy Tarreau115acb92009-12-26 13:56:06 +01003270 if (!ret)
3271 goto missing_data;
3272 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003273 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003274 }
3275
Willy Tarreaud98cf932009-12-27 22:54:55 +01003276 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003277 * We have the first non-header byte in msg->col, which is either the
3278 * beginning of the chunk size or of the data. The first data byte is in
3279 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3280 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003281 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003282
3283 if (msg->hdr_content_len < limit)
3284 limit = msg->hdr_content_len;
3285
Willy Tarreau7c96f672009-12-27 22:47:25 +01003286 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003287 goto http_end;
3288
3289 missing_data:
3290 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003291 if (req->flags & BF_FULL)
3292 goto return_bad_req;
3293
Willy Tarreau522d6c02009-12-06 18:49:18 +01003294 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3295 txn->status = 408;
3296 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3297 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003298 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003299
3300 /* we get here if we need to wait for more data */
3301 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003302 /* Not enough data. We'll re-use the http-request
3303 * timeout here. Ideally, we should set the timeout
3304 * relative to the accept() date. We just set the
3305 * request timeout once at the beginning of the
3306 * request.
3307 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003308 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003309 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003310 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003311 return 0;
3312 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003313
3314 http_end:
3315 /* The situation will not evolve, so let's give up on the analysis. */
3316 s->logs.tv_request = now; /* update the request timer to reflect full request */
3317 req->analysers &= ~an_bit;
3318 req->analyse_exp = TICK_ETERNITY;
3319 return 1;
3320
3321 return_bad_req: /* let's centralize all bad requests */
3322 txn->req.msg_state = HTTP_MSG_ERROR;
3323 txn->status = 400;
3324 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3325
3326 return_err_msg:
3327 req->analysers = 0;
3328 s->fe->counters.failed_req++;
3329 if (s->listener->counters)
3330 s->listener->counters->failed_req++;
3331
3332 if (!(s->flags & SN_ERR_MASK))
3333 s->flags |= SN_ERR_PRXCOND;
3334 if (!(s->flags & SN_FINST_MASK))
3335 s->flags |= SN_FINST_R;
3336 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003337}
3338
Willy Tarreau610ecce2010-01-04 21:15:02 +01003339/* Terminate current transaction and prepare a new one. This is very tricky
3340 * right now but it works.
3341 */
3342void http_end_txn_clean_session(struct session *s)
3343{
3344 /* FIXME: We need a more portable way of releasing a backend's and a
3345 * server's connections. We need a safer way to reinitialize buffer
3346 * flags. We also need a more accurate method for computing per-request
3347 * data.
3348 */
3349 http_silent_debug(__LINE__, s);
3350
3351 s->req->cons->flags |= SI_FL_NOLINGER;
3352 s->req->cons->shutr(s->req->cons);
3353 s->req->cons->shutw(s->req->cons);
3354
3355 http_silent_debug(__LINE__, s);
3356
3357 if (s->flags & SN_BE_ASSIGNED)
3358 s->be->beconn--;
3359
3360 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3361 session_process_counters(s);
3362
3363 if (s->txn.status) {
3364 int n;
3365
3366 n = s->txn.status / 100;
3367 if (n < 1 || n > 5)
3368 n = 0;
3369
3370 if (s->fe->mode == PR_MODE_HTTP)
3371 s->fe->counters.p.http.rsp[n]++;
3372
3373 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3374 (s->be->mode == PR_MODE_HTTP))
3375 s->be->counters.p.http.rsp[n]++;
3376 }
3377
3378 /* don't count other requests' data */
3379 s->logs.bytes_in -= s->req->l - s->req->send_max;
3380 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3381
3382 /* let's do a final log if we need it */
3383 if (s->logs.logwait &&
3384 !(s->flags & SN_MONITOR) &&
3385 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3386 s->do_log(s);
3387 }
3388
3389 s->logs.accept_date = date; /* user-visible date for logging */
3390 s->logs.tv_accept = now; /* corrected date for internal use */
3391 tv_zero(&s->logs.tv_request);
3392 s->logs.t_queue = -1;
3393 s->logs.t_connect = -1;
3394 s->logs.t_data = -1;
3395 s->logs.t_close = 0;
3396 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3397 s->logs.srv_queue_size = 0; /* we will get this number soon */
3398
3399 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3400 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3401
3402 if (s->pend_pos)
3403 pendconn_free(s->pend_pos);
3404
3405 if (s->srv) {
3406 if (s->flags & SN_CURR_SESS) {
3407 s->flags &= ~SN_CURR_SESS;
3408 s->srv->cur_sess--;
3409 }
3410 if (may_dequeue_tasks(s->srv, s->be))
3411 process_srv_queue(s->srv);
3412 }
3413
3414 if (unlikely(s->srv_conn))
3415 sess_change_server(s, NULL);
3416 s->srv = NULL;
3417
3418 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3419 s->req->cons->fd = -1; /* just to help with debugging */
3420 s->req->cons->err_type = SI_ET_NONE;
3421 s->req->cons->err_loc = NULL;
3422 s->req->cons->exp = TICK_ETERNITY;
3423 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003424 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3425 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 +01003426 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3427 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3428 s->txn.meth = 0;
3429 http_reset_txn(s);
3430 s->txn.flags |= TX_NOT_FIRST;
3431 if (s->be->options2 & PR_O2_INDEPSTR)
3432 s->req->cons->flags |= SI_FL_INDEP_STR;
3433
3434 /* if the request buffer is not empty, it means we're
3435 * about to process another request, so send pending
3436 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003437 * Just don't do this if the buffer is close to be full,
3438 * because the request will wait for it to flush a little
3439 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003440 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003441 if (s->req->l > s->req->send_max) {
3442 if (s->rep->send_max &&
3443 !(s->rep->flags & BF_FULL) &&
3444 s->rep->lr <= s->rep->r &&
3445 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3446 s->rep->flags |= BF_EXPECT_MORE;
3447 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003448
3449 /* we're removing the analysers, we MUST re-enable events detection */
3450 buffer_auto_read(s->req);
3451 buffer_auto_close(s->req);
3452 buffer_auto_read(s->rep);
3453 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003454
3455 /* make ->lr point to the first non-forwarded byte */
3456 s->req->lr = s->req->w + s->req->send_max;
3457 if (s->req->lr >= s->req->data + s->req->size)
3458 s->req->lr -= s->req->size;
3459 s->rep->lr = s->rep->w + s->rep->send_max;
3460 if (s->rep->lr >= s->rep->data + s->rep->size)
3461 s->rep->lr -= s->req->size;
3462
3463 s->req->analysers |= s->fe->fe_req_ana;
3464 s->rep->analysers = 0;
3465
3466 http_silent_debug(__LINE__, s);
3467}
3468
3469
3470/* This function updates the request state machine according to the response
3471 * state machine and buffer flags. It returns 1 if it changes anything (flag
3472 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3473 * it is only used to find when a request/response couple is complete. Both
3474 * this function and its equivalent should loop until both return zero. It
3475 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3476 */
3477int http_sync_req_state(struct session *s)
3478{
3479 struct buffer *buf = s->req;
3480 struct http_txn *txn = &s->txn;
3481 unsigned int old_flags = buf->flags;
3482 unsigned int old_state = txn->req.msg_state;
3483
3484 http_silent_debug(__LINE__, s);
3485 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3486 return 0;
3487
3488 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003489 /* No need to read anymore, the request was completely parsed.
3490 * We can shut the read side unless we want to abort_on_close.
3491 */
3492 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3493 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003494
3495 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3496 goto wait_other_side;
3497
3498 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3499 /* The server has not finished to respond, so we
3500 * don't want to move in order not to upset it.
3501 */
3502 goto wait_other_side;
3503 }
3504
3505 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3506 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003507 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003508 txn->req.msg_state = HTTP_MSG_TUNNEL;
3509 goto wait_other_side;
3510 }
3511
3512 /* When we get here, it means that both the request and the
3513 * response have finished receiving. Depending on the connection
3514 * mode, we'll have to wait for the last bytes to leave in either
3515 * direction, and sometimes for a close to be effective.
3516 */
3517
3518 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3519 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3520 /* Server-close mode : queue a connection close to the server */
3521 buffer_shutw_now(buf);
3522 buf->cons->flags |= SI_FL_NOLINGER;
3523 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003524 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003525 /* Option forceclose is set, let's enforce it now
3526 * that we're not expecting any new data to come.
3527 */
3528 buffer_shutr_now(buf);
3529 buffer_shutw_now(buf);
3530 buf->cons->flags |= SI_FL_NOLINGER;
3531 }
3532 /* other modes include httpclose (no action) and keepalive (not implemented) */
3533 }
3534
3535 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3536 /* if we've just closed an output, let's switch */
3537 if (!(buf->flags & BF_OUT_EMPTY)) {
3538 txn->req.msg_state = HTTP_MSG_CLOSING;
3539 goto http_msg_closing;
3540 }
3541 else {
3542 txn->req.msg_state = HTTP_MSG_CLOSED;
3543 goto http_msg_closed;
3544 }
3545 }
3546 else {
3547 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003548 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003549 txn->req.msg_state = HTTP_MSG_TUNNEL;
3550 goto wait_other_side;
3551 }
3552 }
3553
3554 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3555 http_msg_closing:
3556 /* nothing else to forward, just waiting for the output buffer
3557 * to be empty and for the shutw_now to take effect.
3558 */
3559 if (buf->flags & BF_OUT_EMPTY) {
3560 txn->req.msg_state = HTTP_MSG_CLOSED;
3561 goto http_msg_closed;
3562 }
3563 else if (buf->flags & BF_SHUTW) {
3564 txn->req.msg_state = HTTP_MSG_ERROR;
3565 goto wait_other_side;
3566 }
3567 }
3568
3569 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3570 http_msg_closed:
3571 goto wait_other_side;
3572 }
3573
3574 wait_other_side:
3575 http_silent_debug(__LINE__, s);
3576 return txn->req.msg_state != old_state || buf->flags != old_flags;
3577}
3578
3579
3580/* This function updates the response state machine according to the request
3581 * state machine and buffer flags. It returns 1 if it changes anything (flag
3582 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3583 * it is only used to find when a request/response couple is complete. Both
3584 * this function and its equivalent should loop until both return zero. It
3585 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3586 */
3587int http_sync_res_state(struct session *s)
3588{
3589 struct buffer *buf = s->rep;
3590 struct http_txn *txn = &s->txn;
3591 unsigned int old_flags = buf->flags;
3592 unsigned int old_state = txn->rsp.msg_state;
3593
3594 http_silent_debug(__LINE__, s);
3595 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3596 return 0;
3597
3598 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3599 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003600 * still monitor the server connection for a possible close
3601 * while the request is being uploaded, so we don't disable
3602 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003603 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003604 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003605
3606 if (txn->req.msg_state == HTTP_MSG_ERROR)
3607 goto wait_other_side;
3608
3609 if (txn->req.msg_state < HTTP_MSG_DONE) {
3610 /* The client seems to still be sending data, probably
3611 * because we got an error response during an upload.
3612 * We have the choice of either breaking the connection
3613 * or letting it pass through. Let's do the later.
3614 */
3615 goto wait_other_side;
3616 }
3617
3618 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3619 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003620 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003621 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3622 goto wait_other_side;
3623 }
3624
3625 /* When we get here, it means that both the request and the
3626 * response have finished receiving. Depending on the connection
3627 * mode, we'll have to wait for the last bytes to leave in either
3628 * direction, and sometimes for a close to be effective.
3629 */
3630
3631 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3632 /* Server-close mode : shut read and wait for the request
3633 * side to close its output buffer. The caller will detect
3634 * when we're in DONE and the other is in CLOSED and will
3635 * catch that for the final cleanup.
3636 */
3637 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3638 buffer_shutr_now(buf);
3639 goto wait_other_side;
3640 }
3641 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3642 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3643 /* Option forceclose is set, let's enforce it now
3644 * that we're not expecting any new data to come.
3645 * The caller knows the session is complete once
3646 * both states are CLOSED.
3647 */
3648 buffer_shutr_now(buf);
3649 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003650 }
3651 else {
3652 /* other modes include httpclose (no action) and keepalive
3653 * (not implemented). These modes are used as a tunnel right
3654 * now.
3655 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003656 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003657 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3658 goto wait_other_side;
3659 }
3660
3661 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3662 /* if we've just closed an output, let's switch */
3663 if (!(buf->flags & BF_OUT_EMPTY)) {
3664 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3665 goto http_msg_closing;
3666 }
3667 else {
3668 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3669 goto http_msg_closed;
3670 }
3671 }
3672 goto wait_other_side;
3673 }
3674
3675 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3676 http_msg_closing:
3677 /* nothing else to forward, just waiting for the output buffer
3678 * to be empty and for the shutw_now to take effect.
3679 */
3680 if (buf->flags & BF_OUT_EMPTY) {
3681 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3682 goto http_msg_closed;
3683 }
3684 else if (buf->flags & BF_SHUTW) {
3685 txn->rsp.msg_state = HTTP_MSG_ERROR;
3686 goto wait_other_side;
3687 }
3688 }
3689
3690 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3691 http_msg_closed:
3692 /* drop any pending data */
3693 buffer_ignore(buf, buf->l - buf->send_max);
3694 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003695 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003696 goto wait_other_side;
3697 }
3698
3699 wait_other_side:
3700 http_silent_debug(__LINE__, s);
3701 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3702}
3703
3704
3705/* Resync the request and response state machines. Return 1 if either state
3706 * changes.
3707 */
3708int http_resync_states(struct session *s)
3709{
3710 struct http_txn *txn = &s->txn;
3711 int old_req_state = txn->req.msg_state;
3712 int old_res_state = txn->rsp.msg_state;
3713
3714 http_silent_debug(__LINE__, s);
3715 http_sync_req_state(s);
3716 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003717 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003718 if (!http_sync_res_state(s))
3719 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003720 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003721 if (!http_sync_req_state(s))
3722 break;
3723 }
3724 http_silent_debug(__LINE__, s);
3725 /* OK, both state machines agree on a compatible state.
3726 * There are a few cases we're interested in :
3727 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3728 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3729 * directions, so let's simply disable both analysers.
3730 * - HTTP_MSG_CLOSED on the response only means we must abort the
3731 * request.
3732 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3733 * with server-close mode means we've completed one request and we
3734 * must re-initialize the server connection.
3735 */
3736
3737 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3738 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3739 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3740 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3741 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003742 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003743 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003744 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003745 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003746 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003747 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003748 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3749 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3750 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003751 s->rep->analysers = 0;
3752 buffer_auto_close(s->rep);
3753 buffer_auto_read(s->rep);
3754 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003755 buffer_abort(s->req);
3756 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003757 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003758 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003759 }
3760 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3761 txn->rsp.msg_state == HTTP_MSG_DONE &&
3762 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3763 /* server-close: terminate this server connection and
3764 * reinitialize a fresh-new transaction.
3765 */
3766 http_end_txn_clean_session(s);
3767 }
3768
3769 http_silent_debug(__LINE__, s);
3770 return txn->req.msg_state != old_req_state ||
3771 txn->rsp.msg_state != old_res_state;
3772}
3773
Willy Tarreaud98cf932009-12-27 22:54:55 +01003774/* This function is an analyser which forwards request body (including chunk
3775 * sizes if any). It is called as soon as we must forward, even if we forward
3776 * zero byte. The only situation where it must not be called is when we're in
3777 * tunnel mode and we want to forward till the close. It's used both to forward
3778 * remaining data and to resync after end of body. It expects the msg_state to
3779 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3780 * read more data, or 1 once we can go on with next request or end the session.
3781 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3782 * bytes of pending data + the headers if not already done (between som and sov).
3783 * It eventually adjusts som to match sov after the data in between have been sent.
3784 */
3785int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3786{
3787 struct http_txn *txn = &s->txn;
3788 struct http_msg *msg = &s->txn.req;
3789
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003790 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3791 return 0;
3792
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003793 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3794 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3795 /* Output closed while we were sending data. We must abort. */
3796 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003797 buffer_auto_read(req);
3798 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003799 req->analysers &= ~an_bit;
3800 return 1;
3801 }
3802
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003803 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003804
3805 /* Note that we don't have to send 100-continue back because we don't
3806 * need the data to complete our job, and it's up to the server to
3807 * decide whether to return 100, 417 or anything else in return of
3808 * an "Expect: 100-continue" header.
3809 */
3810
3811 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3812 /* we have msg->col and msg->sov which both point to the first
3813 * byte of message body. msg->som still points to the beginning
3814 * of the message. We must save the body in req->lr because it
3815 * survives buffer re-alignments.
3816 */
3817 req->lr = req->data + msg->sov;
3818 if (txn->flags & TX_REQ_TE_CHNK)
3819 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3820 else {
3821 msg->msg_state = HTTP_MSG_DATA;
3822 }
3823 }
3824
Willy Tarreaud98cf932009-12-27 22:54:55 +01003825 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003826 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003827 /* we may have some data pending */
3828 if (msg->hdr_content_len || msg->som != msg->sov) {
3829 int bytes = msg->sov - msg->som;
3830 if (bytes < 0) /* sov may have wrapped at the end */
3831 bytes += req->size;
3832 buffer_forward(req, bytes + msg->hdr_content_len);
3833 msg->hdr_content_len = 0; /* don't forward that again */
3834 msg->som = msg->sov;
3835 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003836
Willy Tarreaucaabe412010-01-03 23:08:28 +01003837 if (msg->msg_state == HTTP_MSG_DATA) {
3838 /* must still forward */
3839 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003840 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003841
3842 /* nothing left to forward */
3843 if (txn->flags & TX_REQ_TE_CHNK)
3844 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003845 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003846 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003847 }
3848 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003849 /* read the chunk size and assign it to ->hdr_content_len, then
3850 * set ->sov and ->lr to point to the body and switch to DATA or
3851 * TRAILERS state.
3852 */
3853 int ret = http_parse_chunk_size(req, msg);
3854
3855 if (!ret)
3856 goto missing_data;
3857 else if (ret < 0)
3858 goto return_bad_req;
3859 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003860 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003861 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3862 /* we want the CRLF after the data */
3863 int ret;
3864
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003865 req->lr = req->w + req->send_max;
3866 if (req->lr >= req->data + req->size)
3867 req->lr -= req->size;
3868
Willy Tarreaud98cf932009-12-27 22:54:55 +01003869 ret = http_skip_chunk_crlf(req, msg);
3870
3871 if (ret == 0)
3872 goto missing_data;
3873 else if (ret < 0)
3874 goto return_bad_req;
3875 /* we're in MSG_CHUNK_SIZE now */
3876 }
3877 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3878 int ret = http_forward_trailers(req, msg);
3879
3880 if (ret == 0)
3881 goto missing_data;
3882 else if (ret < 0)
3883 goto return_bad_req;
3884 /* we're in HTTP_MSG_DONE now */
3885 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886 else {
3887 /* other states, DONE...TUNNEL */
3888 if (http_resync_states(s)) {
3889 /* some state changes occurred, maybe the analyser
3890 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003891 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003892 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3893 goto return_bad_req;
3894 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003895 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003897 }
3898 }
3899
Willy Tarreaud98cf932009-12-27 22:54:55 +01003900 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003901 /* stop waiting for data if the input is closed before the end */
3902 if (req->flags & BF_SHUTR)
3903 goto return_bad_req;
3904
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003905 /* waiting for the last bits to leave the buffer */
3906 if (req->flags & BF_SHUTW)
3907 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003908
3909 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003910 return 0;
3911
Willy Tarreaud98cf932009-12-27 22:54:55 +01003912 return_bad_req: /* let's centralize all bad requests */
3913 txn->req.msg_state = HTTP_MSG_ERROR;
3914 txn->status = 400;
3915 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01003916 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003917 req->analysers = 0;
3918 s->fe->counters.failed_req++;
3919 if (s->listener->counters)
3920 s->listener->counters->failed_req++;
3921
3922 if (!(s->flags & SN_ERR_MASK))
3923 s->flags |= SN_ERR_PRXCOND;
3924 if (!(s->flags & SN_FINST_MASK))
3925 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003926 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003927 return 0;
3928}
3929
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003930/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3931 * processing can continue on next analysers, or zero if it either needs more
3932 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3933 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3934 * when it has nothing left to do, and may remove any analyser when it wants to
3935 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003936 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003937int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003938{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003939 struct http_txn *txn = &s->txn;
3940 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003941 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003942 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003943 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003944 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003945
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003946 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 +02003947 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003948 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003949 rep,
3950 rep->rex, rep->wex,
3951 rep->flags,
3952 rep->l,
3953 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003954
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003955 /*
3956 * Now parse the partial (or complete) lines.
3957 * We will check the response syntax, and also join multi-line
3958 * headers. An index of all the lines will be elaborated while
3959 * parsing.
3960 *
3961 * For the parsing, we use a 28 states FSM.
3962 *
3963 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003964 * rep->data + msg->som = beginning of response
3965 * rep->data + msg->eoh = end of processed headers / start of current one
3966 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003967 * rep->lr = first non-visited byte
3968 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01003969 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003970 */
3971
Willy Tarreau83e3af02009-12-28 17:39:57 +01003972 /* There's a protected area at the end of the buffer for rewriting
3973 * purposes. We don't want to start to parse the request if the
3974 * protected area is affected, because we may have to move processed
3975 * data later, which is much more complicated.
3976 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003977 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3978 if (unlikely((rep->flags & BF_FULL) ||
3979 rep->r < rep->lr ||
3980 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3981 if (rep->send_max) {
3982 /* some data has still not left the buffer, wake us once that's done */
3983 buffer_dont_close(rep);
3984 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3985 return 0;
3986 }
3987 if (rep->l <= rep->size - global.tune.maxrewrite)
3988 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003989 }
3990
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003991 if (likely(rep->lr < rep->r))
3992 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003993 }
3994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003995 /* 1: we might have to print this header in debug mode */
3996 if (unlikely((global.mode & MODE_DEBUG) &&
3997 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003998 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003999 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004000
Willy Tarreau962c3f42010-01-10 00:15:35 +01004001 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004002 eol = sol + msg->sl.rq.l;
4003 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004004
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004005 sol += hdr_idx_first_pos(&txn->hdr_idx);
4006 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004007
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004008 while (cur_idx) {
4009 eol = sol + txn->hdr_idx.v[cur_idx].len;
4010 debug_hdr("srvhdr", s, sol, eol);
4011 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4012 cur_idx = txn->hdr_idx.v[cur_idx].next;
4013 }
4014 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004015
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004016 /*
4017 * Now we quickly check if we have found a full valid response.
4018 * If not so, we check the FD and buffer states before leaving.
4019 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004020 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004021 * responses are checked first.
4022 *
4023 * Depending on whether the client is still there or not, we
4024 * may send an error response back or not. Note that normally
4025 * we should only check for HTTP status there, and check I/O
4026 * errors somewhere else.
4027 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004028
Willy Tarreau655dce92009-11-08 13:10:58 +01004029 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004030 /* Invalid response */
4031 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4032 /* we detected a parsing error. We want to archive this response
4033 * in the dedicated proxy area for later troubleshooting.
4034 */
4035 hdr_response_bad:
4036 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4037 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4038
4039 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004040 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004041 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004042 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4043 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004044
Willy Tarreau90deb182010-01-07 00:20:41 +01004045 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004046 rep->analysers = 0;
4047 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004048 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004049 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4050
4051 if (!(s->flags & SN_ERR_MASK))
4052 s->flags |= SN_ERR_PRXCOND;
4053 if (!(s->flags & SN_FINST_MASK))
4054 s->flags |= SN_FINST_H;
4055
4056 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004057 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004058
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004059 /* too large response does not fit in buffer. */
4060 else if (rep->flags & BF_FULL) {
4061 goto hdr_response_bad;
4062 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004063
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004064 /* read error */
4065 else if (rep->flags & BF_READ_ERROR) {
4066 if (msg->err_pos >= 0)
4067 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004068
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004069 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004070 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004071 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004072 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4073 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004074
Willy Tarreau90deb182010-01-07 00:20:41 +01004075 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004076 rep->analysers = 0;
4077 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004078 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004079 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004080
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004081 if (!(s->flags & SN_ERR_MASK))
4082 s->flags |= SN_ERR_SRVCL;
4083 if (!(s->flags & SN_FINST_MASK))
4084 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004085 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004086 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004087
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004088 /* read timeout : return a 504 to the client. */
4089 else if (rep->flags & BF_READ_TIMEOUT) {
4090 if (msg->err_pos >= 0)
4091 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004092
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004093 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004094 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004095 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004096 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4097 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004098
Willy Tarreau90deb182010-01-07 00:20:41 +01004099 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004100 rep->analysers = 0;
4101 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004102 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004103 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004104
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004105 if (!(s->flags & SN_ERR_MASK))
4106 s->flags |= SN_ERR_SRVTO;
4107 if (!(s->flags & SN_FINST_MASK))
4108 s->flags |= SN_FINST_H;
4109 return 0;
4110 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004111
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004112 /* close from server */
4113 else if (rep->flags & BF_SHUTR) {
4114 if (msg->err_pos >= 0)
4115 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004116
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004117 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004118 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004119 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004120 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4121 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004122
Willy Tarreau90deb182010-01-07 00:20:41 +01004123 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004124 rep->analysers = 0;
4125 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004126 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004127 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004128
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004129 if (!(s->flags & SN_ERR_MASK))
4130 s->flags |= SN_ERR_SRVCL;
4131 if (!(s->flags & SN_FINST_MASK))
4132 s->flags |= SN_FINST_H;
4133 return 0;
4134 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004135
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004136 /* write error to client (we don't send any message then) */
4137 else if (rep->flags & BF_WRITE_ERROR) {
4138 if (msg->err_pos >= 0)
4139 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004141 s->be->counters.failed_resp++;
4142 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004143 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004144
4145 if (!(s->flags & SN_ERR_MASK))
4146 s->flags |= SN_ERR_CLICL;
4147 if (!(s->flags & SN_FINST_MASK))
4148 s->flags |= SN_FINST_H;
4149
4150 /* process_session() will take care of the error */
4151 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004152 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004153
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004154 buffer_dont_close(rep);
4155 return 0;
4156 }
4157
4158 /* More interesting part now : we know that we have a complete
4159 * response which at least looks like HTTP. We have an indicator
4160 * of each header's length, so we can parse them quickly.
4161 */
4162
4163 if (unlikely(msg->err_pos >= 0))
4164 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4165
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004166 /*
4167 * 1: get the status code
4168 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004169 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004170 if (n < 1 || n > 5)
4171 n = 0;
4172 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004173
Willy Tarreau5b154472009-12-21 20:11:07 +01004174 /* check if the response is HTTP/1.1 or above */
4175 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004176 ((msg->sol[5] > '1') ||
4177 ((msg->sol[5] == '1') &&
4178 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004179 txn->flags |= TX_RES_VER_11;
4180
4181 /* "connection" has not been parsed yet */
4182 txn->flags &= ~TX_CON_HDR_PARS;
4183
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004184 /* transfer length unknown*/
4185 txn->flags &= ~TX_RES_XFER_LEN;
4186
Willy Tarreau962c3f42010-01-10 00:15:35 +01004187 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004188
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004189 if (txn->status >= 100 && txn->status < 500)
4190 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4191 else
4192 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4193
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004194 /*
4195 * 2: check for cacheability.
4196 */
4197
4198 switch (txn->status) {
4199 case 200:
4200 case 203:
4201 case 206:
4202 case 300:
4203 case 301:
4204 case 410:
4205 /* RFC2616 @13.4:
4206 * "A response received with a status code of
4207 * 200, 203, 206, 300, 301 or 410 MAY be stored
4208 * by a cache (...) unless a cache-control
4209 * directive prohibits caching."
4210 *
4211 * RFC2616 @9.5: POST method :
4212 * "Responses to this method are not cacheable,
4213 * unless the response includes appropriate
4214 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004215 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004216 if (likely(txn->meth != HTTP_METH_POST) &&
4217 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4218 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4219 break;
4220 default:
4221 break;
4222 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004223
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004224 /*
4225 * 3: we may need to capture headers
4226 */
4227 s->logs.logwait &= ~LW_RESP;
4228 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004229 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004230 txn->rsp.cap, s->fe->rsp_cap);
4231
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004232 /* 4: determine the transfer-length.
4233 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4234 * the presence of a message-body in a RESPONSE and its transfer length
4235 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004236 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004237 * All responses to the HEAD request method MUST NOT include a
4238 * message-body, even though the presence of entity-header fields
4239 * might lead one to believe they do. All 1xx (informational), 204
4240 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4241 * message-body. All other responses do include a message-body,
4242 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004243 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004244 * 1. Any response which "MUST NOT" include a message-body (such as the
4245 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4246 * always terminated by the first empty line after the header fields,
4247 * regardless of the entity-header fields present in the message.
4248 *
4249 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4250 * the "chunked" transfer-coding (Section 6.2) is used, the
4251 * transfer-length is defined by the use of this transfer-coding.
4252 * If a Transfer-Encoding header field is present and the "chunked"
4253 * transfer-coding is not present, the transfer-length is defined by
4254 * the sender closing the connection.
4255 *
4256 * 3. If a Content-Length header field is present, its decimal value in
4257 * OCTETs represents both the entity-length and the transfer-length.
4258 * If a message is received with both a Transfer-Encoding header
4259 * field and a Content-Length header field, the latter MUST be ignored.
4260 *
4261 * 4. If the message uses the media type "multipart/byteranges", and
4262 * the transfer-length is not otherwise specified, then this self-
4263 * delimiting media type defines the transfer-length. This media
4264 * type MUST NOT be used unless the sender knows that the recipient
4265 * can parse it; the presence in a request of a Range header with
4266 * multiple byte-range specifiers from a 1.1 client implies that the
4267 * client can parse multipart/byteranges responses.
4268 *
4269 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004270 */
4271
4272 /* Skip parsing if no content length is possible. The response flags
4273 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004274 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004275 * FIXME: should we parse anyway and return an error on chunked encoding ?
4276 */
4277 if (txn->meth == HTTP_METH_HEAD ||
4278 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004279 txn->status == 204 || txn->status == 304) {
4280 txn->flags |= TX_RES_XFER_LEN;
4281 goto skip_content_length;
4282 }
4283
4284 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004285 goto skip_content_length;
4286
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004287 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004288 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004289 while ((txn->flags & TX_RES_VER_11) &&
4290 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004291 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4292 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4293 else if (txn->flags & TX_RES_TE_CHNK) {
4294 /* bad transfer-encoding (chunked followed by something else) */
4295 use_close_only = 1;
4296 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4297 break;
4298 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004299 }
4300
4301 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4302 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004303 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004304 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4305 signed long long cl;
4306
4307 if (!ctx.vlen)
4308 goto hdr_response_bad;
4309
4310 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4311 goto hdr_response_bad; /* parse failure */
4312
4313 if (cl < 0)
4314 goto hdr_response_bad;
4315
4316 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4317 goto hdr_response_bad; /* already specified, was different */
4318
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004319 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004320 msg->hdr_content_len = cl;
4321 }
4322
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004323 /* FIXME: we should also implement the multipart/byterange method.
4324 * For now on, we resort to close mode in this case (unknown length).
4325 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004326skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004327
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004328 /* end of job, return OK */
4329 rep->analysers &= ~an_bit;
4330 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004331 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004332 return 1;
4333}
4334
4335/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004336 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4337 * and updates t->rep->analysers. It might make sense to explode it into several
4338 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004339 */
4340int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4341{
4342 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004343 struct http_msg *msg = &txn->rsp;
4344 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004345 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004346 int conn_ka = 0, conn_cl = 0;
4347 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004348 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004349
4350 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4351 now_ms, __FUNCTION__,
4352 t,
4353 rep,
4354 rep->rex, rep->wex,
4355 rep->flags,
4356 rep->l,
4357 rep->analysers);
4358
Willy Tarreau655dce92009-11-08 13:10:58 +01004359 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004360 return 0;
4361
4362 rep->analysers &= ~an_bit;
4363 rep->analyse_exp = TICK_ETERNITY;
4364
Willy Tarreau5b154472009-12-21 20:11:07 +01004365 /* Now we have to check if we need to modify the Connection header.
4366 * This is more difficult on the response than it is on the request,
4367 * because we can have two different HTTP versions and we don't know
4368 * how the client will interprete a response. For instance, let's say
4369 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4370 * HTTP/1.1 response without any header. Maybe it will bound itself to
4371 * HTTP/1.0 because it only knows about it, and will consider the lack
4372 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4373 * the lack of header as a keep-alive. Thus we will use two flags
4374 * indicating how a request MAY be understood by the client. In case
4375 * of multiple possibilities, we'll fix the header to be explicit. If
4376 * ambiguous cases such as both close and keepalive are seen, then we
4377 * will fall back to explicit close. Note that we won't take risks with
4378 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4379 */
4380
4381 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004382 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4383 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4384 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004385 int may_keep = 0, may_close = 0; /* how it may be understood */
4386 struct hdr_ctx ctx;
4387
4388 ctx.idx = 0;
4389 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4390 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4391 conn_cl = 1;
4392 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4393 conn_ka = 1;
4394 }
4395
4396 if (conn_cl) {
4397 /* close header present */
4398 may_close = 1;
4399 if (conn_ka) /* we have both close and keep-alive */
4400 may_keep = 1;
4401 }
4402 else if (conn_ka) {
4403 /* keep-alive alone */
4404 may_keep = 1;
4405 }
4406 else {
4407 /* no close nor keep-alive header */
4408 if (txn->flags & TX_RES_VER_11)
4409 may_keep = 1;
4410 else
4411 may_close = 1;
4412
4413 if (txn->flags & TX_REQ_VER_11)
4414 may_keep = 1;
4415 else
4416 may_close = 1;
4417 }
4418
4419 /* let's update the transaction status to reflect any close.
4420 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004421 * handled. We also explicitly state that we will close in
4422 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004423 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004424 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004425 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4426 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004427 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4428
4429 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004430 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004431 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4432 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004433 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004434 if (may_keep &&
4435 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4436 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004437 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004438 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4439 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4440 must_del_close = 1;
4441 if (!(txn->flags & TX_REQ_VER_11))
4442 must_keep = 1;
4443 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004444
4445 txn->flags |= TX_CON_HDR_PARS;
4446 }
4447
4448 /* We might have to check for "Connection:" if the server
4449 * returns a connection status that is not compatible with
4450 * the client's or with the config.
4451 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004452 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004453 char *cur_ptr, *cur_end, *cur_next;
4454 int cur_idx, old_idx, delta, val;
4455 int must_delete;
4456 struct hdr_idx_elem *cur_hdr;
4457
4458 /* we just have to remove the headers if both sides are 1.0 */
4459 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004460
4461 /* same if we want to re-enable keep-alive on 1.1 */
4462 must_delete |= must_del_close;
4463
Willy Tarreau962c3f42010-01-10 00:15:35 +01004464 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau5b154472009-12-21 20:11:07 +01004465
4466 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4467 cur_hdr = &txn->hdr_idx.v[cur_idx];
4468 cur_ptr = cur_next;
4469 cur_end = cur_ptr + cur_hdr->len;
4470 cur_next = cur_end + cur_hdr->cr + 1;
4471
4472 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4473 if (!val)
4474 continue;
4475
4476 /* 3 possibilities :
4477 * - we have already set "Connection: close" or we're in
4478 * HTTP/1.0, so we remove this line.
4479 * - we have not yet set "Connection: close", but this line
4480 * indicates close. We leave it untouched and set the flag.
4481 * - we have not yet set "Connection: close", and this line
4482 * indicates non-close. We replace it and set the flag.
4483 */
4484 if (must_delete) {
4485 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4486 http_msg_move_end(&txn->rsp, delta);
4487 cur_next += delta;
4488 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4489 txn->hdr_idx.used--;
4490 cur_hdr->len = 0;
4491 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004492 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004493 } else {
4494 if (cur_end - cur_ptr - val != 5 ||
4495 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4496 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4497 "close", 5);
4498 cur_next += delta;
4499 cur_hdr->len += delta;
4500 http_msg_move_end(&txn->rsp, delta);
4501 }
4502 must_delete = 1;
4503 must_close = 0;
4504 }
4505 } /* for loop */
4506 } /* if must close keep-alive */
4507
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004508 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004509 /*
4510 * 3: we will have to evaluate the filters.
4511 * As opposed to version 1.2, now they will be evaluated in the
4512 * filters order and not in the header order. This means that
4513 * each filter has to be validated among all headers.
4514 *
4515 * Filters are tried with ->be first, then with ->fe if it is
4516 * different from ->be.
4517 */
4518
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004519 cur_proxy = t->be;
4520 while (1) {
4521 struct proxy *rule_set = cur_proxy;
4522
4523 /* try headers filters */
4524 if (rule_set->rsp_exp != NULL) {
4525 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4526 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004527 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004528 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004529 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4530 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004531 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004532 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004533 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004534 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004535 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004536 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004537 if (!(t->flags & SN_ERR_MASK))
4538 t->flags |= SN_ERR_PRXCOND;
4539 if (!(t->flags & SN_FINST_MASK))
4540 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004541 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004542 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004543 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004544
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004545 /* has the response been denied ? */
4546 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004547 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004548 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004549
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004550 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004551 if (t->listener->counters)
4552 t->listener->counters->denied_resp++;
4553
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004554 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004555 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004556
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004557 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004558 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004559 if (txn->status < 200)
4560 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004561 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004562 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004563 }
4564
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004565 /* check whether we're already working on the frontend */
4566 if (cur_proxy == t->fe)
4567 break;
4568 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004569 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004570
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004571 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004572 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4573 * in which case this is not the right response, and we're waiting for the
4574 * next one. Let's allow this response to go to the client and wait for the
4575 * next one.
4576 */
4577 if (txn->status < 200) {
4578 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004579 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004580 msg->msg_state = HTTP_MSG_RPBEFORE;
4581 txn->status = 0;
4582 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4583 return 1;
4584 }
4585
4586 /* we don't have any 1xx status code now */
4587
4588 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589 * 4: check for server cookie.
4590 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004591 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4592 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004593 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004594
Willy Tarreaubaaee002006-06-26 02:48:02 +02004595
Willy Tarreaua15645d2007-03-18 16:22:39 +01004596 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004597 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004598 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004599 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004600 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004601
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004602 /*
4603 * 6: add server cookie in the response if needed
4604 */
4605 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004606 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004607 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004608
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004609 /* the server is known, it's not the one the client requested, we have to
4610 * insert a set-cookie here, except if we want to insert only on POST
4611 * requests and this one isn't. Note that servers which don't have cookies
4612 * (eg: some backup servers) will return a full cookie removal request.
4613 */
4614 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4615 t->be->cookie_name,
4616 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004617
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004618 if (t->be->cookie_domain)
4619 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004620
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004621 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004622 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004623 goto return_bad_resp;
4624 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004625
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004626 /* Here, we will tell an eventual cache on the client side that we don't
4627 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4628 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4629 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4630 */
4631 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004632
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004633 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4634
4635 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004636 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004637 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004638 }
4639 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004640
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004641 /*
4642 * 7: check if result will be cacheable with a cookie.
4643 * We'll block the response if security checks have caught
4644 * nasty things such as a cacheable cookie.
4645 */
4646 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4647 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004648 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004649
4650 /* we're in presence of a cacheable response containing
4651 * a set-cookie header. We'll block it as requested by
4652 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004653 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004654 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004655 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004656
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004657 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004658 if (t->listener->counters)
4659 t->listener->counters->denied_resp++;
4660
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004661 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4662 t->be->id, t->srv?t->srv->id:"<dispatch>");
4663 send_log(t->be, LOG_ALERT,
4664 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4665 t->be->id, t->srv?t->srv->id:"<dispatch>");
4666 goto return_srv_prx_502;
4667 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004668
4669 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004670 * 8: add "Connection: close" if needed and not yet set. This is
4671 * only needed for 1.1 responses since we know there is no other
4672 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004673 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004674 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004675 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004676 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004677 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004678 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004679 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004680 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4681 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4682 "Connection: keep-alive", 22) < 0))
4683 goto return_bad_resp;
4684 must_keep = 0;
4685 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004686
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687 if (txn->flags & TX_RES_XFER_LEN)
4688 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004689
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004690 /*************************************************************
4691 * OK, that's finished for the headers. We have done what we *
4692 * could. Let's switch to the DATA state. *
4693 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004694
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004695 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004696
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004697 /* if the user wants to log as soon as possible, without counting
4698 * bytes from the server, then this is the right moment. We have
4699 * to temporarily assign bytes_out to log what we currently have.
4700 */
4701 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4702 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4703 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004704 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004705 t->logs.bytes_out = 0;
4706 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004707
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004708 /* Note: we must not try to cheat by jumping directly to DATA,
4709 * otherwise we would not let the client side wake up.
4710 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004711
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004712 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004713 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004714 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004715}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004716
Willy Tarreaud98cf932009-12-27 22:54:55 +01004717/* This function is an analyser which forwards response body (including chunk
4718 * sizes if any). It is called as soon as we must forward, even if we forward
4719 * zero byte. The only situation where it must not be called is when we're in
4720 * tunnel mode and we want to forward till the close. It's used both to forward
4721 * remaining data and to resync after end of body. It expects the msg_state to
4722 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4723 * read more data, or 1 once we can go on with next request or end the session.
4724 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4725 * bytes of pending data + the headers if not already done (between som and sov).
4726 * It eventually adjusts som to match sov after the data in between have been sent.
4727 */
4728int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4729{
4730 struct http_txn *txn = &s->txn;
4731 struct http_msg *msg = &s->txn.rsp;
4732
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004733 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4734 return 0;
4735
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004736 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004737 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004738 !s->req->analysers) {
4739 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4740 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004741 buffer_auto_read(res);
4742 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004743 res->analysers &= ~an_bit;
4744 return 1;
4745 }
4746
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004747 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004748
Willy Tarreaud98cf932009-12-27 22:54:55 +01004749 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4750 /* we have msg->col and msg->sov which both point to the first
4751 * byte of message body. msg->som still points to the beginning
4752 * of the message. We must save the body in req->lr because it
4753 * survives buffer re-alignments.
4754 */
4755 res->lr = res->data + msg->sov;
4756 if (txn->flags & TX_RES_TE_CHNK)
4757 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4758 else {
4759 msg->msg_state = HTTP_MSG_DATA;
4760 }
4761 }
4762
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004764 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004765 /* we may have some data pending */
4766 if (msg->hdr_content_len || msg->som != msg->sov) {
4767 int bytes = msg->sov - msg->som;
4768 if (bytes < 0) /* sov may have wrapped at the end */
4769 bytes += res->size;
4770 buffer_forward(res, bytes + msg->hdr_content_len);
4771 msg->hdr_content_len = 0; /* don't forward that again */
4772 msg->som = msg->sov;
4773 }
4774
Willy Tarreaucaabe412010-01-03 23:08:28 +01004775 if (msg->msg_state == HTTP_MSG_DATA) {
4776 /* must still forward */
4777 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004778 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004779
4780 /* nothing left to forward */
4781 if (txn->flags & TX_RES_TE_CHNK)
4782 msg->msg_state = HTTP_MSG_DATA_CRLF;
4783 else
4784 msg->msg_state = HTTP_MSG_DONE;
4785 }
4786 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004787 /* read the chunk size and assign it to ->hdr_content_len, then
4788 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4789 */
4790 int ret = http_parse_chunk_size(res, msg);
4791
4792 if (!ret)
4793 goto missing_data;
4794 else if (ret < 0)
4795 goto return_bad_res;
4796 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004797 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004798 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4799 /* we want the CRLF after the data */
4800 int ret;
4801
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004802 res->lr = res->w + res->send_max;
4803 if (res->lr >= res->data + res->size)
4804 res->lr -= res->size;
4805
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 ret = http_skip_chunk_crlf(res, msg);
4807
4808 if (!ret)
4809 goto missing_data;
4810 else if (ret < 0)
4811 goto return_bad_res;
4812 /* we're in MSG_CHUNK_SIZE now */
4813 }
4814 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4815 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004816
Willy Tarreaud98cf932009-12-27 22:54:55 +01004817 if (ret == 0)
4818 goto missing_data;
4819 else if (ret < 0)
4820 goto return_bad_res;
4821 /* we're in HTTP_MSG_DONE now */
4822 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 else {
4824 /* other states, DONE...TUNNEL */
4825 if (http_resync_states(s)) {
4826 http_silent_debug(__LINE__, s);
4827 /* some state changes occurred, maybe the analyser
4828 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004829 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004830 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4831 goto return_bad_res;
4832 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004833 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004834 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004835 }
4836 }
4837
Willy Tarreaud98cf932009-12-27 22:54:55 +01004838 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004839 /* stop waiting for data if the input is closed before the end */
4840 if (res->flags & BF_SHUTR)
4841 goto return_bad_res;
4842
Willy Tarreau610ecce2010-01-04 21:15:02 +01004843 if (!s->req->analysers)
4844 goto return_bad_res;
4845
Willy Tarreaud98cf932009-12-27 22:54:55 +01004846 /* forward the chunk size as well as any pending data */
4847 if (msg->hdr_content_len || msg->som != msg->sov) {
4848 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4849 msg->hdr_content_len = 0; /* don't forward that again */
4850 msg->som = msg->sov;
4851 }
4852
Willy Tarreaud98cf932009-12-27 22:54:55 +01004853 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004854 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004855 return 0;
4856
4857 return_bad_res: /* let's centralize all bad resuests */
4858 txn->rsp.msg_state = HTTP_MSG_ERROR;
4859 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01004860 /* don't send any error message as we're in the body */
4861 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004862 res->analysers = 0;
4863 s->be->counters.failed_resp++;
4864 if (s->srv) {
4865 s->srv->counters.failed_resp++;
4866 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4867 }
4868
4869 if (!(s->flags & SN_ERR_MASK))
4870 s->flags |= SN_ERR_PRXCOND;
4871 if (!(s->flags & SN_FINST_MASK))
4872 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004873 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004874 return 0;
4875}
4876
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004877/* Iterate the same filter through all request headers.
4878 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004879 * Since it can manage the switch to another backend, it updates the per-proxy
4880 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004881 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004882int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004883{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004884 char term;
4885 char *cur_ptr, *cur_end, *cur_next;
4886 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004887 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004888 struct hdr_idx_elem *cur_hdr;
4889 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004890
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004891 last_hdr = 0;
4892
Willy Tarreau962c3f42010-01-10 00:15:35 +01004893 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004894 old_idx = 0;
4895
4896 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004897 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004898 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004899 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004900 (exp->action == ACT_ALLOW ||
4901 exp->action == ACT_DENY ||
4902 exp->action == ACT_TARPIT))
4903 return 0;
4904
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004905 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004906 if (!cur_idx)
4907 break;
4908
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004909 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004910 cur_ptr = cur_next;
4911 cur_end = cur_ptr + cur_hdr->len;
4912 cur_next = cur_end + cur_hdr->cr + 1;
4913
4914 /* Now we have one header between cur_ptr and cur_end,
4915 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004916 */
4917
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004918 /* The annoying part is that pattern matching needs
4919 * that we modify the contents to null-terminate all
4920 * strings before testing them.
4921 */
4922
4923 term = *cur_end;
4924 *cur_end = '\0';
4925
4926 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4927 switch (exp->action) {
4928 case ACT_SETBE:
4929 /* It is not possible to jump a second time.
4930 * FIXME: should we return an HTTP/500 here so that
4931 * the admin knows there's a problem ?
4932 */
4933 if (t->be != t->fe)
4934 break;
4935
4936 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004937 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004938 last_hdr = 1;
4939 break;
4940
4941 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004942 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004943 last_hdr = 1;
4944 break;
4945
4946 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004947 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004948 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004949
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004950 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004951 if (t->listener->counters)
4952 t->listener->counters->denied_resp++;
4953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954 break;
4955
4956 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004957 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004958 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004959
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004960 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004961 if (t->listener->counters)
4962 t->listener->counters->denied_resp++;
4963
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004964 break;
4965
4966 case ACT_REPLACE:
4967 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4968 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4969 /* FIXME: if the user adds a newline in the replacement, the
4970 * index will not be recalculated for now, and the new line
4971 * will not be counted as a new header.
4972 */
4973
4974 cur_end += delta;
4975 cur_next += delta;
4976 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004977 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004978 break;
4979
4980 case ACT_REMOVE:
4981 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4982 cur_next += delta;
4983
Willy Tarreaufa355d42009-11-29 18:12:29 +01004984 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004985 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4986 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004987 cur_hdr->len = 0;
4988 cur_end = NULL; /* null-term has been rewritten */
4989 break;
4990
4991 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004992 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004993 if (cur_end)
4994 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004995
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004996 /* keep the link from this header to next one in case of later
4997 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004998 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004999 old_idx = cur_idx;
5000 }
5001 return 0;
5002}
5003
5004
5005/* Apply the filter to the request line.
5006 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5007 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005008 * Since it can manage the switch to another backend, it updates the per-proxy
5009 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005010 */
5011int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5012{
5013 char term;
5014 char *cur_ptr, *cur_end;
5015 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005016 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005017 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005018
Willy Tarreau58f10d72006-12-04 02:26:12 +01005019
Willy Tarreau3d300592007-03-18 18:34:41 +01005020 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005021 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005022 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005023 (exp->action == ACT_ALLOW ||
5024 exp->action == ACT_DENY ||
5025 exp->action == ACT_TARPIT))
5026 return 0;
5027 else if (exp->action == ACT_REMOVE)
5028 return 0;
5029
5030 done = 0;
5031
Willy Tarreau962c3f42010-01-10 00:15:35 +01005032 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005033 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005034
5035 /* Now we have the request line between cur_ptr and cur_end */
5036
5037 /* The annoying part is that pattern matching needs
5038 * that we modify the contents to null-terminate all
5039 * strings before testing them.
5040 */
5041
5042 term = *cur_end;
5043 *cur_end = '\0';
5044
5045 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5046 switch (exp->action) {
5047 case ACT_SETBE:
5048 /* It is not possible to jump a second time.
5049 * FIXME: should we return an HTTP/500 here so that
5050 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005051 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005052 if (t->be != t->fe)
5053 break;
5054
5055 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005056 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005057 done = 1;
5058 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005059
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005060 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005061 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005062 done = 1;
5063 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005065 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005066 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005067
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005068 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005069 if (t->listener->counters)
5070 t->listener->counters->denied_resp++;
5071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005072 done = 1;
5073 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005075 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005076 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005077
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005078 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005079 if (t->listener->counters)
5080 t->listener->counters->denied_resp++;
5081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005082 done = 1;
5083 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005084
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005085 case ACT_REPLACE:
5086 *cur_end = term; /* restore the string terminator */
5087 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5088 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5089 /* FIXME: if the user adds a newline in the replacement, the
5090 * index will not be recalculated for now, and the new line
5091 * will not be counted as a new header.
5092 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005093
Willy Tarreaufa355d42009-11-29 18:12:29 +01005094 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005095 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005096 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005097 HTTP_MSG_RQMETH,
5098 cur_ptr, cur_end + 1,
5099 NULL, NULL);
5100 if (unlikely(!cur_end))
5101 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005103 /* we have a full request and we know that we have either a CR
5104 * or an LF at <ptr>.
5105 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005106 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5107 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005108 /* there is no point trying this regex on headers */
5109 return 1;
5110 }
5111 }
5112 *cur_end = term; /* restore the string terminator */
5113 return done;
5114}
Willy Tarreau97de6242006-12-27 17:18:38 +01005115
Willy Tarreau58f10d72006-12-04 02:26:12 +01005116
Willy Tarreau58f10d72006-12-04 02:26:12 +01005117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005118/*
5119 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5120 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005121 * unparsable request. Since it can manage the switch to another backend, it
5122 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005123 */
5124int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5125{
Willy Tarreau3d300592007-03-18 18:34:41 +01005126 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005128 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005129 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005130
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005131 /*
5132 * The interleaving of transformations and verdicts
5133 * makes it difficult to decide to continue or stop
5134 * the evaluation.
5135 */
5136
Willy Tarreau3d300592007-03-18 18:34:41 +01005137 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005138 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5139 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5140 exp = exp->next;
5141 continue;
5142 }
5143
5144 /* Apply the filter to the request line. */
5145 ret = apply_filter_to_req_line(t, req, exp);
5146 if (unlikely(ret < 0))
5147 return -1;
5148
5149 if (likely(ret == 0)) {
5150 /* The filter did not match the request, it can be
5151 * iterated through all headers.
5152 */
5153 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005154 }
5155 exp = exp->next;
5156 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005157 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005158}
5159
5160
Willy Tarreaua15645d2007-03-18 16:22:39 +01005161
Willy Tarreau58f10d72006-12-04 02:26:12 +01005162/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005163 * Try to retrieve the server associated to the appsession.
5164 * If the server is found, it's assigned to the session.
5165 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005166void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005167 struct http_txn *txn = &t->txn;
5168 appsess *asession = NULL;
5169 char *sessid_temp = NULL;
5170
Cyril Bontéb21570a2009-11-29 20:04:48 +01005171 if (len > t->be->appsession_len) {
5172 len = t->be->appsession_len;
5173 }
5174
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005175 if (t->be->options2 & PR_O2_AS_REQL) {
5176 /* request-learn option is enabled : store the sessid in the session for future use */
5177 if (t->sessid != NULL) {
5178 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5179 pool_free2(apools.sessid, t->sessid);
5180 }
5181
5182 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5183 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5184 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5185 return;
5186 }
5187
Cyril Bontéb21570a2009-11-29 20:04:48 +01005188 memcpy(t->sessid, buf, len);
5189 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005190 }
5191
5192 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5193 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5194 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5195 return;
5196 }
5197
Cyril Bontéb21570a2009-11-29 20:04:48 +01005198 memcpy(sessid_temp, buf, len);
5199 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005200
5201 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5202 /* free previously allocated memory */
5203 pool_free2(apools.sessid, sessid_temp);
5204
5205 if (asession != NULL) {
5206 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5207 if (!(t->be->options2 & PR_O2_AS_REQL))
5208 asession->request_count++;
5209
5210 if (asession->serverid != NULL) {
5211 struct server *srv = t->be->srv;
5212 while (srv) {
5213 if (strcmp(srv->id, asession->serverid) == 0) {
5214 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5215 /* we found the server and it's usable */
5216 txn->flags &= ~TX_CK_MASK;
5217 txn->flags |= TX_CK_VALID;
5218 t->flags |= SN_DIRECT | SN_ASSIGNED;
5219 t->srv = srv;
5220 break;
5221 } else {
5222 txn->flags &= ~TX_CK_MASK;
5223 txn->flags |= TX_CK_DOWN;
5224 }
5225 }
5226 srv = srv->next;
5227 }
5228 }
5229 }
5230}
5231
5232/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005233 * Manage client-side cookie. It can impact performance by about 2% so it is
5234 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005235 */
5236void manage_client_side_cookies(struct session *t, struct buffer *req)
5237{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005238 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005239 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005240 char *del_colon, *del_cookie, *colon;
5241 int app_cookies;
5242
Willy Tarreau58f10d72006-12-04 02:26:12 +01005243 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005244 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005245
Willy Tarreau2a324282006-12-05 00:05:46 +01005246 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005247 * we start with the start line.
5248 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005249 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005250 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005251
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005252 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005253 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005254 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005255
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005256 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005257 cur_ptr = cur_next;
5258 cur_end = cur_ptr + cur_hdr->len;
5259 cur_next = cur_end + cur_hdr->cr + 1;
5260
5261 /* We have one full header between cur_ptr and cur_end, and the
5262 * next header starts at cur_next. We're only interested in
5263 * "Cookie:" headers.
5264 */
5265
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005266 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5267 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005268 old_idx = cur_idx;
5269 continue;
5270 }
5271
5272 /* Now look for cookies. Conforming to RFC2109, we have to support
5273 * attributes whose name begin with a '$', and associate them with
5274 * the right cookie, if we want to delete this cookie.
5275 * So there are 3 cases for each cookie read :
5276 * 1) it's a special attribute, beginning with a '$' : ignore it.
5277 * 2) it's a server id cookie that we *MAY* want to delete : save
5278 * some pointers on it (last semi-colon, beginning of cookie...)
5279 * 3) it's an application cookie : we *MAY* have to delete a previous
5280 * "special" cookie.
5281 * At the end of loop, if a "special" cookie remains, we may have to
5282 * remove it. If no application cookie persists in the header, we
5283 * *MUST* delete it
5284 */
5285
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005286 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005287
Willy Tarreau58f10d72006-12-04 02:26:12 +01005288 /* del_cookie == NULL => nothing to be deleted */
5289 del_colon = del_cookie = NULL;
5290 app_cookies = 0;
5291
5292 while (p1 < cur_end) {
5293 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005294 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005295 while (p1 < cur_end) {
5296 if (*p1 == ';' || *p1 == ',')
5297 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005298 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005299 break;
5300 p1++;
5301 }
5302
5303 if (p1 == cur_end)
5304 break;
5305
5306 /* p1 is at the beginning of the cookie name */
5307 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005308 while (p2 < cur_end && *p2 != '=') {
5309 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5310 /* oops, the cookie name was truncated, resync */
5311 p1 = p2;
5312 goto resync_name;
5313 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005314 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005315 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005316
5317 if (p2 == cur_end)
5318 break;
5319
5320 p3 = p2 + 1; /* skips the '=' sign */
5321 if (p3 == cur_end)
5322 break;
5323
Willy Tarreau305ae852010-01-03 19:45:54 +01005324 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5325 p5 = p4 = p3;
5326 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5327 if (!isspace((unsigned char)*p5))
5328 p4 = p5 + 1;
5329 p5++;
5330 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005331
5332 /* here, we have the cookie name between p1 and p2,
5333 * and its value between p3 and p4.
5334 * we can process it :
5335 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005336 * Cookie: NAME=VALUE ;
5337 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005338 * | || || +--> p4
5339 * | || |+-------> p3
5340 * | || +--------> p2
5341 * | |+------------> p1
5342 * | +-------------> colon
5343 * +--------------------> cur_ptr
5344 */
5345
5346 if (*p1 == '$') {
5347 /* skip this one */
5348 }
5349 else {
5350 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005351 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005352 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005353 (p4 - p1 >= t->fe->capture_namelen) &&
5354 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005355 int log_len = p4 - p1;
5356
Willy Tarreau086b3b42007-05-13 21:45:51 +02005357 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005358 Alert("HTTP logging : out of memory.\n");
5359 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005360 if (log_len > t->fe->capture_len)
5361 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005362 memcpy(txn->cli_cookie, p1, log_len);
5363 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005364 }
5365 }
5366
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005367 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5368 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005369 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005370 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005371 char *delim;
5372
5373 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5374 * have the server ID betweek p3 and delim, and the original cookie between
5375 * delim+1 and p4. Otherwise, delim==p4 :
5376 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005377 * Cookie: NAME=SRV~VALUE ;
5378 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005379 * | || || | +--> p4
5380 * | || || +--------> delim
5381 * | || |+-----------> p3
5382 * | || +------------> p2
5383 * | |+----------------> p1
5384 * | +-----------------> colon
5385 * +------------------------> cur_ptr
5386 */
5387
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005388 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005389 for (delim = p3; delim < p4; delim++)
5390 if (*delim == COOKIE_DELIM)
5391 break;
5392 }
5393 else
5394 delim = p4;
5395
5396
5397 /* Here, we'll look for the first running server which supports the cookie.
5398 * This allows to share a same cookie between several servers, for example
5399 * to dedicate backup servers to specific servers only.
5400 * However, to prevent clients from sticking to cookie-less backup server
5401 * when they have incidentely learned an empty cookie, we simply ignore
5402 * empty cookies and mark them as invalid.
5403 */
5404 if (delim == p3)
5405 srv = NULL;
5406
5407 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005408 if (srv->cookie && (srv->cklen == delim - p3) &&
5409 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005410 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005411 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005412 txn->flags &= ~TX_CK_MASK;
5413 txn->flags |= TX_CK_VALID;
5414 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005415 t->srv = srv;
5416 break;
5417 } else {
5418 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005419 txn->flags &= ~TX_CK_MASK;
5420 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005421 }
5422 }
5423 srv = srv->next;
5424 }
5425
Willy Tarreau3d300592007-03-18 18:34:41 +01005426 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005427 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005428 txn->flags &= ~TX_CK_MASK;
5429 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005430 }
5431
5432 /* depending on the cookie mode, we may have to either :
5433 * - delete the complete cookie if we're in insert+indirect mode, so that
5434 * the server never sees it ;
5435 * - remove the server id from the cookie value, and tag the cookie as an
5436 * application cookie so that it does not get accidentely removed later,
5437 * if we're in cookie prefix mode
5438 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005439 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005440 int delta; /* negative */
5441
5442 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5443 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005444 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005445 cur_end += delta;
5446 cur_next += delta;
5447 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005448 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005449
5450 del_cookie = del_colon = NULL;
5451 app_cookies++; /* protect the header from deletion */
5452 }
5453 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005454 (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 +01005455 del_cookie = p1;
5456 del_colon = colon;
5457 }
5458 } else {
5459 /* now we know that we must keep this cookie since it's
5460 * not ours. But if we wanted to delete our cookie
5461 * earlier, we cannot remove the complete header, but we
5462 * can remove the previous block itself.
5463 */
5464 app_cookies++;
5465
5466 if (del_cookie != NULL) {
5467 int delta; /* negative */
5468
5469 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5470 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005471 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005472 cur_end += delta;
5473 cur_next += delta;
5474 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005475 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005476 del_cookie = del_colon = NULL;
5477 }
5478 }
5479
Cyril Bontéb21570a2009-11-29 20:04:48 +01005480 if (t->be->appsession_name != NULL) {
5481 int cmp_len, value_len;
5482 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005483
Cyril Bontéb21570a2009-11-29 20:04:48 +01005484 if (t->be->options2 & PR_O2_AS_PFX) {
5485 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5486 value_begin = p1 + t->be->appsession_name_len;
5487 value_len = p4 - p1 - t->be->appsession_name_len;
5488 } else {
5489 cmp_len = p2 - p1;
5490 value_begin = p3;
5491 value_len = p4 - p3;
5492 }
5493
5494 /* let's see if the cookie is our appcookie */
5495 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5496 /* Cool... it's the right one */
5497 manage_client_side_appsession(t, value_begin, value_len);
5498 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005499#if defined(DEBUG_HASH)
5500 Alert("manage_client_side_cookies\n");
5501 appsession_hash_dump(&(t->be->htbl_proxy));
5502#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005503 }/* end if ((t->proxy->appsession_name != NULL) ... */
5504 }
5505
5506 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005507 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005508 } /* while (p1 < cur_end) */
5509
5510 /* There's no more cookie on this line.
5511 * We may have marked the last one(s) for deletion.
5512 * We must do this now in two ways :
5513 * - if there is no app cookie, we simply delete the header ;
5514 * - if there are app cookies, we must delete the end of the
5515 * string properly, including the colon/semi-colon before
5516 * the cookie name.
5517 */
5518 if (del_cookie != NULL) {
5519 int delta;
5520 if (app_cookies) {
5521 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5522 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005523 cur_hdr->len += delta;
5524 } else {
5525 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005526
5527 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005528 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5529 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005530 cur_hdr->len = 0;
5531 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005532 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005533 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005534 }
5535
5536 /* keep the link from this header to next one */
5537 old_idx = cur_idx;
5538 } /* end of cookie processing on this header */
5539}
5540
5541
Willy Tarreaua15645d2007-03-18 16:22:39 +01005542/* Iterate the same filter through all response headers contained in <rtr>.
5543 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5544 */
5545int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5546{
5547 char term;
5548 char *cur_ptr, *cur_end, *cur_next;
5549 int cur_idx, old_idx, last_hdr;
5550 struct http_txn *txn = &t->txn;
5551 struct hdr_idx_elem *cur_hdr;
5552 int len, delta;
5553
5554 last_hdr = 0;
5555
Willy Tarreau962c3f42010-01-10 00:15:35 +01005556 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005557 old_idx = 0;
5558
5559 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005560 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005561 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005562 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005563 (exp->action == ACT_ALLOW ||
5564 exp->action == ACT_DENY))
5565 return 0;
5566
5567 cur_idx = txn->hdr_idx.v[old_idx].next;
5568 if (!cur_idx)
5569 break;
5570
5571 cur_hdr = &txn->hdr_idx.v[cur_idx];
5572 cur_ptr = cur_next;
5573 cur_end = cur_ptr + cur_hdr->len;
5574 cur_next = cur_end + cur_hdr->cr + 1;
5575
5576 /* Now we have one header between cur_ptr and cur_end,
5577 * and the next header starts at cur_next.
5578 */
5579
5580 /* The annoying part is that pattern matching needs
5581 * that we modify the contents to null-terminate all
5582 * strings before testing them.
5583 */
5584
5585 term = *cur_end;
5586 *cur_end = '\0';
5587
5588 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5589 switch (exp->action) {
5590 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005591 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005592 last_hdr = 1;
5593 break;
5594
5595 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005596 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005597 last_hdr = 1;
5598 break;
5599
5600 case ACT_REPLACE:
5601 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5602 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5603 /* FIXME: if the user adds a newline in the replacement, the
5604 * index will not be recalculated for now, and the new line
5605 * will not be counted as a new header.
5606 */
5607
5608 cur_end += delta;
5609 cur_next += delta;
5610 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005611 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005612 break;
5613
5614 case ACT_REMOVE:
5615 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5616 cur_next += delta;
5617
Willy Tarreaufa355d42009-11-29 18:12:29 +01005618 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005619 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5620 txn->hdr_idx.used--;
5621 cur_hdr->len = 0;
5622 cur_end = NULL; /* null-term has been rewritten */
5623 break;
5624
5625 }
5626 }
5627 if (cur_end)
5628 *cur_end = term; /* restore the string terminator */
5629
5630 /* keep the link from this header to next one in case of later
5631 * removal of next header.
5632 */
5633 old_idx = cur_idx;
5634 }
5635 return 0;
5636}
5637
5638
5639/* Apply the filter to the status line in the response buffer <rtr>.
5640 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5641 * or -1 if a replacement resulted in an invalid status line.
5642 */
5643int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5644{
5645 char term;
5646 char *cur_ptr, *cur_end;
5647 int done;
5648 struct http_txn *txn = &t->txn;
5649 int len, delta;
5650
5651
Willy Tarreau3d300592007-03-18 18:34:41 +01005652 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005653 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005654 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005655 (exp->action == ACT_ALLOW ||
5656 exp->action == ACT_DENY))
5657 return 0;
5658 else if (exp->action == ACT_REMOVE)
5659 return 0;
5660
5661 done = 0;
5662
Willy Tarreau962c3f42010-01-10 00:15:35 +01005663 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005664 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5665
5666 /* Now we have the status line between cur_ptr and cur_end */
5667
5668 /* The annoying part is that pattern matching needs
5669 * that we modify the contents to null-terminate all
5670 * strings before testing them.
5671 */
5672
5673 term = *cur_end;
5674 *cur_end = '\0';
5675
5676 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5677 switch (exp->action) {
5678 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005679 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005680 done = 1;
5681 break;
5682
5683 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005684 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005685 done = 1;
5686 break;
5687
5688 case ACT_REPLACE:
5689 *cur_end = term; /* restore the string terminator */
5690 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5691 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5692 /* FIXME: if the user adds a newline in the replacement, the
5693 * index will not be recalculated for now, and the new line
5694 * will not be counted as a new header.
5695 */
5696
Willy Tarreaufa355d42009-11-29 18:12:29 +01005697 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005698 cur_end += delta;
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 Tarreau962c3f42010-01-10 00:15:35 +01005709 txn->status = strl2ui(txn->rsp.sol + 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;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005780 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005781
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) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01005949 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005950 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5951 Alert("Not enough Memory process_srv():asession:calloc().\n");
5952 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5953 return;
5954 }
5955 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5956 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5957 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005958 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005959 return;
5960 }
5961 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5962 asession->sessid[t->be->appsession_len] = 0;
5963
Willy Tarreau1fac7532010-01-09 19:23:06 +01005964 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005965 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5966 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5967 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005968 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005969 return;
5970 }
5971 asession->serverid[0] = '\0';
5972 memcpy(asession->serverid, t->srv->id, server_id_len);
5973
5974 asession->request_count = 0;
5975 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5976 }
5977
5978 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5979 asession->request_count++;
5980 }
5981
5982#if defined(DEBUG_HASH)
5983 Alert("manage_server_side_cookies\n");
5984 appsession_hash_dump(&(t->be->htbl_proxy));
5985#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005986}
5987
5988
5989
5990/*
5991 * Check if response is cacheable or not. Updates t->flags.
5992 */
5993void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5994{
5995 struct http_txn *txn = &t->txn;
5996 char *p1, *p2;
5997
5998 char *cur_ptr, *cur_end, *cur_next;
5999 int cur_idx;
6000
Willy Tarreau5df51872007-11-25 16:20:08 +01006001 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006002 return;
6003
6004 /* Iterate through the headers.
6005 * we start with the start line.
6006 */
6007 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006008 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006009
6010 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6011 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006012 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006013
6014 cur_hdr = &txn->hdr_idx.v[cur_idx];
6015 cur_ptr = cur_next;
6016 cur_end = cur_ptr + cur_hdr->len;
6017 cur_next = cur_end + cur_hdr->cr + 1;
6018
6019 /* We have one full header between cur_ptr and cur_end, and the
6020 * next header starts at cur_next. We're only interested in
6021 * "Cookie:" headers.
6022 */
6023
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006024 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6025 if (val) {
6026 if ((cur_end - (cur_ptr + val) >= 8) &&
6027 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6028 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6029 return;
6030 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006031 }
6032
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006033 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6034 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006035 continue;
6036
6037 /* OK, right now we know we have a cache-control header at cur_ptr */
6038
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006039 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006040
6041 if (p1 >= cur_end) /* no more info */
6042 continue;
6043
6044 /* p1 is at the beginning of the value */
6045 p2 = p1;
6046
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006047 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006048 p2++;
6049
6050 /* we have a complete value between p1 and p2 */
6051 if (p2 < cur_end && *p2 == '=') {
6052 /* we have something of the form no-cache="set-cookie" */
6053 if ((cur_end - p1 >= 21) &&
6054 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6055 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006056 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006057 continue;
6058 }
6059
6060 /* OK, so we know that either p2 points to the end of string or to a comma */
6061 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6062 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6063 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6064 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006065 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006066 return;
6067 }
6068
6069 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006070 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006071 continue;
6072 }
6073 }
6074}
6075
6076
Willy Tarreau58f10d72006-12-04 02:26:12 +01006077/*
6078 * Try to retrieve a known appsession in the URI, then the associated server.
6079 * If the server is found, it's assigned to the session.
6080 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006081void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006083 char *end_params, *first_param, *cur_param, *next_param;
6084 char separator;
6085 int value_len;
6086
6087 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006089 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006090 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006091 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006092 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006093
Cyril Bontéb21570a2009-11-29 20:04:48 +01006094 first_param = NULL;
6095 switch (mode) {
6096 case PR_O2_AS_M_PP:
6097 first_param = memchr(begin, ';', len);
6098 break;
6099 case PR_O2_AS_M_QS:
6100 first_param = memchr(begin, '?', len);
6101 break;
6102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103
Cyril Bontéb21570a2009-11-29 20:04:48 +01006104 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006105 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006106 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006107
Cyril Bontéb21570a2009-11-29 20:04:48 +01006108 switch (mode) {
6109 case PR_O2_AS_M_PP:
6110 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6111 end_params = (char *) begin + len;
6112 }
6113 separator = ';';
6114 break;
6115 case PR_O2_AS_M_QS:
6116 end_params = (char *) begin + len;
6117 separator = '&';
6118 break;
6119 default:
6120 /* unknown mode, shouldn't happen */
6121 return;
6122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006123
Cyril Bontéb21570a2009-11-29 20:04:48 +01006124 cur_param = next_param = end_params;
6125 while (cur_param > first_param) {
6126 cur_param--;
6127 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6128 /* let's see if this is the appsession parameter */
6129 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6130 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6131 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6132 /* Cool... it's the right one */
6133 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6134 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6135 if (value_len > 0) {
6136 manage_client_side_appsession(t, cur_param, value_len);
6137 }
6138 break;
6139 }
6140 next_param = cur_param;
6141 }
6142 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006143#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006144 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006145 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006146#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006147}
6148
6149
Willy Tarreaub2513902006-12-17 14:52:38 +01006150/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006151 * In a GET or HEAD request, check if the requested URI matches the stats uri
6152 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006153 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006154 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006155 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006156 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006157 *
6158 * Returns 1 if the session's state changes, otherwise 0.
6159 */
6160int stats_check_uri_auth(struct session *t, struct proxy *backend)
6161{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006162 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006163 struct uri_auth *uri_auth = backend->uri_auth;
6164 struct user_auth *user;
6165 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006166 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006167
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006168 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006170 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006171 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006172 return 0;
6173
Willy Tarreau962c3f42010-01-10 00:15:35 +01006174 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006175
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006176 /* the URI is in h */
6177 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006178 return 0;
6179
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006180 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006181 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006182 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006183 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006184 break;
6185 }
6186 h++;
6187 }
6188
6189 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006190 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6191 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006192 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006193 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006194 break;
6195 }
6196 h++;
6197 }
6198 }
6199
Willy Tarreau962c3f42010-01-10 00:15:35 +01006200 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6201 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006202 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006203 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006204 break;
6205 }
6206 h++;
6207 }
6208
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006209 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6210
Willy Tarreaub2513902006-12-17 14:52:38 +01006211 /* we are in front of a interceptable URI. Let's check
6212 * if there's an authentication and if it's valid.
6213 */
6214 user = uri_auth->users;
6215 if (!user) {
6216 /* no user auth required, it's OK */
6217 authenticated = 1;
6218 } else {
6219 authenticated = 0;
6220
6221 /* a user list is defined, we have to check.
6222 * skip 21 chars for "Authorization: Basic ".
6223 */
6224
6225 /* FIXME: this should move to an earlier place */
6226 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006227 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006228 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6229 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006230 if (len > 14 &&
6231 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006232 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006233 break;
6234 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006235 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006236 }
6237
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006238 if (txn->auth_hdr.len < 21 ||
6239 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006240 user = NULL;
6241
6242 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006243 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6244 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006245 user->user_pwd, user->user_len)) {
6246 authenticated = 1;
6247 break;
6248 }
6249 user = user->next;
6250 }
6251 }
6252
6253 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006254 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006255
6256 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006257 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6258 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006259 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006260 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006261 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006262 if (!(t->flags & SN_ERR_MASK))
6263 t->flags |= SN_ERR_PRXCOND;
6264 if (!(t->flags & SN_FINST_MASK))
6265 t->flags |= SN_FINST_R;
6266 return 1;
6267 }
6268
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006269 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006270 * data.
6271 */
Willy Tarreau70089872008-06-13 21:12:51 +02006272 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006273 t->data_source = DATA_SRC_STATS;
6274 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006275 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006276 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6277 t->rep->prod->private = t;
6278 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006279 return 1;
6280}
6281
Willy Tarreau4076a152009-04-02 15:18:36 +02006282/*
6283 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006284 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6285 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006286 */
6287void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6288 struct buffer *buf, struct http_msg *msg,
6289 struct proxy *other_end)
6290{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006291 es->len = buf->r - (buf->data + msg->som);
6292 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006293 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006294 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006295 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006296 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006297 es->when = date; // user-visible date
6298 es->sid = s->uniq_id;
6299 es->srv = s->srv;
6300 es->oe = other_end;
6301 es->src = s->cli_addr;
6302}
Willy Tarreaub2513902006-12-17 14:52:38 +01006303
Willy Tarreaubaaee002006-06-26 02:48:02 +02006304/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305 * Print a debug line with a header
6306 */
6307void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6308{
6309 int len, max;
6310 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006311 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006312 max = end - start;
6313 UBOUND(max, sizeof(trash) - len - 1);
6314 len += strlcpy2(trash + len, start, max + 1);
6315 trash[len++] = '\n';
6316 write(1, trash, len);
6317}
6318
Willy Tarreau0937bc42009-12-22 15:03:09 +01006319/*
6320 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6321 * the required fields are properly allocated and that we only need to (re)init
6322 * them. This should be used before processing any new request.
6323 */
6324void http_init_txn(struct session *s)
6325{
6326 struct http_txn *txn = &s->txn;
6327 struct proxy *fe = s->fe;
6328
6329 txn->flags = 0;
6330 txn->status = -1;
6331
6332 txn->req.sol = txn->req.eol = NULL;
6333 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6334 txn->rsp.sol = txn->rsp.eol = NULL;
6335 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6336 txn->req.hdr_content_len = 0LL;
6337 txn->rsp.hdr_content_len = 0LL;
6338 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6339 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6340 chunk_reset(&txn->auth_hdr);
6341
6342 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6343 if (fe->options2 & PR_O2_REQBUG_OK)
6344 txn->req.err_pos = -1; /* let buggy requests pass */
6345
Willy Tarreau46023632010-01-07 22:51:47 +01006346 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006347 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6348
Willy Tarreau46023632010-01-07 22:51:47 +01006349 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006350 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6351
6352 if (txn->hdr_idx.v)
6353 hdr_idx_init(&txn->hdr_idx);
6354}
6355
6356/* to be used at the end of a transaction */
6357void http_end_txn(struct session *s)
6358{
6359 struct http_txn *txn = &s->txn;
6360
6361 /* these ones will have been dynamically allocated */
6362 pool_free2(pool2_requri, txn->uri);
6363 pool_free2(pool2_capture, txn->cli_cookie);
6364 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau762a2362010-01-09 13:57:26 +01006365 pool_free2(apools.sessid, s->sessid);
6366 s->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006367 txn->uri = NULL;
6368 txn->srv_cookie = NULL;
6369 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006370
6371 if (txn->req.cap) {
6372 struct cap_hdr *h;
6373 for (h = s->fe->req_cap; h; h = h->next)
6374 pool_free2(h->pool, txn->req.cap[h->index]);
6375 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6376 }
6377
6378 if (txn->rsp.cap) {
6379 struct cap_hdr *h;
6380 for (h = s->fe->rsp_cap; h; h = h->next)
6381 pool_free2(h->pool, txn->rsp.cap[h->index]);
6382 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6383 }
6384
Willy Tarreau0937bc42009-12-22 15:03:09 +01006385}
6386
6387/* to be used at the end of a transaction to prepare a new one */
6388void http_reset_txn(struct session *s)
6389{
6390 http_end_txn(s);
6391 http_init_txn(s);
6392
6393 s->be = s->fe;
6394 s->req->analysers = s->listener->analysers;
6395 s->logs.logwait = s->fe->to_log;
6396 s->srv = s->prev_srv = s->srv_conn = NULL;
6397 s->pend_pos = NULL;
6398 s->conn_retries = s->be->conn_retries;
6399
6400 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6401
6402 s->req->rto = s->fe->timeout.client;
6403 s->req->wto = s->be->timeout.server;
6404 s->req->cto = s->be->timeout.connect;
6405
6406 s->rep->rto = s->be->timeout.server;
6407 s->rep->wto = s->fe->timeout.client;
6408 s->rep->cto = TICK_ETERNITY;
6409
6410 s->req->rex = TICK_ETERNITY;
6411 s->req->wex = TICK_ETERNITY;
6412 s->req->analyse_exp = TICK_ETERNITY;
6413 s->rep->rex = TICK_ETERNITY;
6414 s->rep->wex = TICK_ETERNITY;
6415 s->rep->analyse_exp = TICK_ETERNITY;
6416}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006417
Willy Tarreau8797c062007-05-07 00:55:35 +02006418/************************************************************************/
6419/* The code below is dedicated to ACL parsing and matching */
6420/************************************************************************/
6421
6422
6423
6424
6425/* 1. Check on METHOD
6426 * We use the pre-parsed method if it is known, and store its number as an
6427 * integer. If it is unknown, we use the pointer and the length.
6428 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006429static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006430{
6431 int len, meth;
6432
Willy Tarreauae8b7962007-06-09 23:10:04 +02006433 len = strlen(*text);
6434 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006435
6436 pattern->val.i = meth;
6437 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006438 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006439 if (!pattern->ptr.str)
6440 return 0;
6441 pattern->len = len;
6442 }
6443 return 1;
6444}
6445
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006446static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006447acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6448 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006449{
6450 int meth;
6451 struct http_txn *txn = l7;
6452
Willy Tarreaub6866442008-07-14 23:54:42 +02006453 if (!txn)
6454 return 0;
6455
Willy Tarreau655dce92009-11-08 13:10:58 +01006456 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006457 return 0;
6458
Willy Tarreau8797c062007-05-07 00:55:35 +02006459 meth = txn->meth;
6460 test->i = meth;
6461 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006462 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6463 /* ensure the indexes are not affected */
6464 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006465 test->len = txn->req.sl.rq.m_l;
6466 test->ptr = txn->req.sol;
6467 }
6468 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6469 return 1;
6470}
6471
6472static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6473{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006474 int icase;
6475
Willy Tarreau8797c062007-05-07 00:55:35 +02006476 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006477 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006478
6479 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006480 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006481
6482 /* Other method, we must compare the strings */
6483 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006484 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006485
6486 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6487 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6488 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006489 return ACL_PAT_FAIL;
6490 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006491}
6492
6493/* 2. Check on Request/Status Version
6494 * We simply compare strings here.
6495 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006496static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006497{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006498 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006499 if (!pattern->ptr.str)
6500 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006501 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006502 return 1;
6503}
6504
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006505static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006506acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6507 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006508{
6509 struct http_txn *txn = l7;
6510 char *ptr;
6511 int len;
6512
Willy Tarreaub6866442008-07-14 23:54:42 +02006513 if (!txn)
6514 return 0;
6515
Willy Tarreau655dce92009-11-08 13:10:58 +01006516 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006517 return 0;
6518
Willy Tarreau8797c062007-05-07 00:55:35 +02006519 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006520 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006521
6522 while ((len-- > 0) && (*ptr++ != '/'));
6523 if (len <= 0)
6524 return 0;
6525
6526 test->ptr = ptr;
6527 test->len = len;
6528
6529 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6530 return 1;
6531}
6532
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006533static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006534acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6535 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006536{
6537 struct http_txn *txn = l7;
6538 char *ptr;
6539 int len;
6540
Willy Tarreaub6866442008-07-14 23:54:42 +02006541 if (!txn)
6542 return 0;
6543
Willy Tarreau655dce92009-11-08 13:10:58 +01006544 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006545 return 0;
6546
Willy Tarreau8797c062007-05-07 00:55:35 +02006547 len = txn->rsp.sl.st.v_l;
6548 ptr = txn->rsp.sol;
6549
6550 while ((len-- > 0) && (*ptr++ != '/'));
6551 if (len <= 0)
6552 return 0;
6553
6554 test->ptr = ptr;
6555 test->len = len;
6556
6557 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6558 return 1;
6559}
6560
6561/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006562static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006563acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6564 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006565{
6566 struct http_txn *txn = l7;
6567 char *ptr;
6568 int len;
6569
Willy Tarreaub6866442008-07-14 23:54:42 +02006570 if (!txn)
6571 return 0;
6572
Willy Tarreau655dce92009-11-08 13:10:58 +01006573 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006574 return 0;
6575
Willy Tarreau8797c062007-05-07 00:55:35 +02006576 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006577 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006578
6579 test->i = __strl2ui(ptr, len);
6580 test->flags = ACL_TEST_F_VOL_1ST;
6581 return 1;
6582}
6583
6584/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006585static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006586acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6587 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006588{
6589 struct http_txn *txn = l7;
6590
Willy Tarreaub6866442008-07-14 23:54:42 +02006591 if (!txn)
6592 return 0;
6593
Willy Tarreau655dce92009-11-08 13:10:58 +01006594 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006595 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006596
Willy Tarreauc11416f2007-06-17 16:58:38 +02006597 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6598 /* ensure the indexes are not affected */
6599 return 0;
6600
Willy Tarreau8797c062007-05-07 00:55:35 +02006601 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006602 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006603
Willy Tarreauf3d25982007-05-08 22:45:09 +02006604 /* we do not need to set READ_ONLY because the data is in a buffer */
6605 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006606 return 1;
6607}
6608
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006609static int
6610acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6611 struct acl_expr *expr, struct acl_test *test)
6612{
6613 struct http_txn *txn = l7;
6614
Willy Tarreaub6866442008-07-14 23:54:42 +02006615 if (!txn)
6616 return 0;
6617
Willy Tarreau655dce92009-11-08 13:10:58 +01006618 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006619 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006620
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006621 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6622 /* ensure the indexes are not affected */
6623 return 0;
6624
6625 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006626 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006627 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6628 test->i = AF_INET;
6629
6630 /*
6631 * If we are parsing url in frontend space, we prepare backend stage
6632 * to not parse again the same url ! optimization lazyness...
6633 */
6634 if (px->options & PR_O_HTTP_PROXY)
6635 l4->flags |= SN_ADDR_SET;
6636
6637 test->flags = ACL_TEST_F_READ_ONLY;
6638 return 1;
6639}
6640
6641static int
6642acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6643 struct acl_expr *expr, struct acl_test *test)
6644{
6645 struct http_txn *txn = l7;
6646
Willy Tarreaub6866442008-07-14 23:54:42 +02006647 if (!txn)
6648 return 0;
6649
Willy Tarreau655dce92009-11-08 13:10:58 +01006650 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006651 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006652
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006653 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6654 /* ensure the indexes are not affected */
6655 return 0;
6656
6657 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006658 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006659 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6660
6661 if (px->options & PR_O_HTTP_PROXY)
6662 l4->flags |= SN_ADDR_SET;
6663
6664 test->flags = ACL_TEST_F_READ_ONLY;
6665 return 1;
6666}
6667
Willy Tarreauc11416f2007-06-17 16:58:38 +02006668/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6669 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6670 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006671static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006672acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006673 struct acl_expr *expr, struct acl_test *test)
6674{
6675 struct http_txn *txn = l7;
6676 struct hdr_idx *idx = &txn->hdr_idx;
6677 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006678
Willy Tarreaub6866442008-07-14 23:54:42 +02006679 if (!txn)
6680 return 0;
6681
Willy Tarreau33a7e692007-06-10 19:45:56 +02006682 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6683 /* search for header from the beginning */
6684 ctx->idx = 0;
6685
Willy Tarreau33a7e692007-06-10 19:45:56 +02006686 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6687 test->flags |= ACL_TEST_F_FETCH_MORE;
6688 test->flags |= ACL_TEST_F_VOL_HDR;
6689 test->len = ctx->vlen;
6690 test->ptr = (char *)ctx->line + ctx->val;
6691 return 1;
6692 }
6693
6694 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6695 test->flags |= ACL_TEST_F_VOL_HDR;
6696 return 0;
6697}
6698
Willy Tarreau33a7e692007-06-10 19:45:56 +02006699static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006700acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6701 struct acl_expr *expr, struct acl_test *test)
6702{
6703 struct http_txn *txn = l7;
6704
Willy Tarreaub6866442008-07-14 23:54:42 +02006705 if (!txn)
6706 return 0;
6707
Willy Tarreau655dce92009-11-08 13:10:58 +01006708 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006709 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006710
Willy Tarreauc11416f2007-06-17 16:58:38 +02006711 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6712 /* ensure the indexes are not affected */
6713 return 0;
6714
6715 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6716}
6717
6718static int
6719acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6720 struct acl_expr *expr, struct acl_test *test)
6721{
6722 struct http_txn *txn = l7;
6723
Willy Tarreaub6866442008-07-14 23:54:42 +02006724 if (!txn)
6725 return 0;
6726
Willy Tarreau655dce92009-11-08 13:10:58 +01006727 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006728 return 0;
6729
6730 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6731}
6732
6733/* 6. Check on HTTP header count. The number of occurrences is returned.
6734 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6735 */
6736static int
6737acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006738 struct acl_expr *expr, struct acl_test *test)
6739{
6740 struct http_txn *txn = l7;
6741 struct hdr_idx *idx = &txn->hdr_idx;
6742 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006743 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006744
Willy Tarreaub6866442008-07-14 23:54:42 +02006745 if (!txn)
6746 return 0;
6747
Willy Tarreau33a7e692007-06-10 19:45:56 +02006748 ctx.idx = 0;
6749 cnt = 0;
6750 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6751 cnt++;
6752
6753 test->i = cnt;
6754 test->flags = ACL_TEST_F_VOL_HDR;
6755 return 1;
6756}
6757
Willy Tarreauc11416f2007-06-17 16:58:38 +02006758static int
6759acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6760 struct acl_expr *expr, struct acl_test *test)
6761{
6762 struct http_txn *txn = l7;
6763
Willy Tarreaub6866442008-07-14 23:54:42 +02006764 if (!txn)
6765 return 0;
6766
Willy Tarreau655dce92009-11-08 13:10:58 +01006767 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006768 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006769
Willy Tarreauc11416f2007-06-17 16:58:38 +02006770 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6771 /* ensure the indexes are not affected */
6772 return 0;
6773
6774 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6775}
6776
6777static int
6778acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6779 struct acl_expr *expr, struct acl_test *test)
6780{
6781 struct http_txn *txn = l7;
6782
Willy Tarreaub6866442008-07-14 23:54:42 +02006783 if (!txn)
6784 return 0;
6785
Willy Tarreau655dce92009-11-08 13:10:58 +01006786 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006787 return 0;
6788
6789 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6790}
6791
Willy Tarreau33a7e692007-06-10 19:45:56 +02006792/* 7. Check on HTTP header's integer value. The integer value is returned.
6793 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006794 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006795 */
6796static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006797acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006798 struct acl_expr *expr, struct acl_test *test)
6799{
6800 struct http_txn *txn = l7;
6801 struct hdr_idx *idx = &txn->hdr_idx;
6802 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006803
Willy Tarreaub6866442008-07-14 23:54:42 +02006804 if (!txn)
6805 return 0;
6806
Willy Tarreau33a7e692007-06-10 19:45:56 +02006807 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6808 /* search for header from the beginning */
6809 ctx->idx = 0;
6810
Willy Tarreau33a7e692007-06-10 19:45:56 +02006811 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6812 test->flags |= ACL_TEST_F_FETCH_MORE;
6813 test->flags |= ACL_TEST_F_VOL_HDR;
6814 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6815 return 1;
6816 }
6817
6818 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6819 test->flags |= ACL_TEST_F_VOL_HDR;
6820 return 0;
6821}
6822
Willy Tarreauc11416f2007-06-17 16:58:38 +02006823static int
6824acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6825 struct acl_expr *expr, struct acl_test *test)
6826{
6827 struct http_txn *txn = l7;
6828
Willy Tarreaub6866442008-07-14 23:54:42 +02006829 if (!txn)
6830 return 0;
6831
Willy Tarreau655dce92009-11-08 13:10:58 +01006832 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006833 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006834
Willy Tarreauc11416f2007-06-17 16:58:38 +02006835 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6836 /* ensure the indexes are not affected */
6837 return 0;
6838
6839 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6840}
6841
6842static int
6843acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6844 struct acl_expr *expr, struct acl_test *test)
6845{
6846 struct http_txn *txn = l7;
6847
Willy Tarreaub6866442008-07-14 23:54:42 +02006848 if (!txn)
6849 return 0;
6850
Willy Tarreau655dce92009-11-08 13:10:58 +01006851 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006852 return 0;
6853
6854 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6855}
6856
Willy Tarreau106f9792009-09-19 07:54:16 +02006857/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6858 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6859 */
6860static int
6861acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6862 struct acl_expr *expr, struct acl_test *test)
6863{
6864 struct http_txn *txn = l7;
6865 struct hdr_idx *idx = &txn->hdr_idx;
6866 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6867
6868 if (!txn)
6869 return 0;
6870
6871 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6872 /* search for header from the beginning */
6873 ctx->idx = 0;
6874
6875 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6876 test->flags |= ACL_TEST_F_FETCH_MORE;
6877 test->flags |= ACL_TEST_F_VOL_HDR;
6878 /* Same optimization as url_ip */
6879 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6880 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6881 test->ptr = (void *)&l4->srv_addr.sin_addr;
6882 test->i = AF_INET;
6883 return 1;
6884 }
6885
6886 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6887 test->flags |= ACL_TEST_F_VOL_HDR;
6888 return 0;
6889}
6890
6891static int
6892acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6893 struct acl_expr *expr, struct acl_test *test)
6894{
6895 struct http_txn *txn = l7;
6896
6897 if (!txn)
6898 return 0;
6899
Willy Tarreau655dce92009-11-08 13:10:58 +01006900 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006901 return 0;
6902
6903 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6904 /* ensure the indexes are not affected */
6905 return 0;
6906
6907 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6908}
6909
6910static int
6911acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6912 struct acl_expr *expr, struct acl_test *test)
6913{
6914 struct http_txn *txn = l7;
6915
6916 if (!txn)
6917 return 0;
6918
Willy Tarreau655dce92009-11-08 13:10:58 +01006919 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006920 return 0;
6921
6922 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6923}
6924
Willy Tarreau737b0c12007-06-10 21:28:46 +02006925/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6926 * the first '/' after the possible hostname, and ends before the possible '?'.
6927 */
6928static int
6929acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6930 struct acl_expr *expr, struct acl_test *test)
6931{
6932 struct http_txn *txn = l7;
6933 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006934
Willy Tarreaub6866442008-07-14 23:54:42 +02006935 if (!txn)
6936 return 0;
6937
Willy Tarreau655dce92009-11-08 13:10:58 +01006938 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006939 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006940
Willy Tarreauc11416f2007-06-17 16:58:38 +02006941 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6942 /* ensure the indexes are not affected */
6943 return 0;
6944
Willy Tarreau962c3f42010-01-10 00:15:35 +01006945 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006946 ptr = http_get_path(txn);
6947 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006948 return 0;
6949
6950 /* OK, we got the '/' ! */
6951 test->ptr = ptr;
6952
6953 while (ptr < end && *ptr != '?')
6954 ptr++;
6955
6956 test->len = ptr - test->ptr;
6957
6958 /* we do not need to set READ_ONLY because the data is in a buffer */
6959 test->flags = ACL_TEST_F_VOL_1ST;
6960 return 1;
6961}
6962
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006963static int
6964acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6965 struct acl_expr *expr, struct acl_test *test)
6966{
6967 struct buffer *req = s->req;
6968 struct http_txn *txn = &s->txn;
6969 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006970
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006971 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6972 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6973 */
6974
6975 if (!s || !req)
6976 return 0;
6977
Willy Tarreau655dce92009-11-08 13:10:58 +01006978 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006979 /* Already decoded as OK */
6980 test->flags |= ACL_TEST_F_SET_RES_PASS;
6981 return 1;
6982 }
6983
6984 /* Try to decode HTTP request */
6985 if (likely(req->lr < req->r))
6986 http_msg_analyzer(req, msg, &txn->hdr_idx);
6987
Willy Tarreau655dce92009-11-08 13:10:58 +01006988 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006989 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6990 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6991 return 1;
6992 }
6993 /* wait for final state */
6994 test->flags |= ACL_TEST_F_MAY_CHANGE;
6995 return 0;
6996 }
6997
6998 /* OK we got a valid HTTP request. We have some minor preparation to
6999 * perform so that further checks can rely on HTTP tests.
7000 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007001 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007002 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7003 s->flags |= SN_REDIRECTABLE;
7004
7005 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7006 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7007 return 1;
7008 }
7009
7010 test->flags |= ACL_TEST_F_SET_RES_PASS;
7011 return 1;
7012}
7013
Willy Tarreau8797c062007-05-07 00:55:35 +02007014
7015/************************************************************************/
7016/* All supported keywords must be declared here. */
7017/************************************************************************/
7018
7019/* Note: must not be declared <const> as its list will be overwritten */
7020static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007021 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7022
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007023 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7024 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7025 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7026 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007027
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007028 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7029 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7030 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7031 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7032 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7033 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7034 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7035 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7036 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007037
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007038 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7039 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7040 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7041 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7042 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7043 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7044 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7045 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7046 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7047 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007048 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007049
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007050 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7051 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7052 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7053 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7054 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7055 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7056 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7057 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7058 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007059 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007060
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007061 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7062 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7063 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7064 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7065 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7066 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7067 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007068
Willy Tarreauf3d25982007-05-08 22:45:09 +02007069 { NULL, NULL, NULL, NULL },
7070
7071#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007072 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7073 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7074 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7075 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7076 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7077 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7078 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7079
Willy Tarreau8797c062007-05-07 00:55:35 +02007080 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7081 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7082 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7083 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7084 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7085 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7086 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7087 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7088
7089 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7090 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7091 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7092 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7093 { NULL, NULL, NULL, NULL },
7094#endif
7095}};
7096
7097
7098__attribute__((constructor))
7099static void __http_protocol_init(void)
7100{
7101 acl_register_keywords(&acl_kws);
7102}
7103
7104
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007106 * Local variables:
7107 * c-indent-level: 8
7108 * c-basic-offset: 8
7109 * End:
7110 */