blob: e06ebe68d297fc3e2ecd91866569be80b8390b65 [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
684 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
685 rdr.len += s->srv->rdr_len;
686
687 /* 3: add the request URI */
688 txn = &s->txn;
689 path = http_get_path(txn);
690 if (!path)
691 return;
692
Willy Tarreau962c3f42010-01-10 00:15:35 +0100693 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200694 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695 return;
696
697 memcpy(rdr.str + rdr.len, path, len);
698 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100699 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
700 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701
702 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100703 si->shutr(si);
704 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100705 si->err_type = SI_ET_NONE;
706 si->err_loc = NULL;
707 si->state = SI_ST_CLO;
708
709 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100710 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711
712 /* FIXME: we should increase a counter of redirects per server and per backend. */
713 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100714 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100715}
716
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100717/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100718 * that the server side is closed. Note that err_type is actually a
719 * bitmask, where almost only aborts may be cumulated with other
720 * values. We consider that aborted operations are more important
721 * than timeouts or errors due to the fact that nobody else in the
722 * logs might explain incomplete retries. All others should avoid
723 * being cumulated. It should normally not be possible to have multiple
724 * aborts at once, but just in case, the first one in sequence is reported.
725 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100726void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100727{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100728 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100729
730 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100731 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
732 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100733 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100734 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
735 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100736 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100737 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
738 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100739 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100740 http_server_error(s, si, SN_ERR_SRVCL, 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_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100743 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
744 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100746 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
747 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100748 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100749 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
750 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751}
752
Willy Tarreau42250582007-04-01 01:30:43 +0200753extern const char sess_term_cond[8];
754extern const char sess_fin_state[8];
755extern const char *monthname[12];
756const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
757const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
758 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
759 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200760struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200761struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100762
Emeric Brun3a058f32009-06-30 18:26:00 +0200763void http_sess_clflog(struct session *s)
764{
765 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
766 struct proxy *fe = s->fe;
767 struct proxy *be = s->be;
768 struct proxy *prx_log;
769 struct http_txn *txn = &s->txn;
770 int tolog, level, err;
771 char *uri, *h;
772 char *svid;
773 struct tm tm;
774 static char tmpline[MAX_SYSLOG_LEN];
775 int hdr;
776 size_t w;
777 int t_request;
778
779 prx_log = fe;
780 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
781 (s->conn_retries != be->conn_retries) ||
782 txn->status >= 500;
783
784 if (s->cli_addr.ss_family == AF_INET)
785 inet_ntop(AF_INET,
786 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
787 pn, sizeof(pn));
788 else
789 inet_ntop(AF_INET6,
790 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
791 pn, sizeof(pn));
792
793 get_gmtime(s->logs.accept_date.tv_sec, &tm);
794
795 /* FIXME: let's limit ourselves to frontend logging for now. */
796 tolog = fe->to_log;
797
798 h = tmpline;
799
800 w = snprintf(h, sizeof(tmpline),
801 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
802 pn,
803 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
804 tm.tm_hour, tm.tm_min, tm.tm_sec);
805 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
806 goto trunc;
807 h += w;
808
809 if (h >= tmpline + sizeof(tmpline) - 4)
810 goto trunc;
811
812 *(h++) = ' ';
813 *(h++) = '\"';
814 uri = txn->uri ? txn->uri : "<BADREQ>";
815 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
816 '#', url_encode_map, uri);
817 *(h++) = '\"';
818
819 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
820 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
821 goto trunc;
822 h += w;
823
824 if (h >= tmpline + sizeof(tmpline) - 9)
825 goto trunc;
826 memcpy(h, " \"-\" \"-\"", 8);
827 h += 8;
828
829 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
830 " %d %03d",
831 (s->cli_addr.ss_family == AF_INET) ?
832 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
833 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
834 (int)s->logs.accept_date.tv_usec/1000);
835 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
836 goto trunc;
837 h += w;
838
839 w = strlen(fe->id);
840 if (h >= tmpline + sizeof(tmpline) - 4 - w)
841 goto trunc;
842 *(h++) = ' ';
843 *(h++) = '\"';
844 memcpy(h, fe->id, w);
845 h += w;
846 *(h++) = '\"';
847
848 w = strlen(be->id);
849 if (h >= tmpline + sizeof(tmpline) - 4 - w)
850 goto trunc;
851 *(h++) = ' ';
852 *(h++) = '\"';
853 memcpy(h, be->id, w);
854 h += w;
855 *(h++) = '\"';
856
857 svid = (tolog & LW_SVID) ?
858 (s->data_source != DATA_SRC_STATS) ?
859 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
860
861 w = strlen(svid);
862 if (h >= tmpline + sizeof(tmpline) - 4 - w)
863 goto trunc;
864 *(h++) = ' ';
865 *(h++) = '\"';
866 memcpy(h, svid, w);
867 h += w;
868 *(h++) = '\"';
869
870 t_request = -1;
871 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
872 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
873 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
874 " %d %ld %ld %ld %ld",
875 t_request,
876 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
877 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
878 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
879 s->logs.t_close);
880 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
881 goto trunc;
882 h += w;
883
884 if (h >= tmpline + sizeof(tmpline) - 8)
885 goto trunc;
886 *(h++) = ' ';
887 *(h++) = '\"';
888 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
889 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
890 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
891 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
892 *(h++) = '\"';
893
894 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
895 " %d %d %d %d %d %ld %ld",
896 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
897 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
898 s->logs.srv_queue_size, s->logs.prx_queue_size);
899
900 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
901 goto trunc;
902 h += w;
903
904 if (txn->cli_cookie) {
905 w = strlen(txn->cli_cookie);
906 if (h >= tmpline + sizeof(tmpline) - 4 - w)
907 goto trunc;
908 *(h++) = ' ';
909 *(h++) = '\"';
910 memcpy(h, txn->cli_cookie, w);
911 h += w;
912 *(h++) = '\"';
913 } else {
914 if (h >= tmpline + sizeof(tmpline) - 5)
915 goto trunc;
916 memcpy(h, " \"-\"", 4);
917 h += 4;
918 }
919
920 if (txn->srv_cookie) {
921 w = strlen(txn->srv_cookie);
922 if (h >= tmpline + sizeof(tmpline) - 4 - w)
923 goto trunc;
924 *(h++) = ' ';
925 *(h++) = '\"';
926 memcpy(h, txn->srv_cookie, w);
927 h += w;
928 *(h++) = '\"';
929 } else {
930 if (h >= tmpline + sizeof(tmpline) - 5)
931 goto trunc;
932 memcpy(h, " \"-\"", 4);
933 h += 4;
934 }
935
936 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
937 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
938 if (h >= sizeof (tmpline) + tmpline - 4)
939 goto trunc;
940 *(h++) = ' ';
941 *(h++) = '\"';
942 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
943 '#', hdr_encode_map, txn->req.cap[hdr]);
944 *(h++) = '\"';
945 }
946 }
947
948 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
949 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
950 if (h >= sizeof (tmpline) + tmpline - 4)
951 goto trunc;
952 *(h++) = ' ';
953 *(h++) = '\"';
954 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
955 '#', hdr_encode_map, txn->rsp.cap[hdr]);
956 *(h++) = '\"';
957 }
958 }
959
960trunc:
961 *h = '\0';
962
963 level = LOG_INFO;
964 if (err && (fe->options2 & PR_O2_LOGERRORS))
965 level = LOG_ERR;
966
967 send_log(prx_log, level, "%s\n", tmpline);
968
969 s->logs.logwait = 0;
970}
971
Willy Tarreau42250582007-04-01 01:30:43 +0200972/*
973 * send a log for the session when we have enough info about it.
974 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100976void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200977{
978 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
979 struct proxy *fe = s->fe;
980 struct proxy *be = s->be;
981 struct proxy *prx_log;
982 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200983 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200984 char *uri, *h;
985 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200986 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200987 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200988 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200989 int hdr;
990
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200991 /* if we don't want to log normal traffic, return now */
992 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
993 (s->conn_retries != be->conn_retries) ||
994 txn->status >= 500;
995 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
996 return;
997
Willy Tarreau42250582007-04-01 01:30:43 +0200998 if (fe->logfac1 < 0 && fe->logfac2 < 0)
999 return;
1000 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001001
Emeric Brun3a058f32009-06-30 18:26:00 +02001002 if (prx_log->options2 & PR_O2_CLFLOG)
1003 return http_sess_clflog(s);
1004
Willy Tarreau42250582007-04-01 01:30:43 +02001005 if (s->cli_addr.ss_family == AF_INET)
1006 inet_ntop(AF_INET,
1007 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1008 pn, sizeof(pn));
1009 else
1010 inet_ntop(AF_INET6,
1011 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1012 pn, sizeof(pn));
1013
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001014 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001015
1016 /* FIXME: let's limit ourselves to frontend logging for now. */
1017 tolog = fe->to_log;
1018
1019 h = tmpline;
1020 if (fe->to_log & LW_REQHDR &&
1021 txn->req.cap &&
1022 (h < tmpline + sizeof(tmpline) - 10)) {
1023 *(h++) = ' ';
1024 *(h++) = '{';
1025 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1026 if (hdr)
1027 *(h++) = '|';
1028 if (txn->req.cap[hdr] != NULL)
1029 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1030 '#', hdr_encode_map, txn->req.cap[hdr]);
1031 }
1032 *(h++) = '}';
1033 }
1034
1035 if (fe->to_log & LW_RSPHDR &&
1036 txn->rsp.cap &&
1037 (h < tmpline + sizeof(tmpline) - 7)) {
1038 *(h++) = ' ';
1039 *(h++) = '{';
1040 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1041 if (hdr)
1042 *(h++) = '|';
1043 if (txn->rsp.cap[hdr] != NULL)
1044 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1045 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1046 }
1047 *(h++) = '}';
1048 }
1049
1050 if (h < tmpline + sizeof(tmpline) - 4) {
1051 *(h++) = ' ';
1052 *(h++) = '"';
1053 uri = txn->uri ? txn->uri : "<BADREQ>";
1054 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1055 '#', url_encode_map, uri);
1056 *(h++) = '"';
1057 }
1058 *h = '\0';
1059
1060 svid = (tolog & LW_SVID) ?
1061 (s->data_source != DATA_SRC_STATS) ?
1062 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1063
Willy Tarreau70089872008-06-13 21:12:51 +02001064 t_request = -1;
1065 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1066 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1067
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001068 level = LOG_INFO;
1069 if (err && (fe->options2 & PR_O2_LOGERRORS))
1070 level = LOG_ERR;
1071
1072 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001073 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001074 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1075 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001076 pn,
1077 (s->cli_addr.ss_family == AF_INET) ?
1078 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1079 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001080 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001081 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001082 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001083 t_request,
1084 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001085 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1086 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1087 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1088 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001089 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001090 txn->cli_cookie ? txn->cli_cookie : "-",
1091 txn->srv_cookie ? txn->srv_cookie : "-",
1092 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1093 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1094 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1095 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1096 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001097 (s->flags & SN_REDISP)?"+":"",
1098 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001099 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1100
1101 s->logs.logwait = 0;
1102}
1103
Willy Tarreau117f59e2007-03-04 18:17:17 +01001104
1105/*
1106 * Capture headers from message starting at <som> according to header list
1107 * <cap_hdr>, and fill the <idx> structure appropriately.
1108 */
1109void capture_headers(char *som, struct hdr_idx *idx,
1110 char **cap, struct cap_hdr *cap_hdr)
1111{
1112 char *eol, *sol, *col, *sov;
1113 int cur_idx;
1114 struct cap_hdr *h;
1115 int len;
1116
1117 sol = som + hdr_idx_first_pos(idx);
1118 cur_idx = hdr_idx_first_idx(idx);
1119
1120 while (cur_idx) {
1121 eol = sol + idx->v[cur_idx].len;
1122
1123 col = sol;
1124 while (col < eol && *col != ':')
1125 col++;
1126
1127 sov = col + 1;
1128 while (sov < eol && http_is_lws[(unsigned char)*sov])
1129 sov++;
1130
1131 for (h = cap_hdr; h; h = h->next) {
1132 if ((h->namelen == col - sol) &&
1133 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1134 if (cap[h->index] == NULL)
1135 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001136 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001137
1138 if (cap[h->index] == NULL) {
1139 Alert("HTTP capture : out of memory.\n");
1140 continue;
1141 }
1142
1143 len = eol - sov;
1144 if (len > h->len)
1145 len = h->len;
1146
1147 memcpy(cap[h->index], sov, len);
1148 cap[h->index][len]=0;
1149 }
1150 }
1151 sol = eol + idx->v[cur_idx].cr + 1;
1152 cur_idx = idx->v[cur_idx].next;
1153 }
1154}
1155
1156
Willy Tarreau42250582007-04-01 01:30:43 +02001157/* either we find an LF at <ptr> or we jump to <bad>.
1158 */
1159#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1160
1161/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1162 * otherwise to <http_msg_ood> with <state> set to <st>.
1163 */
1164#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1165 ptr++; \
1166 if (likely(ptr < end)) \
1167 goto good; \
1168 else { \
1169 state = (st); \
1170 goto http_msg_ood; \
1171 } \
1172 } while (0)
1173
1174
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001176 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1178 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1179 * will give undefined results.
1180 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1181 * and that msg->sol points to the beginning of the response.
1182 * If a complete line is found (which implies that at least one CR or LF is
1183 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1184 * returned indicating an incomplete line (which does not mean that parts have
1185 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1186 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1187 * upon next call.
1188 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001189 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1191 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001192 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001194const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1195 unsigned int state, const char *ptr, const char *end,
1196 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001197{
Willy Tarreau8973c702007-01-21 23:58:29 +01001198 switch (state) {
1199 http_msg_rpver:
1200 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001201 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1203
1204 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001205 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1207 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001208 state = HTTP_MSG_ERROR;
1209 break;
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211 http_msg_rpver_sp:
1212 case HTTP_MSG_RPVER_SP:
1213 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001214 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001215 goto http_msg_rpcode;
1216 }
1217 if (likely(HTTP_IS_SPHT(*ptr)))
1218 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1219 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001220 state = HTTP_MSG_ERROR;
1221 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001222
1223 http_msg_rpcode:
1224 case HTTP_MSG_RPCODE:
1225 if (likely(!HTTP_IS_LWS(*ptr)))
1226 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1227
1228 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001229 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001230 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1231 }
1232
1233 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001234 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001235 http_msg_rsp_reason:
1236 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001237 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 msg->sl.st.r_l = 0;
1239 goto http_msg_rpline_eol;
1240
1241 http_msg_rpcode_sp:
1242 case HTTP_MSG_RPCODE_SP:
1243 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001244 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 goto http_msg_rpreason;
1246 }
1247 if (likely(HTTP_IS_SPHT(*ptr)))
1248 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1249 /* so it's a CR/LF, so there is no reason phrase */
1250 goto http_msg_rsp_reason;
1251
1252 http_msg_rpreason:
1253 case HTTP_MSG_RPREASON:
1254 if (likely(!HTTP_IS_CRLF(*ptr)))
1255 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001256 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001257 http_msg_rpline_eol:
1258 /* We have seen the end of line. Note that we do not
1259 * necessarily have the \n yet, but at least we know that we
1260 * have EITHER \r OR \n, otherwise the response would not be
1261 * complete. We can then record the response length and return
1262 * to the caller which will be able to register it.
1263 */
1264 msg->sl.st.l = ptr - msg->sol;
1265 return ptr;
1266
1267#ifdef DEBUG_FULL
1268 default:
1269 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1270 exit(1);
1271#endif
1272 }
1273
1274 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001275 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 if (ret_state)
1277 *ret_state = state;
1278 if (ret_ptr)
1279 *ret_ptr = (char *)ptr;
1280 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001281}
1282
1283
1284/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285 * This function parses a request line between <ptr> and <end>, starting with
1286 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1287 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1288 * will give undefined results.
1289 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1290 * and that msg->sol points to the beginning of the request.
1291 * If a complete line is found (which implies that at least one CR or LF is
1292 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1293 * returned indicating an incomplete line (which does not mean that parts have
1294 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1295 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1296 * upon next call.
1297 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001298 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1300 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001301 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001302 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001303const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1304 unsigned int state, const char *ptr, const char *end,
1305 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 switch (state) {
1308 http_msg_rqmeth:
1309 case HTTP_MSG_RQMETH:
1310 if (likely(HTTP_IS_TOKEN(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001314 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 if (likely(HTTP_IS_CRLF(*ptr))) {
1319 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001320 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001322 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001324 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001326 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 msg->sl.rq.v_l = 0;
1328 goto http_msg_rqline_eol;
1329 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001330 state = HTTP_MSG_ERROR;
1331 break;
1332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 http_msg_rqmeth_sp:
1334 case HTTP_MSG_RQMETH_SP:
1335 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001336 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 goto http_msg_rquri;
1338 }
1339 if (likely(HTTP_IS_SPHT(*ptr)))
1340 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1341 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1342 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 http_msg_rquri:
1345 case HTTP_MSG_RQURI:
1346 if (likely(!HTTP_IS_LWS(*ptr)))
1347 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001348
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001350 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1352 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1355 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 http_msg_rquri_sp:
1358 case HTTP_MSG_RQURI_SP:
1359 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001360 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 goto http_msg_rqver;
1362 }
1363 if (likely(HTTP_IS_SPHT(*ptr)))
1364 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1365 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1366 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 http_msg_rqver:
1369 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001370 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001372
1373 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001374 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001375 http_msg_rqline_eol:
1376 /* We have seen the end of line. Note that we do not
1377 * necessarily have the \n yet, but at least we know that we
1378 * have EITHER \r OR \n, otherwise the request would not be
1379 * complete. We can then record the request length and return
1380 * to the caller which will be able to register it.
1381 */
1382 msg->sl.rq.l = ptr - msg->sol;
1383 return ptr;
1384 }
1385
1386 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001387 state = HTTP_MSG_ERROR;
1388 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390#ifdef DEBUG_FULL
1391 default:
1392 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1393 exit(1);
1394#endif
1395 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001398 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 if (ret_state)
1400 *ret_state = state;
1401 if (ret_ptr)
1402 *ret_ptr = (char *)ptr;
1403 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001405
1406
Willy Tarreau8973c702007-01-21 23:58:29 +01001407/*
1408 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001409 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 * when data are missing and recalled at the exact same location with no
1411 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001412 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001413 * fields. Note that msg->som and msg->sol will be initialized after completing
1414 * the first state, so that none of the msg pointers has to be initialized
1415 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1418{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001419 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001422 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 ptr = buf->lr;
1424 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 if (unlikely(ptr >= end))
1427 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001430 /*
1431 * First, states that are specific to the response only.
1432 * We check them first so that request and headers are
1433 * closer to each other (accessed more often).
1434 */
1435 http_msg_rpbefore:
1436 case HTTP_MSG_RPBEFORE:
1437 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001438 /* we have a start of message, but we have to check
1439 * first if we need to remove some CRLF. We can only
1440 * do this when send_max=0.
1441 */
1442 char *beg = buf->w + buf->send_max;
1443 if (beg >= buf->data + buf->size)
1444 beg -= buf->size;
1445 if (unlikely(ptr != beg)) {
1446 if (buf->send_max)
1447 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001448 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001449 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001451 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001452 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 hdr_idx_init(idx);
1454 state = HTTP_MSG_RPVER;
1455 goto http_msg_rpver;
1456 }
1457
1458 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1459 goto http_msg_invalid;
1460
1461 if (unlikely(*ptr == '\n'))
1462 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1463 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1464 /* stop here */
1465
1466 http_msg_rpbefore_cr:
1467 case HTTP_MSG_RPBEFORE_CR:
1468 EXPECT_LF_HERE(ptr, http_msg_invalid);
1469 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1470 /* stop here */
1471
1472 http_msg_rpver:
1473 case HTTP_MSG_RPVER:
1474 case HTTP_MSG_RPVER_SP:
1475 case HTTP_MSG_RPCODE:
1476 case HTTP_MSG_RPCODE_SP:
1477 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001478 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001479 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001480 if (unlikely(!ptr))
1481 return;
1482
1483 /* we have a full response and we know that we have either a CR
1484 * or an LF at <ptr>.
1485 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001486 //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 +01001487 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1488
1489 msg->sol = ptr;
1490 if (likely(*ptr == '\r'))
1491 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1492 goto http_msg_rpline_end;
1493
1494 http_msg_rpline_end:
1495 case HTTP_MSG_RPLINE_END:
1496 /* msg->sol must point to the first of CR or LF. */
1497 EXPECT_LF_HERE(ptr, http_msg_invalid);
1498 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1499 /* stop here */
1500
1501 /*
1502 * Second, states that are specific to the request only
1503 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 http_msg_rqbefore:
1505 case HTTP_MSG_RQBEFORE:
1506 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001507 /* we have a start of message, but we have to check
1508 * first if we need to remove some CRLF. We can only
1509 * do this when send_max=0.
1510 */
1511 char *beg = buf->w + buf->send_max;
1512 if (beg >= buf->data + buf->size)
1513 beg -= buf->size;
1514 if (likely(ptr != beg)) {
1515 if (buf->send_max)
1516 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001517 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001518 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001520 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001521 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001522 /* we will need this when keep-alive will be supported
1523 hdr_idx_init(idx);
1524 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 state = HTTP_MSG_RQMETH;
1526 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1530 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (unlikely(*ptr == '\n'))
1533 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 http_msg_rqbefore_cr:
1538 case HTTP_MSG_RQBEFORE_CR:
1539 EXPECT_LF_HERE(ptr, http_msg_invalid);
1540 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001541 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 http_msg_rqmeth:
1544 case HTTP_MSG_RQMETH:
1545 case HTTP_MSG_RQMETH_SP:
1546 case HTTP_MSG_RQURI:
1547 case HTTP_MSG_RQURI_SP:
1548 case HTTP_MSG_RQVER:
1549 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001550 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 if (unlikely(!ptr))
1552 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 /* we have a full request and we know that we have either a CR
1555 * or an LF at <ptr>.
1556 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001557 //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 +01001558 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 msg->sol = ptr;
1561 if (likely(*ptr == '\r'))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 http_msg_rqline_end:
1566 case HTTP_MSG_RQLINE_END:
1567 /* check for HTTP/0.9 request : no version information available.
1568 * msg->sol must point to the first of CR or LF.
1569 */
1570 if (unlikely(msg->sl.rq.v_l == 0))
1571 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001572
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 EXPECT_LF_HERE(ptr, http_msg_invalid);
1574 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001575 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001576
Willy Tarreau8973c702007-01-21 23:58:29 +01001577 /*
1578 * Common states below
1579 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 http_msg_hdr_first:
1581 case HTTP_MSG_HDR_FIRST:
1582 msg->sol = ptr;
1583 if (likely(!HTTP_IS_CRLF(*ptr))) {
1584 goto http_msg_hdr_name;
1585 }
1586
1587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1589 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 http_msg_hdr_name:
1592 case HTTP_MSG_HDR_NAME:
1593 /* assumes msg->sol points to the first char */
1594 if (likely(HTTP_IS_TOKEN(*ptr)))
1595 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (likely(*ptr == ':')) {
1598 msg->col = ptr - buf->data;
1599 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1600 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001601
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001602 if (likely(msg->err_pos < -1) || *ptr == '\n')
1603 goto http_msg_invalid;
1604
1605 if (msg->err_pos == -1) /* capture error pointer */
1606 msg->err_pos = ptr - buf->data; /* >= 0 now */
1607
1608 /* and we still accept this non-token character */
1609 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 http_msg_hdr_l1_sp:
1612 case HTTP_MSG_HDR_L1_SP:
1613 /* assumes msg->sol points to the first char and msg->col to the colon */
1614 if (likely(HTTP_IS_SPHT(*ptr)))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 /* header value can be basically anything except CR/LF */
1618 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 if (likely(!HTTP_IS_CRLF(*ptr))) {
1621 goto http_msg_hdr_val;
1622 }
1623
1624 if (likely(*ptr == '\r'))
1625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1626 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 http_msg_hdr_l1_lf:
1629 case HTTP_MSG_HDR_L1_LF:
1630 EXPECT_LF_HERE(ptr, http_msg_invalid);
1631 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 http_msg_hdr_l1_lws:
1634 case HTTP_MSG_HDR_L1_LWS:
1635 if (likely(HTTP_IS_SPHT(*ptr))) {
1636 /* replace HT,CR,LF with spaces */
1637 for (; buf->data+msg->sov < ptr; msg->sov++)
1638 buf->data[msg->sov] = ' ';
1639 goto http_msg_hdr_l1_sp;
1640 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001641 /* we had a header consisting only in spaces ! */
1642 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 goto http_msg_complete_header;
1644
1645 http_msg_hdr_val:
1646 case HTTP_MSG_HDR_VAL:
1647 /* assumes msg->sol points to the first char, msg->col to the
1648 * colon, and msg->sov points to the first character of the
1649 * value.
1650 */
1651 if (likely(!HTTP_IS_CRLF(*ptr)))
1652 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 msg->eol = ptr;
1655 /* Note: we could also copy eol into ->eoh so that we have the
1656 * real header end in case it ends with lots of LWS, but is this
1657 * really needed ?
1658 */
1659 if (likely(*ptr == '\r'))
1660 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1661 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001662
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 http_msg_hdr_l2_lf:
1664 case HTTP_MSG_HDR_L2_LF:
1665 EXPECT_LF_HERE(ptr, http_msg_invalid);
1666 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 http_msg_hdr_l2_lws:
1669 case HTTP_MSG_HDR_L2_LWS:
1670 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1671 /* LWS: replace HT,CR,LF with spaces */
1672 for (; msg->eol < ptr; msg->eol++)
1673 *msg->eol = ' ';
1674 goto http_msg_hdr_val;
1675 }
1676 http_msg_complete_header:
1677 /*
1678 * It was a new header, so the last one is finished.
1679 * Assumes msg->sol points to the first char, msg->col to the
1680 * colon, msg->sov points to the first character of the value
1681 * and msg->eol to the first CR or LF so we know how the line
1682 * ends. We insert last header into the index.
1683 */
1684 /*
1685 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1686 write(2, msg->sol, msg->eol-msg->sol);
1687 fprintf(stderr,"\n");
1688 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1691 idx, idx->tail) < 0))
1692 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 msg->sol = ptr;
1695 if (likely(!HTTP_IS_CRLF(*ptr))) {
1696 goto http_msg_hdr_name;
1697 }
1698
1699 if (likely(*ptr == '\r'))
1700 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1701 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 http_msg_last_lf:
1704 case HTTP_MSG_LAST_LF:
1705 /* Assumes msg->sol points to the first of either CR or LF */
1706 EXPECT_LF_HERE(ptr, http_msg_invalid);
1707 ptr++;
1708 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001709 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001711 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001712 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 return;
1714#ifdef DEBUG_FULL
1715 default:
1716 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1717 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001718#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 }
1720 http_msg_ood:
1721 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001722 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 buf->lr = ptr;
1724 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 http_msg_invalid:
1727 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001728 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001729 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 return;
1731}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001732
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001733/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1734 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1735 * nothing is done and 1 is returned.
1736 */
1737static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1738{
1739 int delta;
1740 char *cur_end;
1741
1742 if (msg->sl.rq.v_l != 0)
1743 return 1;
1744
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001745 cur_end = msg->sol + msg->sl.rq.l;
1746 delta = 0;
1747
1748 if (msg->sl.rq.u_l == 0) {
1749 /* if no URI was set, add "/" */
1750 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1751 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001752 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001753 }
1754 /* add HTTP version */
1755 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001756 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001757 cur_end += delta;
1758 cur_end = (char *)http_parse_reqline(msg, req->data,
1759 HTTP_MSG_RQMETH,
1760 msg->sol, cur_end + 1,
1761 NULL, NULL);
1762 if (unlikely(!cur_end))
1763 return 0;
1764
1765 /* we have a full HTTP/1.0 request now and we know that
1766 * we have either a CR or an LF at <ptr>.
1767 */
1768 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1769 return 1;
1770}
1771
Willy Tarreau5b154472009-12-21 20:11:07 +01001772/* Parse the Connection: headaer of an HTTP request, and set the transaction
1773 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1774 * is also set so that we don't parse a second time. If some dangerous values
1775 * are encountered, we leave the status to indicate that the request might be
1776 * interpreted as keep-alive, but we also set the connection flags to indicate
1777 * that we WANT it to be a close, so that the header will be fixed. This
1778 * function should only be called when we know we're interested in checking
1779 * the request (not a CONNECT, and FE or BE mangles the header).
1780 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001781void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001782{
1783 struct http_msg *msg = &txn->req;
1784 struct hdr_ctx ctx;
1785 int conn_cl, conn_ka;
1786
1787 if (txn->flags & TX_CON_HDR_PARS)
1788 return;
1789
1790 conn_cl = 0;
1791 conn_ka = 0;
1792 ctx.idx = 0;
1793
1794 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1795 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1796 conn_cl = 1;
1797 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1798 conn_ka = 1;
1799 }
1800
1801 /* Determine if the client wishes keep-alive or close.
1802 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1803 * are persistent unless "Connection: close" is explicitly specified.
1804 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1805 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1806 * they explicitly specify "Connection: Keep-Alive", regardless of any
1807 * optional "Keep-Alive" header.
1808 * Note that if we find a request with both "Connection: close" and
1809 * "Connection: Keep-Alive", we indicate we want a close but don't have
1810 * it, so that it can be enforced later.
1811 */
1812
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001813 if (conn_cl && conn_ka) {
1814 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1815 }
1816 else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
Willy Tarreau5b154472009-12-21 20:11:07 +01001817 if (conn_cl) {
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001818 txn->flags |= TX_REQ_CONN_CLO;
1819 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1820 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01001821 }
1822 } else { /* HTTP/1.0 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001823 if (!conn_ka) {
1824 txn->flags |= TX_REQ_CONN_CLO;
1825 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1826 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1827 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001828 }
1829 txn->flags |= TX_CON_HDR_PARS;
1830}
1831
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1833 * first byte of body, and increments msg->sov by the number of bytes parsed,
1834 * so that we know we can forward between ->som and ->sov. Note that due to
1835 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1836 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001837 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001838 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001839 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001841{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 char *ptr = buf->lr;
1843 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001844 unsigned int chunk = 0;
1845
1846 /* The chunk size is in the following form, though we are only
1847 * interested in the size and CRLF :
1848 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1849 */
1850 while (1) {
1851 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 if (c < 0) /* not a hex digit anymore */
1856 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857 if (++ptr >= end)
1858 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001859 if (chunk & 0xF000000) /* overflow will occur */
1860 return -1;
1861 chunk = (chunk << 4) + c;
1862 }
1863
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 /* empty size not allowed */
1865 if (ptr == buf->lr)
1866 return -1;
1867
1868 while (http_is_spht[(unsigned char)*ptr]) {
1869 if (++ptr >= end)
1870 ptr = buf->data;
1871 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001872 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001873 }
1874
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 /* Up to there, we know that at least one byte is present at *ptr. Check
1876 * for the end of chunk size.
1877 */
1878 while (1) {
1879 if (likely(HTTP_IS_CRLF(*ptr))) {
1880 /* we now have a CR or an LF at ptr */
1881 if (likely(*ptr == '\r')) {
1882 if (++ptr >= end)
1883 ptr = buf->data;
1884 if (ptr == buf->r)
1885 return 0;
1886 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001887
Willy Tarreaud98cf932009-12-27 22:54:55 +01001888 if (*ptr != '\n')
1889 return -1;
1890 if (++ptr >= end)
1891 ptr = buf->data;
1892 /* done */
1893 break;
1894 }
1895 else if (*ptr == ';') {
1896 /* chunk extension, ends at next CRLF */
1897 if (++ptr >= end)
1898 ptr = buf->data;
1899 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001900 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901
1902 while (!HTTP_IS_CRLF(*ptr)) {
1903 if (++ptr >= end)
1904 ptr = buf->data;
1905 if (ptr == buf->r)
1906 return 0;
1907 }
1908 /* we have a CRLF now, loop above */
1909 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001910 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 }
1914
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 /* OK we found our CRLF and now <ptr> points to the next byte,
1916 * which may or may not be present. We save that into ->lr and
1917 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 msg->sov += ptr - buf->lr;
1920 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001923 return 1;
1924}
1925
Willy Tarreaud98cf932009-12-27 22:54:55 +01001926/* This function skips trailers in the buffer <buf> associated with HTTP
1927 * message <msg>. The first visited position is buf->lr. If the end of
1928 * the trailers is found, it is automatically scheduled to be forwarded,
1929 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1930 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001931 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1932 * zero. If a parse error is encountered, the function returns < 0 and does not
1933 * change anything except maybe buf->lr and msg->sov. Note that the message
1934 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1935 * which implies that all non-trailers data have already been scheduled for
1936 * forwarding, and that the difference between msg->som and msg->sov exactly
1937 * matches the length of trailers already parsed and not forwarded. It is also
1938 * important to note that this function is designed to be able to parse wrapped
1939 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 */
1941int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1942{
1943 /* we have buf->lr which points to next line. Look for CRLF. */
1944 while (1) {
1945 char *p1 = NULL, *p2 = NULL;
1946 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001947 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948
1949 /* scan current line and stop at LF or CRLF */
1950 while (1) {
1951 if (ptr == buf->r)
1952 return 0;
1953
1954 if (*ptr == '\n') {
1955 if (!p1)
1956 p1 = ptr;
1957 p2 = ptr;
1958 break;
1959 }
1960
1961 if (*ptr == '\r') {
1962 if (p1)
1963 return -1;
1964 p1 = ptr;
1965 }
1966
1967 ptr++;
1968 if (ptr >= buf->data + buf->size)
1969 ptr = buf->data;
1970 }
1971
1972 /* after LF; point to beginning of next line */
1973 p2++;
1974 if (p2 >= buf->data + buf->size)
1975 p2 = buf->data;
1976
Willy Tarreau638cd022010-01-03 07:42:04 +01001977 bytes = p2 - buf->lr;
1978 if (bytes < 0)
1979 bytes += buf->size;
1980
1981 /* schedule this line for forwarding */
1982 msg->sov += bytes;
1983 if (msg->sov >= buf->size)
1984 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985
Willy Tarreau638cd022010-01-03 07:42:04 +01001986 if (p1 == buf->lr) {
1987 /* LF/CRLF at beginning of line => end of trailers at p2.
1988 * Everything was scheduled for forwarding, there's nothing
1989 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001990 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001991 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 msg->msg_state = HTTP_MSG_DONE;
1993 return 1;
1994 }
1995 /* OK, next line then */
1996 buf->lr = p2;
1997 }
1998}
1999
2000/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2001 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2002 * ->som, buf->lr in order to include this part into the next forwarding phase.
2003 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2004 * not enough data are available, the function does not change anything and
2005 * returns zero. If a parse error is encountered, the function returns < 0 and
2006 * does not change anything. Note: this function is designed to parse wrapped
2007 * CRLF at the end of the buffer.
2008 */
2009int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2010{
2011 char *ptr;
2012 int bytes;
2013
2014 /* NB: we'll check data availabilty at the end. It's not a
2015 * problem because whatever we match first will be checked
2016 * against the correct length.
2017 */
2018 bytes = 1;
2019 ptr = buf->lr;
2020 if (*ptr == '\r') {
2021 bytes++;
2022 ptr++;
2023 if (ptr >= buf->data + buf->size)
2024 ptr = buf->data;
2025 }
2026
2027 if (buf->l < bytes)
2028 return 0;
2029
2030 if (*ptr != '\n')
2031 return -1;
2032
2033 ptr++;
2034 if (ptr >= buf->data + buf->size)
2035 ptr = buf->data;
2036 buf->lr = ptr;
2037 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2038 msg->sov = ptr - buf->data;
2039 msg->som = msg->sov - bytes;
2040 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2041 return 1;
2042}
Willy Tarreau5b154472009-12-21 20:11:07 +01002043
Willy Tarreau83e3af02009-12-28 17:39:57 +01002044void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2045{
2046 char *end = buf->data + buf->size;
2047 int off = buf->data + buf->size - buf->w;
2048
2049 /* two possible cases :
2050 * - the buffer is in one contiguous block, we move it in-place
2051 * - the buffer is in two blocks, we move it via the trash
2052 */
2053 if (buf->l) {
2054 int block1 = buf->l;
2055 int block2 = 0;
2056 if (buf->r <= buf->w) {
2057 /* non-contiguous block */
2058 block1 = buf->data + buf->size - buf->w;
2059 block2 = buf->r - buf->data;
2060 }
2061 if (block2)
2062 memcpy(trash, buf->data, block2);
2063 memmove(buf->data, buf->w, block1);
2064 if (block2)
2065 memcpy(buf->data + block1, trash, block2);
2066 }
2067
2068 /* adjust all known pointers */
2069 buf->w = buf->data;
2070 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2071 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2072 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2073 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2074
2075 /* adjust relative pointers */
2076 msg->som = 0;
2077 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2078 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2079 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2080
Willy Tarreau83e3af02009-12-28 17:39:57 +01002081 if (msg->err_pos >= 0) {
2082 msg->err_pos += off;
2083 if (msg->err_pos >= buf->size)
2084 msg->err_pos -= buf->size;
2085 }
2086
2087 buf->flags &= ~BF_FULL;
2088 if (buf->l >= buffer_max_len(buf))
2089 buf->flags |= BF_FULL;
2090}
2091
Willy Tarreaud787e662009-07-07 10:14:51 +02002092/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2093 * processing can continue on next analysers, or zero if it either needs more
2094 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2095 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2096 * when it has nothing left to do, and may remove any analyser when it wants to
2097 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002098 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002099int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002100{
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 /*
2102 * We will parse the partial (or complete) lines.
2103 * We will check the request syntax, and also join multi-line
2104 * headers. An index of all the lines will be elaborated while
2105 * parsing.
2106 *
2107 * For the parsing, we use a 28 states FSM.
2108 *
2109 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002110 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002111 * req->data + msg->eoh = end of processed headers / start of current one
2112 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 * req->lr = first non-visited byte
2114 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002115 *
2116 * At end of parsing, we may perform a capture of the error (if any), and
2117 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2118 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2119 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002120 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002121
Willy Tarreau59234e92008-11-30 23:51:27 +01002122 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002123 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002124 struct http_txn *txn = &s->txn;
2125 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002126 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002127
Willy Tarreau6bf17362009-02-24 10:48:35 +01002128 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2129 now_ms, __FUNCTION__,
2130 s,
2131 req,
2132 req->rex, req->wex,
2133 req->flags,
2134 req->l,
2135 req->analysers);
2136
Willy Tarreau52a0c602009-08-16 22:45:38 +02002137 /* we're speaking HTTP here, so let's speak HTTP to the client */
2138 s->srv_error = http_return_srv_error;
2139
Willy Tarreau83e3af02009-12-28 17:39:57 +01002140 /* There's a protected area at the end of the buffer for rewriting
2141 * purposes. We don't want to start to parse the request if the
2142 * protected area is affected, because we may have to move processed
2143 * data later, which is much more complicated.
2144 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002145 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002146 if ((txn->flags & TX_NOT_FIRST) &&
2147 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002148 req->r < req->lr ||
2149 req->r > req->data + req->size - global.tune.maxrewrite)) {
2150 if (req->send_max) {
2151 /* some data has still not left the buffer, wake us once that's done */
2152 buffer_dont_connect(req);
2153 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2154 return 0;
2155 }
2156 if (req->l <= req->size - global.tune.maxrewrite)
2157 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002158 }
2159
Willy Tarreau065e8332010-01-08 00:30:20 +01002160 /* Note that we have the same problem with the response ; we
2161 * may want to send a redirect, error or anything which requires
2162 * some spare space. So we'll ensure that we have at least
2163 * maxrewrite bytes available in the response buffer before
2164 * processing that one. This will only affect pipelined
2165 * keep-alive requests.
2166 */
2167 if ((txn->flags & TX_NOT_FIRST) &&
2168 unlikely((s->rep->flags & BF_FULL) ||
2169 s->rep->r < s->rep->lr ||
2170 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2171 if (s->rep->send_max) {
2172 /* don't let a connection request be initiated */
2173 buffer_dont_connect(req);
2174 return 0;
2175 }
2176 }
2177
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002178 if (likely(req->lr < req->r))
2179 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002180 }
2181
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 /* 1: we might have to print this header in debug mode */
2183 if (unlikely((global.mode & MODE_DEBUG) &&
2184 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002185 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002187
Willy Tarreau962c3f42010-01-10 00:15:35 +01002188 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 eol = sol + msg->sl.rq.l;
2190 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002191
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 sol += hdr_idx_first_pos(&txn->hdr_idx);
2193 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002194
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 while (cur_idx) {
2196 eol = sol + txn->hdr_idx.v[cur_idx].len;
2197 debug_hdr("clihdr", s, sol, eol);
2198 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2199 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002200 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002201 }
2202
Willy Tarreau58f10d72006-12-04 02:26:12 +01002203
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 /*
2205 * Now we quickly check if we have found a full valid request.
2206 * If not so, we check the FD and buffer states before leaving.
2207 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002208 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002209 * requests are checked first.
2210 *
2211 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002212
Willy Tarreau655dce92009-11-08 13:10:58 +01002213 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002214 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002216 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002217 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2218 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002219
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 /* 1: Since we are in header mode, if there's no space
2221 * left for headers, we won't be able to free more
2222 * later, so the session will never terminate. We
2223 * must terminate it now.
2224 */
2225 if (unlikely(req->flags & BF_FULL)) {
2226 /* FIXME: check if URI is set and return Status
2227 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002228 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002229 goto return_bad_req;
2230 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002231
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 /* 2: have we encountered a read error ? */
2233 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002234 if (txn->flags & TX_NOT_FIRST)
2235 goto failed_keep_alive;
2236
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002238 if (msg->err_pos >= 0)
2239 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002240 msg->msg_state = HTTP_MSG_ERROR;
2241 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002242
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002243 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002244 if (s->listener->counters)
2245 s->listener->counters->failed_req++;
2246
Willy Tarreau59234e92008-11-30 23:51:27 +01002247 if (!(s->flags & SN_ERR_MASK))
2248 s->flags |= SN_ERR_CLICL;
2249 if (!(s->flags & SN_FINST_MASK))
2250 s->flags |= SN_FINST_R;
2251 return 0;
2252 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002253
Willy Tarreau59234e92008-11-30 23:51:27 +01002254 /* 3: has the read timeout expired ? */
2255 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002256 if (txn->flags & TX_NOT_FIRST)
2257 goto failed_keep_alive;
2258
Willy Tarreau59234e92008-11-30 23:51:27 +01002259 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002260 if (msg->err_pos >= 0)
2261 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 txn->status = 408;
2263 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2264 msg->msg_state = HTTP_MSG_ERROR;
2265 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002266
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002267 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002268 if (s->listener->counters)
2269 s->listener->counters->failed_req++;
2270
Willy Tarreau59234e92008-11-30 23:51:27 +01002271 if (!(s->flags & SN_ERR_MASK))
2272 s->flags |= SN_ERR_CLITO;
2273 if (!(s->flags & SN_FINST_MASK))
2274 s->flags |= SN_FINST_R;
2275 return 0;
2276 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002277
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 /* 4: have we encountered a close ? */
2279 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002280 if (txn->flags & TX_NOT_FIRST)
2281 goto failed_keep_alive;
2282
Willy Tarreau4076a152009-04-02 15:18:36 +02002283 if (msg->err_pos >= 0)
2284 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 txn->status = 400;
2286 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2287 msg->msg_state = HTTP_MSG_ERROR;
2288 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002289
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002290 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002291 if (s->listener->counters)
2292 s->listener->counters->failed_req++;
2293
Willy Tarreau59234e92008-11-30 23:51:27 +01002294 if (!(s->flags & SN_ERR_MASK))
2295 s->flags |= SN_ERR_CLICL;
2296 if (!(s->flags & SN_FINST_MASK))
2297 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002298 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002299 }
2300
Willy Tarreau520d95e2009-09-19 21:04:57 +02002301 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002302 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2303
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 /* just set the request timeout once at the beginning of the request */
2305 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002306 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002307
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 /* we're not ready yet */
2309 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002310
2311 failed_keep_alive:
2312 /* Here we process low-level errors for keep-alive requests. In
2313 * short, if the request is not the first one and it experiences
2314 * a timeout, read error or shutdown, we just silently close so
2315 * that the client can try again.
2316 */
2317 txn->status = 0;
2318 msg->msg_state = HTTP_MSG_RQBEFORE;
2319 req->analysers = 0;
2320 s->logs.logwait = 0;
2321 stream_int_cond_close(req->prod, NULL);
2322 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002324
Willy Tarreaud787e662009-07-07 10:14:51 +02002325 /* OK now we have a complete HTTP request with indexed headers. Let's
2326 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002327 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2328 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2329 * points to the CRLF of the request line. req->lr points to the first
2330 * byte after the last LF. msg->col and msg->sov point to the first
2331 * byte of data. msg->eol cannot be trusted because it may have been
2332 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002333 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002334
Willy Tarreaud787e662009-07-07 10:14:51 +02002335 /* Maybe we found in invalid header name while we were configured not
2336 * to block on that, so we have to capture it now.
2337 */
2338 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002339 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 /*
2342 * 1: identify the method
2343 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002344 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002345
2346 /* we can make use of server redirect on GET and HEAD */
2347 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2348 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002349
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 /*
2351 * 2: check if the URI matches the monitor_uri.
2352 * We have to do this for every request which gets in, because
2353 * the monitor-uri is defined by the frontend.
2354 */
2355 if (unlikely((s->fe->monitor_uri_len != 0) &&
2356 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002357 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 s->fe->monitor_uri,
2359 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002360 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002362 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002368 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2369 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002370
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 ret = acl_pass(ret);
2372 if (cond->pol == ACL_COND_UNLESS)
2373 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002374
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 if (ret) {
2376 /* we fail this request, let's return 503 service unavail */
2377 txn->status = 503;
2378 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2379 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002380 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002381 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002382
Willy Tarreau59234e92008-11-30 23:51:27 +01002383 /* nothing to fail, let's reply normaly */
2384 txn->status = 200;
2385 stream_int_retnclose(req->prod, &http_200_chunk);
2386 goto return_prx_cond;
2387 }
2388
2389 /*
2390 * 3: Maybe we have to copy the original REQURI for the logs ?
2391 * Note: we cannot log anymore if the request has been
2392 * classified as invalid.
2393 */
2394 if (unlikely(s->logs.logwait & LW_REQ)) {
2395 /* we have a complete HTTP request that we must log */
2396 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2397 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002398
Willy Tarreau59234e92008-11-30 23:51:27 +01002399 if (urilen >= REQURI_LEN)
2400 urilen = REQURI_LEN - 1;
2401 memcpy(txn->uri, &req->data[msg->som], urilen);
2402 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002403
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 if (!(s->logs.logwait &= ~LW_REQ))
2405 s->do_log(s);
2406 } else {
2407 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002408 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 }
Willy Tarreau06619262006-12-17 08:37:22 +01002410
Willy Tarreau59234e92008-11-30 23:51:27 +01002411 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002412 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2413 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002414
Willy Tarreau5b154472009-12-21 20:11:07 +01002415 /* ... and check if the request is HTTP/1.1 or above */
2416 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002417 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2418 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2419 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002420 txn->flags |= TX_REQ_VER_11;
2421
2422 /* "connection" has not been parsed yet */
2423 txn->flags &= ~TX_CON_HDR_PARS;
2424
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002425 /* transfer length unknown*/
2426 txn->flags &= ~TX_REQ_XFER_LEN;
2427
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 /* 5: we may need to capture headers */
2429 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002430 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002432
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002433 /* 6: determine the transfer-length.
2434 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2435 * the presence of a message-body in a REQUEST and its transfer length
2436 * must be determined that way (in order of precedence) :
2437 * 1. The presence of a message-body in a request is signaled by the
2438 * inclusion of a Content-Length or Transfer-Encoding header field
2439 * in the request's header fields. When a request message contains
2440 * both a message-body of non-zero length and a method that does
2441 * not define any semantics for that request message-body, then an
2442 * origin server SHOULD either ignore the message-body or respond
2443 * with an appropriate error message (e.g., 413). A proxy or
2444 * gateway, when presented the same request, SHOULD either forward
2445 * the request inbound with the message- body or ignore the
2446 * message-body when determining a response.
2447 *
2448 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2449 * and the "chunked" transfer-coding (Section 6.2) is used, the
2450 * transfer-length is defined by the use of this transfer-coding.
2451 * If a Transfer-Encoding header field is present and the "chunked"
2452 * transfer-coding is not present, the transfer-length is defined
2453 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002454 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002455 * 3. If a Content-Length header field is present, its decimal value in
2456 * OCTETs represents both the entity-length and the transfer-length.
2457 * If a message is received with both a Transfer-Encoding header
2458 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002459 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002460 * 4. By the server closing the connection. (Closing the connection
2461 * cannot be used to indicate the end of a request body, since that
2462 * would leave no possibility for the server to send back a response.)
2463 *
2464 * Whenever a transfer-coding is applied to a message-body, the set of
2465 * transfer-codings MUST include "chunked", unless the message indicates
2466 * it is terminated by closing the connection. When the "chunked"
2467 * transfer-coding is used, it MUST be the last transfer-coding applied
2468 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002469 */
2470
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002471 /* CONNECT sets a tunnel and ignores everything else */
2472 if (txn->meth == HTTP_METH_CONNECT)
2473 goto skip_xfer_len;
2474
2475 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002478 while ((txn->flags & TX_REQ_VER_11) &&
2479 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002480 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2481 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2482 else if (txn->flags & TX_REQ_TE_CHNK) {
2483 /* bad transfer-encoding (chunked followed by something else) */
2484 use_close_only = 1;
2485 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2486 break;
2487 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002488 }
2489
Willy Tarreau32b47f42009-10-18 20:55:02 +02002490 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002491 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002492 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2493 signed long long cl;
2494
2495 if (!ctx.vlen)
2496 goto return_bad_req;
2497
2498 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2499 goto return_bad_req; /* parse failure */
2500
2501 if (cl < 0)
2502 goto return_bad_req;
2503
2504 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2505 goto return_bad_req; /* already specified, was different */
2506
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002507 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002508 msg->hdr_content_len = cl;
2509 }
2510
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002511 /* bodyless requests have a known length */
2512 if (!use_close_only)
2513 txn->flags |= TX_REQ_XFER_LEN;
2514
2515 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002516 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002517 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002518 req->analyse_exp = TICK_ETERNITY;
2519 return 1;
2520
2521 return_bad_req:
2522 /* We centralize bad requests processing here */
2523 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2524 /* we detected a parsing error. We want to archive this request
2525 * in the dedicated proxy area for later troubleshooting.
2526 */
2527 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2528 }
2529
2530 txn->req.msg_state = HTTP_MSG_ERROR;
2531 txn->status = 400;
2532 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002533
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002534 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002535 if (s->listener->counters)
2536 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002537
2538 return_prx_cond:
2539 if (!(s->flags & SN_ERR_MASK))
2540 s->flags |= SN_ERR_PRXCOND;
2541 if (!(s->flags & SN_FINST_MASK))
2542 s->flags |= SN_FINST_R;
2543
2544 req->analysers = 0;
2545 req->analyse_exp = TICK_ETERNITY;
2546 return 0;
2547}
2548
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002549/* This stream analyser runs all HTTP request processing which is common to
2550 * frontends and backends, which means blocking ACLs, filters, connection-close,
2551 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002552 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002553 * either needs more data or wants to immediately abort the request (eg: deny,
2554 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002555 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002556int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002557{
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 struct http_txn *txn = &s->txn;
2559 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002560 struct acl_cond *cond;
2561 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002562 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002563 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002564
Willy Tarreau655dce92009-11-08 13:10:58 +01002565 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002566 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002567 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002568 return 0;
2569 }
2570
Willy Tarreau3a816292009-07-07 10:55:49 +02002571 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002572 req->analyse_exp = TICK_ETERNITY;
2573
2574 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2575 now_ms, __FUNCTION__,
2576 s,
2577 req,
2578 req->rex, req->wex,
2579 req->flags,
2580 req->l,
2581 req->analysers);
2582
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002583 /* first check whether we have some ACLs set to block this request */
2584 list_for_each_entry(cond, &px->block_cond, list) {
2585 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002586
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002587 ret = acl_pass(ret);
2588 if (cond->pol == ACL_COND_UNLESS)
2589 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002590
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002591 if (ret) {
2592 txn->status = 403;
2593 /* let's log the request time */
2594 s->logs.tv_request = now;
2595 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2596 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002598 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002599
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002600 /* try headers filters */
2601 if (px->req_exp != NULL) {
2602 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2603 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002604
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* has the request been denied ? */
2606 if (txn->flags & TX_CLDENY) {
2607 /* no need to go further */
2608 txn->status = 403;
2609 /* let's log the request time */
2610 s->logs.tv_request = now;
2611 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2612 goto return_prx_cond;
2613 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002614
2615 /* When a connection is tarpitted, we use the tarpit timeout,
2616 * which may be the same as the connect timeout if unspecified.
2617 * If unset, then set it to zero because we really want it to
2618 * eventually expire. We build the tarpit as an analyser.
2619 */
2620 if (txn->flags & TX_CLTARPIT) {
2621 buffer_erase(s->req);
2622 /* wipe the request out so that we can drop the connection early
2623 * if the client closes first.
2624 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002625 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002626 req->analysers = 0; /* remove switching rules etc... */
2627 req->analysers |= AN_REQ_HTTP_TARPIT;
2628 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2629 if (!req->analyse_exp)
2630 req->analyse_exp = tick_add(now_ms, 0);
2631 return 1;
2632 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002633 }
Willy Tarreau06619262006-12-17 08:37:22 +01002634
Willy Tarreau5b154472009-12-21 20:11:07 +01002635 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2636 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002637 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2638 * in which headers are mangled. However, if another mode is set, it will
2639 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002640 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002641
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002642 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002643 ((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 +01002644 int tmp = TX_CON_WANT_TUN;
2645 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2646 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002647 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2648 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002649 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002650 tmp = TX_CON_WANT_CLO;
2651
Willy Tarreau5b154472009-12-21 20:11:07 +01002652 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2653 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002654
Willy Tarreau8db1c172010-01-05 23:12:12 +01002655 if (!(txn->flags & TX_CON_HDR_PARS))
2656 http_req_parse_connection_header(txn);
2657
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002658 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2659 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2660 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2661 if (!(txn->flags & TX_REQ_XFER_LEN))
2662 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2663 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002664 }
2665
2666 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2667 * once we know the backend, because the tunnel mode can be implied by the
2668 * lack of any close/keepalive options in both the FE and the BE. Since
2669 * this information can evolve with time, we proceed by trying to make the
2670 * header status match the desired status. For this, we'll have to adjust
2671 * the "Connection" header. The test for persistent connections has already
2672 * been performed, so we only enter here if there is a risk the connection
2673 * is considered as persistent and we want it to be closed on the server
2674 * side. It would be nice if we could enter this place only when a
2675 * Connection header exists. Note that a CONNECT method will not enter
2676 * here.
2677 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002678 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2679 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2680 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002681 char *cur_ptr, *cur_end, *cur_next;
2682 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002683 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002684 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002685
Willy Tarreau5b154472009-12-21 20:11:07 +01002686 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002687 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002688
Willy Tarreau5b154472009-12-21 20:11:07 +01002689 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002690 cur_hdr = &txn->hdr_idx.v[cur_idx];
2691 cur_ptr = cur_next;
2692 cur_end = cur_ptr + cur_hdr->len;
2693 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002694
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002695 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002696 if (!val)
2697 continue;
2698
2699 /* 3 possibilities :
2700 * - we have already set "Connection: close" or we're in
2701 * HTTP/1.0, so we remove this line.
2702 * - we have not yet set "Connection: close", but this line
2703 * indicates close. We leave it untouched and set the flag.
2704 * - we have not yet set "Connection: close", and this line
2705 * indicates non-close. We replace it and set the flag.
2706 */
2707 if (must_delete) {
2708 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2709 http_msg_move_end(&txn->req, delta);
2710 cur_next += delta;
2711 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2712 txn->hdr_idx.used--;
2713 cur_hdr->len = 0;
2714 txn->flags |= TX_REQ_CONN_CLO;
2715 } else {
2716 if (cur_end - cur_ptr - val != 5 ||
2717 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2718 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2719 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002720 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002721 cur_hdr->len += delta;
2722 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002723 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002724 txn->flags |= TX_REQ_CONN_CLO;
2725 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002726 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002727 } /* for loop */
2728 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002729
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002730 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002731 list_for_each_entry(wl, &px->req_add, list) {
2732 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 goto return_bad_req;
2734 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002735
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002736 /* check if stats URI was requested, and if an auth is needed */
2737 if (px->uri_auth != NULL &&
2738 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2739 /* we have to check the URI and auth for this request.
2740 * FIXME!!! that one is rather dangerous, we want to
2741 * make it follow standard rules (eg: clear req->analysers).
2742 */
2743 if (stats_check_uri_auth(s, px)) {
2744 req->analysers = 0;
2745 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002746 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002748
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002749 /* check whether we have some ACLs set to redirect this request */
2750 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002751 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002752
Willy Tarreauf285f542010-01-03 20:03:03 +01002753 if (rule->cond) {
2754 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2755 ret = acl_pass(ret);
2756 if (rule->cond->pol == ACL_COND_UNLESS)
2757 ret = !ret;
2758 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002759
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002761 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002762 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002763
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002764 /* build redirect message */
2765 switch(rule->code) {
2766 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 msg_fmt = HTTP_303;
2768 break;
2769 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770 msg_fmt = HTTP_301;
2771 break;
2772 case 302:
2773 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002774 msg_fmt = HTTP_302;
2775 break;
2776 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002777
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002778 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002779 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002780
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002781 switch(rule->type) {
2782 case REDIRECT_TYPE_PREFIX: {
2783 const char *path;
2784 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002785
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 path = http_get_path(txn);
2787 /* build message using path */
2788 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002789 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002790 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2791 int qs = 0;
2792 while (qs < pathlen) {
2793 if (path[qs] == '?') {
2794 pathlen = qs;
2795 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002796 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002797 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002798 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002799 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002800 } else {
2801 path = "/";
2802 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002803 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002804
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002805 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002806 goto return_bad_req;
2807
2808 /* add prefix. Note that if prefix == "/", we don't want to
2809 * add anything, otherwise it makes it hard for the user to
2810 * configure a self-redirection.
2811 */
2812 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002813 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2814 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002815 }
2816
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002817 /* add path */
2818 memcpy(rdr.str + rdr.len, path, pathlen);
2819 rdr.len += pathlen;
2820 break;
2821 }
2822 case REDIRECT_TYPE_LOCATION:
2823 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002824 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002825 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002826
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002827 /* add location */
2828 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2829 rdr.len += rule->rdr_len;
2830 break;
2831 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 if (rule->cookie_len) {
2834 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2835 rdr.len += 14;
2836 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2837 rdr.len += rule->cookie_len;
2838 memcpy(rdr.str + rdr.len, "\r\n", 2);
2839 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002840 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002841
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002842 /* add end of headers and the keep-alive/close status.
2843 * We may choose to set keep-alive if the Location begins
2844 * with a slash, because the client will come back to the
2845 * same server.
2846 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002847 txn->status = rule->code;
2848 /* let's log the request time */
2849 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002850
2851 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2852 (txn->flags & TX_REQ_XFER_LEN) &&
2853 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2854 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2855 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2856 /* keep-alive possible */
2857 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2858 rdr.len += 28;
2859 buffer_write(req->prod->ob, rdr.str, rdr.len);
2860 /* "eat" the request */
2861 buffer_ignore(req, msg->sov - msg->som);
2862 msg->som = msg->sov;
2863 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002864 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2865 txn->req.msg_state = HTTP_MSG_CLOSED;
2866 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002867 break;
2868 } else {
2869 /* keep-alive not possible */
2870 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2871 rdr.len += 23;
Willy Tarreauea65e682010-01-08 00:26:50 +01002872 buffer_write(req->prod->ob, rdr.str, rdr.len);
2873 stream_int_cond_close(req->prod, NULL);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002874 goto return_prx_cond;
2875 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002876 }
2877 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002878
Willy Tarreau2be39392010-01-03 17:24:51 +01002879 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2880 * If this happens, then the data will not come immediately, so we must
2881 * send all what we have without waiting. Note that due to the small gain
2882 * in waiting for the body of the request, it's easier to simply put the
2883 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2884 * itself once used.
2885 */
2886 req->flags |= BF_SEND_DONTWAIT;
2887
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002888 /* that's OK for us now, let's move on to next analysers */
2889 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002890
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002891 return_bad_req:
2892 /* We centralize bad requests processing here */
2893 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2894 /* we detected a parsing error. We want to archive this request
2895 * in the dedicated proxy area for later troubleshooting.
2896 */
2897 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2898 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002899
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002900 txn->req.msg_state = HTTP_MSG_ERROR;
2901 txn->status = 400;
2902 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002903
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002904 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002905 if (s->listener->counters)
2906 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002907
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002908 return_prx_cond:
2909 if (!(s->flags & SN_ERR_MASK))
2910 s->flags |= SN_ERR_PRXCOND;
2911 if (!(s->flags & SN_FINST_MASK))
2912 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002914 req->analysers = 0;
2915 req->analyse_exp = TICK_ETERNITY;
2916 return 0;
2917}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002918
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002919/* This function performs all the processing enabled for the current request.
2920 * It returns 1 if the processing can continue on next analysers, or zero if it
2921 * needs more data, encounters an error, or wants to immediately abort the
2922 * request. It relies on buffers flags, and updates s->req->analysers.
2923 */
2924int http_process_request(struct session *s, struct buffer *req, int an_bit)
2925{
2926 struct http_txn *txn = &s->txn;
2927 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002928
Willy Tarreau655dce92009-11-08 13:10:58 +01002929 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002930 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002931 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002932 return 0;
2933 }
2934
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002935 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2936 now_ms, __FUNCTION__,
2937 s,
2938 req,
2939 req->rex, req->wex,
2940 req->flags,
2941 req->l,
2942 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002943
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 /*
2945 * Right now, we know that we have processed the entire headers
2946 * and that unwanted requests have been filtered out. We can do
2947 * whatever we want with the remaining request. Also, now we
2948 * may have separate values for ->fe, ->be.
2949 */
Willy Tarreau06619262006-12-17 08:37:22 +01002950
Willy Tarreau59234e92008-11-30 23:51:27 +01002951 /*
2952 * If HTTP PROXY is set we simply get remote server address
2953 * parsing incoming request.
2954 */
2955 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002956 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01002957 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002958
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002960 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002961 * Note that doing so might move headers in the request, but
2962 * the fields will stay coherent and the URI will not move.
2963 * This should only be performed in the backend.
2964 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002965 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002966 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2967 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002968
Willy Tarreau59234e92008-11-30 23:51:27 +01002969 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002970 * 8: the appsession cookie was looked up very early in 1.2,
2971 * so let's do the same now.
2972 */
2973
2974 /* It needs to look into the URI */
2975 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002976 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002977 }
2978
2979 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002980 * 9: add X-Forwarded-For if either the frontend or the backend
2981 * asks for it.
2982 */
2983 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2984 if (s->cli_addr.ss_family == AF_INET) {
2985 /* Add an X-Forwarded-For header unless the source IP is
2986 * in the 'except' network range.
2987 */
2988 if ((!s->fe->except_mask.s_addr ||
2989 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2990 != s->fe->except_net.s_addr) &&
2991 (!s->be->except_mask.s_addr ||
2992 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2993 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002994 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002995 unsigned char *pn;
2996 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002997
2998 /* Note: we rely on the backend to get the header name to be used for
2999 * x-forwarded-for, because the header is really meant for the backends.
3000 * However, if the backend did not specify any option, we have to rely
3001 * on the frontend's header name.
3002 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 if (s->be->fwdfor_hdr_len) {
3004 len = s->be->fwdfor_hdr_len;
3005 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003006 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003007 len = s->fe->fwdfor_hdr_len;
3008 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003009 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003010 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003011
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003012 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003013 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003014 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003015 }
3016 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003017 else if (s->cli_addr.ss_family == AF_INET6) {
3018 /* FIXME: for the sake of completeness, we should also support
3019 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003020 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003021 int len;
3022 char pn[INET6_ADDRSTRLEN];
3023 inet_ntop(AF_INET6,
3024 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3025 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003026
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 /* Note: we rely on the backend to get the header name to be used for
3028 * x-forwarded-for, because the header is really meant for the backends.
3029 * However, if the backend did not specify any option, we have to rely
3030 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003031 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 if (s->be->fwdfor_hdr_len) {
3033 len = s->be->fwdfor_hdr_len;
3034 memcpy(trash, s->be->fwdfor_hdr_name, len);
3035 } else {
3036 len = s->fe->fwdfor_hdr_len;
3037 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003038 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003039 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003040
Willy Tarreau59234e92008-11-30 23:51:27 +01003041 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003042 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003043 goto return_bad_req;
3044 }
3045 }
3046
3047 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003048 * 10: add X-Original-To if either the frontend or the backend
3049 * asks for it.
3050 */
3051 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3052
3053 /* FIXME: don't know if IPv6 can handle that case too. */
3054 if (s->cli_addr.ss_family == AF_INET) {
3055 /* Add an X-Original-To header unless the destination IP is
3056 * in the 'except' network range.
3057 */
3058 if (!(s->flags & SN_FRT_ADDR_SET))
3059 get_frt_addr(s);
3060
3061 if ((!s->fe->except_mask_to.s_addr ||
3062 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3063 != s->fe->except_to.s_addr) &&
3064 (!s->be->except_mask_to.s_addr ||
3065 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3066 != s->be->except_to.s_addr)) {
3067 int len;
3068 unsigned char *pn;
3069 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3070
3071 /* Note: we rely on the backend to get the header name to be used for
3072 * x-original-to, because the header is really meant for the backends.
3073 * However, if the backend did not specify any option, we have to rely
3074 * on the frontend's header name.
3075 */
3076 if (s->be->orgto_hdr_len) {
3077 len = s->be->orgto_hdr_len;
3078 memcpy(trash, s->be->orgto_hdr_name, len);
3079 } else {
3080 len = s->fe->orgto_hdr_len;
3081 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003082 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003083 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3084
3085 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003086 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003087 goto return_bad_req;
3088 }
3089 }
3090 }
3091
Willy Tarreau78599912009-10-17 20:12:21 +02003092 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003093 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3094 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3095 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003096 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003097 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003098 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003099 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003100 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003101
3102 /* If we have no server assigned yet and we're balancing on url_param
3103 * with a POST request, we may be interested in checking the body for
3104 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003105 */
3106 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3107 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003108 s->be->url_param_post_limit != 0 &&
3109 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003110 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003111 buffer_dont_connect(req);
3112 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003113 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003114
Willy Tarreaud98cf932009-12-27 22:54:55 +01003115 if (txn->flags & TX_REQ_XFER_LEN)
3116 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003117
Willy Tarreau59234e92008-11-30 23:51:27 +01003118 /*************************************************************
3119 * OK, that's finished for the headers. We have done what we *
3120 * could. Let's switch to the DATA state. *
3121 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003122 req->analyse_exp = TICK_ETERNITY;
3123 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003124
Willy Tarreau59234e92008-11-30 23:51:27 +01003125 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003126 /* OK let's go on with the BODY now */
3127 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003128
Willy Tarreau59234e92008-11-30 23:51:27 +01003129 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003130 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003131 /* we detected a parsing error. We want to archive this request
3132 * in the dedicated proxy area for later troubleshooting.
3133 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003134 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003135 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003136
Willy Tarreau59234e92008-11-30 23:51:27 +01003137 txn->req.msg_state = HTTP_MSG_ERROR;
3138 txn->status = 400;
3139 req->analysers = 0;
3140 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003141
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003142 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003143 if (s->listener->counters)
3144 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003145
Willy Tarreau59234e92008-11-30 23:51:27 +01003146 if (!(s->flags & SN_ERR_MASK))
3147 s->flags |= SN_ERR_PRXCOND;
3148 if (!(s->flags & SN_FINST_MASK))
3149 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003150 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003151}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003152
Willy Tarreau60b85b02008-11-30 23:28:40 +01003153/* This function is an analyser which processes the HTTP tarpit. It always
3154 * returns zero, at the beginning because it prevents any other processing
3155 * from occurring, and at the end because it terminates the request.
3156 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003157int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003158{
3159 struct http_txn *txn = &s->txn;
3160
3161 /* This connection is being tarpitted. The CLIENT side has
3162 * already set the connect expiration date to the right
3163 * timeout. We just have to check that the client is still
3164 * there and that the timeout has not expired.
3165 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003166 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003167 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3168 !tick_is_expired(req->analyse_exp, now_ms))
3169 return 0;
3170
3171 /* We will set the queue timer to the time spent, just for
3172 * logging purposes. We fake a 500 server error, so that the
3173 * attacker will not suspect his connection has been tarpitted.
3174 * It will not cause trouble to the logs because we can exclude
3175 * the tarpitted connections by filtering on the 'PT' status flags.
3176 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003177 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3178
3179 txn->status = 500;
3180 if (req->flags != BF_READ_ERROR)
3181 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3182
3183 req->analysers = 0;
3184 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003185
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003186 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003187 if (s->listener->counters)
3188 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003189
Willy Tarreau60b85b02008-11-30 23:28:40 +01003190 if (!(s->flags & SN_ERR_MASK))
3191 s->flags |= SN_ERR_PRXCOND;
3192 if (!(s->flags & SN_FINST_MASK))
3193 s->flags |= SN_FINST_T;
3194 return 0;
3195}
3196
Willy Tarreaud34af782008-11-30 23:36:37 +01003197/* This function is an analyser which processes the HTTP request body. It looks
3198 * for parameters to be used for the load balancing algorithm (url_param). It
3199 * must only be called after the standard HTTP request processing has occurred,
3200 * because it expects the request to be parsed. It returns zero if it needs to
3201 * read more data, or 1 once it has completed its analysis.
3202 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003203int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003204{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003205 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003206 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003207 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003208
3209 /* We have to parse the HTTP request body to find any required data.
3210 * "balance url_param check_post" should have been the only way to get
3211 * into this. We were brought here after HTTP header analysis, so all
3212 * related structures are ready.
3213 */
3214
Willy Tarreau522d6c02009-12-06 18:49:18 +01003215 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3216 goto missing_data;
3217
3218 if (msg->msg_state < HTTP_MSG_100_SENT) {
3219 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3220 * send an HTTP/1.1 100 Continue intermediate response.
3221 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003222 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003223 struct hdr_ctx ctx;
3224 ctx.idx = 0;
3225 /* Expect is allowed in 1.1, look for it */
3226 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3227 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3228 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3229 }
3230 }
3231 msg->msg_state = HTTP_MSG_100_SENT;
3232 }
3233
3234 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003235 /* we have msg->col and msg->sov which both point to the first
3236 * byte of message body. msg->som still points to the beginning
3237 * of the message. We must save the body in req->lr because it
3238 * survives buffer re-alignments.
3239 */
3240 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003241 if (txn->flags & TX_REQ_TE_CHNK)
3242 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3243 else
3244 msg->msg_state = HTTP_MSG_DATA;
3245 }
3246
3247 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003248 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003249 * set ->sov and ->lr to point to the body and switch to DATA or
3250 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003251 */
3252 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003253
Willy Tarreau115acb92009-12-26 13:56:06 +01003254 if (!ret)
3255 goto missing_data;
3256 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003257 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003258 }
3259
Willy Tarreaud98cf932009-12-27 22:54:55 +01003260 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003261 * We have the first non-header byte in msg->col, which is either the
3262 * beginning of the chunk size or of the data. The first data byte is in
3263 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3264 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003265 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003266
3267 if (msg->hdr_content_len < limit)
3268 limit = msg->hdr_content_len;
3269
Willy Tarreau7c96f672009-12-27 22:47:25 +01003270 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003271 goto http_end;
3272
3273 missing_data:
3274 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003275 if (req->flags & BF_FULL)
3276 goto return_bad_req;
3277
Willy Tarreau522d6c02009-12-06 18:49:18 +01003278 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3279 txn->status = 408;
3280 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3281 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003282 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003283
3284 /* we get here if we need to wait for more data */
3285 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003286 /* Not enough data. We'll re-use the http-request
3287 * timeout here. Ideally, we should set the timeout
3288 * relative to the accept() date. We just set the
3289 * request timeout once at the beginning of the
3290 * request.
3291 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003292 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003293 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003294 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003295 return 0;
3296 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003297
3298 http_end:
3299 /* The situation will not evolve, so let's give up on the analysis. */
3300 s->logs.tv_request = now; /* update the request timer to reflect full request */
3301 req->analysers &= ~an_bit;
3302 req->analyse_exp = TICK_ETERNITY;
3303 return 1;
3304
3305 return_bad_req: /* let's centralize all bad requests */
3306 txn->req.msg_state = HTTP_MSG_ERROR;
3307 txn->status = 400;
3308 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3309
3310 return_err_msg:
3311 req->analysers = 0;
3312 s->fe->counters.failed_req++;
3313 if (s->listener->counters)
3314 s->listener->counters->failed_req++;
3315
3316 if (!(s->flags & SN_ERR_MASK))
3317 s->flags |= SN_ERR_PRXCOND;
3318 if (!(s->flags & SN_FINST_MASK))
3319 s->flags |= SN_FINST_R;
3320 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003321}
3322
Willy Tarreau610ecce2010-01-04 21:15:02 +01003323/* Terminate current transaction and prepare a new one. This is very tricky
3324 * right now but it works.
3325 */
3326void http_end_txn_clean_session(struct session *s)
3327{
3328 /* FIXME: We need a more portable way of releasing a backend's and a
3329 * server's connections. We need a safer way to reinitialize buffer
3330 * flags. We also need a more accurate method for computing per-request
3331 * data.
3332 */
3333 http_silent_debug(__LINE__, s);
3334
3335 s->req->cons->flags |= SI_FL_NOLINGER;
3336 s->req->cons->shutr(s->req->cons);
3337 s->req->cons->shutw(s->req->cons);
3338
3339 http_silent_debug(__LINE__, s);
3340
3341 if (s->flags & SN_BE_ASSIGNED)
3342 s->be->beconn--;
3343
3344 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3345 session_process_counters(s);
3346
3347 if (s->txn.status) {
3348 int n;
3349
3350 n = s->txn.status / 100;
3351 if (n < 1 || n > 5)
3352 n = 0;
3353
3354 if (s->fe->mode == PR_MODE_HTTP)
3355 s->fe->counters.p.http.rsp[n]++;
3356
3357 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3358 (s->be->mode == PR_MODE_HTTP))
3359 s->be->counters.p.http.rsp[n]++;
3360 }
3361
3362 /* don't count other requests' data */
3363 s->logs.bytes_in -= s->req->l - s->req->send_max;
3364 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3365
3366 /* let's do a final log if we need it */
3367 if (s->logs.logwait &&
3368 !(s->flags & SN_MONITOR) &&
3369 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3370 s->do_log(s);
3371 }
3372
3373 s->logs.accept_date = date; /* user-visible date for logging */
3374 s->logs.tv_accept = now; /* corrected date for internal use */
3375 tv_zero(&s->logs.tv_request);
3376 s->logs.t_queue = -1;
3377 s->logs.t_connect = -1;
3378 s->logs.t_data = -1;
3379 s->logs.t_close = 0;
3380 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3381 s->logs.srv_queue_size = 0; /* we will get this number soon */
3382
3383 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3384 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3385
3386 if (s->pend_pos)
3387 pendconn_free(s->pend_pos);
3388
3389 if (s->srv) {
3390 if (s->flags & SN_CURR_SESS) {
3391 s->flags &= ~SN_CURR_SESS;
3392 s->srv->cur_sess--;
3393 }
3394 if (may_dequeue_tasks(s->srv, s->be))
3395 process_srv_queue(s->srv);
3396 }
3397
3398 if (unlikely(s->srv_conn))
3399 sess_change_server(s, NULL);
3400 s->srv = NULL;
3401
3402 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3403 s->req->cons->fd = -1; /* just to help with debugging */
3404 s->req->cons->err_type = SI_ET_NONE;
3405 s->req->cons->err_loc = NULL;
3406 s->req->cons->exp = TICK_ETERNITY;
3407 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003408 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3409 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 +01003410 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3411 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3412 s->txn.meth = 0;
3413 http_reset_txn(s);
3414 s->txn.flags |= TX_NOT_FIRST;
3415 if (s->be->options2 & PR_O2_INDEPSTR)
3416 s->req->cons->flags |= SI_FL_INDEP_STR;
3417
3418 /* if the request buffer is not empty, it means we're
3419 * about to process another request, so send pending
3420 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003421 * Just don't do this if the buffer is close to be full,
3422 * because the request will wait for it to flush a little
3423 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003424 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003425 if (s->req->l > s->req->send_max) {
3426 if (s->rep->send_max &&
3427 !(s->rep->flags & BF_FULL) &&
3428 s->rep->lr <= s->rep->r &&
3429 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3430 s->rep->flags |= BF_EXPECT_MORE;
3431 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003432
3433 /* we're removing the analysers, we MUST re-enable events detection */
3434 buffer_auto_read(s->req);
3435 buffer_auto_close(s->req);
3436 buffer_auto_read(s->rep);
3437 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003438
3439 /* make ->lr point to the first non-forwarded byte */
3440 s->req->lr = s->req->w + s->req->send_max;
3441 if (s->req->lr >= s->req->data + s->req->size)
3442 s->req->lr -= s->req->size;
3443 s->rep->lr = s->rep->w + s->rep->send_max;
3444 if (s->rep->lr >= s->rep->data + s->rep->size)
3445 s->rep->lr -= s->req->size;
3446
3447 s->req->analysers |= s->fe->fe_req_ana;
3448 s->rep->analysers = 0;
3449
3450 http_silent_debug(__LINE__, s);
3451}
3452
3453
3454/* This function updates the request state machine according to the response
3455 * state machine and buffer flags. It returns 1 if it changes anything (flag
3456 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3457 * it is only used to find when a request/response couple is complete. Both
3458 * this function and its equivalent should loop until both return zero. It
3459 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3460 */
3461int http_sync_req_state(struct session *s)
3462{
3463 struct buffer *buf = s->req;
3464 struct http_txn *txn = &s->txn;
3465 unsigned int old_flags = buf->flags;
3466 unsigned int old_state = txn->req.msg_state;
3467
3468 http_silent_debug(__LINE__, s);
3469 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3470 return 0;
3471
3472 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003473 /* No need to read anymore, the request was completely parsed.
3474 * We can shut the read side unless we want to abort_on_close.
3475 */
3476 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3477 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003478
3479 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3480 goto wait_other_side;
3481
3482 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3483 /* The server has not finished to respond, so we
3484 * don't want to move in order not to upset it.
3485 */
3486 goto wait_other_side;
3487 }
3488
3489 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3490 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003491 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003492 txn->req.msg_state = HTTP_MSG_TUNNEL;
3493 goto wait_other_side;
3494 }
3495
3496 /* When we get here, it means that both the request and the
3497 * response have finished receiving. Depending on the connection
3498 * mode, we'll have to wait for the last bytes to leave in either
3499 * direction, and sometimes for a close to be effective.
3500 */
3501
3502 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3503 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3504 /* Server-close mode : queue a connection close to the server */
3505 buffer_shutw_now(buf);
3506 buf->cons->flags |= SI_FL_NOLINGER;
3507 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003508 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003509 /* Option forceclose is set, let's enforce it now
3510 * that we're not expecting any new data to come.
3511 */
3512 buffer_shutr_now(buf);
3513 buffer_shutw_now(buf);
3514 buf->cons->flags |= SI_FL_NOLINGER;
3515 }
3516 /* other modes include httpclose (no action) and keepalive (not implemented) */
3517 }
3518
3519 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3520 /* if we've just closed an output, let's switch */
3521 if (!(buf->flags & BF_OUT_EMPTY)) {
3522 txn->req.msg_state = HTTP_MSG_CLOSING;
3523 goto http_msg_closing;
3524 }
3525 else {
3526 txn->req.msg_state = HTTP_MSG_CLOSED;
3527 goto http_msg_closed;
3528 }
3529 }
3530 else {
3531 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003532 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003533 txn->req.msg_state = HTTP_MSG_TUNNEL;
3534 goto wait_other_side;
3535 }
3536 }
3537
3538 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3539 http_msg_closing:
3540 /* nothing else to forward, just waiting for the output buffer
3541 * to be empty and for the shutw_now to take effect.
3542 */
3543 if (buf->flags & BF_OUT_EMPTY) {
3544 txn->req.msg_state = HTTP_MSG_CLOSED;
3545 goto http_msg_closed;
3546 }
3547 else if (buf->flags & BF_SHUTW) {
3548 txn->req.msg_state = HTTP_MSG_ERROR;
3549 goto wait_other_side;
3550 }
3551 }
3552
3553 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3554 http_msg_closed:
3555 goto wait_other_side;
3556 }
3557
3558 wait_other_side:
3559 http_silent_debug(__LINE__, s);
3560 return txn->req.msg_state != old_state || buf->flags != old_flags;
3561}
3562
3563
3564/* This function updates the response state machine according to the request
3565 * state machine and buffer flags. It returns 1 if it changes anything (flag
3566 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3567 * it is only used to find when a request/response couple is complete. Both
3568 * this function and its equivalent should loop until both return zero. It
3569 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3570 */
3571int http_sync_res_state(struct session *s)
3572{
3573 struct buffer *buf = s->rep;
3574 struct http_txn *txn = &s->txn;
3575 unsigned int old_flags = buf->flags;
3576 unsigned int old_state = txn->rsp.msg_state;
3577
3578 http_silent_debug(__LINE__, s);
3579 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3580 return 0;
3581
3582 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3583 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003584 * still monitor the server connection for a possible close
3585 * while the request is being uploaded, so we don't disable
3586 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003587 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003588 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003589
3590 if (txn->req.msg_state == HTTP_MSG_ERROR)
3591 goto wait_other_side;
3592
3593 if (txn->req.msg_state < HTTP_MSG_DONE) {
3594 /* The client seems to still be sending data, probably
3595 * because we got an error response during an upload.
3596 * We have the choice of either breaking the connection
3597 * or letting it pass through. Let's do the later.
3598 */
3599 goto wait_other_side;
3600 }
3601
3602 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3603 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003604 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003605 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3606 goto wait_other_side;
3607 }
3608
3609 /* When we get here, it means that both the request and the
3610 * response have finished receiving. Depending on the connection
3611 * mode, we'll have to wait for the last bytes to leave in either
3612 * direction, and sometimes for a close to be effective.
3613 */
3614
3615 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3616 /* Server-close mode : shut read and wait for the request
3617 * side to close its output buffer. The caller will detect
3618 * when we're in DONE and the other is in CLOSED and will
3619 * catch that for the final cleanup.
3620 */
3621 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3622 buffer_shutr_now(buf);
3623 goto wait_other_side;
3624 }
3625 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3626 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3627 /* Option forceclose is set, let's enforce it now
3628 * that we're not expecting any new data to come.
3629 * The caller knows the session is complete once
3630 * both states are CLOSED.
3631 */
3632 buffer_shutr_now(buf);
3633 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003634 }
3635 else {
3636 /* other modes include httpclose (no action) and keepalive
3637 * (not implemented). These modes are used as a tunnel right
3638 * now.
3639 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003640 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003641 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3642 goto wait_other_side;
3643 }
3644
3645 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3646 /* if we've just closed an output, let's switch */
3647 if (!(buf->flags & BF_OUT_EMPTY)) {
3648 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3649 goto http_msg_closing;
3650 }
3651 else {
3652 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3653 goto http_msg_closed;
3654 }
3655 }
3656 goto wait_other_side;
3657 }
3658
3659 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3660 http_msg_closing:
3661 /* nothing else to forward, just waiting for the output buffer
3662 * to be empty and for the shutw_now to take effect.
3663 */
3664 if (buf->flags & BF_OUT_EMPTY) {
3665 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3666 goto http_msg_closed;
3667 }
3668 else if (buf->flags & BF_SHUTW) {
3669 txn->rsp.msg_state = HTTP_MSG_ERROR;
3670 goto wait_other_side;
3671 }
3672 }
3673
3674 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3675 http_msg_closed:
3676 /* drop any pending data */
3677 buffer_ignore(buf, buf->l - buf->send_max);
3678 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003679 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003680 goto wait_other_side;
3681 }
3682
3683 wait_other_side:
3684 http_silent_debug(__LINE__, s);
3685 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3686}
3687
3688
3689/* Resync the request and response state machines. Return 1 if either state
3690 * changes.
3691 */
3692int http_resync_states(struct session *s)
3693{
3694 struct http_txn *txn = &s->txn;
3695 int old_req_state = txn->req.msg_state;
3696 int old_res_state = txn->rsp.msg_state;
3697
3698 http_silent_debug(__LINE__, s);
3699 http_sync_req_state(s);
3700 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003701 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003702 if (!http_sync_res_state(s))
3703 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003704 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705 if (!http_sync_req_state(s))
3706 break;
3707 }
3708 http_silent_debug(__LINE__, s);
3709 /* OK, both state machines agree on a compatible state.
3710 * There are a few cases we're interested in :
3711 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3712 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3713 * directions, so let's simply disable both analysers.
3714 * - HTTP_MSG_CLOSED on the response only means we must abort the
3715 * request.
3716 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3717 * with server-close mode means we've completed one request and we
3718 * must re-initialize the server connection.
3719 */
3720
3721 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3722 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3723 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3724 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3725 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003726 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003727 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003728 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003729 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003730 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003732 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3733 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3734 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003735 s->rep->analysers = 0;
3736 buffer_auto_close(s->rep);
3737 buffer_auto_read(s->rep);
3738 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003739 buffer_abort(s->req);
3740 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003741 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743 }
3744 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3745 txn->rsp.msg_state == HTTP_MSG_DONE &&
3746 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3747 /* server-close: terminate this server connection and
3748 * reinitialize a fresh-new transaction.
3749 */
3750 http_end_txn_clean_session(s);
3751 }
3752
3753 http_silent_debug(__LINE__, s);
3754 return txn->req.msg_state != old_req_state ||
3755 txn->rsp.msg_state != old_res_state;
3756}
3757
Willy Tarreaud98cf932009-12-27 22:54:55 +01003758/* This function is an analyser which forwards request body (including chunk
3759 * sizes if any). It is called as soon as we must forward, even if we forward
3760 * zero byte. The only situation where it must not be called is when we're in
3761 * tunnel mode and we want to forward till the close. It's used both to forward
3762 * remaining data and to resync after end of body. It expects the msg_state to
3763 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3764 * read more data, or 1 once we can go on with next request or end the session.
3765 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3766 * bytes of pending data + the headers if not already done (between som and sov).
3767 * It eventually adjusts som to match sov after the data in between have been sent.
3768 */
3769int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3770{
3771 struct http_txn *txn = &s->txn;
3772 struct http_msg *msg = &s->txn.req;
3773
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003774 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3775 return 0;
3776
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003777 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3778 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3779 /* Output closed while we were sending data. We must abort. */
3780 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003781 buffer_auto_read(req);
3782 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003783 req->analysers &= ~an_bit;
3784 return 1;
3785 }
3786
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003787 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003788
3789 /* Note that we don't have to send 100-continue back because we don't
3790 * need the data to complete our job, and it's up to the server to
3791 * decide whether to return 100, 417 or anything else in return of
3792 * an "Expect: 100-continue" header.
3793 */
3794
3795 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3796 /* we have msg->col and msg->sov which both point to the first
3797 * byte of message body. msg->som still points to the beginning
3798 * of the message. We must save the body in req->lr because it
3799 * survives buffer re-alignments.
3800 */
3801 req->lr = req->data + msg->sov;
3802 if (txn->flags & TX_REQ_TE_CHNK)
3803 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3804 else {
3805 msg->msg_state = HTTP_MSG_DATA;
3806 }
3807 }
3808
Willy Tarreaud98cf932009-12-27 22:54:55 +01003809 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003811 /* we may have some data pending */
3812 if (msg->hdr_content_len || msg->som != msg->sov) {
3813 int bytes = msg->sov - msg->som;
3814 if (bytes < 0) /* sov may have wrapped at the end */
3815 bytes += req->size;
3816 buffer_forward(req, bytes + msg->hdr_content_len);
3817 msg->hdr_content_len = 0; /* don't forward that again */
3818 msg->som = msg->sov;
3819 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003820
Willy Tarreaucaabe412010-01-03 23:08:28 +01003821 if (msg->msg_state == HTTP_MSG_DATA) {
3822 /* must still forward */
3823 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003824 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003825
3826 /* nothing left to forward */
3827 if (txn->flags & TX_REQ_TE_CHNK)
3828 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003830 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003831 }
3832 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003833 /* read the chunk size and assign it to ->hdr_content_len, then
3834 * set ->sov and ->lr to point to the body and switch to DATA or
3835 * TRAILERS state.
3836 */
3837 int ret = http_parse_chunk_size(req, msg);
3838
3839 if (!ret)
3840 goto missing_data;
3841 else if (ret < 0)
3842 goto return_bad_req;
3843 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003844 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003845 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3846 /* we want the CRLF after the data */
3847 int ret;
3848
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003849 req->lr = req->w + req->send_max;
3850 if (req->lr >= req->data + req->size)
3851 req->lr -= req->size;
3852
Willy Tarreaud98cf932009-12-27 22:54:55 +01003853 ret = http_skip_chunk_crlf(req, msg);
3854
3855 if (ret == 0)
3856 goto missing_data;
3857 else if (ret < 0)
3858 goto return_bad_req;
3859 /* we're in MSG_CHUNK_SIZE now */
3860 }
3861 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3862 int ret = http_forward_trailers(req, msg);
3863
3864 if (ret == 0)
3865 goto missing_data;
3866 else if (ret < 0)
3867 goto return_bad_req;
3868 /* we're in HTTP_MSG_DONE now */
3869 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003870 else {
3871 /* other states, DONE...TUNNEL */
3872 if (http_resync_states(s)) {
3873 /* some state changes occurred, maybe the analyser
3874 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003875 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003876 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3877 goto return_bad_req;
3878 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003879 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003881 }
3882 }
3883
Willy Tarreaud98cf932009-12-27 22:54:55 +01003884 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003885 /* stop waiting for data if the input is closed before the end */
3886 if (req->flags & BF_SHUTR)
3887 goto return_bad_req;
3888
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003889 /* waiting for the last bits to leave the buffer */
3890 if (req->flags & BF_SHUTW)
3891 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003892
3893 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003894 return 0;
3895
Willy Tarreaud98cf932009-12-27 22:54:55 +01003896 return_bad_req: /* let's centralize all bad requests */
3897 txn->req.msg_state = HTTP_MSG_ERROR;
3898 txn->status = 400;
3899 /* Note: we don't send any error if some data were already sent */
3900 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3901
Willy Tarreau90deb182010-01-07 00:20:41 +01003902 buffer_auto_read(req);
3903 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003904 req->analysers = 0;
3905 s->fe->counters.failed_req++;
3906 if (s->listener->counters)
3907 s->listener->counters->failed_req++;
3908
3909 if (!(s->flags & SN_ERR_MASK))
3910 s->flags |= SN_ERR_PRXCOND;
3911 if (!(s->flags & SN_FINST_MASK))
3912 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003913 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003914 return 0;
3915}
3916
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003917/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3918 * processing can continue on next analysers, or zero if it either needs more
3919 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3920 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3921 * when it has nothing left to do, and may remove any analyser when it wants to
3922 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003923 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003924int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003925{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003926 struct http_txn *txn = &s->txn;
3927 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003928 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003929 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003930 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003931 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003932
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003933 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 +02003934 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003935 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003936 rep,
3937 rep->rex, rep->wex,
3938 rep->flags,
3939 rep->l,
3940 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003941
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003942 /*
3943 * Now parse the partial (or complete) lines.
3944 * We will check the response syntax, and also join multi-line
3945 * headers. An index of all the lines will be elaborated while
3946 * parsing.
3947 *
3948 * For the parsing, we use a 28 states FSM.
3949 *
3950 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003951 * rep->data + msg->som = beginning of response
3952 * rep->data + msg->eoh = end of processed headers / start of current one
3953 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003954 * rep->lr = first non-visited byte
3955 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01003956 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003957 */
3958
Willy Tarreau83e3af02009-12-28 17:39:57 +01003959 /* There's a protected area at the end of the buffer for rewriting
3960 * purposes. We don't want to start to parse the request if the
3961 * protected area is affected, because we may have to move processed
3962 * data later, which is much more complicated.
3963 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003964 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3965 if (unlikely((rep->flags & BF_FULL) ||
3966 rep->r < rep->lr ||
3967 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3968 if (rep->send_max) {
3969 /* some data has still not left the buffer, wake us once that's done */
3970 buffer_dont_close(rep);
3971 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3972 return 0;
3973 }
3974 if (rep->l <= rep->size - global.tune.maxrewrite)
3975 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003976 }
3977
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003978 if (likely(rep->lr < rep->r))
3979 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003980 }
3981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003982 /* 1: we might have to print this header in debug mode */
3983 if (unlikely((global.mode & MODE_DEBUG) &&
3984 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003985 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003986 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003987
Willy Tarreau962c3f42010-01-10 00:15:35 +01003988 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003989 eol = sol + msg->sl.rq.l;
3990 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003991
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003992 sol += hdr_idx_first_pos(&txn->hdr_idx);
3993 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003995 while (cur_idx) {
3996 eol = sol + txn->hdr_idx.v[cur_idx].len;
3997 debug_hdr("srvhdr", s, sol, eol);
3998 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3999 cur_idx = txn->hdr_idx.v[cur_idx].next;
4000 }
4001 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004002
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004003 /*
4004 * Now we quickly check if we have found a full valid response.
4005 * If not so, we check the FD and buffer states before leaving.
4006 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004007 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004008 * responses are checked first.
4009 *
4010 * Depending on whether the client is still there or not, we
4011 * may send an error response back or not. Note that normally
4012 * we should only check for HTTP status there, and check I/O
4013 * errors somewhere else.
4014 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004015
Willy Tarreau655dce92009-11-08 13:10:58 +01004016 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004017 /* Invalid response */
4018 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4019 /* we detected a parsing error. We want to archive this response
4020 * in the dedicated proxy area for later troubleshooting.
4021 */
4022 hdr_response_bad:
4023 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4024 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4025
4026 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004027 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004028 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004029 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4030 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004031
Willy Tarreau90deb182010-01-07 00:20:41 +01004032 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004033 rep->analysers = 0;
4034 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004035 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004036 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4037
4038 if (!(s->flags & SN_ERR_MASK))
4039 s->flags |= SN_ERR_PRXCOND;
4040 if (!(s->flags & SN_FINST_MASK))
4041 s->flags |= SN_FINST_H;
4042
4043 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004044 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004046 /* too large response does not fit in buffer. */
4047 else if (rep->flags & BF_FULL) {
4048 goto hdr_response_bad;
4049 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004050
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004051 /* read error */
4052 else if (rep->flags & BF_READ_ERROR) {
4053 if (msg->err_pos >= 0)
4054 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004055
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004056 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004057 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004058 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004059 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4060 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004061
Willy Tarreau90deb182010-01-07 00:20:41 +01004062 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004063 rep->analysers = 0;
4064 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004065 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004066 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004068 if (!(s->flags & SN_ERR_MASK))
4069 s->flags |= SN_ERR_SRVCL;
4070 if (!(s->flags & SN_FINST_MASK))
4071 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004072 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004073 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004074
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004075 /* read timeout : return a 504 to the client. */
4076 else if (rep->flags & BF_READ_TIMEOUT) {
4077 if (msg->err_pos >= 0)
4078 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004080 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004081 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004082 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004083 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4084 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004085
Willy Tarreau90deb182010-01-07 00:20:41 +01004086 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004087 rep->analysers = 0;
4088 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004089 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004090 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004091
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004092 if (!(s->flags & SN_ERR_MASK))
4093 s->flags |= SN_ERR_SRVTO;
4094 if (!(s->flags & SN_FINST_MASK))
4095 s->flags |= SN_FINST_H;
4096 return 0;
4097 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004098
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004099 /* close from server */
4100 else if (rep->flags & BF_SHUTR) {
4101 if (msg->err_pos >= 0)
4102 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004104 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004105 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004106 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004107 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4108 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004109
Willy Tarreau90deb182010-01-07 00:20:41 +01004110 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004111 rep->analysers = 0;
4112 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004113 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004114 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004115
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004116 if (!(s->flags & SN_ERR_MASK))
4117 s->flags |= SN_ERR_SRVCL;
4118 if (!(s->flags & SN_FINST_MASK))
4119 s->flags |= SN_FINST_H;
4120 return 0;
4121 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004122
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004123 /* write error to client (we don't send any message then) */
4124 else if (rep->flags & BF_WRITE_ERROR) {
4125 if (msg->err_pos >= 0)
4126 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004127
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004128 s->be->counters.failed_resp++;
4129 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004130 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004131
4132 if (!(s->flags & SN_ERR_MASK))
4133 s->flags |= SN_ERR_CLICL;
4134 if (!(s->flags & SN_FINST_MASK))
4135 s->flags |= SN_FINST_H;
4136
4137 /* process_session() will take care of the error */
4138 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004139 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004141 buffer_dont_close(rep);
4142 return 0;
4143 }
4144
4145 /* More interesting part now : we know that we have a complete
4146 * response which at least looks like HTTP. We have an indicator
4147 * of each header's length, so we can parse them quickly.
4148 */
4149
4150 if (unlikely(msg->err_pos >= 0))
4151 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004153 /*
4154 * 1: get the status code
4155 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004156 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004157 if (n < 1 || n > 5)
4158 n = 0;
4159 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004160
Willy Tarreau5b154472009-12-21 20:11:07 +01004161 /* check if the response is HTTP/1.1 or above */
4162 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004163 ((msg->sol[5] > '1') ||
4164 ((msg->sol[5] == '1') &&
4165 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004166 txn->flags |= TX_RES_VER_11;
4167
4168 /* "connection" has not been parsed yet */
4169 txn->flags &= ~TX_CON_HDR_PARS;
4170
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004171 /* transfer length unknown*/
4172 txn->flags &= ~TX_RES_XFER_LEN;
4173
Willy Tarreau962c3f42010-01-10 00:15:35 +01004174 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004175
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004176 if (txn->status >= 100 && txn->status < 500)
4177 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4178 else
4179 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4180
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004181 /*
4182 * 2: check for cacheability.
4183 */
4184
4185 switch (txn->status) {
4186 case 200:
4187 case 203:
4188 case 206:
4189 case 300:
4190 case 301:
4191 case 410:
4192 /* RFC2616 @13.4:
4193 * "A response received with a status code of
4194 * 200, 203, 206, 300, 301 or 410 MAY be stored
4195 * by a cache (...) unless a cache-control
4196 * directive prohibits caching."
4197 *
4198 * RFC2616 @9.5: POST method :
4199 * "Responses to this method are not cacheable,
4200 * unless the response includes appropriate
4201 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004202 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004203 if (likely(txn->meth != HTTP_METH_POST) &&
4204 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4205 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4206 break;
4207 default:
4208 break;
4209 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004210
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004211 /*
4212 * 3: we may need to capture headers
4213 */
4214 s->logs.logwait &= ~LW_RESP;
4215 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004216 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004217 txn->rsp.cap, s->fe->rsp_cap);
4218
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004219 /* 4: determine the transfer-length.
4220 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4221 * the presence of a message-body in a RESPONSE and its transfer length
4222 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004223 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004224 * All responses to the HEAD request method MUST NOT include a
4225 * message-body, even though the presence of entity-header fields
4226 * might lead one to believe they do. All 1xx (informational), 204
4227 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4228 * message-body. All other responses do include a message-body,
4229 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004230 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004231 * 1. Any response which "MUST NOT" include a message-body (such as the
4232 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4233 * always terminated by the first empty line after the header fields,
4234 * regardless of the entity-header fields present in the message.
4235 *
4236 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4237 * the "chunked" transfer-coding (Section 6.2) is used, the
4238 * transfer-length is defined by the use of this transfer-coding.
4239 * If a Transfer-Encoding header field is present and the "chunked"
4240 * transfer-coding is not present, the transfer-length is defined by
4241 * the sender closing the connection.
4242 *
4243 * 3. If a Content-Length header field is present, its decimal value in
4244 * OCTETs represents both the entity-length and the transfer-length.
4245 * If a message is received with both a Transfer-Encoding header
4246 * field and a Content-Length header field, the latter MUST be ignored.
4247 *
4248 * 4. If the message uses the media type "multipart/byteranges", and
4249 * the transfer-length is not otherwise specified, then this self-
4250 * delimiting media type defines the transfer-length. This media
4251 * type MUST NOT be used unless the sender knows that the recipient
4252 * can parse it; the presence in a request of a Range header with
4253 * multiple byte-range specifiers from a 1.1 client implies that the
4254 * client can parse multipart/byteranges responses.
4255 *
4256 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004257 */
4258
4259 /* Skip parsing if no content length is possible. The response flags
4260 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004261 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004262 * FIXME: should we parse anyway and return an error on chunked encoding ?
4263 */
4264 if (txn->meth == HTTP_METH_HEAD ||
4265 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004266 txn->status == 204 || txn->status == 304) {
4267 txn->flags |= TX_RES_XFER_LEN;
4268 goto skip_content_length;
4269 }
4270
4271 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004272 goto skip_content_length;
4273
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004274 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004275 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004276 while ((txn->flags & TX_RES_VER_11) &&
4277 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004278 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4279 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4280 else if (txn->flags & TX_RES_TE_CHNK) {
4281 /* bad transfer-encoding (chunked followed by something else) */
4282 use_close_only = 1;
4283 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4284 break;
4285 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004286 }
4287
4288 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4289 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004290 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004291 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4292 signed long long cl;
4293
4294 if (!ctx.vlen)
4295 goto hdr_response_bad;
4296
4297 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4298 goto hdr_response_bad; /* parse failure */
4299
4300 if (cl < 0)
4301 goto hdr_response_bad;
4302
4303 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4304 goto hdr_response_bad; /* already specified, was different */
4305
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004306 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004307 msg->hdr_content_len = cl;
4308 }
4309
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004310 /* FIXME: we should also implement the multipart/byterange method.
4311 * For now on, we resort to close mode in this case (unknown length).
4312 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004313skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004314
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004315 /* end of job, return OK */
4316 rep->analysers &= ~an_bit;
4317 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004318 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004319 return 1;
4320}
4321
4322/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004323 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4324 * and updates t->rep->analysers. It might make sense to explode it into several
4325 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004326 */
4327int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4328{
4329 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004330 struct http_msg *msg = &txn->rsp;
4331 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004332 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004333 int conn_ka = 0, conn_cl = 0;
4334 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004335 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004336
4337 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4338 now_ms, __FUNCTION__,
4339 t,
4340 rep,
4341 rep->rex, rep->wex,
4342 rep->flags,
4343 rep->l,
4344 rep->analysers);
4345
Willy Tarreau655dce92009-11-08 13:10:58 +01004346 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004347 return 0;
4348
4349 rep->analysers &= ~an_bit;
4350 rep->analyse_exp = TICK_ETERNITY;
4351
Willy Tarreau5b154472009-12-21 20:11:07 +01004352 /* Now we have to check if we need to modify the Connection header.
4353 * This is more difficult on the response than it is on the request,
4354 * because we can have two different HTTP versions and we don't know
4355 * how the client will interprete a response. For instance, let's say
4356 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4357 * HTTP/1.1 response without any header. Maybe it will bound itself to
4358 * HTTP/1.0 because it only knows about it, and will consider the lack
4359 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4360 * the lack of header as a keep-alive. Thus we will use two flags
4361 * indicating how a request MAY be understood by the client. In case
4362 * of multiple possibilities, we'll fix the header to be explicit. If
4363 * ambiguous cases such as both close and keepalive are seen, then we
4364 * will fall back to explicit close. Note that we won't take risks with
4365 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4366 */
4367
4368 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004369 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4370 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4371 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004372 int may_keep = 0, may_close = 0; /* how it may be understood */
4373 struct hdr_ctx ctx;
4374
4375 ctx.idx = 0;
4376 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4377 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4378 conn_cl = 1;
4379 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4380 conn_ka = 1;
4381 }
4382
4383 if (conn_cl) {
4384 /* close header present */
4385 may_close = 1;
4386 if (conn_ka) /* we have both close and keep-alive */
4387 may_keep = 1;
4388 }
4389 else if (conn_ka) {
4390 /* keep-alive alone */
4391 may_keep = 1;
4392 }
4393 else {
4394 /* no close nor keep-alive header */
4395 if (txn->flags & TX_RES_VER_11)
4396 may_keep = 1;
4397 else
4398 may_close = 1;
4399
4400 if (txn->flags & TX_REQ_VER_11)
4401 may_keep = 1;
4402 else
4403 may_close = 1;
4404 }
4405
4406 /* let's update the transaction status to reflect any close.
4407 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004408 * handled. We also explicitly state that we will close in
4409 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004410 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004411 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004412 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4413 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004414 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4415
4416 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004417 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004418 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4419 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004420 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004421 if (may_keep &&
4422 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4423 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004424 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004425 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4426 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4427 must_del_close = 1;
4428 if (!(txn->flags & TX_REQ_VER_11))
4429 must_keep = 1;
4430 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004431
4432 txn->flags |= TX_CON_HDR_PARS;
4433 }
4434
4435 /* We might have to check for "Connection:" if the server
4436 * returns a connection status that is not compatible with
4437 * the client's or with the config.
4438 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004439 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004440 char *cur_ptr, *cur_end, *cur_next;
4441 int cur_idx, old_idx, delta, val;
4442 int must_delete;
4443 struct hdr_idx_elem *cur_hdr;
4444
4445 /* we just have to remove the headers if both sides are 1.0 */
4446 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004447
4448 /* same if we want to re-enable keep-alive on 1.1 */
4449 must_delete |= must_del_close;
4450
Willy Tarreau962c3f42010-01-10 00:15:35 +01004451 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau5b154472009-12-21 20:11:07 +01004452
4453 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4454 cur_hdr = &txn->hdr_idx.v[cur_idx];
4455 cur_ptr = cur_next;
4456 cur_end = cur_ptr + cur_hdr->len;
4457 cur_next = cur_end + cur_hdr->cr + 1;
4458
4459 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4460 if (!val)
4461 continue;
4462
4463 /* 3 possibilities :
4464 * - we have already set "Connection: close" or we're in
4465 * HTTP/1.0, so we remove this line.
4466 * - we have not yet set "Connection: close", but this line
4467 * indicates close. We leave it untouched and set the flag.
4468 * - we have not yet set "Connection: close", and this line
4469 * indicates non-close. We replace it and set the flag.
4470 */
4471 if (must_delete) {
4472 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4473 http_msg_move_end(&txn->rsp, delta);
4474 cur_next += delta;
4475 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4476 txn->hdr_idx.used--;
4477 cur_hdr->len = 0;
4478 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004479 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004480 } else {
4481 if (cur_end - cur_ptr - val != 5 ||
4482 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4483 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4484 "close", 5);
4485 cur_next += delta;
4486 cur_hdr->len += delta;
4487 http_msg_move_end(&txn->rsp, delta);
4488 }
4489 must_delete = 1;
4490 must_close = 0;
4491 }
4492 } /* for loop */
4493 } /* if must close keep-alive */
4494
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004495 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004496 /*
4497 * 3: we will have to evaluate the filters.
4498 * As opposed to version 1.2, now they will be evaluated in the
4499 * filters order and not in the header order. This means that
4500 * each filter has to be validated among all headers.
4501 *
4502 * Filters are tried with ->be first, then with ->fe if it is
4503 * different from ->be.
4504 */
4505
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004506 cur_proxy = t->be;
4507 while (1) {
4508 struct proxy *rule_set = cur_proxy;
4509
4510 /* try headers filters */
4511 if (rule_set->rsp_exp != NULL) {
4512 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4513 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004514 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004515 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004516 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4517 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004518 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004519 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004520 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004521 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004522 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004523 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004524 if (!(t->flags & SN_ERR_MASK))
4525 t->flags |= SN_ERR_PRXCOND;
4526 if (!(t->flags & SN_FINST_MASK))
4527 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004528 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004529 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004530 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004531
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004532 /* has the response been denied ? */
4533 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004534 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004535 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004536
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004537 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004538 if (t->listener->counters)
4539 t->listener->counters->denied_resp++;
4540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004541 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004542 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004543
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004544 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004545 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004546 if (txn->status < 200)
4547 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004548 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004549 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004550 }
4551
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004552 /* check whether we're already working on the frontend */
4553 if (cur_proxy == t->fe)
4554 break;
4555 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004556 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004557
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004558 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004559 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4560 * in which case this is not the right response, and we're waiting for the
4561 * next one. Let's allow this response to go to the client and wait for the
4562 * next one.
4563 */
4564 if (txn->status < 200) {
4565 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004566 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004567 msg->msg_state = HTTP_MSG_RPBEFORE;
4568 txn->status = 0;
4569 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4570 return 1;
4571 }
4572
4573 /* we don't have any 1xx status code now */
4574
4575 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576 * 4: check for server cookie.
4577 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004578 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4579 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004580 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004581
Willy Tarreaubaaee002006-06-26 02:48:02 +02004582
Willy Tarreaua15645d2007-03-18 16:22:39 +01004583 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004585 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004586 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004587 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004588
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589 /*
4590 * 6: add server cookie in the response if needed
4591 */
4592 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004593 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004594 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004595
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596 /* the server is known, it's not the one the client requested, we have to
4597 * insert a set-cookie here, except if we want to insert only on POST
4598 * requests and this one isn't. Note that servers which don't have cookies
4599 * (eg: some backup servers) will return a full cookie removal request.
4600 */
4601 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4602 t->be->cookie_name,
4603 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004604
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004605 if (t->be->cookie_domain)
4606 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004607
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004608 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004609 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004610 goto return_bad_resp;
4611 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004612
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613 /* Here, we will tell an eventual cache on the client side that we don't
4614 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4615 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4616 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4617 */
4618 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004619
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004620 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4621
4622 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004623 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004624 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004625 }
4626 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004627
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004628 /*
4629 * 7: check if result will be cacheable with a cookie.
4630 * We'll block the response if security checks have caught
4631 * nasty things such as a cacheable cookie.
4632 */
4633 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4634 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004635 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004636
4637 /* we're in presence of a cacheable response containing
4638 * a set-cookie header. We'll block it as requested by
4639 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004640 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004641 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004642 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004643
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004644 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004645 if (t->listener->counters)
4646 t->listener->counters->denied_resp++;
4647
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004648 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4649 t->be->id, t->srv?t->srv->id:"<dispatch>");
4650 send_log(t->be, LOG_ALERT,
4651 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4652 t->be->id, t->srv?t->srv->id:"<dispatch>");
4653 goto return_srv_prx_502;
4654 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004655
4656 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004657 * 8: add "Connection: close" if needed and not yet set. This is
4658 * only needed for 1.1 responses since we know there is no other
4659 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004660 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004661 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004662 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004663 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004664 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004665 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004666 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004667 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4668 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4669 "Connection: keep-alive", 22) < 0))
4670 goto return_bad_resp;
4671 must_keep = 0;
4672 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004673
Willy Tarreaud98cf932009-12-27 22:54:55 +01004674 if (txn->flags & TX_RES_XFER_LEN)
4675 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004676
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004677 /*************************************************************
4678 * OK, that's finished for the headers. We have done what we *
4679 * could. Let's switch to the DATA state. *
4680 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004681
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004682 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004683
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004684 /* if the user wants to log as soon as possible, without counting
4685 * bytes from the server, then this is the right moment. We have
4686 * to temporarily assign bytes_out to log what we currently have.
4687 */
4688 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4689 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4690 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004691 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004692 t->logs.bytes_out = 0;
4693 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004694
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004695 /* Note: we must not try to cheat by jumping directly to DATA,
4696 * otherwise we would not let the client side wake up.
4697 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004698
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004699 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004700 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004701 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004702}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004703
Willy Tarreaud98cf932009-12-27 22:54:55 +01004704/* This function is an analyser which forwards response body (including chunk
4705 * sizes if any). It is called as soon as we must forward, even if we forward
4706 * zero byte. The only situation where it must not be called is when we're in
4707 * tunnel mode and we want to forward till the close. It's used both to forward
4708 * remaining data and to resync after end of body. It expects the msg_state to
4709 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4710 * read more data, or 1 once we can go on with next request or end the session.
4711 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4712 * bytes of pending data + the headers if not already done (between som and sov).
4713 * It eventually adjusts som to match sov after the data in between have been sent.
4714 */
4715int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4716{
4717 struct http_txn *txn = &s->txn;
4718 struct http_msg *msg = &s->txn.rsp;
4719
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004720 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4721 return 0;
4722
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004723 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004724 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004725 !s->req->analysers) {
4726 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4727 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004728 buffer_auto_read(res);
4729 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004730 res->analysers &= ~an_bit;
4731 return 1;
4732 }
4733
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004734 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004735
Willy Tarreaud98cf932009-12-27 22:54:55 +01004736 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4737 /* we have msg->col and msg->sov which both point to the first
4738 * byte of message body. msg->som still points to the beginning
4739 * of the message. We must save the body in req->lr because it
4740 * survives buffer re-alignments.
4741 */
4742 res->lr = res->data + msg->sov;
4743 if (txn->flags & TX_RES_TE_CHNK)
4744 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4745 else {
4746 msg->msg_state = HTTP_MSG_DATA;
4747 }
4748 }
4749
Willy Tarreaud98cf932009-12-27 22:54:55 +01004750 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004752 /* we may have some data pending */
4753 if (msg->hdr_content_len || msg->som != msg->sov) {
4754 int bytes = msg->sov - msg->som;
4755 if (bytes < 0) /* sov may have wrapped at the end */
4756 bytes += res->size;
4757 buffer_forward(res, bytes + msg->hdr_content_len);
4758 msg->hdr_content_len = 0; /* don't forward that again */
4759 msg->som = msg->sov;
4760 }
4761
Willy Tarreaucaabe412010-01-03 23:08:28 +01004762 if (msg->msg_state == HTTP_MSG_DATA) {
4763 /* must still forward */
4764 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004765 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004766
4767 /* nothing left to forward */
4768 if (txn->flags & TX_RES_TE_CHNK)
4769 msg->msg_state = HTTP_MSG_DATA_CRLF;
4770 else
4771 msg->msg_state = HTTP_MSG_DONE;
4772 }
4773 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004774 /* read the chunk size and assign it to ->hdr_content_len, then
4775 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4776 */
4777 int ret = http_parse_chunk_size(res, msg);
4778
4779 if (!ret)
4780 goto missing_data;
4781 else if (ret < 0)
4782 goto return_bad_res;
4783 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004785 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4786 /* we want the CRLF after the data */
4787 int ret;
4788
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004789 res->lr = res->w + res->send_max;
4790 if (res->lr >= res->data + res->size)
4791 res->lr -= res->size;
4792
Willy Tarreaud98cf932009-12-27 22:54:55 +01004793 ret = http_skip_chunk_crlf(res, msg);
4794
4795 if (!ret)
4796 goto missing_data;
4797 else if (ret < 0)
4798 goto return_bad_res;
4799 /* we're in MSG_CHUNK_SIZE now */
4800 }
4801 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4802 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004803
Willy Tarreaud98cf932009-12-27 22:54:55 +01004804 if (ret == 0)
4805 goto missing_data;
4806 else if (ret < 0)
4807 goto return_bad_res;
4808 /* we're in HTTP_MSG_DONE now */
4809 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810 else {
4811 /* other states, DONE...TUNNEL */
4812 if (http_resync_states(s)) {
4813 http_silent_debug(__LINE__, s);
4814 /* some state changes occurred, maybe the analyser
4815 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004816 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004817 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4818 goto return_bad_res;
4819 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004820 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822 }
4823 }
4824
Willy Tarreaud98cf932009-12-27 22:54:55 +01004825 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004826 /* stop waiting for data if the input is closed before the end */
4827 if (res->flags & BF_SHUTR)
4828 goto return_bad_res;
4829
Willy Tarreau610ecce2010-01-04 21:15:02 +01004830 if (!s->req->analysers)
4831 goto return_bad_res;
4832
Willy Tarreaud98cf932009-12-27 22:54:55 +01004833 /* forward the chunk size as well as any pending data */
4834 if (msg->hdr_content_len || msg->som != msg->sov) {
4835 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4836 msg->hdr_content_len = 0; /* don't forward that again */
4837 msg->som = msg->sov;
4838 }
4839
Willy Tarreaud98cf932009-12-27 22:54:55 +01004840 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004841 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004842 return 0;
4843
4844 return_bad_res: /* let's centralize all bad resuests */
4845 txn->rsp.msg_state = HTTP_MSG_ERROR;
4846 txn->status = 502;
4847 stream_int_cond_close(res->cons, NULL);
4848
Willy Tarreau90deb182010-01-07 00:20:41 +01004849 buffer_auto_close(res);
4850 buffer_auto_read(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004851 res->analysers = 0;
4852 s->be->counters.failed_resp++;
4853 if (s->srv) {
4854 s->srv->counters.failed_resp++;
4855 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4856 }
4857
4858 if (!(s->flags & SN_ERR_MASK))
4859 s->flags |= SN_ERR_PRXCOND;
4860 if (!(s->flags & SN_FINST_MASK))
4861 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004862 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004863 return 0;
4864}
4865
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004866/* Iterate the same filter through all request headers.
4867 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004868 * Since it can manage the switch to another backend, it updates the per-proxy
4869 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004870 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004871int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004872{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004873 char term;
4874 char *cur_ptr, *cur_end, *cur_next;
4875 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004876 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004877 struct hdr_idx_elem *cur_hdr;
4878 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004880 last_hdr = 0;
4881
Willy Tarreau962c3f42010-01-10 00:15:35 +01004882 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004883 old_idx = 0;
4884
4885 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004886 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004887 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004888 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004889 (exp->action == ACT_ALLOW ||
4890 exp->action == ACT_DENY ||
4891 exp->action == ACT_TARPIT))
4892 return 0;
4893
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004894 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 if (!cur_idx)
4896 break;
4897
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004898 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004899 cur_ptr = cur_next;
4900 cur_end = cur_ptr + cur_hdr->len;
4901 cur_next = cur_end + cur_hdr->cr + 1;
4902
4903 /* Now we have one header between cur_ptr and cur_end,
4904 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004905 */
4906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004907 /* The annoying part is that pattern matching needs
4908 * that we modify the contents to null-terminate all
4909 * strings before testing them.
4910 */
4911
4912 term = *cur_end;
4913 *cur_end = '\0';
4914
4915 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4916 switch (exp->action) {
4917 case ACT_SETBE:
4918 /* It is not possible to jump a second time.
4919 * FIXME: should we return an HTTP/500 here so that
4920 * the admin knows there's a problem ?
4921 */
4922 if (t->be != t->fe)
4923 break;
4924
4925 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004926 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004927 last_hdr = 1;
4928 break;
4929
4930 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004931 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004932 last_hdr = 1;
4933 break;
4934
4935 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004936 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004937 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004938
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004939 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004940 if (t->listener->counters)
4941 t->listener->counters->denied_resp++;
4942
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004943 break;
4944
4945 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004946 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004947 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004948
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004949 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004950 if (t->listener->counters)
4951 t->listener->counters->denied_resp++;
4952
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004953 break;
4954
4955 case ACT_REPLACE:
4956 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4957 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4958 /* FIXME: if the user adds a newline in the replacement, the
4959 * index will not be recalculated for now, and the new line
4960 * will not be counted as a new header.
4961 */
4962
4963 cur_end += delta;
4964 cur_next += delta;
4965 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004966 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004967 break;
4968
4969 case ACT_REMOVE:
4970 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4971 cur_next += delta;
4972
Willy Tarreaufa355d42009-11-29 18:12:29 +01004973 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004974 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4975 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004976 cur_hdr->len = 0;
4977 cur_end = NULL; /* null-term has been rewritten */
4978 break;
4979
4980 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004981 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004982 if (cur_end)
4983 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004984
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004985 /* keep the link from this header to next one in case of later
4986 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004988 old_idx = cur_idx;
4989 }
4990 return 0;
4991}
4992
4993
4994/* Apply the filter to the request line.
4995 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4996 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004997 * Since it can manage the switch to another backend, it updates the per-proxy
4998 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004999 */
5000int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5001{
5002 char term;
5003 char *cur_ptr, *cur_end;
5004 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005005 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005006 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005007
Willy Tarreau58f10d72006-12-04 02:26:12 +01005008
Willy Tarreau3d300592007-03-18 18:34:41 +01005009 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005010 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005011 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005012 (exp->action == ACT_ALLOW ||
5013 exp->action == ACT_DENY ||
5014 exp->action == ACT_TARPIT))
5015 return 0;
5016 else if (exp->action == ACT_REMOVE)
5017 return 0;
5018
5019 done = 0;
5020
Willy Tarreau962c3f42010-01-10 00:15:35 +01005021 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005022 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005023
5024 /* Now we have the request line between cur_ptr and cur_end */
5025
5026 /* The annoying part is that pattern matching needs
5027 * that we modify the contents to null-terminate all
5028 * strings before testing them.
5029 */
5030
5031 term = *cur_end;
5032 *cur_end = '\0';
5033
5034 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5035 switch (exp->action) {
5036 case ACT_SETBE:
5037 /* It is not possible to jump a second time.
5038 * FIXME: should we return an HTTP/500 here so that
5039 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005040 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005041 if (t->be != t->fe)
5042 break;
5043
5044 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005045 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005046 done = 1;
5047 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005049 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005050 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005051 done = 1;
5052 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005053
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005054 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005055 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005056
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005057 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005058 if (t->listener->counters)
5059 t->listener->counters->denied_resp++;
5060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005061 done = 1;
5062 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005064 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005065 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005066
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005067 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005068 if (t->listener->counters)
5069 t->listener->counters->denied_resp++;
5070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005071 done = 1;
5072 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005074 case ACT_REPLACE:
5075 *cur_end = term; /* restore the string terminator */
5076 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5077 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5078 /* FIXME: if the user adds a newline in the replacement, the
5079 * index will not be recalculated for now, and the new line
5080 * will not be counted as a new header.
5081 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005082
Willy Tarreaufa355d42009-11-29 18:12:29 +01005083 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005084 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005085 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005086 HTTP_MSG_RQMETH,
5087 cur_ptr, cur_end + 1,
5088 NULL, NULL);
5089 if (unlikely(!cur_end))
5090 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005092 /* we have a full request and we know that we have either a CR
5093 * or an LF at <ptr>.
5094 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005095 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5096 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005097 /* there is no point trying this regex on headers */
5098 return 1;
5099 }
5100 }
5101 *cur_end = term; /* restore the string terminator */
5102 return done;
5103}
Willy Tarreau97de6242006-12-27 17:18:38 +01005104
Willy Tarreau58f10d72006-12-04 02:26:12 +01005105
Willy Tarreau58f10d72006-12-04 02:26:12 +01005106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005107/*
5108 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5109 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005110 * unparsable request. Since it can manage the switch to another backend, it
5111 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005112 */
5113int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5114{
Willy Tarreau3d300592007-03-18 18:34:41 +01005115 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005116 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005117 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005118 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005120 /*
5121 * The interleaving of transformations and verdicts
5122 * makes it difficult to decide to continue or stop
5123 * the evaluation.
5124 */
5125
Willy Tarreau3d300592007-03-18 18:34:41 +01005126 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5128 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5129 exp = exp->next;
5130 continue;
5131 }
5132
5133 /* Apply the filter to the request line. */
5134 ret = apply_filter_to_req_line(t, req, exp);
5135 if (unlikely(ret < 0))
5136 return -1;
5137
5138 if (likely(ret == 0)) {
5139 /* The filter did not match the request, it can be
5140 * iterated through all headers.
5141 */
5142 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005143 }
5144 exp = exp->next;
5145 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005146 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005147}
5148
5149
Willy Tarreaua15645d2007-03-18 16:22:39 +01005150
Willy Tarreau58f10d72006-12-04 02:26:12 +01005151/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005152 * Try to retrieve the server associated to the appsession.
5153 * If the server is found, it's assigned to the session.
5154 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005155void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005156 struct http_txn *txn = &t->txn;
5157 appsess *asession = NULL;
5158 char *sessid_temp = NULL;
5159
Cyril Bontéb21570a2009-11-29 20:04:48 +01005160 if (len > t->be->appsession_len) {
5161 len = t->be->appsession_len;
5162 }
5163
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005164 if (t->be->options2 & PR_O2_AS_REQL) {
5165 /* request-learn option is enabled : store the sessid in the session for future use */
5166 if (t->sessid != NULL) {
5167 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5168 pool_free2(apools.sessid, t->sessid);
5169 }
5170
5171 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5172 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5173 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5174 return;
5175 }
5176
Cyril Bontéb21570a2009-11-29 20:04:48 +01005177 memcpy(t->sessid, buf, len);
5178 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005179 }
5180
5181 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5182 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5183 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5184 return;
5185 }
5186
Cyril Bontéb21570a2009-11-29 20:04:48 +01005187 memcpy(sessid_temp, buf, len);
5188 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005189
5190 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5191 /* free previously allocated memory */
5192 pool_free2(apools.sessid, sessid_temp);
5193
5194 if (asession != NULL) {
5195 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5196 if (!(t->be->options2 & PR_O2_AS_REQL))
5197 asession->request_count++;
5198
5199 if (asession->serverid != NULL) {
5200 struct server *srv = t->be->srv;
5201 while (srv) {
5202 if (strcmp(srv->id, asession->serverid) == 0) {
5203 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5204 /* we found the server and it's usable */
5205 txn->flags &= ~TX_CK_MASK;
5206 txn->flags |= TX_CK_VALID;
5207 t->flags |= SN_DIRECT | SN_ASSIGNED;
5208 t->srv = srv;
5209 break;
5210 } else {
5211 txn->flags &= ~TX_CK_MASK;
5212 txn->flags |= TX_CK_DOWN;
5213 }
5214 }
5215 srv = srv->next;
5216 }
5217 }
5218 }
5219}
5220
5221/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005222 * Manage client-side cookie. It can impact performance by about 2% so it is
5223 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005224 */
5225void manage_client_side_cookies(struct session *t, struct buffer *req)
5226{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005227 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005228 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005229 char *del_colon, *del_cookie, *colon;
5230 int app_cookies;
5231
Willy Tarreau58f10d72006-12-04 02:26:12 +01005232 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005233 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005234
Willy Tarreau2a324282006-12-05 00:05:46 +01005235 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005236 * we start with the start line.
5237 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005238 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005239 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005240
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005241 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005242 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005243 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005244
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005245 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246 cur_ptr = cur_next;
5247 cur_end = cur_ptr + cur_hdr->len;
5248 cur_next = cur_end + cur_hdr->cr + 1;
5249
5250 /* We have one full header between cur_ptr and cur_end, and the
5251 * next header starts at cur_next. We're only interested in
5252 * "Cookie:" headers.
5253 */
5254
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005255 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5256 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005257 old_idx = cur_idx;
5258 continue;
5259 }
5260
5261 /* Now look for cookies. Conforming to RFC2109, we have to support
5262 * attributes whose name begin with a '$', and associate them with
5263 * the right cookie, if we want to delete this cookie.
5264 * So there are 3 cases for each cookie read :
5265 * 1) it's a special attribute, beginning with a '$' : ignore it.
5266 * 2) it's a server id cookie that we *MAY* want to delete : save
5267 * some pointers on it (last semi-colon, beginning of cookie...)
5268 * 3) it's an application cookie : we *MAY* have to delete a previous
5269 * "special" cookie.
5270 * At the end of loop, if a "special" cookie remains, we may have to
5271 * remove it. If no application cookie persists in the header, we
5272 * *MUST* delete it
5273 */
5274
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005275 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005276
Willy Tarreau58f10d72006-12-04 02:26:12 +01005277 /* del_cookie == NULL => nothing to be deleted */
5278 del_colon = del_cookie = NULL;
5279 app_cookies = 0;
5280
5281 while (p1 < cur_end) {
5282 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005283 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005284 while (p1 < cur_end) {
5285 if (*p1 == ';' || *p1 == ',')
5286 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005287 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005288 break;
5289 p1++;
5290 }
5291
5292 if (p1 == cur_end)
5293 break;
5294
5295 /* p1 is at the beginning of the cookie name */
5296 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005297 while (p2 < cur_end && *p2 != '=') {
5298 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5299 /* oops, the cookie name was truncated, resync */
5300 p1 = p2;
5301 goto resync_name;
5302 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005303 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005304 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005305
5306 if (p2 == cur_end)
5307 break;
5308
5309 p3 = p2 + 1; /* skips the '=' sign */
5310 if (p3 == cur_end)
5311 break;
5312
Willy Tarreau305ae852010-01-03 19:45:54 +01005313 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5314 p5 = p4 = p3;
5315 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5316 if (!isspace((unsigned char)*p5))
5317 p4 = p5 + 1;
5318 p5++;
5319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005320
5321 /* here, we have the cookie name between p1 and p2,
5322 * and its value between p3 and p4.
5323 * we can process it :
5324 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005325 * Cookie: NAME=VALUE ;
5326 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005327 * | || || +--> p4
5328 * | || |+-------> p3
5329 * | || +--------> p2
5330 * | |+------------> p1
5331 * | +-------------> colon
5332 * +--------------------> cur_ptr
5333 */
5334
5335 if (*p1 == '$') {
5336 /* skip this one */
5337 }
5338 else {
5339 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005340 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005341 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005342 (p4 - p1 >= t->fe->capture_namelen) &&
5343 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005344 int log_len = p4 - p1;
5345
Willy Tarreau086b3b42007-05-13 21:45:51 +02005346 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005347 Alert("HTTP logging : out of memory.\n");
5348 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005349 if (log_len > t->fe->capture_len)
5350 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005351 memcpy(txn->cli_cookie, p1, log_len);
5352 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005353 }
5354 }
5355
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005356 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5357 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005358 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005359 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005360 char *delim;
5361
5362 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5363 * have the server ID betweek p3 and delim, and the original cookie between
5364 * delim+1 and p4. Otherwise, delim==p4 :
5365 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005366 * Cookie: NAME=SRV~VALUE ;
5367 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005368 * | || || | +--> p4
5369 * | || || +--------> delim
5370 * | || |+-----------> p3
5371 * | || +------------> p2
5372 * | |+----------------> p1
5373 * | +-----------------> colon
5374 * +------------------------> cur_ptr
5375 */
5376
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005377 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005378 for (delim = p3; delim < p4; delim++)
5379 if (*delim == COOKIE_DELIM)
5380 break;
5381 }
5382 else
5383 delim = p4;
5384
5385
5386 /* Here, we'll look for the first running server which supports the cookie.
5387 * This allows to share a same cookie between several servers, for example
5388 * to dedicate backup servers to specific servers only.
5389 * However, to prevent clients from sticking to cookie-less backup server
5390 * when they have incidentely learned an empty cookie, we simply ignore
5391 * empty cookies and mark them as invalid.
5392 */
5393 if (delim == p3)
5394 srv = NULL;
5395
5396 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005397 if (srv->cookie && (srv->cklen == delim - p3) &&
5398 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005399 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005400 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005401 txn->flags &= ~TX_CK_MASK;
5402 txn->flags |= TX_CK_VALID;
5403 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005404 t->srv = srv;
5405 break;
5406 } else {
5407 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005408 txn->flags &= ~TX_CK_MASK;
5409 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005410 }
5411 }
5412 srv = srv->next;
5413 }
5414
Willy Tarreau3d300592007-03-18 18:34:41 +01005415 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005416 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005417 txn->flags &= ~TX_CK_MASK;
5418 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005419 }
5420
5421 /* depending on the cookie mode, we may have to either :
5422 * - delete the complete cookie if we're in insert+indirect mode, so that
5423 * the server never sees it ;
5424 * - remove the server id from the cookie value, and tag the cookie as an
5425 * application cookie so that it does not get accidentely removed later,
5426 * if we're in cookie prefix mode
5427 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005428 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005429 int delta; /* negative */
5430
5431 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5432 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005433 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005434 cur_end += delta;
5435 cur_next += delta;
5436 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005437 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005438
5439 del_cookie = del_colon = NULL;
5440 app_cookies++; /* protect the header from deletion */
5441 }
5442 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005443 (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 +01005444 del_cookie = p1;
5445 del_colon = colon;
5446 }
5447 } else {
5448 /* now we know that we must keep this cookie since it's
5449 * not ours. But if we wanted to delete our cookie
5450 * earlier, we cannot remove the complete header, but we
5451 * can remove the previous block itself.
5452 */
5453 app_cookies++;
5454
5455 if (del_cookie != NULL) {
5456 int delta; /* negative */
5457
5458 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5459 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005460 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005461 cur_end += delta;
5462 cur_next += delta;
5463 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005464 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005465 del_cookie = del_colon = NULL;
5466 }
5467 }
5468
Cyril Bontéb21570a2009-11-29 20:04:48 +01005469 if (t->be->appsession_name != NULL) {
5470 int cmp_len, value_len;
5471 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005472
Cyril Bontéb21570a2009-11-29 20:04:48 +01005473 if (t->be->options2 & PR_O2_AS_PFX) {
5474 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5475 value_begin = p1 + t->be->appsession_name_len;
5476 value_len = p4 - p1 - t->be->appsession_name_len;
5477 } else {
5478 cmp_len = p2 - p1;
5479 value_begin = p3;
5480 value_len = p4 - p3;
5481 }
5482
5483 /* let's see if the cookie is our appcookie */
5484 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5485 /* Cool... it's the right one */
5486 manage_client_side_appsession(t, value_begin, value_len);
5487 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005488#if defined(DEBUG_HASH)
5489 Alert("manage_client_side_cookies\n");
5490 appsession_hash_dump(&(t->be->htbl_proxy));
5491#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005492 }/* end if ((t->proxy->appsession_name != NULL) ... */
5493 }
5494
5495 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005496 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005497 } /* while (p1 < cur_end) */
5498
5499 /* There's no more cookie on this line.
5500 * We may have marked the last one(s) for deletion.
5501 * We must do this now in two ways :
5502 * - if there is no app cookie, we simply delete the header ;
5503 * - if there are app cookies, we must delete the end of the
5504 * string properly, including the colon/semi-colon before
5505 * the cookie name.
5506 */
5507 if (del_cookie != NULL) {
5508 int delta;
5509 if (app_cookies) {
5510 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5511 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005512 cur_hdr->len += delta;
5513 } else {
5514 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005515
5516 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005517 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5518 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005519 cur_hdr->len = 0;
5520 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005521 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005522 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005523 }
5524
5525 /* keep the link from this header to next one */
5526 old_idx = cur_idx;
5527 } /* end of cookie processing on this header */
5528}
5529
5530
Willy Tarreaua15645d2007-03-18 16:22:39 +01005531/* Iterate the same filter through all response headers contained in <rtr>.
5532 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5533 */
5534int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5535{
5536 char term;
5537 char *cur_ptr, *cur_end, *cur_next;
5538 int cur_idx, old_idx, last_hdr;
5539 struct http_txn *txn = &t->txn;
5540 struct hdr_idx_elem *cur_hdr;
5541 int len, delta;
5542
5543 last_hdr = 0;
5544
Willy Tarreau962c3f42010-01-10 00:15:35 +01005545 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005546 old_idx = 0;
5547
5548 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005549 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005550 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005551 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005552 (exp->action == ACT_ALLOW ||
5553 exp->action == ACT_DENY))
5554 return 0;
5555
5556 cur_idx = txn->hdr_idx.v[old_idx].next;
5557 if (!cur_idx)
5558 break;
5559
5560 cur_hdr = &txn->hdr_idx.v[cur_idx];
5561 cur_ptr = cur_next;
5562 cur_end = cur_ptr + cur_hdr->len;
5563 cur_next = cur_end + cur_hdr->cr + 1;
5564
5565 /* Now we have one header between cur_ptr and cur_end,
5566 * and the next header starts at cur_next.
5567 */
5568
5569 /* The annoying part is that pattern matching needs
5570 * that we modify the contents to null-terminate all
5571 * strings before testing them.
5572 */
5573
5574 term = *cur_end;
5575 *cur_end = '\0';
5576
5577 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5578 switch (exp->action) {
5579 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005580 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005581 last_hdr = 1;
5582 break;
5583
5584 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005585 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005586 last_hdr = 1;
5587 break;
5588
5589 case ACT_REPLACE:
5590 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5591 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5592 /* FIXME: if the user adds a newline in the replacement, the
5593 * index will not be recalculated for now, and the new line
5594 * will not be counted as a new header.
5595 */
5596
5597 cur_end += delta;
5598 cur_next += delta;
5599 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005600 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005601 break;
5602
5603 case ACT_REMOVE:
5604 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5605 cur_next += delta;
5606
Willy Tarreaufa355d42009-11-29 18:12:29 +01005607 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005608 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5609 txn->hdr_idx.used--;
5610 cur_hdr->len = 0;
5611 cur_end = NULL; /* null-term has been rewritten */
5612 break;
5613
5614 }
5615 }
5616 if (cur_end)
5617 *cur_end = term; /* restore the string terminator */
5618
5619 /* keep the link from this header to next one in case of later
5620 * removal of next header.
5621 */
5622 old_idx = cur_idx;
5623 }
5624 return 0;
5625}
5626
5627
5628/* Apply the filter to the status line in the response buffer <rtr>.
5629 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5630 * or -1 if a replacement resulted in an invalid status line.
5631 */
5632int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5633{
5634 char term;
5635 char *cur_ptr, *cur_end;
5636 int done;
5637 struct http_txn *txn = &t->txn;
5638 int len, delta;
5639
5640
Willy Tarreau3d300592007-03-18 18:34:41 +01005641 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005642 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005643 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005644 (exp->action == ACT_ALLOW ||
5645 exp->action == ACT_DENY))
5646 return 0;
5647 else if (exp->action == ACT_REMOVE)
5648 return 0;
5649
5650 done = 0;
5651
Willy Tarreau962c3f42010-01-10 00:15:35 +01005652 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005653 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5654
5655 /* Now we have the status line between cur_ptr and cur_end */
5656
5657 /* The annoying part is that pattern matching needs
5658 * that we modify the contents to null-terminate all
5659 * strings before testing them.
5660 */
5661
5662 term = *cur_end;
5663 *cur_end = '\0';
5664
5665 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5666 switch (exp->action) {
5667 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005668 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005669 done = 1;
5670 break;
5671
5672 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005673 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005674 done = 1;
5675 break;
5676
5677 case ACT_REPLACE:
5678 *cur_end = term; /* restore the string terminator */
5679 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5680 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5681 /* FIXME: if the user adds a newline in the replacement, the
5682 * index will not be recalculated for now, and the new line
5683 * will not be counted as a new header.
5684 */
5685
Willy Tarreaufa355d42009-11-29 18:12:29 +01005686 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005687 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005688 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005689 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005690 cur_ptr, cur_end + 1,
5691 NULL, NULL);
5692 if (unlikely(!cur_end))
5693 return -1;
5694
5695 /* we have a full respnse and we know that we have either a CR
5696 * or an LF at <ptr>.
5697 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005698 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005699 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5700 /* there is no point trying this regex on headers */
5701 return 1;
5702 }
5703 }
5704 *cur_end = term; /* restore the string terminator */
5705 return done;
5706}
5707
5708
5709
5710/*
5711 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5712 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5713 * unparsable response.
5714 */
5715int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5716{
Willy Tarreau3d300592007-03-18 18:34:41 +01005717 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005718 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005719 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005720 int ret;
5721
5722 /*
5723 * The interleaving of transformations and verdicts
5724 * makes it difficult to decide to continue or stop
5725 * the evaluation.
5726 */
5727
Willy Tarreau3d300592007-03-18 18:34:41 +01005728 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005729 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5730 exp->action == ACT_PASS)) {
5731 exp = exp->next;
5732 continue;
5733 }
5734
5735 /* Apply the filter to the status line. */
5736 ret = apply_filter_to_sts_line(t, rtr, exp);
5737 if (unlikely(ret < 0))
5738 return -1;
5739
5740 if (likely(ret == 0)) {
5741 /* The filter did not match the response, it can be
5742 * iterated through all headers.
5743 */
5744 apply_filter_to_resp_headers(t, rtr, exp);
5745 }
5746 exp = exp->next;
5747 }
5748 return 0;
5749}
5750
5751
5752
5753/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005754 * Manage server-side cookies. It can impact performance by about 2% so it is
5755 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005756 */
5757void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5758{
5759 struct http_txn *txn = &t->txn;
5760 char *p1, *p2, *p3, *p4;
5761
Willy Tarreaua15645d2007-03-18 16:22:39 +01005762 char *cur_ptr, *cur_end, *cur_next;
5763 int cur_idx, old_idx, delta;
5764
Willy Tarreaua15645d2007-03-18 16:22:39 +01005765 /* Iterate through the headers.
5766 * we start with the start line.
5767 */
5768 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005769 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005770
5771 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5772 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005773 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005774
5775 cur_hdr = &txn->hdr_idx.v[cur_idx];
5776 cur_ptr = cur_next;
5777 cur_end = cur_ptr + cur_hdr->len;
5778 cur_next = cur_end + cur_hdr->cr + 1;
5779
5780 /* We have one full header between cur_ptr and cur_end, and the
5781 * next header starts at cur_next. We're only interested in
5782 * "Cookie:" headers.
5783 */
5784
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005785 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5786 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005787 old_idx = cur_idx;
5788 continue;
5789 }
5790
5791 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005792 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005793
5794
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005795 /* maybe we only wanted to see if there was a set-cookie. Note that
5796 * the cookie capture is declared in the fronend.
5797 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005798 if (t->be->cookie_name == NULL &&
5799 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005800 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005801 return;
5802
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005803 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005804
5805 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005806 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5807 break;
5808
5809 /* p1 is at the beginning of the cookie name */
5810 p2 = p1;
5811
5812 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5813 p2++;
5814
5815 if (p2 == cur_end || *p2 == ';') /* next cookie */
5816 break;
5817
5818 p3 = p2 + 1; /* skip the '=' sign */
5819 if (p3 == cur_end)
5820 break;
5821
5822 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005823 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005824 p4++;
5825
5826 /* here, we have the cookie name between p1 and p2,
5827 * and its value between p3 and p4.
5828 * we can process it.
5829 */
5830
5831 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005832 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005833 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005834 (p4 - p1 >= t->fe->capture_namelen) &&
5835 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005836 int log_len = p4 - p1;
5837
Willy Tarreau086b3b42007-05-13 21:45:51 +02005838 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005839 Alert("HTTP logging : out of memory.\n");
5840 }
5841
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005842 if (log_len > t->fe->capture_len)
5843 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005844 memcpy(txn->srv_cookie, p1, log_len);
5845 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005846 }
5847
5848 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005849 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5850 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005851 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005852 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005853
5854 /* If the cookie is in insert mode on a known server, we'll delete
5855 * this occurrence because we'll insert another one later.
5856 * We'll delete it too if the "indirect" option is set and we're in
5857 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005858 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5859 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005860 /* this header must be deleted */
5861 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5862 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5863 txn->hdr_idx.used--;
5864 cur_hdr->len = 0;
5865 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005866 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005867
Willy Tarreau3d300592007-03-18 18:34:41 +01005868 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005869 }
5870 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005871 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005872 /* replace bytes p3->p4 with the cookie name associated
5873 * with this server since we know it.
5874 */
5875 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5876 cur_hdr->len += delta;
5877 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005878 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879
Willy Tarreau3d300592007-03-18 18:34:41 +01005880 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005881 }
5882 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005883 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005884 /* insert the cookie name associated with this server
5885 * before existing cookie, and insert a delimitor between them..
5886 */
5887 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5888 cur_hdr->len += delta;
5889 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005890 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891
5892 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005893 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005894 }
5895 }
5896 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005897 else if (t->be->appsession_name != NULL) {
5898 int cmp_len, value_len;
5899 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005900
Cyril Bontéb21570a2009-11-29 20:04:48 +01005901 if (t->be->options2 & PR_O2_AS_PFX) {
5902 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5903 value_begin = p1 + t->be->appsession_name_len;
5904 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5905 } else {
5906 cmp_len = p2 - p1;
5907 value_begin = p3;
5908 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005909 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005910
5911 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5912 /* Cool... it's the right one */
5913 if (t->sessid != NULL) {
5914 /* free previously allocated memory as we don't need it anymore */
5915 pool_free2(apools.sessid, t->sessid);
5916 }
5917 /* Store the sessid in the session for future use */
5918 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5919 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5920 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5921 return;
5922 }
5923 memcpy(t->sessid, value_begin, value_len);
5924 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005925 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005926 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005927 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5928 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005929 /* keep the link from this header to next one */
5930 old_idx = cur_idx;
5931 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005932
5933 if (t->sessid != NULL) {
5934 appsess *asession = NULL;
5935 /* only do insert, if lookup fails */
5936 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5937 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01005938 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005939 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5940 Alert("Not enough Memory process_srv():asession:calloc().\n");
5941 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5942 return;
5943 }
5944 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5945 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5946 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5947 return;
5948 }
5949 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5950 asession->sessid[t->be->appsession_len] = 0;
5951
Willy Tarreau1fac7532010-01-09 19:23:06 +01005952 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005953 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5954 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5955 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5956 return;
5957 }
5958 asession->serverid[0] = '\0';
5959 memcpy(asession->serverid, t->srv->id, server_id_len);
5960
5961 asession->request_count = 0;
5962 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5963 }
5964
5965 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5966 asession->request_count++;
5967 }
5968
5969#if defined(DEBUG_HASH)
5970 Alert("manage_server_side_cookies\n");
5971 appsession_hash_dump(&(t->be->htbl_proxy));
5972#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005973}
5974
5975
5976
5977/*
5978 * Check if response is cacheable or not. Updates t->flags.
5979 */
5980void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5981{
5982 struct http_txn *txn = &t->txn;
5983 char *p1, *p2;
5984
5985 char *cur_ptr, *cur_end, *cur_next;
5986 int cur_idx;
5987
Willy Tarreau5df51872007-11-25 16:20:08 +01005988 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005989 return;
5990
5991 /* Iterate through the headers.
5992 * we start with the start line.
5993 */
5994 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005995 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005996
5997 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5998 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005999 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006000
6001 cur_hdr = &txn->hdr_idx.v[cur_idx];
6002 cur_ptr = cur_next;
6003 cur_end = cur_ptr + cur_hdr->len;
6004 cur_next = cur_end + cur_hdr->cr + 1;
6005
6006 /* We have one full header between cur_ptr and cur_end, and the
6007 * next header starts at cur_next. We're only interested in
6008 * "Cookie:" headers.
6009 */
6010
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006011 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6012 if (val) {
6013 if ((cur_end - (cur_ptr + val) >= 8) &&
6014 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6015 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6016 return;
6017 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006018 }
6019
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006020 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6021 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006022 continue;
6023
6024 /* OK, right now we know we have a cache-control header at cur_ptr */
6025
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006026 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006027
6028 if (p1 >= cur_end) /* no more info */
6029 continue;
6030
6031 /* p1 is at the beginning of the value */
6032 p2 = p1;
6033
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006034 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006035 p2++;
6036
6037 /* we have a complete value between p1 and p2 */
6038 if (p2 < cur_end && *p2 == '=') {
6039 /* we have something of the form no-cache="set-cookie" */
6040 if ((cur_end - p1 >= 21) &&
6041 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6042 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006043 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006044 continue;
6045 }
6046
6047 /* OK, so we know that either p2 points to the end of string or to a comma */
6048 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6049 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6050 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6051 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006052 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006053 return;
6054 }
6055
6056 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006057 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006058 continue;
6059 }
6060 }
6061}
6062
6063
Willy Tarreau58f10d72006-12-04 02:26:12 +01006064/*
6065 * Try to retrieve a known appsession in the URI, then the associated server.
6066 * If the server is found, it's assigned to the session.
6067 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006068void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006070 char *end_params, *first_param, *cur_param, *next_param;
6071 char separator;
6072 int value_len;
6073
6074 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006075
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006076 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006077 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006079 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006080
Cyril Bontéb21570a2009-11-29 20:04:48 +01006081 first_param = NULL;
6082 switch (mode) {
6083 case PR_O2_AS_M_PP:
6084 first_param = memchr(begin, ';', len);
6085 break;
6086 case PR_O2_AS_M_QS:
6087 first_param = memchr(begin, '?', len);
6088 break;
6089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Cyril Bontéb21570a2009-11-29 20:04:48 +01006091 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006093 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006094
Cyril Bontéb21570a2009-11-29 20:04:48 +01006095 switch (mode) {
6096 case PR_O2_AS_M_PP:
6097 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6098 end_params = (char *) begin + len;
6099 }
6100 separator = ';';
6101 break;
6102 case PR_O2_AS_M_QS:
6103 end_params = (char *) begin + len;
6104 separator = '&';
6105 break;
6106 default:
6107 /* unknown mode, shouldn't happen */
6108 return;
6109 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110
Cyril Bontéb21570a2009-11-29 20:04:48 +01006111 cur_param = next_param = end_params;
6112 while (cur_param > first_param) {
6113 cur_param--;
6114 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6115 /* let's see if this is the appsession parameter */
6116 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6117 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6118 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6119 /* Cool... it's the right one */
6120 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6121 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6122 if (value_len > 0) {
6123 manage_client_side_appsession(t, cur_param, value_len);
6124 }
6125 break;
6126 }
6127 next_param = cur_param;
6128 }
6129 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006131 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006132 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006133#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134}
6135
6136
Willy Tarreaub2513902006-12-17 14:52:38 +01006137/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006138 * In a GET or HEAD request, check if the requested URI matches the stats uri
6139 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006140 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006141 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006142 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006143 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006144 *
6145 * Returns 1 if the session's state changes, otherwise 0.
6146 */
6147int stats_check_uri_auth(struct session *t, struct proxy *backend)
6148{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006149 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006150 struct uri_auth *uri_auth = backend->uri_auth;
6151 struct user_auth *user;
6152 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006153 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006154
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006155 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006157 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006158 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006159 return 0;
6160
Willy Tarreau962c3f42010-01-10 00:15:35 +01006161 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006162
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006163 /* the URI is in h */
6164 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006165 return 0;
6166
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006167 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006168 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006169 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006170 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006171 break;
6172 }
6173 h++;
6174 }
6175
6176 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006177 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6178 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006179 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006180 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006181 break;
6182 }
6183 h++;
6184 }
6185 }
6186
Willy Tarreau962c3f42010-01-10 00:15:35 +01006187 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6188 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006189 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006190 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006191 break;
6192 }
6193 h++;
6194 }
6195
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006196 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6197
Willy Tarreaub2513902006-12-17 14:52:38 +01006198 /* we are in front of a interceptable URI. Let's check
6199 * if there's an authentication and if it's valid.
6200 */
6201 user = uri_auth->users;
6202 if (!user) {
6203 /* no user auth required, it's OK */
6204 authenticated = 1;
6205 } else {
6206 authenticated = 0;
6207
6208 /* a user list is defined, we have to check.
6209 * skip 21 chars for "Authorization: Basic ".
6210 */
6211
6212 /* FIXME: this should move to an earlier place */
6213 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006214 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006215 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6216 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006217 if (len > 14 &&
6218 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006219 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006220 break;
6221 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006222 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006223 }
6224
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006225 if (txn->auth_hdr.len < 21 ||
6226 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006227 user = NULL;
6228
6229 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006230 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6231 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006232 user->user_pwd, user->user_len)) {
6233 authenticated = 1;
6234 break;
6235 }
6236 user = user->next;
6237 }
6238 }
6239
6240 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006241 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006242
6243 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006244 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6245 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006246 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006247 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006248 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006249 if (!(t->flags & SN_ERR_MASK))
6250 t->flags |= SN_ERR_PRXCOND;
6251 if (!(t->flags & SN_FINST_MASK))
6252 t->flags |= SN_FINST_R;
6253 return 1;
6254 }
6255
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006256 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006257 * data.
6258 */
Willy Tarreau70089872008-06-13 21:12:51 +02006259 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006260 t->data_source = DATA_SRC_STATS;
6261 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006262 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006263 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6264 t->rep->prod->private = t;
6265 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006266 return 1;
6267}
6268
Willy Tarreau4076a152009-04-02 15:18:36 +02006269/*
6270 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006271 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6272 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006273 */
6274void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6275 struct buffer *buf, struct http_msg *msg,
6276 struct proxy *other_end)
6277{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006278 es->len = buf->r - (buf->data + msg->som);
6279 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006280 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006281 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006282 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006283 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006284 es->when = date; // user-visible date
6285 es->sid = s->uniq_id;
6286 es->srv = s->srv;
6287 es->oe = other_end;
6288 es->src = s->cli_addr;
6289}
Willy Tarreaub2513902006-12-17 14:52:38 +01006290
Willy Tarreaubaaee002006-06-26 02:48:02 +02006291/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006292 * Print a debug line with a header
6293 */
6294void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6295{
6296 int len, max;
6297 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006298 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006299 max = end - start;
6300 UBOUND(max, sizeof(trash) - len - 1);
6301 len += strlcpy2(trash + len, start, max + 1);
6302 trash[len++] = '\n';
6303 write(1, trash, len);
6304}
6305
Willy Tarreau0937bc42009-12-22 15:03:09 +01006306/*
6307 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6308 * the required fields are properly allocated and that we only need to (re)init
6309 * them. This should be used before processing any new request.
6310 */
6311void http_init_txn(struct session *s)
6312{
6313 struct http_txn *txn = &s->txn;
6314 struct proxy *fe = s->fe;
6315
6316 txn->flags = 0;
6317 txn->status = -1;
6318
6319 txn->req.sol = txn->req.eol = NULL;
6320 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6321 txn->rsp.sol = txn->rsp.eol = NULL;
6322 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6323 txn->req.hdr_content_len = 0LL;
6324 txn->rsp.hdr_content_len = 0LL;
6325 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6326 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6327 chunk_reset(&txn->auth_hdr);
6328
6329 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6330 if (fe->options2 & PR_O2_REQBUG_OK)
6331 txn->req.err_pos = -1; /* let buggy requests pass */
6332
Willy Tarreau46023632010-01-07 22:51:47 +01006333 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006334 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6335
Willy Tarreau46023632010-01-07 22:51:47 +01006336 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006337 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6338
6339 if (txn->hdr_idx.v)
6340 hdr_idx_init(&txn->hdr_idx);
6341}
6342
6343/* to be used at the end of a transaction */
6344void http_end_txn(struct session *s)
6345{
6346 struct http_txn *txn = &s->txn;
6347
6348 /* these ones will have been dynamically allocated */
6349 pool_free2(pool2_requri, txn->uri);
6350 pool_free2(pool2_capture, txn->cli_cookie);
6351 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau762a2362010-01-09 13:57:26 +01006352 pool_free2(apools.sessid, s->sessid);
6353 s->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006354 txn->uri = NULL;
6355 txn->srv_cookie = NULL;
6356 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006357
6358 if (txn->req.cap) {
6359 struct cap_hdr *h;
6360 for (h = s->fe->req_cap; h; h = h->next)
6361 pool_free2(h->pool, txn->req.cap[h->index]);
6362 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6363 }
6364
6365 if (txn->rsp.cap) {
6366 struct cap_hdr *h;
6367 for (h = s->fe->rsp_cap; h; h = h->next)
6368 pool_free2(h->pool, txn->rsp.cap[h->index]);
6369 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6370 }
6371
Willy Tarreau0937bc42009-12-22 15:03:09 +01006372}
6373
6374/* to be used at the end of a transaction to prepare a new one */
6375void http_reset_txn(struct session *s)
6376{
6377 http_end_txn(s);
6378 http_init_txn(s);
6379
6380 s->be = s->fe;
6381 s->req->analysers = s->listener->analysers;
6382 s->logs.logwait = s->fe->to_log;
6383 s->srv = s->prev_srv = s->srv_conn = NULL;
6384 s->pend_pos = NULL;
6385 s->conn_retries = s->be->conn_retries;
6386
6387 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6388
6389 s->req->rto = s->fe->timeout.client;
6390 s->req->wto = s->be->timeout.server;
6391 s->req->cto = s->be->timeout.connect;
6392
6393 s->rep->rto = s->be->timeout.server;
6394 s->rep->wto = s->fe->timeout.client;
6395 s->rep->cto = TICK_ETERNITY;
6396
6397 s->req->rex = TICK_ETERNITY;
6398 s->req->wex = TICK_ETERNITY;
6399 s->req->analyse_exp = TICK_ETERNITY;
6400 s->rep->rex = TICK_ETERNITY;
6401 s->rep->wex = TICK_ETERNITY;
6402 s->rep->analyse_exp = TICK_ETERNITY;
6403}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006404
Willy Tarreau8797c062007-05-07 00:55:35 +02006405/************************************************************************/
6406/* The code below is dedicated to ACL parsing and matching */
6407/************************************************************************/
6408
6409
6410
6411
6412/* 1. Check on METHOD
6413 * We use the pre-parsed method if it is known, and store its number as an
6414 * integer. If it is unknown, we use the pointer and the length.
6415 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006416static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006417{
6418 int len, meth;
6419
Willy Tarreauae8b7962007-06-09 23:10:04 +02006420 len = strlen(*text);
6421 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006422
6423 pattern->val.i = meth;
6424 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006425 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006426 if (!pattern->ptr.str)
6427 return 0;
6428 pattern->len = len;
6429 }
6430 return 1;
6431}
6432
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006433static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006434acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6435 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006436{
6437 int meth;
6438 struct http_txn *txn = l7;
6439
Willy Tarreaub6866442008-07-14 23:54:42 +02006440 if (!txn)
6441 return 0;
6442
Willy Tarreau655dce92009-11-08 13:10:58 +01006443 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006444 return 0;
6445
Willy Tarreau8797c062007-05-07 00:55:35 +02006446 meth = txn->meth;
6447 test->i = meth;
6448 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006449 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6450 /* ensure the indexes are not affected */
6451 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006452 test->len = txn->req.sl.rq.m_l;
6453 test->ptr = txn->req.sol;
6454 }
6455 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6456 return 1;
6457}
6458
6459static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6460{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006461 int icase;
6462
Willy Tarreau8797c062007-05-07 00:55:35 +02006463 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006464 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006465
6466 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006467 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006468
6469 /* Other method, we must compare the strings */
6470 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006471 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006472
6473 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6474 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6475 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006476 return ACL_PAT_FAIL;
6477 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006478}
6479
6480/* 2. Check on Request/Status Version
6481 * We simply compare strings here.
6482 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006483static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006484{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006485 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006486 if (!pattern->ptr.str)
6487 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006488 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006489 return 1;
6490}
6491
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006492static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006493acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6494 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006495{
6496 struct http_txn *txn = l7;
6497 char *ptr;
6498 int len;
6499
Willy Tarreaub6866442008-07-14 23:54:42 +02006500 if (!txn)
6501 return 0;
6502
Willy Tarreau655dce92009-11-08 13:10:58 +01006503 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006504 return 0;
6505
Willy Tarreau8797c062007-05-07 00:55:35 +02006506 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006507 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006508
6509 while ((len-- > 0) && (*ptr++ != '/'));
6510 if (len <= 0)
6511 return 0;
6512
6513 test->ptr = ptr;
6514 test->len = len;
6515
6516 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6517 return 1;
6518}
6519
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006520static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006521acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6522 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006523{
6524 struct http_txn *txn = l7;
6525 char *ptr;
6526 int len;
6527
Willy Tarreaub6866442008-07-14 23:54:42 +02006528 if (!txn)
6529 return 0;
6530
Willy Tarreau655dce92009-11-08 13:10:58 +01006531 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006532 return 0;
6533
Willy Tarreau8797c062007-05-07 00:55:35 +02006534 len = txn->rsp.sl.st.v_l;
6535 ptr = txn->rsp.sol;
6536
6537 while ((len-- > 0) && (*ptr++ != '/'));
6538 if (len <= 0)
6539 return 0;
6540
6541 test->ptr = ptr;
6542 test->len = len;
6543
6544 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6545 return 1;
6546}
6547
6548/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006549static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006550acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6551 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006552{
6553 struct http_txn *txn = l7;
6554 char *ptr;
6555 int len;
6556
Willy Tarreaub6866442008-07-14 23:54:42 +02006557 if (!txn)
6558 return 0;
6559
Willy Tarreau655dce92009-11-08 13:10:58 +01006560 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006561 return 0;
6562
Willy Tarreau8797c062007-05-07 00:55:35 +02006563 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006564 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006565
6566 test->i = __strl2ui(ptr, len);
6567 test->flags = ACL_TEST_F_VOL_1ST;
6568 return 1;
6569}
6570
6571/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006572static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006573acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6574 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006575{
6576 struct http_txn *txn = l7;
6577
Willy Tarreaub6866442008-07-14 23:54:42 +02006578 if (!txn)
6579 return 0;
6580
Willy Tarreau655dce92009-11-08 13:10:58 +01006581 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006582 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006583
Willy Tarreauc11416f2007-06-17 16:58:38 +02006584 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6585 /* ensure the indexes are not affected */
6586 return 0;
6587
Willy Tarreau8797c062007-05-07 00:55:35 +02006588 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006589 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006590
Willy Tarreauf3d25982007-05-08 22:45:09 +02006591 /* we do not need to set READ_ONLY because the data is in a buffer */
6592 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006593 return 1;
6594}
6595
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006596static int
6597acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6598 struct acl_expr *expr, struct acl_test *test)
6599{
6600 struct http_txn *txn = l7;
6601
Willy Tarreaub6866442008-07-14 23:54:42 +02006602 if (!txn)
6603 return 0;
6604
Willy Tarreau655dce92009-11-08 13:10:58 +01006605 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006606 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006607
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006608 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6609 /* ensure the indexes are not affected */
6610 return 0;
6611
6612 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006613 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 +01006614 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6615 test->i = AF_INET;
6616
6617 /*
6618 * If we are parsing url in frontend space, we prepare backend stage
6619 * to not parse again the same url ! optimization lazyness...
6620 */
6621 if (px->options & PR_O_HTTP_PROXY)
6622 l4->flags |= SN_ADDR_SET;
6623
6624 test->flags = ACL_TEST_F_READ_ONLY;
6625 return 1;
6626}
6627
6628static int
6629acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6630 struct acl_expr *expr, struct acl_test *test)
6631{
6632 struct http_txn *txn = l7;
6633
Willy Tarreaub6866442008-07-14 23:54:42 +02006634 if (!txn)
6635 return 0;
6636
Willy Tarreau655dce92009-11-08 13:10:58 +01006637 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006638 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006639
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006640 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6641 /* ensure the indexes are not affected */
6642 return 0;
6643
6644 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006645 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 +01006646 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6647
6648 if (px->options & PR_O_HTTP_PROXY)
6649 l4->flags |= SN_ADDR_SET;
6650
6651 test->flags = ACL_TEST_F_READ_ONLY;
6652 return 1;
6653}
6654
Willy Tarreauc11416f2007-06-17 16:58:38 +02006655/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6656 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6657 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006658static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006659acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006660 struct acl_expr *expr, struct acl_test *test)
6661{
6662 struct http_txn *txn = l7;
6663 struct hdr_idx *idx = &txn->hdr_idx;
6664 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006665
Willy Tarreaub6866442008-07-14 23:54:42 +02006666 if (!txn)
6667 return 0;
6668
Willy Tarreau33a7e692007-06-10 19:45:56 +02006669 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6670 /* search for header from the beginning */
6671 ctx->idx = 0;
6672
Willy Tarreau33a7e692007-06-10 19:45:56 +02006673 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6674 test->flags |= ACL_TEST_F_FETCH_MORE;
6675 test->flags |= ACL_TEST_F_VOL_HDR;
6676 test->len = ctx->vlen;
6677 test->ptr = (char *)ctx->line + ctx->val;
6678 return 1;
6679 }
6680
6681 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6682 test->flags |= ACL_TEST_F_VOL_HDR;
6683 return 0;
6684}
6685
Willy Tarreau33a7e692007-06-10 19:45:56 +02006686static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006687acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6688 struct acl_expr *expr, struct acl_test *test)
6689{
6690 struct http_txn *txn = l7;
6691
Willy Tarreaub6866442008-07-14 23:54:42 +02006692 if (!txn)
6693 return 0;
6694
Willy Tarreau655dce92009-11-08 13:10:58 +01006695 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006696 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006697
Willy Tarreauc11416f2007-06-17 16:58:38 +02006698 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6699 /* ensure the indexes are not affected */
6700 return 0;
6701
6702 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6703}
6704
6705static int
6706acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6707 struct acl_expr *expr, struct acl_test *test)
6708{
6709 struct http_txn *txn = l7;
6710
Willy Tarreaub6866442008-07-14 23:54:42 +02006711 if (!txn)
6712 return 0;
6713
Willy Tarreau655dce92009-11-08 13:10:58 +01006714 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006715 return 0;
6716
6717 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6718}
6719
6720/* 6. Check on HTTP header count. The number of occurrences is returned.
6721 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6722 */
6723static int
6724acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006725 struct acl_expr *expr, struct acl_test *test)
6726{
6727 struct http_txn *txn = l7;
6728 struct hdr_idx *idx = &txn->hdr_idx;
6729 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006730 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006731
Willy Tarreaub6866442008-07-14 23:54:42 +02006732 if (!txn)
6733 return 0;
6734
Willy Tarreau33a7e692007-06-10 19:45:56 +02006735 ctx.idx = 0;
6736 cnt = 0;
6737 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6738 cnt++;
6739
6740 test->i = cnt;
6741 test->flags = ACL_TEST_F_VOL_HDR;
6742 return 1;
6743}
6744
Willy Tarreauc11416f2007-06-17 16:58:38 +02006745static int
6746acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6747 struct acl_expr *expr, struct acl_test *test)
6748{
6749 struct http_txn *txn = l7;
6750
Willy Tarreaub6866442008-07-14 23:54:42 +02006751 if (!txn)
6752 return 0;
6753
Willy Tarreau655dce92009-11-08 13:10:58 +01006754 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006755 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006756
Willy Tarreauc11416f2007-06-17 16:58:38 +02006757 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6758 /* ensure the indexes are not affected */
6759 return 0;
6760
6761 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6762}
6763
6764static int
6765acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6766 struct acl_expr *expr, struct acl_test *test)
6767{
6768 struct http_txn *txn = l7;
6769
Willy Tarreaub6866442008-07-14 23:54:42 +02006770 if (!txn)
6771 return 0;
6772
Willy Tarreau655dce92009-11-08 13:10:58 +01006773 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006774 return 0;
6775
6776 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6777}
6778
Willy Tarreau33a7e692007-06-10 19:45:56 +02006779/* 7. Check on HTTP header's integer value. The integer value is returned.
6780 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006781 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006782 */
6783static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006784acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006785 struct acl_expr *expr, struct acl_test *test)
6786{
6787 struct http_txn *txn = l7;
6788 struct hdr_idx *idx = &txn->hdr_idx;
6789 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006790
Willy Tarreaub6866442008-07-14 23:54:42 +02006791 if (!txn)
6792 return 0;
6793
Willy Tarreau33a7e692007-06-10 19:45:56 +02006794 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6795 /* search for header from the beginning */
6796 ctx->idx = 0;
6797
Willy Tarreau33a7e692007-06-10 19:45:56 +02006798 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6799 test->flags |= ACL_TEST_F_FETCH_MORE;
6800 test->flags |= ACL_TEST_F_VOL_HDR;
6801 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6802 return 1;
6803 }
6804
6805 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6806 test->flags |= ACL_TEST_F_VOL_HDR;
6807 return 0;
6808}
6809
Willy Tarreauc11416f2007-06-17 16:58:38 +02006810static int
6811acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6812 struct acl_expr *expr, struct acl_test *test)
6813{
6814 struct http_txn *txn = l7;
6815
Willy Tarreaub6866442008-07-14 23:54:42 +02006816 if (!txn)
6817 return 0;
6818
Willy Tarreau655dce92009-11-08 13:10:58 +01006819 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006820 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006821
Willy Tarreauc11416f2007-06-17 16:58:38 +02006822 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6823 /* ensure the indexes are not affected */
6824 return 0;
6825
6826 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6827}
6828
6829static int
6830acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6831 struct acl_expr *expr, struct acl_test *test)
6832{
6833 struct http_txn *txn = l7;
6834
Willy Tarreaub6866442008-07-14 23:54:42 +02006835 if (!txn)
6836 return 0;
6837
Willy Tarreau655dce92009-11-08 13:10:58 +01006838 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006839 return 0;
6840
6841 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6842}
6843
Willy Tarreau106f9792009-09-19 07:54:16 +02006844/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6845 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6846 */
6847static int
6848acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6849 struct acl_expr *expr, struct acl_test *test)
6850{
6851 struct http_txn *txn = l7;
6852 struct hdr_idx *idx = &txn->hdr_idx;
6853 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6854
6855 if (!txn)
6856 return 0;
6857
6858 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6859 /* search for header from the beginning */
6860 ctx->idx = 0;
6861
6862 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6863 test->flags |= ACL_TEST_F_FETCH_MORE;
6864 test->flags |= ACL_TEST_F_VOL_HDR;
6865 /* Same optimization as url_ip */
6866 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6867 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6868 test->ptr = (void *)&l4->srv_addr.sin_addr;
6869 test->i = AF_INET;
6870 return 1;
6871 }
6872
6873 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6874 test->flags |= ACL_TEST_F_VOL_HDR;
6875 return 0;
6876}
6877
6878static int
6879acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6880 struct acl_expr *expr, struct acl_test *test)
6881{
6882 struct http_txn *txn = l7;
6883
6884 if (!txn)
6885 return 0;
6886
Willy Tarreau655dce92009-11-08 13:10:58 +01006887 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006888 return 0;
6889
6890 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6891 /* ensure the indexes are not affected */
6892 return 0;
6893
6894 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6895}
6896
6897static int
6898acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6899 struct acl_expr *expr, struct acl_test *test)
6900{
6901 struct http_txn *txn = l7;
6902
6903 if (!txn)
6904 return 0;
6905
Willy Tarreau655dce92009-11-08 13:10:58 +01006906 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006907 return 0;
6908
6909 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6910}
6911
Willy Tarreau737b0c12007-06-10 21:28:46 +02006912/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6913 * the first '/' after the possible hostname, and ends before the possible '?'.
6914 */
6915static int
6916acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6917 struct acl_expr *expr, struct acl_test *test)
6918{
6919 struct http_txn *txn = l7;
6920 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006921
Willy Tarreaub6866442008-07-14 23:54:42 +02006922 if (!txn)
6923 return 0;
6924
Willy Tarreau655dce92009-11-08 13:10:58 +01006925 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006926 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006927
Willy Tarreauc11416f2007-06-17 16:58:38 +02006928 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6929 /* ensure the indexes are not affected */
6930 return 0;
6931
Willy Tarreau962c3f42010-01-10 00:15:35 +01006932 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006933 ptr = http_get_path(txn);
6934 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006935 return 0;
6936
6937 /* OK, we got the '/' ! */
6938 test->ptr = ptr;
6939
6940 while (ptr < end && *ptr != '?')
6941 ptr++;
6942
6943 test->len = ptr - test->ptr;
6944
6945 /* we do not need to set READ_ONLY because the data is in a buffer */
6946 test->flags = ACL_TEST_F_VOL_1ST;
6947 return 1;
6948}
6949
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006950static int
6951acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6952 struct acl_expr *expr, struct acl_test *test)
6953{
6954 struct buffer *req = s->req;
6955 struct http_txn *txn = &s->txn;
6956 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006957
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006958 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6959 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6960 */
6961
6962 if (!s || !req)
6963 return 0;
6964
Willy Tarreau655dce92009-11-08 13:10:58 +01006965 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006966 /* Already decoded as OK */
6967 test->flags |= ACL_TEST_F_SET_RES_PASS;
6968 return 1;
6969 }
6970
6971 /* Try to decode HTTP request */
6972 if (likely(req->lr < req->r))
6973 http_msg_analyzer(req, msg, &txn->hdr_idx);
6974
Willy Tarreau655dce92009-11-08 13:10:58 +01006975 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006976 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6977 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6978 return 1;
6979 }
6980 /* wait for final state */
6981 test->flags |= ACL_TEST_F_MAY_CHANGE;
6982 return 0;
6983 }
6984
6985 /* OK we got a valid HTTP request. We have some minor preparation to
6986 * perform so that further checks can rely on HTTP tests.
6987 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006988 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006989 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6990 s->flags |= SN_REDIRECTABLE;
6991
6992 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6993 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6994 return 1;
6995 }
6996
6997 test->flags |= ACL_TEST_F_SET_RES_PASS;
6998 return 1;
6999}
7000
Willy Tarreau8797c062007-05-07 00:55:35 +02007001
7002/************************************************************************/
7003/* All supported keywords must be declared here. */
7004/************************************************************************/
7005
7006/* Note: must not be declared <const> as its list will be overwritten */
7007static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007008 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7009
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007010 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7011 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7012 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7013 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007014
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007015 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7016 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7017 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7018 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7019 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7020 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7021 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7022 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7023 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007024
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007025 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7026 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7027 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7028 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7029 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7030 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7031 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7032 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7033 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7034 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007035 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007036
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007037 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7038 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7039 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7040 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7041 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7042 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7043 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7044 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7045 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007046 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007047
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007048 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7049 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7050 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7051 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7052 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7053 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7054 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007055
Willy Tarreauf3d25982007-05-08 22:45:09 +02007056 { NULL, NULL, NULL, NULL },
7057
7058#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007059 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7060 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7061 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7062 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7063 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7064 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7065 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7066
Willy Tarreau8797c062007-05-07 00:55:35 +02007067 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7068 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7069 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7070 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7071 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7072 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7073 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7074 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7075
7076 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7077 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7078 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7079 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7080 { NULL, NULL, NULL, NULL },
7081#endif
7082}};
7083
7084
7085__attribute__((constructor))
7086static void __http_protocol_init(void)
7087{
7088 acl_register_keywords(&acl_kws);
7089}
7090
7091
Willy Tarreau58f10d72006-12-04 02:26:12 +01007092/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007093 * Local variables:
7094 * c-indent-level: 8
7095 * c-basic-offset: 8
7096 * End:
7097 */