blob: da0a8a83f79a96c50e59877899ee445e26d703b6 [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 Tarreau0f772532006-12-23 20:51:41 +0100566 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100567 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100568 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 }
570 if (!(t->flags & SN_ERR_MASK))
571 t->flags |= err;
572 if (!(t->flags & SN_FINST_MASK))
573 t->flags |= finst;
574}
575
Willy Tarreau80587432006-12-24 17:47:20 +0100576/* This function returns the appropriate error location for the given session
577 * and message.
578 */
579
580struct chunk *error_message(struct session *s, int msgnum)
581{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200582 if (s->be->errmsg[msgnum].str)
583 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100584 else if (s->fe->errmsg[msgnum].str)
585 return &s->fe->errmsg[msgnum];
586 else
587 return &http_err_chunks[msgnum];
588}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200589
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590/*
591 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
592 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
593 */
594static http_meth_t find_http_meth(const char *str, const int len)
595{
596 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100597 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100598
599 m = ((unsigned)*str - 'A');
600
601 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100602 for (h = http_methods[m]; h->len > 0; h++) {
603 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100604 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100605 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100606 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100607 };
608 return HTTP_METH_OTHER;
609 }
610 return HTTP_METH_NONE;
611
612}
613
Willy Tarreau21d2af32008-02-14 20:25:24 +0100614/* Parse the URI from the given transaction (which is assumed to be in request
615 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
616 * It is returned otherwise.
617 */
618static char *
619http_get_path(struct http_txn *txn)
620{
621 char *ptr, *end;
622
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100623 ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100624 end = ptr + txn->req.sl.rq.u_l;
625
626 if (ptr >= end)
627 return NULL;
628
629 /* RFC2616, par. 5.1.2 :
630 * Request-URI = "*" | absuri | abspath | authority
631 */
632
633 if (*ptr == '*')
634 return NULL;
635
636 if (isalpha((unsigned char)*ptr)) {
637 /* this is a scheme as described by RFC3986, par. 3.1 */
638 ptr++;
639 while (ptr < end &&
640 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
641 ptr++;
642 /* skip '://' */
643 if (ptr == end || *ptr++ != ':')
644 return NULL;
645 if (ptr == end || *ptr++ != '/')
646 return NULL;
647 if (ptr == end || *ptr++ != '/')
648 return NULL;
649 }
650 /* skip [user[:passwd]@]host[:[port]] */
651
652 while (ptr < end && *ptr != '/')
653 ptr++;
654
655 if (ptr == end)
656 return NULL;
657
658 /* OK, we got the '/' ! */
659 return ptr;
660}
661
Willy Tarreauefb453c2008-10-26 20:49:47 +0100662/* Returns a 302 for a redirectable request. This may only be called just after
663 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
664 * left unchanged and will follow normal proxy processing.
665 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100667{
668 struct http_txn *txn;
669 struct chunk rdr;
670 char *path;
671 int len;
672
673 /* 1: create the response header */
674 rdr.len = strlen(HTTP_302);
675 rdr.str = trash;
676 memcpy(rdr.str, HTTP_302, rdr.len);
677
678 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200679 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100680 return;
681
682 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
683 rdr.len += s->srv->rdr_len;
684
685 /* 3: add the request URI */
686 txn = &s->txn;
687 path = http_get_path(txn);
688 if (!path)
689 return;
690
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100691 len = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200692 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100693 return;
694
695 memcpy(rdr.str + rdr.len, path, len);
696 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100697 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
698 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699
700 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100701 si->shutr(si);
702 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100703 si->err_type = SI_ET_NONE;
704 si->err_loc = NULL;
705 si->state = SI_ST_CLO;
706
707 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100708 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100709
710 /* FIXME: we should increase a counter of redirects per server and per backend. */
711 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100712 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713}
714
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100715/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100716 * that the server side is closed. Note that err_type is actually a
717 * bitmask, where almost only aborts may be cumulated with other
718 * values. We consider that aborted operations are more important
719 * than timeouts or errors due to the fact that nobody else in the
720 * logs might explain incomplete retries. All others should avoid
721 * being cumulated. It should normally not be possible to have multiple
722 * aborts at once, but just in case, the first one in sequence is reported.
723 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100724void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100725{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100726 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100727
728 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100729 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
730 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100731 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100732 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
733 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100734 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100735 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
736 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100737 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100738 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
739 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100741 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
742 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100743 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100744 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
745 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100746 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100747 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
748 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100749}
750
Willy Tarreau42250582007-04-01 01:30:43 +0200751extern const char sess_term_cond[8];
752extern const char sess_fin_state[8];
753extern const char *monthname[12];
754const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
755const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
756 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
757 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200758struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200759struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100760
Emeric Brun3a058f32009-06-30 18:26:00 +0200761void http_sess_clflog(struct session *s)
762{
763 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
764 struct proxy *fe = s->fe;
765 struct proxy *be = s->be;
766 struct proxy *prx_log;
767 struct http_txn *txn = &s->txn;
768 int tolog, level, err;
769 char *uri, *h;
770 char *svid;
771 struct tm tm;
772 static char tmpline[MAX_SYSLOG_LEN];
773 int hdr;
774 size_t w;
775 int t_request;
776
777 prx_log = fe;
778 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
779 (s->conn_retries != be->conn_retries) ||
780 txn->status >= 500;
781
782 if (s->cli_addr.ss_family == AF_INET)
783 inet_ntop(AF_INET,
784 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
785 pn, sizeof(pn));
786 else
787 inet_ntop(AF_INET6,
788 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
789 pn, sizeof(pn));
790
791 get_gmtime(s->logs.accept_date.tv_sec, &tm);
792
793 /* FIXME: let's limit ourselves to frontend logging for now. */
794 tolog = fe->to_log;
795
796 h = tmpline;
797
798 w = snprintf(h, sizeof(tmpline),
799 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
800 pn,
801 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
802 tm.tm_hour, tm.tm_min, tm.tm_sec);
803 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
804 goto trunc;
805 h += w;
806
807 if (h >= tmpline + sizeof(tmpline) - 4)
808 goto trunc;
809
810 *(h++) = ' ';
811 *(h++) = '\"';
812 uri = txn->uri ? txn->uri : "<BADREQ>";
813 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
814 '#', url_encode_map, uri);
815 *(h++) = '\"';
816
817 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
818 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
819 goto trunc;
820 h += w;
821
822 if (h >= tmpline + sizeof(tmpline) - 9)
823 goto trunc;
824 memcpy(h, " \"-\" \"-\"", 8);
825 h += 8;
826
827 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
828 " %d %03d",
829 (s->cli_addr.ss_family == AF_INET) ?
830 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
831 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
832 (int)s->logs.accept_date.tv_usec/1000);
833 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
834 goto trunc;
835 h += w;
836
837 w = strlen(fe->id);
838 if (h >= tmpline + sizeof(tmpline) - 4 - w)
839 goto trunc;
840 *(h++) = ' ';
841 *(h++) = '\"';
842 memcpy(h, fe->id, w);
843 h += w;
844 *(h++) = '\"';
845
846 w = strlen(be->id);
847 if (h >= tmpline + sizeof(tmpline) - 4 - w)
848 goto trunc;
849 *(h++) = ' ';
850 *(h++) = '\"';
851 memcpy(h, be->id, w);
852 h += w;
853 *(h++) = '\"';
854
855 svid = (tolog & LW_SVID) ?
856 (s->data_source != DATA_SRC_STATS) ?
857 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
858
859 w = strlen(svid);
860 if (h >= tmpline + sizeof(tmpline) - 4 - w)
861 goto trunc;
862 *(h++) = ' ';
863 *(h++) = '\"';
864 memcpy(h, svid, w);
865 h += w;
866 *(h++) = '\"';
867
868 t_request = -1;
869 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
870 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
871 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
872 " %d %ld %ld %ld %ld",
873 t_request,
874 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
875 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
876 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
877 s->logs.t_close);
878 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
879 goto trunc;
880 h += w;
881
882 if (h >= tmpline + sizeof(tmpline) - 8)
883 goto trunc;
884 *(h++) = ' ';
885 *(h++) = '\"';
886 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
887 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
888 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
889 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
890 *(h++) = '\"';
891
892 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
893 " %d %d %d %d %d %ld %ld",
894 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
895 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
896 s->logs.srv_queue_size, s->logs.prx_queue_size);
897
898 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
899 goto trunc;
900 h += w;
901
902 if (txn->cli_cookie) {
903 w = strlen(txn->cli_cookie);
904 if (h >= tmpline + sizeof(tmpline) - 4 - w)
905 goto trunc;
906 *(h++) = ' ';
907 *(h++) = '\"';
908 memcpy(h, txn->cli_cookie, w);
909 h += w;
910 *(h++) = '\"';
911 } else {
912 if (h >= tmpline + sizeof(tmpline) - 5)
913 goto trunc;
914 memcpy(h, " \"-\"", 4);
915 h += 4;
916 }
917
918 if (txn->srv_cookie) {
919 w = strlen(txn->srv_cookie);
920 if (h >= tmpline + sizeof(tmpline) - 4 - w)
921 goto trunc;
922 *(h++) = ' ';
923 *(h++) = '\"';
924 memcpy(h, txn->srv_cookie, w);
925 h += w;
926 *(h++) = '\"';
927 } else {
928 if (h >= tmpline + sizeof(tmpline) - 5)
929 goto trunc;
930 memcpy(h, " \"-\"", 4);
931 h += 4;
932 }
933
934 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
935 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
936 if (h >= sizeof (tmpline) + tmpline - 4)
937 goto trunc;
938 *(h++) = ' ';
939 *(h++) = '\"';
940 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
941 '#', hdr_encode_map, txn->req.cap[hdr]);
942 *(h++) = '\"';
943 }
944 }
945
946 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
947 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
948 if (h >= sizeof (tmpline) + tmpline - 4)
949 goto trunc;
950 *(h++) = ' ';
951 *(h++) = '\"';
952 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
953 '#', hdr_encode_map, txn->rsp.cap[hdr]);
954 *(h++) = '\"';
955 }
956 }
957
958trunc:
959 *h = '\0';
960
961 level = LOG_INFO;
962 if (err && (fe->options2 & PR_O2_LOGERRORS))
963 level = LOG_ERR;
964
965 send_log(prx_log, level, "%s\n", tmpline);
966
967 s->logs.logwait = 0;
968}
969
Willy Tarreau42250582007-04-01 01:30:43 +0200970/*
971 * send a log for the session when we have enough info about it.
972 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100974void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200975{
976 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
977 struct proxy *fe = s->fe;
978 struct proxy *be = s->be;
979 struct proxy *prx_log;
980 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200981 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200982 char *uri, *h;
983 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200984 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200985 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200986 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200987 int hdr;
988
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200989 /* if we don't want to log normal traffic, return now */
990 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
991 (s->conn_retries != be->conn_retries) ||
992 txn->status >= 500;
993 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
994 return;
995
Willy Tarreau42250582007-04-01 01:30:43 +0200996 if (fe->logfac1 < 0 && fe->logfac2 < 0)
997 return;
998 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100999
Emeric Brun3a058f32009-06-30 18:26:00 +02001000 if (prx_log->options2 & PR_O2_CLFLOG)
1001 return http_sess_clflog(s);
1002
Willy Tarreau42250582007-04-01 01:30:43 +02001003 if (s->cli_addr.ss_family == AF_INET)
1004 inet_ntop(AF_INET,
1005 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1006 pn, sizeof(pn));
1007 else
1008 inet_ntop(AF_INET6,
1009 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1010 pn, sizeof(pn));
1011
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001012 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001013
1014 /* FIXME: let's limit ourselves to frontend logging for now. */
1015 tolog = fe->to_log;
1016
1017 h = tmpline;
1018 if (fe->to_log & LW_REQHDR &&
1019 txn->req.cap &&
1020 (h < tmpline + sizeof(tmpline) - 10)) {
1021 *(h++) = ' ';
1022 *(h++) = '{';
1023 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1024 if (hdr)
1025 *(h++) = '|';
1026 if (txn->req.cap[hdr] != NULL)
1027 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1028 '#', hdr_encode_map, txn->req.cap[hdr]);
1029 }
1030 *(h++) = '}';
1031 }
1032
1033 if (fe->to_log & LW_RSPHDR &&
1034 txn->rsp.cap &&
1035 (h < tmpline + sizeof(tmpline) - 7)) {
1036 *(h++) = ' ';
1037 *(h++) = '{';
1038 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1039 if (hdr)
1040 *(h++) = '|';
1041 if (txn->rsp.cap[hdr] != NULL)
1042 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1043 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1044 }
1045 *(h++) = '}';
1046 }
1047
1048 if (h < tmpline + sizeof(tmpline) - 4) {
1049 *(h++) = ' ';
1050 *(h++) = '"';
1051 uri = txn->uri ? txn->uri : "<BADREQ>";
1052 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1053 '#', url_encode_map, uri);
1054 *(h++) = '"';
1055 }
1056 *h = '\0';
1057
1058 svid = (tolog & LW_SVID) ?
1059 (s->data_source != DATA_SRC_STATS) ?
1060 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1061
Willy Tarreau70089872008-06-13 21:12:51 +02001062 t_request = -1;
1063 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1064 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1065
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001066 level = LOG_INFO;
1067 if (err && (fe->options2 & PR_O2_LOGERRORS))
1068 level = LOG_ERR;
1069
1070 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001071 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001072 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1073 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001074 pn,
1075 (s->cli_addr.ss_family == AF_INET) ?
1076 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1077 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001078 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001079 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001080 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001081 t_request,
1082 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001083 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1084 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1085 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1086 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001087 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001088 txn->cli_cookie ? txn->cli_cookie : "-",
1089 txn->srv_cookie ? txn->srv_cookie : "-",
1090 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1091 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1092 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1093 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1094 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001095 (s->flags & SN_REDISP)?"+":"",
1096 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001097 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1098
1099 s->logs.logwait = 0;
1100}
1101
Willy Tarreau117f59e2007-03-04 18:17:17 +01001102
1103/*
1104 * Capture headers from message starting at <som> according to header list
1105 * <cap_hdr>, and fill the <idx> structure appropriately.
1106 */
1107void capture_headers(char *som, struct hdr_idx *idx,
1108 char **cap, struct cap_hdr *cap_hdr)
1109{
1110 char *eol, *sol, *col, *sov;
1111 int cur_idx;
1112 struct cap_hdr *h;
1113 int len;
1114
1115 sol = som + hdr_idx_first_pos(idx);
1116 cur_idx = hdr_idx_first_idx(idx);
1117
1118 while (cur_idx) {
1119 eol = sol + idx->v[cur_idx].len;
1120
1121 col = sol;
1122 while (col < eol && *col != ':')
1123 col++;
1124
1125 sov = col + 1;
1126 while (sov < eol && http_is_lws[(unsigned char)*sov])
1127 sov++;
1128
1129 for (h = cap_hdr; h; h = h->next) {
1130 if ((h->namelen == col - sol) &&
1131 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1132 if (cap[h->index] == NULL)
1133 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001134 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001135
1136 if (cap[h->index] == NULL) {
1137 Alert("HTTP capture : out of memory.\n");
1138 continue;
1139 }
1140
1141 len = eol - sov;
1142 if (len > h->len)
1143 len = h->len;
1144
1145 memcpy(cap[h->index], sov, len);
1146 cap[h->index][len]=0;
1147 }
1148 }
1149 sol = eol + idx->v[cur_idx].cr + 1;
1150 cur_idx = idx->v[cur_idx].next;
1151 }
1152}
1153
1154
Willy Tarreau42250582007-04-01 01:30:43 +02001155/* either we find an LF at <ptr> or we jump to <bad>.
1156 */
1157#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1158
1159/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1160 * otherwise to <http_msg_ood> with <state> set to <st>.
1161 */
1162#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1163 ptr++; \
1164 if (likely(ptr < end)) \
1165 goto good; \
1166 else { \
1167 state = (st); \
1168 goto http_msg_ood; \
1169 } \
1170 } while (0)
1171
1172
Willy Tarreaubaaee002006-06-26 02:48:02 +02001173/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001174 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001175 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1176 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1177 * will give undefined results.
1178 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1179 * and that msg->sol points to the beginning of the response.
1180 * If a complete line is found (which implies that at least one CR or LF is
1181 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1182 * returned indicating an incomplete line (which does not mean that parts have
1183 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1184 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1185 * upon next call.
1186 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001187 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001188 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1189 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001190 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001192const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1193 unsigned int state, const char *ptr, const char *end,
1194 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001195{
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 switch (state) {
1197 http_msg_rpver:
1198 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001199 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1201
1202 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001203 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1205 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001206 state = HTTP_MSG_ERROR;
1207 break;
1208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 http_msg_rpver_sp:
1210 case HTTP_MSG_RPVER_SP:
1211 if (likely(!HTTP_IS_LWS(*ptr))) {
1212 msg->sl.st.c = ptr - msg_buf;
1213 goto http_msg_rpcode;
1214 }
1215 if (likely(HTTP_IS_SPHT(*ptr)))
1216 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1217 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001218 state = HTTP_MSG_ERROR;
1219 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001220
1221 http_msg_rpcode:
1222 case HTTP_MSG_RPCODE:
1223 if (likely(!HTTP_IS_LWS(*ptr)))
1224 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1225
1226 if (likely(HTTP_IS_SPHT(*ptr))) {
1227 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1228 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1229 }
1230
1231 /* so it's a CR/LF, so there is no reason phrase */
1232 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1233 http_msg_rsp_reason:
1234 /* FIXME: should we support HTTP responses without any reason phrase ? */
1235 msg->sl.st.r = ptr - msg_buf;
1236 msg->sl.st.r_l = 0;
1237 goto http_msg_rpline_eol;
1238
1239 http_msg_rpcode_sp:
1240 case HTTP_MSG_RPCODE_SP:
1241 if (likely(!HTTP_IS_LWS(*ptr))) {
1242 msg->sl.st.r = ptr - msg_buf;
1243 goto http_msg_rpreason;
1244 }
1245 if (likely(HTTP_IS_SPHT(*ptr)))
1246 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1247 /* so it's a CR/LF, so there is no reason phrase */
1248 goto http_msg_rsp_reason;
1249
1250 http_msg_rpreason:
1251 case HTTP_MSG_RPREASON:
1252 if (likely(!HTTP_IS_CRLF(*ptr)))
1253 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1254 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1255 http_msg_rpline_eol:
1256 /* We have seen the end of line. Note that we do not
1257 * necessarily have the \n yet, but at least we know that we
1258 * have EITHER \r OR \n, otherwise the response would not be
1259 * complete. We can then record the response length and return
1260 * to the caller which will be able to register it.
1261 */
1262 msg->sl.st.l = ptr - msg->sol;
1263 return ptr;
1264
1265#ifdef DEBUG_FULL
1266 default:
1267 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1268 exit(1);
1269#endif
1270 }
1271
1272 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001273 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 if (ret_state)
1275 *ret_state = state;
1276 if (ret_ptr)
1277 *ret_ptr = (char *)ptr;
1278 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001279}
1280
1281
1282/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 * This function parses a request line between <ptr> and <end>, starting with
1284 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1285 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1286 * will give undefined results.
1287 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1288 * and that msg->sol points to the beginning of the request.
1289 * If a complete line is found (which implies that at least one CR or LF is
1290 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1291 * returned indicating an incomplete line (which does not mean that parts have
1292 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1293 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1294 * upon next call.
1295 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001296 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1298 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001299 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001301const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1302 unsigned int state, const char *ptr, const char *end,
1303 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001304{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 switch (state) {
1306 http_msg_rqmeth:
1307 case HTTP_MSG_RQMETH:
1308 if (likely(HTTP_IS_TOKEN(*ptr)))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001312 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 if (likely(HTTP_IS_CRLF(*ptr))) {
1317 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001318 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 http_msg_req09_uri:
1320 msg->sl.rq.u = ptr - msg_buf;
1321 http_msg_req09_uri_e:
1322 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1323 http_msg_req09_ver:
1324 msg->sl.rq.v = ptr - msg_buf;
1325 msg->sl.rq.v_l = 0;
1326 goto http_msg_rqline_eol;
1327 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001328 state = HTTP_MSG_ERROR;
1329 break;
1330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 http_msg_rqmeth_sp:
1332 case HTTP_MSG_RQMETH_SP:
1333 if (likely(!HTTP_IS_LWS(*ptr))) {
1334 msg->sl.rq.u = ptr - msg_buf;
1335 goto http_msg_rquri;
1336 }
1337 if (likely(HTTP_IS_SPHT(*ptr)))
1338 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1339 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1340 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 http_msg_rquri:
1343 case HTTP_MSG_RQURI:
1344 if (likely(!HTTP_IS_LWS(*ptr)))
1345 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 if (likely(HTTP_IS_SPHT(*ptr))) {
1348 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1353 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 http_msg_rquri_sp:
1356 case HTTP_MSG_RQURI_SP:
1357 if (likely(!HTTP_IS_LWS(*ptr))) {
1358 msg->sl.rq.v = ptr - msg_buf;
1359 goto http_msg_rqver;
1360 }
1361 if (likely(HTTP_IS_SPHT(*ptr)))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1363 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1364 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 http_msg_rqver:
1367 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001368 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001370
1371 if (likely(HTTP_IS_CRLF(*ptr))) {
1372 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1373 http_msg_rqline_eol:
1374 /* We have seen the end of line. Note that we do not
1375 * necessarily have the \n yet, but at least we know that we
1376 * have EITHER \r OR \n, otherwise the request would not be
1377 * complete. We can then record the request length and return
1378 * to the caller which will be able to register it.
1379 */
1380 msg->sl.rq.l = ptr - msg->sol;
1381 return ptr;
1382 }
1383
1384 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001385 state = HTTP_MSG_ERROR;
1386 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388#ifdef DEBUG_FULL
1389 default:
1390 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1391 exit(1);
1392#endif
1393 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001396 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 if (ret_state)
1398 *ret_state = state;
1399 if (ret_ptr)
1400 *ret_ptr = (char *)ptr;
1401 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001403
1404
Willy Tarreau8973c702007-01-21 23:58:29 +01001405/*
1406 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001407 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 * when data are missing and recalled at the exact same location with no
1409 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001410 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001411 * fields. Note that msg->som and msg->sol will be initialized after completing
1412 * the first state, so that none of the msg pointers has to be initialized
1413 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1416{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001417 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001420 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 ptr = buf->lr;
1422 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 if (unlikely(ptr >= end))
1425 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001428 /*
1429 * First, states that are specific to the response only.
1430 * We check them first so that request and headers are
1431 * closer to each other (accessed more often).
1432 */
1433 http_msg_rpbefore:
1434 case HTTP_MSG_RPBEFORE:
1435 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001436 /* we have a start of message, but we have to check
1437 * first if we need to remove some CRLF. We can only
1438 * do this when send_max=0.
1439 */
1440 char *beg = buf->w + buf->send_max;
1441 if (beg >= buf->data + buf->size)
1442 beg -= buf->size;
1443 if (unlikely(ptr != beg)) {
1444 if (buf->send_max)
1445 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001446 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001447 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001449 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001450 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 hdr_idx_init(idx);
1452 state = HTTP_MSG_RPVER;
1453 goto http_msg_rpver;
1454 }
1455
1456 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1457 goto http_msg_invalid;
1458
1459 if (unlikely(*ptr == '\n'))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1461 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1462 /* stop here */
1463
1464 http_msg_rpbefore_cr:
1465 case HTTP_MSG_RPBEFORE_CR:
1466 EXPECT_LF_HERE(ptr, http_msg_invalid);
1467 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1468 /* stop here */
1469
1470 http_msg_rpver:
1471 case HTTP_MSG_RPVER:
1472 case HTTP_MSG_RPVER_SP:
1473 case HTTP_MSG_RPCODE:
1474 case HTTP_MSG_RPCODE_SP:
1475 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001476 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001477 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001478 if (unlikely(!ptr))
1479 return;
1480
1481 /* we have a full response and we know that we have either a CR
1482 * or an LF at <ptr>.
1483 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001484 //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 +01001485 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1486
1487 msg->sol = ptr;
1488 if (likely(*ptr == '\r'))
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1490 goto http_msg_rpline_end;
1491
1492 http_msg_rpline_end:
1493 case HTTP_MSG_RPLINE_END:
1494 /* msg->sol must point to the first of CR or LF. */
1495 EXPECT_LF_HERE(ptr, http_msg_invalid);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1497 /* stop here */
1498
1499 /*
1500 * Second, states that are specific to the request only
1501 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 http_msg_rqbefore:
1503 case HTTP_MSG_RQBEFORE:
1504 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001505 /* we have a start of message, but we have to check
1506 * first if we need to remove some CRLF. We can only
1507 * do this when send_max=0.
1508 */
1509 char *beg = buf->w + buf->send_max;
1510 if (beg >= buf->data + buf->size)
1511 beg -= buf->size;
1512 if (likely(ptr != beg)) {
1513 if (buf->send_max)
1514 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001515 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001516 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001518 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001519 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001520 /* we will need this when keep-alive will be supported
1521 hdr_idx_init(idx);
1522 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 state = HTTP_MSG_RQMETH;
1524 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1528 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 if (unlikely(*ptr == '\n'))
1531 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1532 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001533 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 http_msg_rqbefore_cr:
1536 case HTTP_MSG_RQBEFORE_CR:
1537 EXPECT_LF_HERE(ptr, http_msg_invalid);
1538 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001539 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001540
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 http_msg_rqmeth:
1542 case HTTP_MSG_RQMETH:
1543 case HTTP_MSG_RQMETH_SP:
1544 case HTTP_MSG_RQURI:
1545 case HTTP_MSG_RQURI_SP:
1546 case HTTP_MSG_RQVER:
1547 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001548 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 if (unlikely(!ptr))
1550 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001551
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 /* we have a full request and we know that we have either a CR
1553 * or an LF at <ptr>.
1554 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001555 //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 +01001556 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 msg->sol = ptr;
1559 if (likely(*ptr == '\r'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 http_msg_rqline_end:
1564 case HTTP_MSG_RQLINE_END:
1565 /* check for HTTP/0.9 request : no version information available.
1566 * msg->sol must point to the first of CR or LF.
1567 */
1568 if (unlikely(msg->sl.rq.v_l == 0))
1569 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 EXPECT_LF_HERE(ptr, http_msg_invalid);
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001573 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8973c702007-01-21 23:58:29 +01001575 /*
1576 * Common states below
1577 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 http_msg_hdr_first:
1579 case HTTP_MSG_HDR_FIRST:
1580 msg->sol = ptr;
1581 if (likely(!HTTP_IS_CRLF(*ptr))) {
1582 goto http_msg_hdr_name;
1583 }
1584
1585 if (likely(*ptr == '\r'))
1586 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1587 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 http_msg_hdr_name:
1590 case HTTP_MSG_HDR_NAME:
1591 /* assumes msg->sol points to the first char */
1592 if (likely(HTTP_IS_TOKEN(*ptr)))
1593 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001595 if (likely(*ptr == ':')) {
1596 msg->col = ptr - buf->data;
1597 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1598 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001600 if (likely(msg->err_pos < -1) || *ptr == '\n')
1601 goto http_msg_invalid;
1602
1603 if (msg->err_pos == -1) /* capture error pointer */
1604 msg->err_pos = ptr - buf->data; /* >= 0 now */
1605
1606 /* and we still accept this non-token character */
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 http_msg_hdr_l1_sp:
1610 case HTTP_MSG_HDR_L1_SP:
1611 /* assumes msg->sol points to the first char and msg->col to the colon */
1612 if (likely(HTTP_IS_SPHT(*ptr)))
1613 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 /* header value can be basically anything except CR/LF */
1616 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 if (likely(!HTTP_IS_CRLF(*ptr))) {
1619 goto http_msg_hdr_val;
1620 }
1621
1622 if (likely(*ptr == '\r'))
1623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1624 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 http_msg_hdr_l1_lf:
1627 case HTTP_MSG_HDR_L1_LF:
1628 EXPECT_LF_HERE(ptr, http_msg_invalid);
1629 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 http_msg_hdr_l1_lws:
1632 case HTTP_MSG_HDR_L1_LWS:
1633 if (likely(HTTP_IS_SPHT(*ptr))) {
1634 /* replace HT,CR,LF with spaces */
1635 for (; buf->data+msg->sov < ptr; msg->sov++)
1636 buf->data[msg->sov] = ' ';
1637 goto http_msg_hdr_l1_sp;
1638 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001639 /* we had a header consisting only in spaces ! */
1640 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 goto http_msg_complete_header;
1642
1643 http_msg_hdr_val:
1644 case HTTP_MSG_HDR_VAL:
1645 /* assumes msg->sol points to the first char, msg->col to the
1646 * colon, and msg->sov points to the first character of the
1647 * value.
1648 */
1649 if (likely(!HTTP_IS_CRLF(*ptr)))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 msg->eol = ptr;
1653 /* Note: we could also copy eol into ->eoh so that we have the
1654 * real header end in case it ends with lots of LWS, but is this
1655 * really needed ?
1656 */
1657 if (likely(*ptr == '\r'))
1658 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1659 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 http_msg_hdr_l2_lf:
1662 case HTTP_MSG_HDR_L2_LF:
1663 EXPECT_LF_HERE(ptr, http_msg_invalid);
1664 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 http_msg_hdr_l2_lws:
1667 case HTTP_MSG_HDR_L2_LWS:
1668 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1669 /* LWS: replace HT,CR,LF with spaces */
1670 for (; msg->eol < ptr; msg->eol++)
1671 *msg->eol = ' ';
1672 goto http_msg_hdr_val;
1673 }
1674 http_msg_complete_header:
1675 /*
1676 * It was a new header, so the last one is finished.
1677 * Assumes msg->sol points to the first char, msg->col to the
1678 * colon, msg->sov points to the first character of the value
1679 * and msg->eol to the first CR or LF so we know how the line
1680 * ends. We insert last header into the index.
1681 */
1682 /*
1683 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1684 write(2, msg->sol, msg->eol-msg->sol);
1685 fprintf(stderr,"\n");
1686 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1689 idx, idx->tail) < 0))
1690 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 msg->sol = ptr;
1693 if (likely(!HTTP_IS_CRLF(*ptr))) {
1694 goto http_msg_hdr_name;
1695 }
1696
1697 if (likely(*ptr == '\r'))
1698 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1699 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001700
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 http_msg_last_lf:
1702 case HTTP_MSG_LAST_LF:
1703 /* Assumes msg->sol points to the first of either CR or LF */
1704 EXPECT_LF_HERE(ptr, http_msg_invalid);
1705 ptr++;
1706 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001707 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001709 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 return;
1711#ifdef DEBUG_FULL
1712 default:
1713 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1714 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001715#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 }
1717 http_msg_ood:
1718 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001719 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 buf->lr = ptr;
1721 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 http_msg_invalid:
1724 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001725 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001726 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 return;
1728}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001729
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001730/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1731 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1732 * nothing is done and 1 is returned.
1733 */
1734static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1735{
1736 int delta;
1737 char *cur_end;
1738
1739 if (msg->sl.rq.v_l != 0)
1740 return 1;
1741
1742 msg->sol = req->data + msg->som;
1743 cur_end = msg->sol + msg->sl.rq.l;
1744 delta = 0;
1745
1746 if (msg->sl.rq.u_l == 0) {
1747 /* if no URI was set, add "/" */
1748 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1749 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001750 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001751 }
1752 /* add HTTP version */
1753 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001754 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001755 cur_end += delta;
1756 cur_end = (char *)http_parse_reqline(msg, req->data,
1757 HTTP_MSG_RQMETH,
1758 msg->sol, cur_end + 1,
1759 NULL, NULL);
1760 if (unlikely(!cur_end))
1761 return 0;
1762
1763 /* we have a full HTTP/1.0 request now and we know that
1764 * we have either a CR or an LF at <ptr>.
1765 */
1766 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1767 return 1;
1768}
1769
Willy Tarreau5b154472009-12-21 20:11:07 +01001770/* Parse the Connection: headaer of an HTTP request, and set the transaction
1771 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1772 * is also set so that we don't parse a second time. If some dangerous values
1773 * are encountered, we leave the status to indicate that the request might be
1774 * interpreted as keep-alive, but we also set the connection flags to indicate
1775 * that we WANT it to be a close, so that the header will be fixed. This
1776 * function should only be called when we know we're interested in checking
1777 * the request (not a CONNECT, and FE or BE mangles the header).
1778 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001779void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001780{
1781 struct http_msg *msg = &txn->req;
1782 struct hdr_ctx ctx;
1783 int conn_cl, conn_ka;
1784
1785 if (txn->flags & TX_CON_HDR_PARS)
1786 return;
1787
1788 conn_cl = 0;
1789 conn_ka = 0;
1790 ctx.idx = 0;
1791
1792 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1793 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1794 conn_cl = 1;
1795 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1796 conn_ka = 1;
1797 }
1798
1799 /* Determine if the client wishes keep-alive or close.
1800 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1801 * are persistent unless "Connection: close" is explicitly specified.
1802 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1803 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1804 * they explicitly specify "Connection: Keep-Alive", regardless of any
1805 * optional "Keep-Alive" header.
1806 * Note that if we find a request with both "Connection: close" and
1807 * "Connection: Keep-Alive", we indicate we want a close but don't have
1808 * it, so that it can be enforced later.
1809 */
1810
1811 if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
1812 if (conn_cl) {
1813 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1814 if (!conn_ka)
1815 txn->flags |= TX_REQ_CONN_CLO;
1816 }
1817 } else { /* HTTP/1.0 */
1818 if (!conn_ka)
1819 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO;
1820 else if (conn_cl)
1821 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1822 }
1823 txn->flags |= TX_CON_HDR_PARS;
1824}
1825
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1827 * first byte of body, and increments msg->sov by the number of bytes parsed,
1828 * so that we know we can forward between ->som and ->sov. Note that due to
1829 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1830 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001831 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001833 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001835{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 char *ptr = buf->lr;
1837 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 unsigned int chunk = 0;
1839
1840 /* The chunk size is in the following form, though we are only
1841 * interested in the size and CRLF :
1842 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1843 */
1844 while (1) {
1845 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001846 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001849 if (c < 0) /* not a hex digit anymore */
1850 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001851 if (++ptr >= end)
1852 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 if (chunk & 0xF000000) /* overflow will occur */
1854 return -1;
1855 chunk = (chunk << 4) + c;
1856 }
1857
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 /* empty size not allowed */
1859 if (ptr == buf->lr)
1860 return -1;
1861
1862 while (http_is_spht[(unsigned char)*ptr]) {
1863 if (++ptr >= end)
1864 ptr = buf->data;
1865 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001866 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 }
1868
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 /* Up to there, we know that at least one byte is present at *ptr. Check
1870 * for the end of chunk size.
1871 */
1872 while (1) {
1873 if (likely(HTTP_IS_CRLF(*ptr))) {
1874 /* we now have a CR or an LF at ptr */
1875 if (likely(*ptr == '\r')) {
1876 if (++ptr >= end)
1877 ptr = buf->data;
1878 if (ptr == buf->r)
1879 return 0;
1880 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001881
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 if (*ptr != '\n')
1883 return -1;
1884 if (++ptr >= end)
1885 ptr = buf->data;
1886 /* done */
1887 break;
1888 }
1889 else if (*ptr == ';') {
1890 /* chunk extension, ends at next CRLF */
1891 if (++ptr >= end)
1892 ptr = buf->data;
1893 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001894 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
1896 while (!HTTP_IS_CRLF(*ptr)) {
1897 if (++ptr >= end)
1898 ptr = buf->data;
1899 if (ptr == buf->r)
1900 return 0;
1901 }
1902 /* we have a CRLF now, loop above */
1903 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001904 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001906 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 }
1908
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 /* OK we found our CRLF and now <ptr> points to the next byte,
1910 * which may or may not be present. We save that into ->lr and
1911 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 msg->sov += ptr - buf->lr;
1914 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001915 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001916 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001917 return 1;
1918}
1919
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920/* This function skips trailers in the buffer <buf> associated with HTTP
1921 * message <msg>. The first visited position is buf->lr. If the end of
1922 * the trailers is found, it is automatically scheduled to be forwarded,
1923 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1924 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001925 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1926 * zero. If a parse error is encountered, the function returns < 0 and does not
1927 * change anything except maybe buf->lr and msg->sov. Note that the message
1928 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1929 * which implies that all non-trailers data have already been scheduled for
1930 * forwarding, and that the difference between msg->som and msg->sov exactly
1931 * matches the length of trailers already parsed and not forwarded. It is also
1932 * important to note that this function is designed to be able to parse wrapped
1933 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 */
1935int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1936{
1937 /* we have buf->lr which points to next line. Look for CRLF. */
1938 while (1) {
1939 char *p1 = NULL, *p2 = NULL;
1940 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001941 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942
1943 /* scan current line and stop at LF or CRLF */
1944 while (1) {
1945 if (ptr == buf->r)
1946 return 0;
1947
1948 if (*ptr == '\n') {
1949 if (!p1)
1950 p1 = ptr;
1951 p2 = ptr;
1952 break;
1953 }
1954
1955 if (*ptr == '\r') {
1956 if (p1)
1957 return -1;
1958 p1 = ptr;
1959 }
1960
1961 ptr++;
1962 if (ptr >= buf->data + buf->size)
1963 ptr = buf->data;
1964 }
1965
1966 /* after LF; point to beginning of next line */
1967 p2++;
1968 if (p2 >= buf->data + buf->size)
1969 p2 = buf->data;
1970
Willy Tarreau638cd022010-01-03 07:42:04 +01001971 bytes = p2 - buf->lr;
1972 if (bytes < 0)
1973 bytes += buf->size;
1974
1975 /* schedule this line for forwarding */
1976 msg->sov += bytes;
1977 if (msg->sov >= buf->size)
1978 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979
Willy Tarreau638cd022010-01-03 07:42:04 +01001980 if (p1 == buf->lr) {
1981 /* LF/CRLF at beginning of line => end of trailers at p2.
1982 * Everything was scheduled for forwarding, there's nothing
1983 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001984 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001985 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001986 msg->msg_state = HTTP_MSG_DONE;
1987 return 1;
1988 }
1989 /* OK, next line then */
1990 buf->lr = p2;
1991 }
1992}
1993
1994/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1995 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1996 * ->som, buf->lr in order to include this part into the next forwarding phase.
1997 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1998 * not enough data are available, the function does not change anything and
1999 * returns zero. If a parse error is encountered, the function returns < 0 and
2000 * does not change anything. Note: this function is designed to parse wrapped
2001 * CRLF at the end of the buffer.
2002 */
2003int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2004{
2005 char *ptr;
2006 int bytes;
2007
2008 /* NB: we'll check data availabilty at the end. It's not a
2009 * problem because whatever we match first will be checked
2010 * against the correct length.
2011 */
2012 bytes = 1;
2013 ptr = buf->lr;
2014 if (*ptr == '\r') {
2015 bytes++;
2016 ptr++;
2017 if (ptr >= buf->data + buf->size)
2018 ptr = buf->data;
2019 }
2020
2021 if (buf->l < bytes)
2022 return 0;
2023
2024 if (*ptr != '\n')
2025 return -1;
2026
2027 ptr++;
2028 if (ptr >= buf->data + buf->size)
2029 ptr = buf->data;
2030 buf->lr = ptr;
2031 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2032 msg->sov = ptr - buf->data;
2033 msg->som = msg->sov - bytes;
2034 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2035 return 1;
2036}
Willy Tarreau5b154472009-12-21 20:11:07 +01002037
Willy Tarreau83e3af02009-12-28 17:39:57 +01002038void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2039{
2040 char *end = buf->data + buf->size;
2041 int off = buf->data + buf->size - buf->w;
2042
2043 /* two possible cases :
2044 * - the buffer is in one contiguous block, we move it in-place
2045 * - the buffer is in two blocks, we move it via the trash
2046 */
2047 if (buf->l) {
2048 int block1 = buf->l;
2049 int block2 = 0;
2050 if (buf->r <= buf->w) {
2051 /* non-contiguous block */
2052 block1 = buf->data + buf->size - buf->w;
2053 block2 = buf->r - buf->data;
2054 }
2055 if (block2)
2056 memcpy(trash, buf->data, block2);
2057 memmove(buf->data, buf->w, block1);
2058 if (block2)
2059 memcpy(buf->data + block1, trash, block2);
2060 }
2061
2062 /* adjust all known pointers */
2063 buf->w = buf->data;
2064 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2065 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2066 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2067 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2068
2069 /* adjust relative pointers */
2070 msg->som = 0;
2071 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2072 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2073 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2074
2075 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2076 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2077
2078 if (msg->err_pos >= 0) {
2079 msg->err_pos += off;
2080 if (msg->err_pos >= buf->size)
2081 msg->err_pos -= buf->size;
2082 }
2083
2084 buf->flags &= ~BF_FULL;
2085 if (buf->l >= buffer_max_len(buf))
2086 buf->flags |= BF_FULL;
2087}
2088
Willy Tarreaud787e662009-07-07 10:14:51 +02002089/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2090 * processing can continue on next analysers, or zero if it either needs more
2091 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2092 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2093 * when it has nothing left to do, and may remove any analyser when it wants to
2094 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002095 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002096int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002097{
Willy Tarreau59234e92008-11-30 23:51:27 +01002098 /*
2099 * We will parse the partial (or complete) lines.
2100 * We will check the request syntax, and also join multi-line
2101 * headers. An index of all the lines will be elaborated while
2102 * parsing.
2103 *
2104 * For the parsing, we use a 28 states FSM.
2105 *
2106 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002107 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002108 * req->data + msg->eoh = end of processed headers / start of current one
2109 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 * req->lr = first non-visited byte
2111 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002112 *
2113 * At end of parsing, we may perform a capture of the error (if any), and
2114 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2115 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2116 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002118
Willy Tarreau59234e92008-11-30 23:51:27 +01002119 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002120 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002121 struct http_txn *txn = &s->txn;
2122 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002123 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002124
Willy Tarreau6bf17362009-02-24 10:48:35 +01002125 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2126 now_ms, __FUNCTION__,
2127 s,
2128 req,
2129 req->rex, req->wex,
2130 req->flags,
2131 req->l,
2132 req->analysers);
2133
Willy Tarreau52a0c602009-08-16 22:45:38 +02002134 /* we're speaking HTTP here, so let's speak HTTP to the client */
2135 s->srv_error = http_return_srv_error;
2136
Willy Tarreau83e3af02009-12-28 17:39:57 +01002137 /* There's a protected area at the end of the buffer for rewriting
2138 * purposes. We don't want to start to parse the request if the
2139 * protected area is affected, because we may have to move processed
2140 * data later, which is much more complicated.
2141 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002142 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
2143 if (unlikely((req->flags & BF_FULL) ||
2144 req->r < req->lr ||
2145 req->r > req->data + req->size - global.tune.maxrewrite)) {
2146 if (req->send_max) {
2147 /* some data has still not left the buffer, wake us once that's done */
2148 buffer_dont_connect(req);
2149 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2150 return 0;
2151 }
2152 if (req->l <= req->size - global.tune.maxrewrite)
2153 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002154 }
2155
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002156 if (likely(req->lr < req->r))
2157 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002158 }
2159
Willy Tarreau59234e92008-11-30 23:51:27 +01002160 /* 1: we might have to print this header in debug mode */
2161 if (unlikely((global.mode & MODE_DEBUG) &&
2162 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002163 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002164 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 sol = req->data + msg->som;
2167 eol = sol + msg->sl.rq.l;
2168 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002169
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 sol += hdr_idx_first_pos(&txn->hdr_idx);
2171 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002172
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 while (cur_idx) {
2174 eol = sol + txn->hdr_idx.v[cur_idx].len;
2175 debug_hdr("clihdr", s, sol, eol);
2176 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2177 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002178 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002179 }
2180
Willy Tarreau58f10d72006-12-04 02:26:12 +01002181
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 /*
2183 * Now we quickly check if we have found a full valid request.
2184 * If not so, we check the FD and buffer states before leaving.
2185 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002186 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 * requests are checked first.
2188 *
2189 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002190
Willy Tarreau655dce92009-11-08 13:10:58 +01002191 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002192 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002193 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002194 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2196 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 /* 1: Since we are in header mode, if there's no space
2199 * left for headers, we won't be able to free more
2200 * later, so the session will never terminate. We
2201 * must terminate it now.
2202 */
2203 if (unlikely(req->flags & BF_FULL)) {
2204 /* FIXME: check if URI is set and return Status
2205 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002206 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002207 goto return_bad_req;
2208 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002209
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 /* 2: have we encountered a read error ? */
2211 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002212 if (txn->flags & TX_NOT_FIRST)
2213 goto failed_keep_alive;
2214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002216 if (msg->err_pos >= 0)
2217 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002218 msg->msg_state = HTTP_MSG_ERROR;
2219 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002220
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002221 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002222 if (s->listener->counters)
2223 s->listener->counters->failed_req++;
2224
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 if (!(s->flags & SN_ERR_MASK))
2226 s->flags |= SN_ERR_CLICL;
2227 if (!(s->flags & SN_FINST_MASK))
2228 s->flags |= SN_FINST_R;
2229 return 0;
2230 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002231
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 /* 3: has the read timeout expired ? */
2233 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
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 /* read timeout : give up with an error message. */
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 txn->status = 408;
2241 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2242 msg->msg_state = HTTP_MSG_ERROR;
2243 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002244
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002245 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002246 if (s->listener->counters)
2247 s->listener->counters->failed_req++;
2248
Willy Tarreau59234e92008-11-30 23:51:27 +01002249 if (!(s->flags & SN_ERR_MASK))
2250 s->flags |= SN_ERR_CLITO;
2251 if (!(s->flags & SN_FINST_MASK))
2252 s->flags |= SN_FINST_R;
2253 return 0;
2254 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002255
Willy Tarreau59234e92008-11-30 23:51:27 +01002256 /* 4: have we encountered a close ? */
2257 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002258 if (txn->flags & TX_NOT_FIRST)
2259 goto failed_keep_alive;
2260
Willy Tarreau4076a152009-04-02 15:18:36 +02002261 if (msg->err_pos >= 0)
2262 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 txn->status = 400;
2264 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2265 msg->msg_state = HTTP_MSG_ERROR;
2266 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002267
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002268 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002269 if (s->listener->counters)
2270 s->listener->counters->failed_req++;
2271
Willy Tarreau59234e92008-11-30 23:51:27 +01002272 if (!(s->flags & SN_ERR_MASK))
2273 s->flags |= SN_ERR_CLICL;
2274 if (!(s->flags & SN_FINST_MASK))
2275 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002276 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002277 }
2278
Willy Tarreau520d95e2009-09-19 21:04:57 +02002279 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002280 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002281 req->flags &= ~BF_DONT_READ;
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002282
Willy Tarreau59234e92008-11-30 23:51:27 +01002283 /* just set the request timeout once at the beginning of the request */
2284 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002285 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002286
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 /* we're not ready yet */
2288 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002289
2290 failed_keep_alive:
2291 /* Here we process low-level errors for keep-alive requests. In
2292 * short, if the request is not the first one and it experiences
2293 * a timeout, read error or shutdown, we just silently close so
2294 * that the client can try again.
2295 */
2296 txn->status = 0;
2297 msg->msg_state = HTTP_MSG_RQBEFORE;
2298 req->analysers = 0;
2299 s->logs.logwait = 0;
2300 stream_int_cond_close(req->prod, NULL);
2301 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002302 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002303
Willy Tarreaud787e662009-07-07 10:14:51 +02002304 /* OK now we have a complete HTTP request with indexed headers. Let's
2305 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002306 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2307 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2308 * points to the CRLF of the request line. req->lr points to the first
2309 * byte after the last LF. msg->col and msg->sov point to the first
2310 * byte of data. msg->eol cannot be trusted because it may have been
2311 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002312 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002313
Willy Tarreaud787e662009-07-07 10:14:51 +02002314 /* Maybe we found in invalid header name while we were configured not
2315 * to block on that, so we have to capture it now.
2316 */
2317 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002318 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2319
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 /* ensure we keep this pointer to the beginning of the message */
2321 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 /*
2324 * 1: identify the method
2325 */
2326 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2327
2328 /* we can make use of server redirect on GET and HEAD */
2329 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2330 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /*
2333 * 2: check if the URI matches the monitor_uri.
2334 * We have to do this for every request which gets in, because
2335 * the monitor-uri is defined by the frontend.
2336 */
2337 if (unlikely((s->fe->monitor_uri_len != 0) &&
2338 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2339 !memcmp(&req->data[msg->sl.rq.u],
2340 s->fe->monitor_uri,
2341 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002344 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002348
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002350 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2351 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 ret = acl_pass(ret);
2354 if (cond->pol == ACL_COND_UNLESS)
2355 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 if (ret) {
2358 /* we fail this request, let's return 503 service unavail */
2359 txn->status = 503;
2360 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2361 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002362 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002363 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 /* nothing to fail, let's reply normaly */
2366 txn->status = 200;
2367 stream_int_retnclose(req->prod, &http_200_chunk);
2368 goto return_prx_cond;
2369 }
2370
2371 /*
2372 * 3: Maybe we have to copy the original REQURI for the logs ?
2373 * Note: we cannot log anymore if the request has been
2374 * classified as invalid.
2375 */
2376 if (unlikely(s->logs.logwait & LW_REQ)) {
2377 /* we have a complete HTTP request that we must log */
2378 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2379 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (urilen >= REQURI_LEN)
2382 urilen = REQURI_LEN - 1;
2383 memcpy(txn->uri, &req->data[msg->som], urilen);
2384 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002385
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 if (!(s->logs.logwait &= ~LW_REQ))
2387 s->do_log(s);
2388 } else {
2389 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002390 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 }
Willy Tarreau06619262006-12-17 08:37:22 +01002392
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002394 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2395 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002396
Willy Tarreau5b154472009-12-21 20:11:07 +01002397 /* ... and check if the request is HTTP/1.1 or above */
2398 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002399 ((req->data[msg->sl.rq.v + 5] > '1') ||
2400 ((req->data[msg->sl.rq.v + 5] == '1') &&
2401 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002402 txn->flags |= TX_REQ_VER_11;
2403
2404 /* "connection" has not been parsed yet */
2405 txn->flags &= ~TX_CON_HDR_PARS;
2406
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002407 /* transfer length unknown*/
2408 txn->flags &= ~TX_REQ_XFER_LEN;
2409
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 /* 5: we may need to capture headers */
2411 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2412 capture_headers(req->data + msg->som, &txn->hdr_idx,
2413 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002414
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002415 /* 6: determine the transfer-length.
2416 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2417 * the presence of a message-body in a REQUEST and its transfer length
2418 * must be determined that way (in order of precedence) :
2419 * 1. The presence of a message-body in a request is signaled by the
2420 * inclusion of a Content-Length or Transfer-Encoding header field
2421 * in the request's header fields. When a request message contains
2422 * both a message-body of non-zero length and a method that does
2423 * not define any semantics for that request message-body, then an
2424 * origin server SHOULD either ignore the message-body or respond
2425 * with an appropriate error message (e.g., 413). A proxy or
2426 * gateway, when presented the same request, SHOULD either forward
2427 * the request inbound with the message- body or ignore the
2428 * message-body when determining a response.
2429 *
2430 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2431 * and the "chunked" transfer-coding (Section 6.2) is used, the
2432 * transfer-length is defined by the use of this transfer-coding.
2433 * If a Transfer-Encoding header field is present and the "chunked"
2434 * transfer-coding is not present, the transfer-length is defined
2435 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002436 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002437 * 3. If a Content-Length header field is present, its decimal value in
2438 * OCTETs represents both the entity-length and the transfer-length.
2439 * If a message is received with both a Transfer-Encoding header
2440 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002441 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 * 4. By the server closing the connection. (Closing the connection
2443 * cannot be used to indicate the end of a request body, since that
2444 * would leave no possibility for the server to send back a response.)
2445 *
2446 * Whenever a transfer-coding is applied to a message-body, the set of
2447 * transfer-codings MUST include "chunked", unless the message indicates
2448 * it is terminated by closing the connection. When the "chunked"
2449 * transfer-coding is used, it MUST be the last transfer-coding applied
2450 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002451 */
2452
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002453 /* CONNECT sets a tunnel and ignores everything else */
2454 if (txn->meth == HTTP_METH_CONNECT)
2455 goto skip_xfer_len;
2456
2457 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002458 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002459 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002460 while ((txn->flags & TX_REQ_VER_11) &&
2461 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002462 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2463 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2464 else if (txn->flags & TX_REQ_TE_CHNK) {
2465 /* bad transfer-encoding (chunked followed by something else) */
2466 use_close_only = 1;
2467 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2468 break;
2469 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002470 }
2471
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002473 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2475 signed long long cl;
2476
2477 if (!ctx.vlen)
2478 goto return_bad_req;
2479
2480 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2481 goto return_bad_req; /* parse failure */
2482
2483 if (cl < 0)
2484 goto return_bad_req;
2485
2486 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2487 goto return_bad_req; /* already specified, was different */
2488
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002489 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002490 msg->hdr_content_len = cl;
2491 }
2492
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002493 /* bodyless requests have a known length */
2494 if (!use_close_only)
2495 txn->flags |= TX_REQ_XFER_LEN;
2496
2497 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002498 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002499 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002500 req->analyse_exp = TICK_ETERNITY;
2501 return 1;
2502
2503 return_bad_req:
2504 /* We centralize bad requests processing here */
2505 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2506 /* we detected a parsing error. We want to archive this request
2507 * in the dedicated proxy area for later troubleshooting.
2508 */
2509 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2510 }
2511
2512 txn->req.msg_state = HTTP_MSG_ERROR;
2513 txn->status = 400;
2514 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002515
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002516 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002517 if (s->listener->counters)
2518 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002519
2520 return_prx_cond:
2521 if (!(s->flags & SN_ERR_MASK))
2522 s->flags |= SN_ERR_PRXCOND;
2523 if (!(s->flags & SN_FINST_MASK))
2524 s->flags |= SN_FINST_R;
2525
2526 req->analysers = 0;
2527 req->analyse_exp = TICK_ETERNITY;
2528 return 0;
2529}
2530
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002531/* This stream analyser runs all HTTP request processing which is common to
2532 * frontends and backends, which means blocking ACLs, filters, connection-close,
2533 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002534 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002535 * either needs more data or wants to immediately abort the request (eg: deny,
2536 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002538int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002539{
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 struct http_txn *txn = &s->txn;
2541 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002542 struct acl_cond *cond;
2543 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002544 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002545 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002546
Willy Tarreau655dce92009-11-08 13:10:58 +01002547 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002548 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002549 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002550 return 0;
2551 }
2552
Willy Tarreau3a816292009-07-07 10:55:49 +02002553 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002554 req->analyse_exp = TICK_ETERNITY;
2555
2556 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2557 now_ms, __FUNCTION__,
2558 s,
2559 req,
2560 req->rex, req->wex,
2561 req->flags,
2562 req->l,
2563 req->analysers);
2564
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002565 /* first check whether we have some ACLs set to block this request */
2566 list_for_each_entry(cond, &px->block_cond, list) {
2567 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002568
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002569 ret = acl_pass(ret);
2570 if (cond->pol == ACL_COND_UNLESS)
2571 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002572
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002573 if (ret) {
2574 txn->status = 403;
2575 /* let's log the request time */
2576 s->logs.tv_request = now;
2577 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2578 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002580 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002581
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002582 /* try headers filters */
2583 if (px->req_exp != NULL) {
2584 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2585 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002586
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 /* has the request been denied ? */
2588 if (txn->flags & TX_CLDENY) {
2589 /* no need to go further */
2590 txn->status = 403;
2591 /* let's log the request time */
2592 s->logs.tv_request = now;
2593 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2594 goto return_prx_cond;
2595 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002596
2597 /* When a connection is tarpitted, we use the tarpit timeout,
2598 * which may be the same as the connect timeout if unspecified.
2599 * If unset, then set it to zero because we really want it to
2600 * eventually expire. We build the tarpit as an analyser.
2601 */
2602 if (txn->flags & TX_CLTARPIT) {
2603 buffer_erase(s->req);
2604 /* wipe the request out so that we can drop the connection early
2605 * if the client closes first.
2606 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002607 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002608 req->analysers = 0; /* remove switching rules etc... */
2609 req->analysers |= AN_REQ_HTTP_TARPIT;
2610 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2611 if (!req->analyse_exp)
2612 req->analyse_exp = tick_add(now_ms, 0);
2613 return 1;
2614 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002615 }
Willy Tarreau06619262006-12-17 08:37:22 +01002616
Willy Tarreau5b154472009-12-21 20:11:07 +01002617 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2618 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002619 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002620
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002621 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622 ((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 +01002623 int tmp = TX_CON_WANT_TUN;
2624 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2625 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002626 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2627 tmp = TX_CON_WANT_SCL;
Willy Tarreau5b154472009-12-21 20:11:07 +01002628 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2629 tmp = TX_CON_WANT_CLO;
2630
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002631 if (!(txn->flags & TX_REQ_XFER_LEN))
2632 tmp = TX_CON_WANT_CLO;
2633
Willy Tarreau5b154472009-12-21 20:11:07 +01002634 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002635 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002636
2637 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2638 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2639 }
2640
2641 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2642 * once we know the backend, because the tunnel mode can be implied by the
2643 * lack of any close/keepalive options in both the FE and the BE. Since
2644 * this information can evolve with time, we proceed by trying to make the
2645 * header status match the desired status. For this, we'll have to adjust
2646 * the "Connection" header. The test for persistent connections has already
2647 * been performed, so we only enter here if there is a risk the connection
2648 * is considered as persistent and we want it to be closed on the server
2649 * side. It would be nice if we could enter this place only when a
2650 * Connection header exists. Note that a CONNECT method will not enter
2651 * here.
2652 */
2653 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002654 char *cur_ptr, *cur_end, *cur_next;
2655 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002656 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002657 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002658
Willy Tarreau5b154472009-12-21 20:11:07 +01002659 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002660 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002661
Willy Tarreau5b154472009-12-21 20:11:07 +01002662 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002663 cur_hdr = &txn->hdr_idx.v[cur_idx];
2664 cur_ptr = cur_next;
2665 cur_end = cur_ptr + cur_hdr->len;
2666 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002667
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002668 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002669 if (!val)
2670 continue;
2671
2672 /* 3 possibilities :
2673 * - we have already set "Connection: close" or we're in
2674 * HTTP/1.0, so we remove this line.
2675 * - we have not yet set "Connection: close", but this line
2676 * indicates close. We leave it untouched and set the flag.
2677 * - we have not yet set "Connection: close", and this line
2678 * indicates non-close. We replace it and set the flag.
2679 */
2680 if (must_delete) {
2681 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2682 http_msg_move_end(&txn->req, delta);
2683 cur_next += delta;
2684 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2685 txn->hdr_idx.used--;
2686 cur_hdr->len = 0;
2687 txn->flags |= TX_REQ_CONN_CLO;
2688 } else {
2689 if (cur_end - cur_ptr - val != 5 ||
2690 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2691 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2692 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002693 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002694 cur_hdr->len += delta;
2695 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002696 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002697 txn->flags |= TX_REQ_CONN_CLO;
2698 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002699 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002700 } /* for loop */
2701 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002702
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002703 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002704 list_for_each_entry(wl, &px->req_add, list) {
2705 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706 goto return_bad_req;
2707 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002708
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002709 /* check if stats URI was requested, and if an auth is needed */
2710 if (px->uri_auth != NULL &&
2711 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2712 /* we have to check the URI and auth for this request.
2713 * FIXME!!! that one is rather dangerous, we want to
2714 * make it follow standard rules (eg: clear req->analysers).
2715 */
2716 if (stats_check_uri_auth(s, px)) {
2717 req->analysers = 0;
2718 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002719 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002720 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002721
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722 /* check whether we have some ACLs set to redirect this request */
2723 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002724 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002725
Willy Tarreauf285f542010-01-03 20:03:03 +01002726 if (rule->cond) {
2727 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2728 ret = acl_pass(ret);
2729 if (rule->cond->pol == ACL_COND_UNLESS)
2730 ret = !ret;
2731 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002732
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002734 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002735 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002736
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002737 /* build redirect message */
2738 switch(rule->code) {
2739 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002740 msg_fmt = HTTP_303;
2741 break;
2742 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002743 msg_fmt = HTTP_301;
2744 break;
2745 case 302:
2746 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 msg_fmt = HTTP_302;
2748 break;
2749 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002750
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002751 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002753
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 switch(rule->type) {
2755 case REDIRECT_TYPE_PREFIX: {
2756 const char *path;
2757 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002758
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002759 path = http_get_path(txn);
2760 /* build message using path */
2761 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002762 pathlen = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2764 int qs = 0;
2765 while (qs < pathlen) {
2766 if (path[qs] == '?') {
2767 pathlen = qs;
2768 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002769 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002771 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002772 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 } else {
2774 path = "/";
2775 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002776 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002777
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002778 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002779 goto return_bad_req;
2780
2781 /* add prefix. Note that if prefix == "/", we don't want to
2782 * add anything, otherwise it makes it hard for the user to
2783 * configure a self-redirection.
2784 */
2785 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002786 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2787 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002788 }
2789
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002790 /* add path */
2791 memcpy(rdr.str + rdr.len, path, pathlen);
2792 rdr.len += pathlen;
2793 break;
2794 }
2795 case REDIRECT_TYPE_LOCATION:
2796 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002797 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002799
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002800 /* add location */
2801 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2802 rdr.len += rule->rdr_len;
2803 break;
2804 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002805
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002806 if (rule->cookie_len) {
2807 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2808 rdr.len += 14;
2809 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2810 rdr.len += rule->cookie_len;
2811 memcpy(rdr.str + rdr.len, "\r\n", 2);
2812 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002813 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002814
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002815 /* add end of headers and the keep-alive/close status.
2816 * We may choose to set keep-alive if the Location begins
2817 * with a slash, because the client will come back to the
2818 * same server.
2819 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820 txn->status = rule->code;
2821 /* let's log the request time */
2822 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002823
2824 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2825 (txn->flags & TX_REQ_XFER_LEN) &&
2826 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2827 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2828 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2829 /* keep-alive possible */
2830 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2831 rdr.len += 28;
2832 buffer_write(req->prod->ob, rdr.str, rdr.len);
2833 /* "eat" the request */
2834 buffer_ignore(req, msg->sov - msg->som);
2835 msg->som = msg->sov;
2836 req->analysers = AN_REQ_HTTP_XFER_BODY;
2837 txn->req.msg_state = HTTP_MSG_DONE;
2838 txn->rsp.msg_state = HTTP_MSG_CLOSED;
2839 break;
2840 } else {
2841 /* keep-alive not possible */
2842 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2843 rdr.len += 23;
2844 stream_int_cond_close(req->prod, &rdr);
2845 goto return_prx_cond;
2846 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002847 }
2848 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002849
Willy Tarreaud98cf932009-12-27 22:54:55 +01002850 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2851 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002852 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002853 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002854 req->flags |= BF_DONT_READ;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002855 else
2856 req->flags &= ~BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002857
Willy Tarreau2be39392010-01-03 17:24:51 +01002858 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2859 * If this happens, then the data will not come immediately, so we must
2860 * send all what we have without waiting. Note that due to the small gain
2861 * in waiting for the body of the request, it's easier to simply put the
2862 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2863 * itself once used.
2864 */
2865 req->flags |= BF_SEND_DONTWAIT;
2866
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002867 /* that's OK for us now, let's move on to next analysers */
2868 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002869
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002870 return_bad_req:
2871 /* We centralize bad requests processing here */
2872 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2873 /* we detected a parsing error. We want to archive this request
2874 * in the dedicated proxy area for later troubleshooting.
2875 */
2876 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2877 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002878
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002879 txn->req.msg_state = HTTP_MSG_ERROR;
2880 txn->status = 400;
2881 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002882
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002883 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002884 if (s->listener->counters)
2885 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002886
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002887 return_prx_cond:
2888 if (!(s->flags & SN_ERR_MASK))
2889 s->flags |= SN_ERR_PRXCOND;
2890 if (!(s->flags & SN_FINST_MASK))
2891 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002892
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 req->analysers = 0;
2894 req->analyse_exp = TICK_ETERNITY;
2895 return 0;
2896}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002897
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002898/* This function performs all the processing enabled for the current request.
2899 * It returns 1 if the processing can continue on next analysers, or zero if it
2900 * needs more data, encounters an error, or wants to immediately abort the
2901 * request. It relies on buffers flags, and updates s->req->analysers.
2902 */
2903int http_process_request(struct session *s, struct buffer *req, int an_bit)
2904{
2905 struct http_txn *txn = &s->txn;
2906 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002907
Willy Tarreau655dce92009-11-08 13:10:58 +01002908 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002909 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002910 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002911 return 0;
2912 }
2913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002914 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2915 now_ms, __FUNCTION__,
2916 s,
2917 req,
2918 req->rex, req->wex,
2919 req->flags,
2920 req->l,
2921 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002922
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 /*
2924 * Right now, we know that we have processed the entire headers
2925 * and that unwanted requests have been filtered out. We can do
2926 * whatever we want with the remaining request. Also, now we
2927 * may have separate values for ->fe, ->be.
2928 */
Willy Tarreau06619262006-12-17 08:37:22 +01002929
Willy Tarreau59234e92008-11-30 23:51:27 +01002930 /*
2931 * If HTTP PROXY is set we simply get remote server address
2932 * parsing incoming request.
2933 */
2934 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2935 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2936 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002937
Willy Tarreau59234e92008-11-30 23:51:27 +01002938 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002939 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002940 * Note that doing so might move headers in the request, but
2941 * the fields will stay coherent and the URI will not move.
2942 * This should only be performed in the backend.
2943 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002944 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2946 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002947
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002949 * 8: the appsession cookie was looked up very early in 1.2,
2950 * so let's do the same now.
2951 */
2952
2953 /* It needs to look into the URI */
2954 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002955 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002956 }
2957
2958 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 * 9: add X-Forwarded-For if either the frontend or the backend
2960 * asks for it.
2961 */
2962 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2963 if (s->cli_addr.ss_family == AF_INET) {
2964 /* Add an X-Forwarded-For header unless the source IP is
2965 * in the 'except' network range.
2966 */
2967 if ((!s->fe->except_mask.s_addr ||
2968 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2969 != s->fe->except_net.s_addr) &&
2970 (!s->be->except_mask.s_addr ||
2971 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2972 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002973 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002974 unsigned char *pn;
2975 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002976
2977 /* Note: we rely on the backend to get the header name to be used for
2978 * x-forwarded-for, because the header is really meant for the backends.
2979 * However, if the backend did not specify any option, we have to rely
2980 * on the frontend's header name.
2981 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002982 if (s->be->fwdfor_hdr_len) {
2983 len = s->be->fwdfor_hdr_len;
2984 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002985 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002986 len = s->fe->fwdfor_hdr_len;
2987 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002988 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002990
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002991 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002992 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002993 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002994 }
2995 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 else if (s->cli_addr.ss_family == AF_INET6) {
2997 /* FIXME: for the sake of completeness, we should also support
2998 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002999 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003000 int len;
3001 char pn[INET6_ADDRSTRLEN];
3002 inet_ntop(AF_INET6,
3003 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3004 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003005
Willy Tarreau59234e92008-11-30 23:51:27 +01003006 /* Note: we rely on the backend to get the header name to be used for
3007 * x-forwarded-for, because the header is really meant for the backends.
3008 * However, if the backend did not specify any option, we have to rely
3009 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003010 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003011 if (s->be->fwdfor_hdr_len) {
3012 len = s->be->fwdfor_hdr_len;
3013 memcpy(trash, s->be->fwdfor_hdr_name, len);
3014 } else {
3015 len = s->fe->fwdfor_hdr_len;
3016 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003017 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003018 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003019
Willy Tarreau59234e92008-11-30 23:51:27 +01003020 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003021 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003022 goto return_bad_req;
3023 }
3024 }
3025
3026 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003027 * 10: add X-Original-To if either the frontend or the backend
3028 * asks for it.
3029 */
3030 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3031
3032 /* FIXME: don't know if IPv6 can handle that case too. */
3033 if (s->cli_addr.ss_family == AF_INET) {
3034 /* Add an X-Original-To header unless the destination IP is
3035 * in the 'except' network range.
3036 */
3037 if (!(s->flags & SN_FRT_ADDR_SET))
3038 get_frt_addr(s);
3039
3040 if ((!s->fe->except_mask_to.s_addr ||
3041 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3042 != s->fe->except_to.s_addr) &&
3043 (!s->be->except_mask_to.s_addr ||
3044 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3045 != s->be->except_to.s_addr)) {
3046 int len;
3047 unsigned char *pn;
3048 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3049
3050 /* Note: we rely on the backend to get the header name to be used for
3051 * x-original-to, because the header is really meant for the backends.
3052 * However, if the backend did not specify any option, we have to rely
3053 * on the frontend's header name.
3054 */
3055 if (s->be->orgto_hdr_len) {
3056 len = s->be->orgto_hdr_len;
3057 memcpy(trash, s->be->orgto_hdr_name, len);
3058 } else {
3059 len = s->fe->orgto_hdr_len;
3060 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003061 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003062 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3063
3064 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003065 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003066 goto return_bad_req;
3067 }
3068 }
3069 }
3070
Willy Tarreau78599912009-10-17 20:12:21 +02003071 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01003072 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02003073 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003074 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003075 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003076 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003077 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003078
3079 /* If we have no server assigned yet and we're balancing on url_param
3080 * with a POST request, we may be interested in checking the body for
3081 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003082 */
3083 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3084 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003085 s->be->url_param_post_limit != 0 &&
3086 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003087 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003088 buffer_dont_connect(req);
3089 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003090 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003091
Willy Tarreaud98cf932009-12-27 22:54:55 +01003092 if (txn->flags & TX_REQ_XFER_LEN)
3093 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003094
Willy Tarreau59234e92008-11-30 23:51:27 +01003095 /*************************************************************
3096 * OK, that's finished for the headers. We have done what we *
3097 * could. Let's switch to the DATA state. *
3098 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003099 req->analyse_exp = TICK_ETERNITY;
3100 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003101
Willy Tarreau59234e92008-11-30 23:51:27 +01003102 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003103 /* OK let's go on with the BODY now */
3104 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003105
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003107 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003108 /* we detected a parsing error. We want to archive this request
3109 * in the dedicated proxy area for later troubleshooting.
3110 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003111 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003112 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003113
Willy Tarreau59234e92008-11-30 23:51:27 +01003114 txn->req.msg_state = HTTP_MSG_ERROR;
3115 txn->status = 400;
3116 req->analysers = 0;
3117 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003118
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003119 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003120 if (s->listener->counters)
3121 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003122
Willy Tarreau59234e92008-11-30 23:51:27 +01003123 if (!(s->flags & SN_ERR_MASK))
3124 s->flags |= SN_ERR_PRXCOND;
3125 if (!(s->flags & SN_FINST_MASK))
3126 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003127 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003128}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003129
Willy Tarreau60b85b02008-11-30 23:28:40 +01003130/* This function is an analyser which processes the HTTP tarpit. It always
3131 * returns zero, at the beginning because it prevents any other processing
3132 * from occurring, and at the end because it terminates the request.
3133 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003134int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003135{
3136 struct http_txn *txn = &s->txn;
3137
3138 /* This connection is being tarpitted. The CLIENT side has
3139 * already set the connect expiration date to the right
3140 * timeout. We just have to check that the client is still
3141 * there and that the timeout has not expired.
3142 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003143 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003144 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3145 !tick_is_expired(req->analyse_exp, now_ms))
3146 return 0;
3147
3148 /* We will set the queue timer to the time spent, just for
3149 * logging purposes. We fake a 500 server error, so that the
3150 * attacker will not suspect his connection has been tarpitted.
3151 * It will not cause trouble to the logs because we can exclude
3152 * the tarpitted connections by filtering on the 'PT' status flags.
3153 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003154 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3155
3156 txn->status = 500;
3157 if (req->flags != BF_READ_ERROR)
3158 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3159
3160 req->analysers = 0;
3161 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003162
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003163 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003164 if (s->listener->counters)
3165 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003166
Willy Tarreau60b85b02008-11-30 23:28:40 +01003167 if (!(s->flags & SN_ERR_MASK))
3168 s->flags |= SN_ERR_PRXCOND;
3169 if (!(s->flags & SN_FINST_MASK))
3170 s->flags |= SN_FINST_T;
3171 return 0;
3172}
3173
Willy Tarreaud34af782008-11-30 23:36:37 +01003174/* This function is an analyser which processes the HTTP request body. It looks
3175 * for parameters to be used for the load balancing algorithm (url_param). It
3176 * must only be called after the standard HTTP request processing has occurred,
3177 * because it expects the request to be parsed. It returns zero if it needs to
3178 * read more data, or 1 once it has completed its analysis.
3179 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003180int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003181{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003182 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003183 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003184 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003185
3186 /* We have to parse the HTTP request body to find any required data.
3187 * "balance url_param check_post" should have been the only way to get
3188 * into this. We were brought here after HTTP header analysis, so all
3189 * related structures are ready.
3190 */
3191
Willy Tarreau522d6c02009-12-06 18:49:18 +01003192 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3193 goto missing_data;
3194
3195 if (msg->msg_state < HTTP_MSG_100_SENT) {
3196 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3197 * send an HTTP/1.1 100 Continue intermediate response.
3198 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003199 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003200 struct hdr_ctx ctx;
3201 ctx.idx = 0;
3202 /* Expect is allowed in 1.1, look for it */
3203 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3204 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3205 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3206 }
3207 }
3208 msg->msg_state = HTTP_MSG_100_SENT;
3209 }
3210
3211 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003212 /* we have msg->col and msg->sov which both point to the first
3213 * byte of message body. msg->som still points to the beginning
3214 * of the message. We must save the body in req->lr because it
3215 * survives buffer re-alignments.
3216 */
3217 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003218 if (txn->flags & TX_REQ_TE_CHNK)
3219 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3220 else
3221 msg->msg_state = HTTP_MSG_DATA;
3222 }
3223
3224 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003225 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003226 * set ->sov and ->lr to point to the body and switch to DATA or
3227 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003228 */
3229 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003230
Willy Tarreau115acb92009-12-26 13:56:06 +01003231 if (!ret)
3232 goto missing_data;
3233 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003234 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003235 }
3236
Willy Tarreaud98cf932009-12-27 22:54:55 +01003237 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003238 * We have the first non-header byte in msg->col, which is either the
3239 * beginning of the chunk size or of the data. The first data byte is in
3240 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3241 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003242 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003243
3244 if (msg->hdr_content_len < limit)
3245 limit = msg->hdr_content_len;
3246
Willy Tarreau7c96f672009-12-27 22:47:25 +01003247 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003248 goto http_end;
3249
3250 missing_data:
3251 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003252 if (req->flags & BF_FULL)
3253 goto return_bad_req;
3254
Willy Tarreau522d6c02009-12-06 18:49:18 +01003255 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3256 txn->status = 408;
3257 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3258 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003259 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003260
3261 /* we get here if we need to wait for more data */
3262 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003263 /* Not enough data. We'll re-use the http-request
3264 * timeout here. Ideally, we should set the timeout
3265 * relative to the accept() date. We just set the
3266 * request timeout once at the beginning of the
3267 * request.
3268 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003269 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003270 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003271 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003272 return 0;
3273 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003274
3275 http_end:
3276 /* The situation will not evolve, so let's give up on the analysis. */
3277 s->logs.tv_request = now; /* update the request timer to reflect full request */
3278 req->analysers &= ~an_bit;
3279 req->analyse_exp = TICK_ETERNITY;
3280 return 1;
3281
3282 return_bad_req: /* let's centralize all bad requests */
3283 txn->req.msg_state = HTTP_MSG_ERROR;
3284 txn->status = 400;
3285 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3286
3287 return_err_msg:
3288 req->analysers = 0;
3289 s->fe->counters.failed_req++;
3290 if (s->listener->counters)
3291 s->listener->counters->failed_req++;
3292
3293 if (!(s->flags & SN_ERR_MASK))
3294 s->flags |= SN_ERR_PRXCOND;
3295 if (!(s->flags & SN_FINST_MASK))
3296 s->flags |= SN_FINST_R;
3297 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003298}
3299
Willy Tarreaud98cf932009-12-27 22:54:55 +01003300/* This function is an analyser which forwards request body (including chunk
3301 * sizes if any). It is called as soon as we must forward, even if we forward
3302 * zero byte. The only situation where it must not be called is when we're in
3303 * tunnel mode and we want to forward till the close. It's used both to forward
3304 * remaining data and to resync after end of body. It expects the msg_state to
3305 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3306 * read more data, or 1 once we can go on with next request or end the session.
3307 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3308 * bytes of pending data + the headers if not already done (between som and sov).
3309 * It eventually adjusts som to match sov after the data in between have been sent.
3310 */
3311int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3312{
3313 struct http_txn *txn = &s->txn;
3314 struct http_msg *msg = &s->txn.req;
3315
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003316 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3317 return 0;
3318
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003319 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3320 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3321 /* Output closed while we were sending data. We must abort. */
3322 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003323 req->analysers &= ~an_bit;
3324 return 1;
3325 }
3326
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003327 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003328
3329 /* Note that we don't have to send 100-continue back because we don't
3330 * need the data to complete our job, and it's up to the server to
3331 * decide whether to return 100, 417 or anything else in return of
3332 * an "Expect: 100-continue" header.
3333 */
3334
3335 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3336 /* we have msg->col and msg->sov which both point to the first
3337 * byte of message body. msg->som still points to the beginning
3338 * of the message. We must save the body in req->lr because it
3339 * survives buffer re-alignments.
3340 */
3341 req->lr = req->data + msg->sov;
3342 if (txn->flags & TX_REQ_TE_CHNK)
3343 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3344 else {
3345 msg->msg_state = HTTP_MSG_DATA;
3346 }
3347 }
3348
Willy Tarreaud98cf932009-12-27 22:54:55 +01003349 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01003350 /* we may have some data pending */
3351 if (msg->hdr_content_len || msg->som != msg->sov) {
3352 int bytes = msg->sov - msg->som;
3353 if (bytes < 0) /* sov may have wrapped at the end */
3354 bytes += req->size;
3355 buffer_forward(req, bytes + msg->hdr_content_len);
3356 msg->hdr_content_len = 0; /* don't forward that again */
3357 msg->som = msg->sov;
3358 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003359
Willy Tarreaucaabe412010-01-03 23:08:28 +01003360 if (msg->msg_state == HTTP_MSG_DATA) {
3361 /* must still forward */
3362 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003363 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003364
3365 /* nothing left to forward */
3366 if (txn->flags & TX_REQ_TE_CHNK)
3367 msg->msg_state = HTTP_MSG_DATA_CRLF;
3368 else {
3369 msg->msg_state = HTTP_MSG_DONE;
3370 goto http_msg_done;
3371 }
3372 }
3373 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003374 /* read the chunk size and assign it to ->hdr_content_len, then
3375 * set ->sov and ->lr to point to the body and switch to DATA or
3376 * TRAILERS state.
3377 */
3378 int ret = http_parse_chunk_size(req, msg);
3379
3380 if (!ret)
3381 goto missing_data;
3382 else if (ret < 0)
3383 goto return_bad_req;
3384 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003385 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003386 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3387 /* we want the CRLF after the data */
3388 int ret;
3389
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003390 req->lr = req->w + req->send_max;
3391 if (req->lr >= req->data + req->size)
3392 req->lr -= req->size;
3393
Willy Tarreaud98cf932009-12-27 22:54:55 +01003394 ret = http_skip_chunk_crlf(req, msg);
3395
3396 if (ret == 0)
3397 goto missing_data;
3398 else if (ret < 0)
3399 goto return_bad_req;
3400 /* we're in MSG_CHUNK_SIZE now */
3401 }
3402 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3403 int ret = http_forward_trailers(req, msg);
3404
3405 if (ret == 0)
3406 goto missing_data;
3407 else if (ret < 0)
3408 goto return_bad_req;
3409 /* we're in HTTP_MSG_DONE now */
3410 }
3411 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003412 http_msg_done:
Willy Tarreau5523b322009-12-29 12:05:52 +01003413 /* No need to read anymore, the request was completely parsed */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003414 req->flags |= BF_DONT_READ;
3415
Willy Tarreau5523b322009-12-29 12:05:52 +01003416 if (txn->rsp.msg_state < HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) {
3417 /* The server has not finished to respond, so we
3418 * don't want to move in order not to upset it.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003419 */
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003420 goto wait_other_side;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003421 }
3422
3423 /* when we support keep-alive or server-close modes, we'll have
3424 * to reset the transaction here.
3425 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003426
Willy Tarreaub608feb2010-01-02 22:47:18 +01003427 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3428 /* initiate a connection close to the server */
3429 req->cons->flags |= SI_FL_NOLINGER;
3430 buffer_shutw_now(req);
3431 }
3432 else if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
Willy Tarreau9438c712009-12-29 14:39:48 +01003433 /* Option forceclose is set, let's enforce it now
3434 * that the transfer is complete. We can safely speed
3435 * up the close because we know the server has received
3436 * everything we wanted it to receive.
3437 */
3438 req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau82eeaf22009-12-29 12:09:05 +01003439 buffer_abort(req);
3440 }
3441
Willy Tarreau5523b322009-12-29 12:05:52 +01003442 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003443 if (req->flags & BF_OUT_EMPTY) {
Willy Tarreau5523b322009-12-29 12:05:52 +01003444 msg->msg_state = HTTP_MSG_CLOSED;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003445 goto http_msg_closed;
3446 }
3447 else {
Willy Tarreau5523b322009-12-29 12:05:52 +01003448 msg->msg_state = HTTP_MSG_CLOSING;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003449 goto http_msg_closing;
3450 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003451 }
3452 else {
3453 /* for other modes, we let further requests pass for now */
3454 req->flags &= ~BF_DONT_READ;
3455 /* FIXME: we're still forced to do that here */
3456 s->rep->flags &= ~BF_DONT_READ;
3457 break;
3458 }
3459 }
3460 else if (msg->msg_state == HTTP_MSG_CLOSING) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003461 http_msg_closing:
Willy Tarreau5523b322009-12-29 12:05:52 +01003462 /* nothing else to forward, just waiting for the buffer to be empty */
3463 if (!(req->flags & BF_OUT_EMPTY))
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003464 goto wait_empty;
Willy Tarreau5523b322009-12-29 12:05:52 +01003465 msg->msg_state = HTTP_MSG_CLOSED;
3466 }
3467 else if (msg->msg_state == HTTP_MSG_CLOSED) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003468 http_msg_closed:
Willy Tarreau5523b322009-12-29 12:05:52 +01003469 req->flags &= ~BF_DONT_READ;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003470
3471 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3472
3473 /* FIXME : this part is 1) awful, 2) tricky, 3) duplicated
3474 * ... but it works.
3475 * We need a better way to force a connection close without
3476 * any risk of propagation to the other side. We need a more
3477 * portable way of releasing a backend's and a server's
3478 * connections. We need a safer way to reinitialize buffer
3479 * flags. We also need a more accurate method for computing
3480 * per-request data.
3481 */
3482 s->req->cons->flags |= SI_FL_NOLINGER;
3483 s->req->cons->shutr(s->req->cons);
3484 s->req->cons->shutw(s->req->cons);
3485
3486 if (s->flags & SN_BE_ASSIGNED)
3487 s->be->beconn--;
3488
3489 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3490 session_process_counters(s);
3491
3492 if (s->txn.status) {
3493 int n;
3494
3495 n = s->txn.status / 100;
3496 if (n < 1 || n > 5)
3497 n = 0;
3498
3499 if (s->fe->mode == PR_MODE_HTTP)
3500 s->fe->counters.p.http.rsp[n]++;
3501
3502 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3503 (s->be->mode == PR_MODE_HTTP))
3504 s->be->counters.p.http.rsp[n]++;
3505 }
3506
3507 /* don't count other requests' data */
3508 s->logs.bytes_in -= s->req->l - s->req->send_max;
3509 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3510
3511 /* let's do a final log if we need it */
3512 if (s->logs.logwait &&
3513 !(s->flags & SN_MONITOR) &&
3514 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3515 s->do_log(s);
3516 }
3517
3518 s->logs.accept_date = date; /* user-visible date for logging */
3519 s->logs.tv_accept = now; /* corrected date for internal use */
3520 tv_zero(&s->logs.tv_request);
3521 s->logs.t_queue = -1;
3522 s->logs.t_connect = -1;
3523 s->logs.t_data = -1;
3524 s->logs.t_close = 0;
3525 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3526 s->logs.srv_queue_size = 0; /* we will get this number soon */
3527
3528 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3529 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3530
3531 if (s->pend_pos)
3532 pendconn_free(s->pend_pos);
3533
3534 if (s->srv) {
3535 if (s->flags & SN_CURR_SESS) {
3536 s->flags &= ~SN_CURR_SESS;
3537 s->srv->cur_sess--;
3538 }
3539 if (may_dequeue_tasks(s->srv, s->be))
3540 process_srv_queue(s->srv);
3541 }
3542
3543 if (unlikely(s->srv_conn))
3544 sess_change_server(s, NULL);
3545 s->srv = NULL;
3546
3547 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3548 s->req->cons->fd = -1; /* just to help with debugging */
3549 s->req->cons->err_type = SI_ET_NONE;
3550 s->req->cons->err_loc = NULL;
3551 s->req->cons->exp = TICK_ETERNITY;
3552 s->req->cons->flags = SI_FL_NONE;
3553 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE);
3554 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE|BF_WRITE_PARTIAL);
3555 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3556 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3557 s->txn.meth = 0;
3558 http_reset_txn(s);
3559 txn->flags |= TX_NOT_FIRST;
3560 if (s->be->options2 & PR_O2_INDEPSTR)
3561 s->req->cons->flags |= SI_FL_INDEP_STR;
3562
Willy Tarreauface8392010-01-03 11:37:54 +01003563 /* if the request buffer is not empty, it means we're
3564 * about to process another request, so send pending
3565 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003566 * Also, let's not start reading a small request packet,
3567 * we may prefer to read a larger one later.
Willy Tarreauface8392010-01-03 11:37:54 +01003568 */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003569 if (s->req->l > s->req->send_max) {
Willy Tarreauface8392010-01-03 11:37:54 +01003570 s->rep->flags |= BF_EXPECT_MORE;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003571 s->req->flags |= BF_DONT_READ;
3572 }
Willy Tarreauface8392010-01-03 11:37:54 +01003573
Willy Tarreaub608feb2010-01-02 22:47:18 +01003574 /* make ->lr point to the first non-forwarded byte */
3575 s->req->lr = s->req->w + s->req->send_max;
3576 if (s->req->lr >= s->req->data + s->req->size)
3577 s->req->lr -= s->req->size;
3578 s->rep->lr = s->rep->w + s->rep->send_max;
3579 if (s->rep->lr >= s->rep->data + s->rep->size)
3580 s->rep->lr -= s->req->size;
3581
3582 s->req->analysers |= s->fe->fe_req_ana;
3583 s->rep->analysers = 0;
3584 }
3585
Willy Tarreau5523b322009-12-29 12:05:52 +01003586 /* FIXME: we're still forced to do that here */
3587 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003588 break;
3589 }
3590 }
3591
3592 /* OK we're done with the data phase */
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003593 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003594 req->analysers &= ~an_bit;
3595 return 1;
3596
3597 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003598 /* stop waiting for data if the input is closed before the end */
3599 if (req->flags & BF_SHUTR)
3600 goto return_bad_req;
3601
3602 wait_other_side:
Willy Tarreaud98cf932009-12-27 22:54:55 +01003603 /* forward the chunk size as well as any pending data */
3604 if (msg->hdr_content_len || msg->som != msg->sov) {
3605 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3606 msg->hdr_content_len = 0; /* don't forward that again */
3607 msg->som = msg->sov;
3608 }
3609
Willy Tarreaud98cf932009-12-27 22:54:55 +01003610 /* the session handler will take care of timeouts and errors */
3611 return 0;
3612
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003613 wait_empty:
3614 /* waiting for the last bits to leave the buffer */
3615 if (req->flags & BF_SHUTW)
3616 goto return_bad_req;
3617 return 0;
3618
Willy Tarreaud98cf932009-12-27 22:54:55 +01003619 return_bad_req: /* let's centralize all bad requests */
3620 txn->req.msg_state = HTTP_MSG_ERROR;
3621 txn->status = 400;
3622 /* Note: we don't send any error if some data were already sent */
3623 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3624
3625 req->analysers = 0;
3626 s->fe->counters.failed_req++;
3627 if (s->listener->counters)
3628 s->listener->counters->failed_req++;
3629
3630 if (!(s->flags & SN_ERR_MASK))
3631 s->flags |= SN_ERR_PRXCOND;
3632 if (!(s->flags & SN_FINST_MASK))
3633 s->flags |= SN_FINST_R;
3634 return 0;
3635}
3636
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003637/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3638 * processing can continue on next analysers, or zero if it either needs more
3639 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3640 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3641 * when it has nothing left to do, and may remove any analyser when it wants to
3642 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003643 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003644int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003645{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003646 struct http_txn *txn = &s->txn;
3647 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003648 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003649 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003650 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003651 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003652
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003653 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 +02003654 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003655 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003656 rep,
3657 rep->rex, rep->wex,
3658 rep->flags,
3659 rep->l,
3660 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003662 /*
3663 * Now parse the partial (or complete) lines.
3664 * We will check the response syntax, and also join multi-line
3665 * headers. An index of all the lines will be elaborated while
3666 * parsing.
3667 *
3668 * For the parsing, we use a 28 states FSM.
3669 *
3670 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003671 * rep->data + msg->som = beginning of response
3672 * rep->data + msg->eoh = end of processed headers / start of current one
3673 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003674 * rep->lr = first non-visited byte
3675 * rep->r = end of data
3676 */
3677
Willy Tarreau83e3af02009-12-28 17:39:57 +01003678 /* There's a protected area at the end of the buffer for rewriting
3679 * purposes. We don't want to start to parse the request if the
3680 * protected area is affected, because we may have to move processed
3681 * data later, which is much more complicated.
3682 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003683 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3684 if (unlikely((rep->flags & BF_FULL) ||
3685 rep->r < rep->lr ||
3686 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3687 if (rep->send_max) {
3688 /* some data has still not left the buffer, wake us once that's done */
3689 buffer_dont_close(rep);
3690 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3691 return 0;
3692 }
3693 if (rep->l <= rep->size - global.tune.maxrewrite)
3694 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003695 }
3696
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003697 if (likely(rep->lr < rep->r))
3698 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003699 }
3700
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003701 /* 1: we might have to print this header in debug mode */
3702 if (unlikely((global.mode & MODE_DEBUG) &&
3703 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003704 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003705 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003707 sol = rep->data + msg->som;
3708 eol = sol + msg->sl.rq.l;
3709 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003710
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003711 sol += hdr_idx_first_pos(&txn->hdr_idx);
3712 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003713
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003714 while (cur_idx) {
3715 eol = sol + txn->hdr_idx.v[cur_idx].len;
3716 debug_hdr("srvhdr", s, sol, eol);
3717 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3718 cur_idx = txn->hdr_idx.v[cur_idx].next;
3719 }
3720 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003721
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003722 /*
3723 * Now we quickly check if we have found a full valid response.
3724 * If not so, we check the FD and buffer states before leaving.
3725 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003726 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003727 * responses are checked first.
3728 *
3729 * Depending on whether the client is still there or not, we
3730 * may send an error response back or not. Note that normally
3731 * we should only check for HTTP status there, and check I/O
3732 * errors somewhere else.
3733 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003734
Willy Tarreau655dce92009-11-08 13:10:58 +01003735 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003736 /* Invalid response */
3737 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3738 /* we detected a parsing error. We want to archive this response
3739 * in the dedicated proxy area for later troubleshooting.
3740 */
3741 hdr_response_bad:
3742 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3743 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3744
3745 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003746 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003747 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003748 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3749 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003750
3751 rep->analysers = 0;
3752 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003753 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003754 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3755
3756 if (!(s->flags & SN_ERR_MASK))
3757 s->flags |= SN_ERR_PRXCOND;
3758 if (!(s->flags & SN_FINST_MASK))
3759 s->flags |= SN_FINST_H;
3760
3761 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003762 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003763
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003764 /* too large response does not fit in buffer. */
3765 else if (rep->flags & BF_FULL) {
3766 goto hdr_response_bad;
3767 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003768
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003769 /* read error */
3770 else if (rep->flags & BF_READ_ERROR) {
3771 if (msg->err_pos >= 0)
3772 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003773
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003774 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003775 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003776 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003777 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3778 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003779
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003780 rep->analysers = 0;
3781 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003782 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003783 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003784
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003785 if (!(s->flags & SN_ERR_MASK))
3786 s->flags |= SN_ERR_SRVCL;
3787 if (!(s->flags & SN_FINST_MASK))
3788 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003789 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003790 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003792 /* read timeout : return a 504 to the client. */
3793 else if (rep->flags & BF_READ_TIMEOUT) {
3794 if (msg->err_pos >= 0)
3795 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003796
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003797 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003798 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003799 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003800 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3801 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003802
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003803 rep->analysers = 0;
3804 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003805 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003806 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003807
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003808 if (!(s->flags & SN_ERR_MASK))
3809 s->flags |= SN_ERR_SRVTO;
3810 if (!(s->flags & SN_FINST_MASK))
3811 s->flags |= SN_FINST_H;
3812 return 0;
3813 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003814
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003815 /* close from server */
3816 else if (rep->flags & BF_SHUTR) {
3817 if (msg->err_pos >= 0)
3818 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003819
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003820 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003821 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003822 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003823 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3824 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003825
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003826 rep->analysers = 0;
3827 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003828 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003829 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003830
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003831 if (!(s->flags & SN_ERR_MASK))
3832 s->flags |= SN_ERR_SRVCL;
3833 if (!(s->flags & SN_FINST_MASK))
3834 s->flags |= SN_FINST_H;
3835 return 0;
3836 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003837
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003838 /* write error to client (we don't send any message then) */
3839 else if (rep->flags & BF_WRITE_ERROR) {
3840 if (msg->err_pos >= 0)
3841 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003842
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003843 s->be->counters.failed_resp++;
3844 rep->analysers = 0;
3845
3846 if (!(s->flags & SN_ERR_MASK))
3847 s->flags |= SN_ERR_CLICL;
3848 if (!(s->flags & SN_FINST_MASK))
3849 s->flags |= SN_FINST_H;
3850
3851 /* process_session() will take care of the error */
3852 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003853 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003854
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003855 buffer_dont_close(rep);
3856 return 0;
3857 }
3858
3859 /* More interesting part now : we know that we have a complete
3860 * response which at least looks like HTTP. We have an indicator
3861 * of each header's length, so we can parse them quickly.
3862 */
3863
3864 if (unlikely(msg->err_pos >= 0))
3865 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3866
3867 /* ensure we keep this pointer to the beginning of the message */
3868 msg->sol = rep->data + msg->som;
3869
3870 /*
3871 * 1: get the status code
3872 */
3873 n = rep->data[msg->sl.st.c] - '0';
3874 if (n < 1 || n > 5)
3875 n = 0;
3876 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003877
Willy Tarreau5b154472009-12-21 20:11:07 +01003878 /* check if the response is HTTP/1.1 or above */
3879 if ((msg->sl.st.v_l == 8) &&
3880 ((rep->data[msg->som + 5] > '1') ||
3881 ((rep->data[msg->som + 5] == '1') &&
3882 (rep->data[msg->som + 7] >= '1'))))
3883 txn->flags |= TX_RES_VER_11;
3884
3885 /* "connection" has not been parsed yet */
3886 txn->flags &= ~TX_CON_HDR_PARS;
3887
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003888 /* transfer length unknown*/
3889 txn->flags &= ~TX_RES_XFER_LEN;
3890
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003891 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3892
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003893 if (txn->status >= 100 && txn->status < 500)
3894 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3895 else
3896 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3897
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003898 /*
3899 * 2: check for cacheability.
3900 */
3901
3902 switch (txn->status) {
3903 case 200:
3904 case 203:
3905 case 206:
3906 case 300:
3907 case 301:
3908 case 410:
3909 /* RFC2616 @13.4:
3910 * "A response received with a status code of
3911 * 200, 203, 206, 300, 301 or 410 MAY be stored
3912 * by a cache (...) unless a cache-control
3913 * directive prohibits caching."
3914 *
3915 * RFC2616 @9.5: POST method :
3916 * "Responses to this method are not cacheable,
3917 * unless the response includes appropriate
3918 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003919 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003920 if (likely(txn->meth != HTTP_METH_POST) &&
3921 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3922 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3923 break;
3924 default:
3925 break;
3926 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003927
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003928 /*
3929 * 3: we may need to capture headers
3930 */
3931 s->logs.logwait &= ~LW_RESP;
3932 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3933 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3934 txn->rsp.cap, s->fe->rsp_cap);
3935
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003936 /* 4: determine the transfer-length.
3937 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3938 * the presence of a message-body in a RESPONSE and its transfer length
3939 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003940 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003941 * All responses to the HEAD request method MUST NOT include a
3942 * message-body, even though the presence of entity-header fields
3943 * might lead one to believe they do. All 1xx (informational), 204
3944 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3945 * message-body. All other responses do include a message-body,
3946 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003947 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003948 * 1. Any response which "MUST NOT" include a message-body (such as the
3949 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3950 * always terminated by the first empty line after the header fields,
3951 * regardless of the entity-header fields present in the message.
3952 *
3953 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3954 * the "chunked" transfer-coding (Section 6.2) is used, the
3955 * transfer-length is defined by the use of this transfer-coding.
3956 * If a Transfer-Encoding header field is present and the "chunked"
3957 * transfer-coding is not present, the transfer-length is defined by
3958 * the sender closing the connection.
3959 *
3960 * 3. If a Content-Length header field is present, its decimal value in
3961 * OCTETs represents both the entity-length and the transfer-length.
3962 * If a message is received with both a Transfer-Encoding header
3963 * field and a Content-Length header field, the latter MUST be ignored.
3964 *
3965 * 4. If the message uses the media type "multipart/byteranges", and
3966 * the transfer-length is not otherwise specified, then this self-
3967 * delimiting media type defines the transfer-length. This media
3968 * type MUST NOT be used unless the sender knows that the recipient
3969 * can parse it; the presence in a request of a Range header with
3970 * multiple byte-range specifiers from a 1.1 client implies that the
3971 * client can parse multipart/byteranges responses.
3972 *
3973 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003974 */
3975
3976 /* Skip parsing if no content length is possible. The response flags
3977 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003978 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003979 * FIXME: should we parse anyway and return an error on chunked encoding ?
3980 */
3981 if (txn->meth == HTTP_METH_HEAD ||
3982 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003983 txn->status == 204 || txn->status == 304) {
3984 txn->flags |= TX_RES_XFER_LEN;
3985 goto skip_content_length;
3986 }
3987
3988 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003989 goto skip_content_length;
3990
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003991 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003992 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003993 while ((txn->flags & TX_RES_VER_11) &&
3994 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003995 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3996 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3997 else if (txn->flags & TX_RES_TE_CHNK) {
3998 /* bad transfer-encoding (chunked followed by something else) */
3999 use_close_only = 1;
4000 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4001 break;
4002 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004003 }
4004
4005 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4006 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004007 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004008 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4009 signed long long cl;
4010
4011 if (!ctx.vlen)
4012 goto hdr_response_bad;
4013
4014 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4015 goto hdr_response_bad; /* parse failure */
4016
4017 if (cl < 0)
4018 goto hdr_response_bad;
4019
4020 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4021 goto hdr_response_bad; /* already specified, was different */
4022
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004023 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004024 msg->hdr_content_len = cl;
4025 }
4026
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004027 /* FIXME: we should also implement the multipart/byterange method.
4028 * For now on, we resort to close mode in this case (unknown length).
4029 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004030skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004031
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004032 /* end of job, return OK */
4033 rep->analysers &= ~an_bit;
4034 rep->analyse_exp = TICK_ETERNITY;
4035 return 1;
4036}
4037
4038/* This function performs all the processing enabled for the current response.
4039 * It normally returns zero, but may return 1 if it absolutely needs to be
4040 * called again after other functions. It relies on buffers flags, and updates
4041 * t->rep->analysers. It might make sense to explode it into several other
4042 * functions. It works like process_request (see indications above).
4043 */
4044int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4045{
4046 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004047 struct http_msg *msg = &txn->rsp;
4048 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004049 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004050 int conn_ka = 0, conn_cl = 0;
4051 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004052 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004053
4054 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4055 now_ms, __FUNCTION__,
4056 t,
4057 rep,
4058 rep->rex, rep->wex,
4059 rep->flags,
4060 rep->l,
4061 rep->analysers);
4062
Willy Tarreau655dce92009-11-08 13:10:58 +01004063 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004064 return 0;
4065
4066 rep->analysers &= ~an_bit;
4067 rep->analyse_exp = TICK_ETERNITY;
4068
Willy Tarreau5b154472009-12-21 20:11:07 +01004069 /* Now we have to check if we need to modify the Connection header.
4070 * This is more difficult on the response than it is on the request,
4071 * because we can have two different HTTP versions and we don't know
4072 * how the client will interprete a response. For instance, let's say
4073 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4074 * HTTP/1.1 response without any header. Maybe it will bound itself to
4075 * HTTP/1.0 because it only knows about it, and will consider the lack
4076 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4077 * the lack of header as a keep-alive. Thus we will use two flags
4078 * indicating how a request MAY be understood by the client. In case
4079 * of multiple possibilities, we'll fix the header to be explicit. If
4080 * ambiguous cases such as both close and keepalive are seen, then we
4081 * will fall back to explicit close. Note that we won't take risks with
4082 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4083 */
4084
4085 if ((txn->meth != HTTP_METH_CONNECT) &&
4086 (txn->status >= 200) &&
4087 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
4088 !(txn->flags & TX_CON_HDR_PARS)) {
4089 int may_keep = 0, may_close = 0; /* how it may be understood */
4090 struct hdr_ctx ctx;
4091
4092 ctx.idx = 0;
4093 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4094 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4095 conn_cl = 1;
4096 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4097 conn_ka = 1;
4098 }
4099
4100 if (conn_cl) {
4101 /* close header present */
4102 may_close = 1;
4103 if (conn_ka) /* we have both close and keep-alive */
4104 may_keep = 1;
4105 }
4106 else if (conn_ka) {
4107 /* keep-alive alone */
4108 may_keep = 1;
4109 }
4110 else {
4111 /* no close nor keep-alive header */
4112 if (txn->flags & TX_RES_VER_11)
4113 may_keep = 1;
4114 else
4115 may_close = 1;
4116
4117 if (txn->flags & TX_REQ_VER_11)
4118 may_keep = 1;
4119 else
4120 may_close = 1;
4121 }
4122
4123 /* let's update the transaction status to reflect any close.
4124 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004125 * handled. We also explicitly state that we will close in
4126 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004127 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004128 if ((may_close &&
4129 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4130 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004131 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4132
4133 /* Now we must adjust the response header :
4134 * - set "close" if may_keep and WANT_CLO
4135 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4136 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
4137 *
4138 * Until we support the server-close mode, we'll only support the set "close".
4139 */
4140 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
4141 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004142 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4143 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4144 must_del_close = 1;
4145 if (!(txn->flags & TX_REQ_VER_11))
4146 must_keep = 1;
4147 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004148
4149 txn->flags |= TX_CON_HDR_PARS;
4150 }
4151
4152 /* We might have to check for "Connection:" if the server
4153 * returns a connection status that is not compatible with
4154 * the client's or with the config.
4155 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004156 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004157 char *cur_ptr, *cur_end, *cur_next;
4158 int cur_idx, old_idx, delta, val;
4159 int must_delete;
4160 struct hdr_idx_elem *cur_hdr;
4161
4162 /* we just have to remove the headers if both sides are 1.0 */
4163 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004164
4165 /* same if we want to re-enable keep-alive on 1.1 */
4166 must_delete |= must_del_close;
4167
Willy Tarreau5b154472009-12-21 20:11:07 +01004168 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4169
4170 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4171 cur_hdr = &txn->hdr_idx.v[cur_idx];
4172 cur_ptr = cur_next;
4173 cur_end = cur_ptr + cur_hdr->len;
4174 cur_next = cur_end + cur_hdr->cr + 1;
4175
4176 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4177 if (!val)
4178 continue;
4179
4180 /* 3 possibilities :
4181 * - we have already set "Connection: close" or we're in
4182 * HTTP/1.0, so we remove this line.
4183 * - we have not yet set "Connection: close", but this line
4184 * indicates close. We leave it untouched and set the flag.
4185 * - we have not yet set "Connection: close", and this line
4186 * indicates non-close. We replace it and set the flag.
4187 */
4188 if (must_delete) {
4189 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4190 http_msg_move_end(&txn->rsp, delta);
4191 cur_next += delta;
4192 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4193 txn->hdr_idx.used--;
4194 cur_hdr->len = 0;
4195 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004196 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004197 } else {
4198 if (cur_end - cur_ptr - val != 5 ||
4199 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4200 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4201 "close", 5);
4202 cur_next += delta;
4203 cur_hdr->len += delta;
4204 http_msg_move_end(&txn->rsp, delta);
4205 }
4206 must_delete = 1;
4207 must_close = 0;
4208 }
4209 } /* for loop */
4210 } /* if must close keep-alive */
4211
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004212 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004213 /*
4214 * 3: we will have to evaluate the filters.
4215 * As opposed to version 1.2, now they will be evaluated in the
4216 * filters order and not in the header order. This means that
4217 * each filter has to be validated among all headers.
4218 *
4219 * Filters are tried with ->be first, then with ->fe if it is
4220 * different from ->be.
4221 */
4222
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004223 cur_proxy = t->be;
4224 while (1) {
4225 struct proxy *rule_set = cur_proxy;
4226
4227 /* try headers filters */
4228 if (rule_set->rsp_exp != NULL) {
4229 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4230 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004231 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004232 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004233 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4234 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004235 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004236 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004237 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004238 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004239 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004240 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004241 if (!(t->flags & SN_ERR_MASK))
4242 t->flags |= SN_ERR_PRXCOND;
4243 if (!(t->flags & SN_FINST_MASK))
4244 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004245 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004246 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004247 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004248
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004249 /* has the response been denied ? */
4250 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004251 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004252 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004253
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004254 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004255 if (t->listener->counters)
4256 t->listener->counters->denied_resp++;
4257
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004258 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004259 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004260
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004261 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004262 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004263 if (txn->status < 200)
4264 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004265 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004266 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004267 }
4268
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004269 /* check whether we're already working on the frontend */
4270 if (cur_proxy == t->fe)
4271 break;
4272 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004273 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004274
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004275 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004276 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4277 * in which case this is not the right response, and we're waiting for the
4278 * next one. Let's allow this response to go to the client and wait for the
4279 * next one.
4280 */
4281 if (txn->status < 200) {
4282 hdr_idx_init(&txn->hdr_idx);
4283 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4284 msg->msg_state = HTTP_MSG_RPBEFORE;
4285 txn->status = 0;
4286 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4287 return 1;
4288 }
4289
4290 /* we don't have any 1xx status code now */
4291
4292 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004293 * 4: check for server cookie.
4294 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004295 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4296 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004297 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004298
Willy Tarreaubaaee002006-06-26 02:48:02 +02004299
Willy Tarreaua15645d2007-03-18 16:22:39 +01004300 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004301 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004302 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004303 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004304 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004305
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004306 /*
4307 * 6: add server cookie in the response if needed
4308 */
4309 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004310 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004311 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004312
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004313 /* the server is known, it's not the one the client requested, we have to
4314 * insert a set-cookie here, except if we want to insert only on POST
4315 * requests and this one isn't. Note that servers which don't have cookies
4316 * (eg: some backup servers) will return a full cookie removal request.
4317 */
4318 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4319 t->be->cookie_name,
4320 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004321
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004322 if (t->be->cookie_domain)
4323 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004324
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004325 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004326 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004327 goto return_bad_resp;
4328 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004329
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004330 /* Here, we will tell an eventual cache on the client side that we don't
4331 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4332 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4333 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4334 */
4335 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004336
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004337 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4338
4339 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004340 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004341 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004342 }
4343 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004344
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004345 /*
4346 * 7: check if result will be cacheable with a cookie.
4347 * We'll block the response if security checks have caught
4348 * nasty things such as a cacheable cookie.
4349 */
4350 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4351 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004352 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004353
4354 /* we're in presence of a cacheable response containing
4355 * a set-cookie header. We'll block it as requested by
4356 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004357 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004358 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004359 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004360
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004361 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004362 if (t->listener->counters)
4363 t->listener->counters->denied_resp++;
4364
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004365 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4366 t->be->id, t->srv?t->srv->id:"<dispatch>");
4367 send_log(t->be, LOG_ALERT,
4368 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4369 t->be->id, t->srv?t->srv->id:"<dispatch>");
4370 goto return_srv_prx_502;
4371 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004372
4373 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004374 * 8: add "Connection: close" if needed and not yet set. This is
4375 * only needed for 1.1 responses since we know there is no other
4376 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004377 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004378 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004379 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004380 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004381 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004382 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004383 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004384 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4385 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4386 "Connection: keep-alive", 22) < 0))
4387 goto return_bad_resp;
4388 must_keep = 0;
4389 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004390
Willy Tarreaud98cf932009-12-27 22:54:55 +01004391 if (txn->flags & TX_RES_XFER_LEN)
4392 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004393
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004394 /*************************************************************
4395 * OK, that's finished for the headers. We have done what we *
4396 * could. Let's switch to the DATA state. *
4397 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004398
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004399 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004400
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004401 /* if the user wants to log as soon as possible, without counting
4402 * bytes from the server, then this is the right moment. We have
4403 * to temporarily assign bytes_out to log what we currently have.
4404 */
4405 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4406 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4407 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004408 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004409 t->logs.bytes_out = 0;
4410 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004411
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004412 /* Note: we must not try to cheat by jumping directly to DATA,
4413 * otherwise we would not let the client side wake up.
4414 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004415
Willy Tarreaudafde432008-08-17 01:00:46 +02004416 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004417 }
4418 return 0;
4419}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004420
Willy Tarreaud98cf932009-12-27 22:54:55 +01004421/* This function is an analyser which forwards response body (including chunk
4422 * sizes if any). It is called as soon as we must forward, even if we forward
4423 * zero byte. The only situation where it must not be called is when we're in
4424 * tunnel mode and we want to forward till the close. It's used both to forward
4425 * remaining data and to resync after end of body. It expects the msg_state to
4426 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4427 * read more data, or 1 once we can go on with next request or end the session.
4428 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4429 * bytes of pending data + the headers if not already done (between som and sov).
4430 * It eventually adjusts som to match sov after the data in between have been sent.
4431 */
4432int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4433{
4434 struct http_txn *txn = &s->txn;
4435 struct http_msg *msg = &s->txn.rsp;
4436
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004437 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4438 return 0;
4439
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004440 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4441 !s->req->analysers) {
4442 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4443 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004444 res->analysers &= ~an_bit;
4445 return 1;
4446 }
4447
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004448 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004449
Willy Tarreaud98cf932009-12-27 22:54:55 +01004450 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4451 /* we have msg->col and msg->sov which both point to the first
4452 * byte of message body. msg->som still points to the beginning
4453 * of the message. We must save the body in req->lr because it
4454 * survives buffer re-alignments.
4455 */
4456 res->lr = res->data + msg->sov;
4457 if (txn->flags & TX_RES_TE_CHNK)
4458 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4459 else {
4460 msg->msg_state = HTTP_MSG_DATA;
4461 }
4462 }
4463
Willy Tarreaud98cf932009-12-27 22:54:55 +01004464 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004465 /* we may have some data pending */
4466 if (msg->hdr_content_len || msg->som != msg->sov) {
4467 int bytes = msg->sov - msg->som;
4468 if (bytes < 0) /* sov may have wrapped at the end */
4469 bytes += res->size;
4470 buffer_forward(res, bytes + msg->hdr_content_len);
4471 msg->hdr_content_len = 0; /* don't forward that again */
4472 msg->som = msg->sov;
4473 }
4474
Willy Tarreaucaabe412010-01-03 23:08:28 +01004475 if (msg->msg_state == HTTP_MSG_DATA) {
4476 /* must still forward */
4477 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004478 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004479
4480 /* nothing left to forward */
4481 if (txn->flags & TX_RES_TE_CHNK)
4482 msg->msg_state = HTTP_MSG_DATA_CRLF;
4483 else
4484 msg->msg_state = HTTP_MSG_DONE;
4485 }
4486 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004487 /* read the chunk size and assign it to ->hdr_content_len, then
4488 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4489 */
4490 int ret = http_parse_chunk_size(res, msg);
4491
4492 if (!ret)
4493 goto missing_data;
4494 else if (ret < 0)
4495 goto return_bad_res;
4496 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004497 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004498 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4499 /* we want the CRLF after the data */
4500 int ret;
4501
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004502 res->lr = res->w + res->send_max;
4503 if (res->lr >= res->data + res->size)
4504 res->lr -= res->size;
4505
Willy Tarreaud98cf932009-12-27 22:54:55 +01004506 ret = http_skip_chunk_crlf(res, msg);
4507
4508 if (!ret)
4509 goto missing_data;
4510 else if (ret < 0)
4511 goto return_bad_res;
4512 /* we're in MSG_CHUNK_SIZE now */
4513 }
4514 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4515 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004516
Willy Tarreaud98cf932009-12-27 22:54:55 +01004517 if (ret == 0)
4518 goto missing_data;
4519 else if (ret < 0)
4520 goto return_bad_res;
4521 /* we're in HTTP_MSG_DONE now */
4522 }
4523 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004524 /* In theory, we don't need to read anymore, but we must
4525 * still monitor the server connection for a possible close,
4526 * so we don't set the BF_DONT_READ flag here.
4527 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004528
Willy Tarreau5523b322009-12-29 12:05:52 +01004529 if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) {
4530 /* The client seems to still be sending data, probably
4531 * because we got an error response during an upload.
4532 * We have the choice of either breaking the connection
4533 * or letting it pass through. Let's do the later.
4534 */
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004535 goto wait_other_side;
Willy Tarreau5523b322009-12-29 12:05:52 +01004536 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004537
4538 /* when we support keep-alive or server-close modes, we'll have
4539 * to reset the transaction here.
4540 */
Willy Tarreau5523b322009-12-29 12:05:52 +01004541
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004542 if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
4543 /* option forceclose is set, let's enforce it now that the transfer is complete. */
4544 buffer_abort(res);
4545 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004546 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4547 /* server close is handled entirely on the req analyser */
4548 s->req->cons->flags |= SI_FL_NOLINGER;
4549 buffer_shutw_now(s->req);
4550 }
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004551
Willy Tarreau5523b322009-12-29 12:05:52 +01004552 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01004553 if (res->flags & BF_OUT_EMPTY) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004554 msg->msg_state = HTTP_MSG_CLOSED;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004555 goto http_msg_closed;
4556 }
4557 else {
Willy Tarreau5523b322009-12-29 12:05:52 +01004558 msg->msg_state = HTTP_MSG_CLOSING;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004559 goto http_msg_closing;
4560 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004561 }
4562 else {
4563 /* for other modes, we let further responses pass for now */
4564 res->flags &= ~BF_DONT_READ;
4565 /* FIXME: we're still forced to do that here */
4566 s->req->flags &= ~BF_DONT_READ;
4567 break;
4568 }
4569 }
4570 else if (msg->msg_state == HTTP_MSG_CLOSING) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01004571 http_msg_closing:
Willy Tarreau5523b322009-12-29 12:05:52 +01004572 /* nothing else to forward, just waiting for the buffer to be empty */
4573 if (!(res->flags & BF_OUT_EMPTY))
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004574 goto wait_empty;
Willy Tarreau5523b322009-12-29 12:05:52 +01004575 msg->msg_state = HTTP_MSG_CLOSED;
4576 }
4577 else if (msg->msg_state == HTTP_MSG_CLOSED) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01004578 http_msg_closed:
Willy Tarreau5523b322009-12-29 12:05:52 +01004579 res->flags &= ~BF_DONT_READ;
4580 /* FIXME: we're still forced to do that here */
4581 s->req->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004582 break;
4583 }
4584 }
4585
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004586 buffer_ignore(res, res->l - res->send_max);
4587 buffer_auto_close(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004588 res->analysers &= ~an_bit;
4589 return 1;
4590
4591 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004592 /* stop waiting for data if the input is closed before the end */
4593 if (res->flags & BF_SHUTR)
4594 goto return_bad_res;
4595
4596 wait_other_side:
Willy Tarreaud98cf932009-12-27 22:54:55 +01004597 /* forward the chunk size as well as any pending data */
4598 if (msg->hdr_content_len || msg->som != msg->sov) {
4599 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4600 msg->hdr_content_len = 0; /* don't forward that again */
4601 msg->som = msg->sov;
4602 }
4603
Willy Tarreaud98cf932009-12-27 22:54:55 +01004604 /* the session handler will take care of timeouts and errors */
4605 return 0;
4606
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004607 wait_empty:
4608 /* waiting for last bits to leave the buffer */
4609 if (res->flags & BF_SHUTW)
4610 goto return_bad_res;
4611 return 0;
4612
Willy Tarreaud98cf932009-12-27 22:54:55 +01004613 return_bad_res: /* let's centralize all bad resuests */
4614 txn->rsp.msg_state = HTTP_MSG_ERROR;
4615 txn->status = 502;
4616 stream_int_cond_close(res->cons, NULL);
4617
4618 res->analysers = 0;
4619 s->be->counters.failed_resp++;
4620 if (s->srv) {
4621 s->srv->counters.failed_resp++;
4622 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4623 }
4624
4625 if (!(s->flags & SN_ERR_MASK))
4626 s->flags |= SN_ERR_PRXCOND;
4627 if (!(s->flags & SN_FINST_MASK))
4628 s->flags |= SN_FINST_R;
4629 return 0;
4630}
4631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004632/* Iterate the same filter through all request headers.
4633 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004634 * Since it can manage the switch to another backend, it updates the per-proxy
4635 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004636 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004637int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004638{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004639 char term;
4640 char *cur_ptr, *cur_end, *cur_next;
4641 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004642 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004643 struct hdr_idx_elem *cur_hdr;
4644 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004646 last_hdr = 0;
4647
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004648 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004649 old_idx = 0;
4650
4651 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004652 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004653 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004654 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004655 (exp->action == ACT_ALLOW ||
4656 exp->action == ACT_DENY ||
4657 exp->action == ACT_TARPIT))
4658 return 0;
4659
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004660 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004661 if (!cur_idx)
4662 break;
4663
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004664 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004665 cur_ptr = cur_next;
4666 cur_end = cur_ptr + cur_hdr->len;
4667 cur_next = cur_end + cur_hdr->cr + 1;
4668
4669 /* Now we have one header between cur_ptr and cur_end,
4670 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004671 */
4672
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004673 /* The annoying part is that pattern matching needs
4674 * that we modify the contents to null-terminate all
4675 * strings before testing them.
4676 */
4677
4678 term = *cur_end;
4679 *cur_end = '\0';
4680
4681 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4682 switch (exp->action) {
4683 case ACT_SETBE:
4684 /* It is not possible to jump a second time.
4685 * FIXME: should we return an HTTP/500 here so that
4686 * the admin knows there's a problem ?
4687 */
4688 if (t->be != t->fe)
4689 break;
4690
4691 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004692 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004693 last_hdr = 1;
4694 break;
4695
4696 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004697 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004698 last_hdr = 1;
4699 break;
4700
4701 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004702 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004703 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004704
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004705 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004706 if (t->listener->counters)
4707 t->listener->counters->denied_resp++;
4708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004709 break;
4710
4711 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004712 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004713 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004714
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004715 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004716 if (t->listener->counters)
4717 t->listener->counters->denied_resp++;
4718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004719 break;
4720
4721 case ACT_REPLACE:
4722 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4723 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4724 /* FIXME: if the user adds a newline in the replacement, the
4725 * index will not be recalculated for now, and the new line
4726 * will not be counted as a new header.
4727 */
4728
4729 cur_end += delta;
4730 cur_next += delta;
4731 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004732 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004733 break;
4734
4735 case ACT_REMOVE:
4736 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4737 cur_next += delta;
4738
Willy Tarreaufa355d42009-11-29 18:12:29 +01004739 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004740 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4741 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004742 cur_hdr->len = 0;
4743 cur_end = NULL; /* null-term has been rewritten */
4744 break;
4745
4746 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004747 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004748 if (cur_end)
4749 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004751 /* keep the link from this header to next one in case of later
4752 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004753 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004754 old_idx = cur_idx;
4755 }
4756 return 0;
4757}
4758
4759
4760/* Apply the filter to the request line.
4761 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4762 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763 * Since it can manage the switch to another backend, it updates the per-proxy
4764 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004765 */
4766int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4767{
4768 char term;
4769 char *cur_ptr, *cur_end;
4770 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004771 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004772 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004773
Willy Tarreau58f10d72006-12-04 02:26:12 +01004774
Willy Tarreau3d300592007-03-18 18:34:41 +01004775 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004776 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004777 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004778 (exp->action == ACT_ALLOW ||
4779 exp->action == ACT_DENY ||
4780 exp->action == ACT_TARPIT))
4781 return 0;
4782 else if (exp->action == ACT_REMOVE)
4783 return 0;
4784
4785 done = 0;
4786
Willy Tarreau9cdde232007-05-02 20:58:19 +02004787 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004788 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004789
4790 /* Now we have the request line between cur_ptr and cur_end */
4791
4792 /* The annoying part is that pattern matching needs
4793 * that we modify the contents to null-terminate all
4794 * strings before testing them.
4795 */
4796
4797 term = *cur_end;
4798 *cur_end = '\0';
4799
4800 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4801 switch (exp->action) {
4802 case ACT_SETBE:
4803 /* It is not possible to jump a second time.
4804 * FIXME: should we return an HTTP/500 here so that
4805 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004806 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004807 if (t->be != t->fe)
4808 break;
4809
4810 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004811 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004812 done = 1;
4813 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004814
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004815 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004816 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004817 done = 1;
4818 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004819
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004820 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004821 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004822
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004823 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004824 if (t->listener->counters)
4825 t->listener->counters->denied_resp++;
4826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004827 done = 1;
4828 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004830 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004831 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004832
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004833 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004834 if (t->listener->counters)
4835 t->listener->counters->denied_resp++;
4836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004837 done = 1;
4838 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004840 case ACT_REPLACE:
4841 *cur_end = term; /* restore the string terminator */
4842 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4843 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4844 /* FIXME: if the user adds a newline in the replacement, the
4845 * index will not be recalculated for now, and the new line
4846 * will not be counted as a new header.
4847 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004848
Willy Tarreaufa355d42009-11-29 18:12:29 +01004849 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004850 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004851
Willy Tarreau9cdde232007-05-02 20:58:19 +02004852 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004853 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004854 HTTP_MSG_RQMETH,
4855 cur_ptr, cur_end + 1,
4856 NULL, NULL);
4857 if (unlikely(!cur_end))
4858 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004859
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004860 /* we have a full request and we know that we have either a CR
4861 * or an LF at <ptr>.
4862 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004863 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4864 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004865 /* there is no point trying this regex on headers */
4866 return 1;
4867 }
4868 }
4869 *cur_end = term; /* restore the string terminator */
4870 return done;
4871}
Willy Tarreau97de6242006-12-27 17:18:38 +01004872
Willy Tarreau58f10d72006-12-04 02:26:12 +01004873
Willy Tarreau58f10d72006-12-04 02:26:12 +01004874
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004875/*
4876 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4877 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004878 * unparsable request. Since it can manage the switch to another backend, it
4879 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004880 */
4881int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4882{
Willy Tarreau3d300592007-03-18 18:34:41 +01004883 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004884 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004885 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004886 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004888 /*
4889 * The interleaving of transformations and verdicts
4890 * makes it difficult to decide to continue or stop
4891 * the evaluation.
4892 */
4893
Willy Tarreau3d300592007-03-18 18:34:41 +01004894 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4896 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4897 exp = exp->next;
4898 continue;
4899 }
4900
4901 /* Apply the filter to the request line. */
4902 ret = apply_filter_to_req_line(t, req, exp);
4903 if (unlikely(ret < 0))
4904 return -1;
4905
4906 if (likely(ret == 0)) {
4907 /* The filter did not match the request, it can be
4908 * iterated through all headers.
4909 */
4910 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004911 }
4912 exp = exp->next;
4913 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004914 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004915}
4916
4917
Willy Tarreaua15645d2007-03-18 16:22:39 +01004918
Willy Tarreau58f10d72006-12-04 02:26:12 +01004919/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004920 * Try to retrieve the server associated to the appsession.
4921 * If the server is found, it's assigned to the session.
4922 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004923void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004924 struct http_txn *txn = &t->txn;
4925 appsess *asession = NULL;
4926 char *sessid_temp = NULL;
4927
Cyril Bontéb21570a2009-11-29 20:04:48 +01004928 if (len > t->be->appsession_len) {
4929 len = t->be->appsession_len;
4930 }
4931
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004932 if (t->be->options2 & PR_O2_AS_REQL) {
4933 /* request-learn option is enabled : store the sessid in the session for future use */
4934 if (t->sessid != NULL) {
4935 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4936 pool_free2(apools.sessid, t->sessid);
4937 }
4938
4939 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4940 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4941 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4942 return;
4943 }
4944
Cyril Bontéb21570a2009-11-29 20:04:48 +01004945 memcpy(t->sessid, buf, len);
4946 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004947 }
4948
4949 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4950 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4951 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4952 return;
4953 }
4954
Cyril Bontéb21570a2009-11-29 20:04:48 +01004955 memcpy(sessid_temp, buf, len);
4956 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004957
4958 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4959 /* free previously allocated memory */
4960 pool_free2(apools.sessid, sessid_temp);
4961
4962 if (asession != NULL) {
4963 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4964 if (!(t->be->options2 & PR_O2_AS_REQL))
4965 asession->request_count++;
4966
4967 if (asession->serverid != NULL) {
4968 struct server *srv = t->be->srv;
4969 while (srv) {
4970 if (strcmp(srv->id, asession->serverid) == 0) {
4971 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4972 /* we found the server and it's usable */
4973 txn->flags &= ~TX_CK_MASK;
4974 txn->flags |= TX_CK_VALID;
4975 t->flags |= SN_DIRECT | SN_ASSIGNED;
4976 t->srv = srv;
4977 break;
4978 } else {
4979 txn->flags &= ~TX_CK_MASK;
4980 txn->flags |= TX_CK_DOWN;
4981 }
4982 }
4983 srv = srv->next;
4984 }
4985 }
4986 }
4987}
4988
4989/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004990 * Manage client-side cookie. It can impact performance by about 2% so it is
4991 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004992 */
4993void manage_client_side_cookies(struct session *t, struct buffer *req)
4994{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004995 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01004996 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004997 char *del_colon, *del_cookie, *colon;
4998 int app_cookies;
4999
Willy Tarreau58f10d72006-12-04 02:26:12 +01005000 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005001 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005002
Willy Tarreau2a324282006-12-05 00:05:46 +01005003 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005004 * we start with the start line.
5005 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005006 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005007 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005008
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005009 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005010 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005011 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005012
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005013 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005014 cur_ptr = cur_next;
5015 cur_end = cur_ptr + cur_hdr->len;
5016 cur_next = cur_end + cur_hdr->cr + 1;
5017
5018 /* We have one full header between cur_ptr and cur_end, and the
5019 * next header starts at cur_next. We're only interested in
5020 * "Cookie:" headers.
5021 */
5022
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005023 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5024 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005025 old_idx = cur_idx;
5026 continue;
5027 }
5028
5029 /* Now look for cookies. Conforming to RFC2109, we have to support
5030 * attributes whose name begin with a '$', and associate them with
5031 * the right cookie, if we want to delete this cookie.
5032 * So there are 3 cases for each cookie read :
5033 * 1) it's a special attribute, beginning with a '$' : ignore it.
5034 * 2) it's a server id cookie that we *MAY* want to delete : save
5035 * some pointers on it (last semi-colon, beginning of cookie...)
5036 * 3) it's an application cookie : we *MAY* have to delete a previous
5037 * "special" cookie.
5038 * At the end of loop, if a "special" cookie remains, we may have to
5039 * remove it. If no application cookie persists in the header, we
5040 * *MUST* delete it
5041 */
5042
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005043 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005044
Willy Tarreau58f10d72006-12-04 02:26:12 +01005045 /* del_cookie == NULL => nothing to be deleted */
5046 del_colon = del_cookie = NULL;
5047 app_cookies = 0;
5048
5049 while (p1 < cur_end) {
5050 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005051 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005052 while (p1 < cur_end) {
5053 if (*p1 == ';' || *p1 == ',')
5054 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005055 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005056 break;
5057 p1++;
5058 }
5059
5060 if (p1 == cur_end)
5061 break;
5062
5063 /* p1 is at the beginning of the cookie name */
5064 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005065 while (p2 < cur_end && *p2 != '=') {
5066 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5067 /* oops, the cookie name was truncated, resync */
5068 p1 = p2;
5069 goto resync_name;
5070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005071 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005073
5074 if (p2 == cur_end)
5075 break;
5076
5077 p3 = p2 + 1; /* skips the '=' sign */
5078 if (p3 == cur_end)
5079 break;
5080
Willy Tarreau305ae852010-01-03 19:45:54 +01005081 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5082 p5 = p4 = p3;
5083 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5084 if (!isspace((unsigned char)*p5))
5085 p4 = p5 + 1;
5086 p5++;
5087 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088
5089 /* here, we have the cookie name between p1 and p2,
5090 * and its value between p3 and p4.
5091 * we can process it :
5092 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005093 * Cookie: NAME=VALUE ;
5094 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005095 * | || || +--> p4
5096 * | || |+-------> p3
5097 * | || +--------> p2
5098 * | |+------------> p1
5099 * | +-------------> colon
5100 * +--------------------> cur_ptr
5101 */
5102
5103 if (*p1 == '$') {
5104 /* skip this one */
5105 }
5106 else {
5107 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005108 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005109 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005110 (p4 - p1 >= t->fe->capture_namelen) &&
5111 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005112 int log_len = p4 - p1;
5113
Willy Tarreau086b3b42007-05-13 21:45:51 +02005114 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005115 Alert("HTTP logging : out of memory.\n");
5116 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005117 if (log_len > t->fe->capture_len)
5118 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005119 memcpy(txn->cli_cookie, p1, log_len);
5120 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005121 }
5122 }
5123
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005124 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5125 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005126 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005127 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005128 char *delim;
5129
5130 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5131 * have the server ID betweek p3 and delim, and the original cookie between
5132 * delim+1 and p4. Otherwise, delim==p4 :
5133 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005134 * Cookie: NAME=SRV~VALUE ;
5135 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005136 * | || || | +--> p4
5137 * | || || +--------> delim
5138 * | || |+-----------> p3
5139 * | || +------------> p2
5140 * | |+----------------> p1
5141 * | +-----------------> colon
5142 * +------------------------> cur_ptr
5143 */
5144
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005145 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005146 for (delim = p3; delim < p4; delim++)
5147 if (*delim == COOKIE_DELIM)
5148 break;
5149 }
5150 else
5151 delim = p4;
5152
5153
5154 /* Here, we'll look for the first running server which supports the cookie.
5155 * This allows to share a same cookie between several servers, for example
5156 * to dedicate backup servers to specific servers only.
5157 * However, to prevent clients from sticking to cookie-less backup server
5158 * when they have incidentely learned an empty cookie, we simply ignore
5159 * empty cookies and mark them as invalid.
5160 */
5161 if (delim == p3)
5162 srv = NULL;
5163
5164 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005165 if (srv->cookie && (srv->cklen == delim - p3) &&
5166 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005167 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005168 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005169 txn->flags &= ~TX_CK_MASK;
5170 txn->flags |= TX_CK_VALID;
5171 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005172 t->srv = srv;
5173 break;
5174 } else {
5175 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005176 txn->flags &= ~TX_CK_MASK;
5177 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005178 }
5179 }
5180 srv = srv->next;
5181 }
5182
Willy Tarreau3d300592007-03-18 18:34:41 +01005183 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005184 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005185 txn->flags &= ~TX_CK_MASK;
5186 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005187 }
5188
5189 /* depending on the cookie mode, we may have to either :
5190 * - delete the complete cookie if we're in insert+indirect mode, so that
5191 * the server never sees it ;
5192 * - remove the server id from the cookie value, and tag the cookie as an
5193 * application cookie so that it does not get accidentely removed later,
5194 * if we're in cookie prefix mode
5195 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005196 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005197 int delta; /* negative */
5198
5199 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5200 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005201 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005202 cur_end += delta;
5203 cur_next += delta;
5204 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005205 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005206
5207 del_cookie = del_colon = NULL;
5208 app_cookies++; /* protect the header from deletion */
5209 }
5210 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005211 (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 +01005212 del_cookie = p1;
5213 del_colon = colon;
5214 }
5215 } else {
5216 /* now we know that we must keep this cookie since it's
5217 * not ours. But if we wanted to delete our cookie
5218 * earlier, we cannot remove the complete header, but we
5219 * can remove the previous block itself.
5220 */
5221 app_cookies++;
5222
5223 if (del_cookie != NULL) {
5224 int delta; /* negative */
5225
5226 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5227 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005228 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005229 cur_end += delta;
5230 cur_next += delta;
5231 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005232 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005233 del_cookie = del_colon = NULL;
5234 }
5235 }
5236
Cyril Bontéb21570a2009-11-29 20:04:48 +01005237 if (t->be->appsession_name != NULL) {
5238 int cmp_len, value_len;
5239 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005240
Cyril Bontéb21570a2009-11-29 20:04:48 +01005241 if (t->be->options2 & PR_O2_AS_PFX) {
5242 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5243 value_begin = p1 + t->be->appsession_name_len;
5244 value_len = p4 - p1 - t->be->appsession_name_len;
5245 } else {
5246 cmp_len = p2 - p1;
5247 value_begin = p3;
5248 value_len = p4 - p3;
5249 }
5250
5251 /* let's see if the cookie is our appcookie */
5252 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5253 /* Cool... it's the right one */
5254 manage_client_side_appsession(t, value_begin, value_len);
5255 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005256#if defined(DEBUG_HASH)
5257 Alert("manage_client_side_cookies\n");
5258 appsession_hash_dump(&(t->be->htbl_proxy));
5259#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005260 }/* end if ((t->proxy->appsession_name != NULL) ... */
5261 }
5262
5263 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005264 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005265 } /* while (p1 < cur_end) */
5266
5267 /* There's no more cookie on this line.
5268 * We may have marked the last one(s) for deletion.
5269 * We must do this now in two ways :
5270 * - if there is no app cookie, we simply delete the header ;
5271 * - if there are app cookies, we must delete the end of the
5272 * string properly, including the colon/semi-colon before
5273 * the cookie name.
5274 */
5275 if (del_cookie != NULL) {
5276 int delta;
5277 if (app_cookies) {
5278 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5279 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005280 cur_hdr->len += delta;
5281 } else {
5282 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005283
5284 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005285 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5286 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005287 cur_hdr->len = 0;
5288 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005289 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005290 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005291 }
5292
5293 /* keep the link from this header to next one */
5294 old_idx = cur_idx;
5295 } /* end of cookie processing on this header */
5296}
5297
5298
Willy Tarreaua15645d2007-03-18 16:22:39 +01005299/* Iterate the same filter through all response headers contained in <rtr>.
5300 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5301 */
5302int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5303{
5304 char term;
5305 char *cur_ptr, *cur_end, *cur_next;
5306 int cur_idx, old_idx, last_hdr;
5307 struct http_txn *txn = &t->txn;
5308 struct hdr_idx_elem *cur_hdr;
5309 int len, delta;
5310
5311 last_hdr = 0;
5312
5313 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5314 old_idx = 0;
5315
5316 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005317 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005318 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005319 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005320 (exp->action == ACT_ALLOW ||
5321 exp->action == ACT_DENY))
5322 return 0;
5323
5324 cur_idx = txn->hdr_idx.v[old_idx].next;
5325 if (!cur_idx)
5326 break;
5327
5328 cur_hdr = &txn->hdr_idx.v[cur_idx];
5329 cur_ptr = cur_next;
5330 cur_end = cur_ptr + cur_hdr->len;
5331 cur_next = cur_end + cur_hdr->cr + 1;
5332
5333 /* Now we have one header between cur_ptr and cur_end,
5334 * and the next header starts at cur_next.
5335 */
5336
5337 /* The annoying part is that pattern matching needs
5338 * that we modify the contents to null-terminate all
5339 * strings before testing them.
5340 */
5341
5342 term = *cur_end;
5343 *cur_end = '\0';
5344
5345 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5346 switch (exp->action) {
5347 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005348 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005349 last_hdr = 1;
5350 break;
5351
5352 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005353 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005354 last_hdr = 1;
5355 break;
5356
5357 case ACT_REPLACE:
5358 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5359 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5360 /* FIXME: if the user adds a newline in the replacement, the
5361 * index will not be recalculated for now, and the new line
5362 * will not be counted as a new header.
5363 */
5364
5365 cur_end += delta;
5366 cur_next += delta;
5367 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005368 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005369 break;
5370
5371 case ACT_REMOVE:
5372 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5373 cur_next += delta;
5374
Willy Tarreaufa355d42009-11-29 18:12:29 +01005375 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005376 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5377 txn->hdr_idx.used--;
5378 cur_hdr->len = 0;
5379 cur_end = NULL; /* null-term has been rewritten */
5380 break;
5381
5382 }
5383 }
5384 if (cur_end)
5385 *cur_end = term; /* restore the string terminator */
5386
5387 /* keep the link from this header to next one in case of later
5388 * removal of next header.
5389 */
5390 old_idx = cur_idx;
5391 }
5392 return 0;
5393}
5394
5395
5396/* Apply the filter to the status line in the response buffer <rtr>.
5397 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5398 * or -1 if a replacement resulted in an invalid status line.
5399 */
5400int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5401{
5402 char term;
5403 char *cur_ptr, *cur_end;
5404 int done;
5405 struct http_txn *txn = &t->txn;
5406 int len, delta;
5407
5408
Willy Tarreau3d300592007-03-18 18:34:41 +01005409 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005410 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005411 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005412 (exp->action == ACT_ALLOW ||
5413 exp->action == ACT_DENY))
5414 return 0;
5415 else if (exp->action == ACT_REMOVE)
5416 return 0;
5417
5418 done = 0;
5419
Willy Tarreau9cdde232007-05-02 20:58:19 +02005420 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005421 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5422
5423 /* Now we have the status line between cur_ptr and cur_end */
5424
5425 /* The annoying part is that pattern matching needs
5426 * that we modify the contents to null-terminate all
5427 * strings before testing them.
5428 */
5429
5430 term = *cur_end;
5431 *cur_end = '\0';
5432
5433 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5434 switch (exp->action) {
5435 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005436 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005437 done = 1;
5438 break;
5439
5440 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005441 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005442 done = 1;
5443 break;
5444
5445 case ACT_REPLACE:
5446 *cur_end = term; /* restore the string terminator */
5447 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5448 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5449 /* FIXME: if the user adds a newline in the replacement, the
5450 * index will not be recalculated for now, and the new line
5451 * will not be counted as a new header.
5452 */
5453
Willy Tarreaufa355d42009-11-29 18:12:29 +01005454 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005455 cur_end += delta;
5456
Willy Tarreau9cdde232007-05-02 20:58:19 +02005457 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005458 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005459 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005460 cur_ptr, cur_end + 1,
5461 NULL, NULL);
5462 if (unlikely(!cur_end))
5463 return -1;
5464
5465 /* we have a full respnse and we know that we have either a CR
5466 * or an LF at <ptr>.
5467 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005468 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005469 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5470 /* there is no point trying this regex on headers */
5471 return 1;
5472 }
5473 }
5474 *cur_end = term; /* restore the string terminator */
5475 return done;
5476}
5477
5478
5479
5480/*
5481 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5482 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5483 * unparsable response.
5484 */
5485int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5486{
Willy Tarreau3d300592007-03-18 18:34:41 +01005487 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005488 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005489 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005490 int ret;
5491
5492 /*
5493 * The interleaving of transformations and verdicts
5494 * makes it difficult to decide to continue or stop
5495 * the evaluation.
5496 */
5497
Willy Tarreau3d300592007-03-18 18:34:41 +01005498 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005499 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5500 exp->action == ACT_PASS)) {
5501 exp = exp->next;
5502 continue;
5503 }
5504
5505 /* Apply the filter to the status line. */
5506 ret = apply_filter_to_sts_line(t, rtr, exp);
5507 if (unlikely(ret < 0))
5508 return -1;
5509
5510 if (likely(ret == 0)) {
5511 /* The filter did not match the response, it can be
5512 * iterated through all headers.
5513 */
5514 apply_filter_to_resp_headers(t, rtr, exp);
5515 }
5516 exp = exp->next;
5517 }
5518 return 0;
5519}
5520
5521
5522
5523/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005524 * Manage server-side cookies. It can impact performance by about 2% so it is
5525 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005526 */
5527void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5528{
5529 struct http_txn *txn = &t->txn;
5530 char *p1, *p2, *p3, *p4;
5531
Willy Tarreaua15645d2007-03-18 16:22:39 +01005532 char *cur_ptr, *cur_end, *cur_next;
5533 int cur_idx, old_idx, delta;
5534
Willy Tarreaua15645d2007-03-18 16:22:39 +01005535 /* Iterate through the headers.
5536 * we start with the start line.
5537 */
5538 old_idx = 0;
5539 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5540
5541 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5542 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005543 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005544
5545 cur_hdr = &txn->hdr_idx.v[cur_idx];
5546 cur_ptr = cur_next;
5547 cur_end = cur_ptr + cur_hdr->len;
5548 cur_next = cur_end + cur_hdr->cr + 1;
5549
5550 /* We have one full header between cur_ptr and cur_end, and the
5551 * next header starts at cur_next. We're only interested in
5552 * "Cookie:" headers.
5553 */
5554
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005555 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5556 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005557 old_idx = cur_idx;
5558 continue;
5559 }
5560
5561 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005562 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005563
5564
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005565 /* maybe we only wanted to see if there was a set-cookie. Note that
5566 * the cookie capture is declared in the fronend.
5567 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005568 if (t->be->cookie_name == NULL &&
5569 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005570 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005571 return;
5572
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005573 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005574
5575 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005576 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5577 break;
5578
5579 /* p1 is at the beginning of the cookie name */
5580 p2 = p1;
5581
5582 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5583 p2++;
5584
5585 if (p2 == cur_end || *p2 == ';') /* next cookie */
5586 break;
5587
5588 p3 = p2 + 1; /* skip the '=' sign */
5589 if (p3 == cur_end)
5590 break;
5591
5592 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005593 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005594 p4++;
5595
5596 /* here, we have the cookie name between p1 and p2,
5597 * and its value between p3 and p4.
5598 * we can process it.
5599 */
5600
5601 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005602 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005603 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005604 (p4 - p1 >= t->fe->capture_namelen) &&
5605 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005606 int log_len = p4 - p1;
5607
Willy Tarreau086b3b42007-05-13 21:45:51 +02005608 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005609 Alert("HTTP logging : out of memory.\n");
5610 }
5611
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005612 if (log_len > t->fe->capture_len)
5613 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005614 memcpy(txn->srv_cookie, p1, log_len);
5615 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005616 }
5617
5618 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005619 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5620 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005621 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005622 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005623
5624 /* If the cookie is in insert mode on a known server, we'll delete
5625 * this occurrence because we'll insert another one later.
5626 * We'll delete it too if the "indirect" option is set and we're in
5627 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005628 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5629 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005630 /* this header must be deleted */
5631 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5632 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5633 txn->hdr_idx.used--;
5634 cur_hdr->len = 0;
5635 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005636 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005637
Willy Tarreau3d300592007-03-18 18:34:41 +01005638 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005639 }
5640 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005641 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005642 /* replace bytes p3->p4 with the cookie name associated
5643 * with this server since we know it.
5644 */
5645 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5646 cur_hdr->len += delta;
5647 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005648 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005649
Willy Tarreau3d300592007-03-18 18:34:41 +01005650 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005651 }
5652 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005653 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005654 /* insert the cookie name associated with this server
5655 * before existing cookie, and insert a delimitor between them..
5656 */
5657 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5658 cur_hdr->len += delta;
5659 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005660 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005661
5662 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005663 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005664 }
5665 }
5666 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005667 else if (t->be->appsession_name != NULL) {
5668 int cmp_len, value_len;
5669 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005670
Cyril Bontéb21570a2009-11-29 20:04:48 +01005671 if (t->be->options2 & PR_O2_AS_PFX) {
5672 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5673 value_begin = p1 + t->be->appsession_name_len;
5674 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5675 } else {
5676 cmp_len = p2 - p1;
5677 value_begin = p3;
5678 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005679 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005680
5681 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5682 /* Cool... it's the right one */
5683 if (t->sessid != NULL) {
5684 /* free previously allocated memory as we don't need it anymore */
5685 pool_free2(apools.sessid, t->sessid);
5686 }
5687 /* Store the sessid in the session for future use */
5688 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5689 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5690 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5691 return;
5692 }
5693 memcpy(t->sessid, value_begin, value_len);
5694 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005695 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005696 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005697 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5698 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005699 /* keep the link from this header to next one */
5700 old_idx = cur_idx;
5701 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005702
5703 if (t->sessid != NULL) {
5704 appsess *asession = NULL;
5705 /* only do insert, if lookup fails */
5706 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5707 if (asession == NULL) {
5708 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5709 Alert("Not enough Memory process_srv():asession:calloc().\n");
5710 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5711 return;
5712 }
5713 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5714 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5715 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5716 return;
5717 }
5718 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5719 asession->sessid[t->be->appsession_len] = 0;
5720
5721 size_t server_id_len = strlen(t->srv->id) + 1;
5722 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5723 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5724 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5725 return;
5726 }
5727 asession->serverid[0] = '\0';
5728 memcpy(asession->serverid, t->srv->id, server_id_len);
5729
5730 asession->request_count = 0;
5731 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5732 }
5733
5734 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5735 asession->request_count++;
5736 }
5737
5738#if defined(DEBUG_HASH)
5739 Alert("manage_server_side_cookies\n");
5740 appsession_hash_dump(&(t->be->htbl_proxy));
5741#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005742}
5743
5744
5745
5746/*
5747 * Check if response is cacheable or not. Updates t->flags.
5748 */
5749void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5750{
5751 struct http_txn *txn = &t->txn;
5752 char *p1, *p2;
5753
5754 char *cur_ptr, *cur_end, *cur_next;
5755 int cur_idx;
5756
Willy Tarreau5df51872007-11-25 16:20:08 +01005757 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005758 return;
5759
5760 /* Iterate through the headers.
5761 * we start with the start line.
5762 */
5763 cur_idx = 0;
5764 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5765
5766 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5767 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005768 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005769
5770 cur_hdr = &txn->hdr_idx.v[cur_idx];
5771 cur_ptr = cur_next;
5772 cur_end = cur_ptr + cur_hdr->len;
5773 cur_next = cur_end + cur_hdr->cr + 1;
5774
5775 /* We have one full header between cur_ptr and cur_end, and the
5776 * next header starts at cur_next. We're only interested in
5777 * "Cookie:" headers.
5778 */
5779
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005780 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5781 if (val) {
5782 if ((cur_end - (cur_ptr + val) >= 8) &&
5783 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5784 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5785 return;
5786 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005787 }
5788
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005789 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5790 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005791 continue;
5792
5793 /* OK, right now we know we have a cache-control header at cur_ptr */
5794
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005795 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005796
5797 if (p1 >= cur_end) /* no more info */
5798 continue;
5799
5800 /* p1 is at the beginning of the value */
5801 p2 = p1;
5802
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005803 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005804 p2++;
5805
5806 /* we have a complete value between p1 and p2 */
5807 if (p2 < cur_end && *p2 == '=') {
5808 /* we have something of the form no-cache="set-cookie" */
5809 if ((cur_end - p1 >= 21) &&
5810 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5811 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005812 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005813 continue;
5814 }
5815
5816 /* OK, so we know that either p2 points to the end of string or to a comma */
5817 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5818 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5819 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5820 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005821 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005822 return;
5823 }
5824
5825 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005826 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005827 continue;
5828 }
5829 }
5830}
5831
5832
Willy Tarreau58f10d72006-12-04 02:26:12 +01005833/*
5834 * Try to retrieve a known appsession in the URI, then the associated server.
5835 * If the server is found, it's assigned to the session.
5836 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005837void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005838{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005839 char *end_params, *first_param, *cur_param, *next_param;
5840 char separator;
5841 int value_len;
5842
5843 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005844
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005845 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005846 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005847 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005848 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005849
Cyril Bontéb21570a2009-11-29 20:04:48 +01005850 first_param = NULL;
5851 switch (mode) {
5852 case PR_O2_AS_M_PP:
5853 first_param = memchr(begin, ';', len);
5854 break;
5855 case PR_O2_AS_M_QS:
5856 first_param = memchr(begin, '?', len);
5857 break;
5858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005859
Cyril Bontéb21570a2009-11-29 20:04:48 +01005860 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005861 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005862 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005863
Cyril Bontéb21570a2009-11-29 20:04:48 +01005864 switch (mode) {
5865 case PR_O2_AS_M_PP:
5866 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5867 end_params = (char *) begin + len;
5868 }
5869 separator = ';';
5870 break;
5871 case PR_O2_AS_M_QS:
5872 end_params = (char *) begin + len;
5873 separator = '&';
5874 break;
5875 default:
5876 /* unknown mode, shouldn't happen */
5877 return;
5878 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005879
Cyril Bontéb21570a2009-11-29 20:04:48 +01005880 cur_param = next_param = end_params;
5881 while (cur_param > first_param) {
5882 cur_param--;
5883 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5884 /* let's see if this is the appsession parameter */
5885 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5886 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5887 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5888 /* Cool... it's the right one */
5889 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5890 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5891 if (value_len > 0) {
5892 manage_client_side_appsession(t, cur_param, value_len);
5893 }
5894 break;
5895 }
5896 next_param = cur_param;
5897 }
5898 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005899#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005900 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005901 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005903}
5904
5905
Willy Tarreaub2513902006-12-17 14:52:38 +01005906/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005907 * In a GET or HEAD request, check if the requested URI matches the stats uri
5908 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005909 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005910 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005911 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005912 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005913 *
5914 * Returns 1 if the session's state changes, otherwise 0.
5915 */
5916int stats_check_uri_auth(struct session *t, struct proxy *backend)
5917{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005918 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005919 struct uri_auth *uri_auth = backend->uri_auth;
5920 struct user_auth *user;
5921 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005923
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005924 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5925
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005926 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005927 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005928 return 0;
5929
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005930 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005931
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005932 /* the URI is in h */
5933 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005934 return 0;
5935
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005936 h += uri_auth->uri_len;
5937 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5938 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005939 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005940 break;
5941 }
5942 h++;
5943 }
5944
5945 if (uri_auth->refresh) {
5946 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5947 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5948 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005949 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005950 break;
5951 }
5952 h++;
5953 }
5954 }
5955
Willy Tarreau55bb8452007-10-17 18:44:57 +02005956 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5957 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5958 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005959 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005960 break;
5961 }
5962 h++;
5963 }
5964
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005965 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5966
Willy Tarreaub2513902006-12-17 14:52:38 +01005967 /* we are in front of a interceptable URI. Let's check
5968 * if there's an authentication and if it's valid.
5969 */
5970 user = uri_auth->users;
5971 if (!user) {
5972 /* no user auth required, it's OK */
5973 authenticated = 1;
5974 } else {
5975 authenticated = 0;
5976
5977 /* a user list is defined, we have to check.
5978 * skip 21 chars for "Authorization: Basic ".
5979 */
5980
5981 /* FIXME: this should move to an earlier place */
5982 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005983 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5984 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5985 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005986 if (len > 14 &&
5987 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005988 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005989 break;
5990 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005991 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005992 }
5993
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005994 if (txn->auth_hdr.len < 21 ||
5995 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005996 user = NULL;
5997
5998 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005999 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6000 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006001 user->user_pwd, user->user_len)) {
6002 authenticated = 1;
6003 break;
6004 }
6005 user = user->next;
6006 }
6007 }
6008
6009 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006010 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006011
6012 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006013 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6014 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006015 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006016 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006017 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006018 if (!(t->flags & SN_ERR_MASK))
6019 t->flags |= SN_ERR_PRXCOND;
6020 if (!(t->flags & SN_FINST_MASK))
6021 t->flags |= SN_FINST_R;
6022 return 1;
6023 }
6024
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006025 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006026 * data.
6027 */
Willy Tarreau70089872008-06-13 21:12:51 +02006028 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006029 t->data_source = DATA_SRC_STATS;
6030 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006031 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006032 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6033 t->rep->prod->private = t;
6034 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006035 return 1;
6036}
6037
Willy Tarreau4076a152009-04-02 15:18:36 +02006038/*
6039 * Capture a bad request or response and archive it in the proxy's structure.
6040 */
6041void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6042 struct buffer *buf, struct http_msg *msg,
6043 struct proxy *other_end)
6044{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006045 es->len = buf->r - (buf->data + msg->som);
6046 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006047 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006048 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006049 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006050 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006051 es->when = date; // user-visible date
6052 es->sid = s->uniq_id;
6053 es->srv = s->srv;
6054 es->oe = other_end;
6055 es->src = s->cli_addr;
6056}
Willy Tarreaub2513902006-12-17 14:52:38 +01006057
Willy Tarreaubaaee002006-06-26 02:48:02 +02006058/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006059 * Print a debug line with a header
6060 */
6061void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6062{
6063 int len, max;
6064 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006065 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006066 max = end - start;
6067 UBOUND(max, sizeof(trash) - len - 1);
6068 len += strlcpy2(trash + len, start, max + 1);
6069 trash[len++] = '\n';
6070 write(1, trash, len);
6071}
6072
Willy Tarreau0937bc42009-12-22 15:03:09 +01006073/*
6074 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6075 * the required fields are properly allocated and that we only need to (re)init
6076 * them. This should be used before processing any new request.
6077 */
6078void http_init_txn(struct session *s)
6079{
6080 struct http_txn *txn = &s->txn;
6081 struct proxy *fe = s->fe;
6082
6083 txn->flags = 0;
6084 txn->status = -1;
6085
6086 txn->req.sol = txn->req.eol = NULL;
6087 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6088 txn->rsp.sol = txn->rsp.eol = NULL;
6089 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6090 txn->req.hdr_content_len = 0LL;
6091 txn->rsp.hdr_content_len = 0LL;
6092 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6093 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6094 chunk_reset(&txn->auth_hdr);
6095
6096 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6097 if (fe->options2 & PR_O2_REQBUG_OK)
6098 txn->req.err_pos = -1; /* let buggy requests pass */
6099
6100 if (txn->req.cap)
6101 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6102
6103 if (txn->rsp.cap)
6104 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6105
6106 if (txn->hdr_idx.v)
6107 hdr_idx_init(&txn->hdr_idx);
6108}
6109
6110/* to be used at the end of a transaction */
6111void http_end_txn(struct session *s)
6112{
6113 struct http_txn *txn = &s->txn;
6114
6115 /* these ones will have been dynamically allocated */
6116 pool_free2(pool2_requri, txn->uri);
6117 pool_free2(pool2_capture, txn->cli_cookie);
6118 pool_free2(pool2_capture, txn->srv_cookie);
6119 txn->uri = NULL;
6120 txn->srv_cookie = NULL;
6121 txn->cli_cookie = NULL;
6122}
6123
6124/* to be used at the end of a transaction to prepare a new one */
6125void http_reset_txn(struct session *s)
6126{
6127 http_end_txn(s);
6128 http_init_txn(s);
6129
6130 s->be = s->fe;
6131 s->req->analysers = s->listener->analysers;
6132 s->logs.logwait = s->fe->to_log;
6133 s->srv = s->prev_srv = s->srv_conn = NULL;
6134 s->pend_pos = NULL;
6135 s->conn_retries = s->be->conn_retries;
6136
6137 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6138
6139 s->req->rto = s->fe->timeout.client;
6140 s->req->wto = s->be->timeout.server;
6141 s->req->cto = s->be->timeout.connect;
6142
6143 s->rep->rto = s->be->timeout.server;
6144 s->rep->wto = s->fe->timeout.client;
6145 s->rep->cto = TICK_ETERNITY;
6146
6147 s->req->rex = TICK_ETERNITY;
6148 s->req->wex = TICK_ETERNITY;
6149 s->req->analyse_exp = TICK_ETERNITY;
6150 s->rep->rex = TICK_ETERNITY;
6151 s->rep->wex = TICK_ETERNITY;
6152 s->rep->analyse_exp = TICK_ETERNITY;
6153}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006154
Willy Tarreau8797c062007-05-07 00:55:35 +02006155/************************************************************************/
6156/* The code below is dedicated to ACL parsing and matching */
6157/************************************************************************/
6158
6159
6160
6161
6162/* 1. Check on METHOD
6163 * We use the pre-parsed method if it is known, and store its number as an
6164 * integer. If it is unknown, we use the pointer and the length.
6165 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006166static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006167{
6168 int len, meth;
6169
Willy Tarreauae8b7962007-06-09 23:10:04 +02006170 len = strlen(*text);
6171 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006172
6173 pattern->val.i = meth;
6174 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006175 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006176 if (!pattern->ptr.str)
6177 return 0;
6178 pattern->len = len;
6179 }
6180 return 1;
6181}
6182
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006183static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006184acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6185 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006186{
6187 int meth;
6188 struct http_txn *txn = l7;
6189
Willy Tarreaub6866442008-07-14 23:54:42 +02006190 if (!txn)
6191 return 0;
6192
Willy Tarreau655dce92009-11-08 13:10:58 +01006193 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006194 return 0;
6195
Willy Tarreau8797c062007-05-07 00:55:35 +02006196 meth = txn->meth;
6197 test->i = meth;
6198 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006199 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6200 /* ensure the indexes are not affected */
6201 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006202 test->len = txn->req.sl.rq.m_l;
6203 test->ptr = txn->req.sol;
6204 }
6205 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6206 return 1;
6207}
6208
6209static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6210{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006211 int icase;
6212
Willy Tarreau8797c062007-05-07 00:55:35 +02006213 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006214 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006215
6216 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006217 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006218
6219 /* Other method, we must compare the strings */
6220 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006221 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006222
6223 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6224 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6225 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006226 return ACL_PAT_FAIL;
6227 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006228}
6229
6230/* 2. Check on Request/Status Version
6231 * We simply compare strings here.
6232 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006233static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006234{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006235 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006236 if (!pattern->ptr.str)
6237 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006238 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006239 return 1;
6240}
6241
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006242static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006243acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6244 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006245{
6246 struct http_txn *txn = l7;
6247 char *ptr;
6248 int len;
6249
Willy Tarreaub6866442008-07-14 23:54:42 +02006250 if (!txn)
6251 return 0;
6252
Willy Tarreau655dce92009-11-08 13:10:58 +01006253 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006254 return 0;
6255
Willy Tarreau8797c062007-05-07 00:55:35 +02006256 len = txn->req.sl.rq.v_l;
6257 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6258
6259 while ((len-- > 0) && (*ptr++ != '/'));
6260 if (len <= 0)
6261 return 0;
6262
6263 test->ptr = ptr;
6264 test->len = len;
6265
6266 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6267 return 1;
6268}
6269
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006270static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006271acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6272 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006273{
6274 struct http_txn *txn = l7;
6275 char *ptr;
6276 int len;
6277
Willy Tarreaub6866442008-07-14 23:54:42 +02006278 if (!txn)
6279 return 0;
6280
Willy Tarreau655dce92009-11-08 13:10:58 +01006281 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006282 return 0;
6283
Willy Tarreau8797c062007-05-07 00:55:35 +02006284 len = txn->rsp.sl.st.v_l;
6285 ptr = txn->rsp.sol;
6286
6287 while ((len-- > 0) && (*ptr++ != '/'));
6288 if (len <= 0)
6289 return 0;
6290
6291 test->ptr = ptr;
6292 test->len = len;
6293
6294 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6295 return 1;
6296}
6297
6298/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006299static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006300acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6301 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006302{
6303 struct http_txn *txn = l7;
6304 char *ptr;
6305 int len;
6306
Willy Tarreaub6866442008-07-14 23:54:42 +02006307 if (!txn)
6308 return 0;
6309
Willy Tarreau655dce92009-11-08 13:10:58 +01006310 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006311 return 0;
6312
Willy Tarreau8797c062007-05-07 00:55:35 +02006313 len = txn->rsp.sl.st.c_l;
6314 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6315
6316 test->i = __strl2ui(ptr, len);
6317 test->flags = ACL_TEST_F_VOL_1ST;
6318 return 1;
6319}
6320
6321/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006322static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006323acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6324 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006325{
6326 struct http_txn *txn = l7;
6327
Willy Tarreaub6866442008-07-14 23:54:42 +02006328 if (!txn)
6329 return 0;
6330
Willy Tarreau655dce92009-11-08 13:10:58 +01006331 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006332 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006333
Willy Tarreauc11416f2007-06-17 16:58:38 +02006334 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6335 /* ensure the indexes are not affected */
6336 return 0;
6337
Willy Tarreau8797c062007-05-07 00:55:35 +02006338 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006339 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006340
Willy Tarreauf3d25982007-05-08 22:45:09 +02006341 /* we do not need to set READ_ONLY because the data is in a buffer */
6342 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006343 return 1;
6344}
6345
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006346static int
6347acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6348 struct acl_expr *expr, struct acl_test *test)
6349{
6350 struct http_txn *txn = l7;
6351
Willy Tarreaub6866442008-07-14 23:54:42 +02006352 if (!txn)
6353 return 0;
6354
Willy Tarreau655dce92009-11-08 13:10:58 +01006355 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006356 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006357
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006358 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6359 /* ensure the indexes are not affected */
6360 return 0;
6361
6362 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006363 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006364 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6365 test->i = AF_INET;
6366
6367 /*
6368 * If we are parsing url in frontend space, we prepare backend stage
6369 * to not parse again the same url ! optimization lazyness...
6370 */
6371 if (px->options & PR_O_HTTP_PROXY)
6372 l4->flags |= SN_ADDR_SET;
6373
6374 test->flags = ACL_TEST_F_READ_ONLY;
6375 return 1;
6376}
6377
6378static int
6379acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6380 struct acl_expr *expr, struct acl_test *test)
6381{
6382 struct http_txn *txn = l7;
6383
Willy Tarreaub6866442008-07-14 23:54:42 +02006384 if (!txn)
6385 return 0;
6386
Willy Tarreau655dce92009-11-08 13:10:58 +01006387 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006388 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006389
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006390 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6391 /* ensure the indexes are not affected */
6392 return 0;
6393
6394 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006395 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006396 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6397
6398 if (px->options & PR_O_HTTP_PROXY)
6399 l4->flags |= SN_ADDR_SET;
6400
6401 test->flags = ACL_TEST_F_READ_ONLY;
6402 return 1;
6403}
6404
Willy Tarreauc11416f2007-06-17 16:58:38 +02006405/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6406 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6407 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006408static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006409acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006410 struct acl_expr *expr, struct acl_test *test)
6411{
6412 struct http_txn *txn = l7;
6413 struct hdr_idx *idx = &txn->hdr_idx;
6414 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006415
Willy Tarreaub6866442008-07-14 23:54:42 +02006416 if (!txn)
6417 return 0;
6418
Willy Tarreau33a7e692007-06-10 19:45:56 +02006419 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6420 /* search for header from the beginning */
6421 ctx->idx = 0;
6422
Willy Tarreau33a7e692007-06-10 19:45:56 +02006423 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6424 test->flags |= ACL_TEST_F_FETCH_MORE;
6425 test->flags |= ACL_TEST_F_VOL_HDR;
6426 test->len = ctx->vlen;
6427 test->ptr = (char *)ctx->line + ctx->val;
6428 return 1;
6429 }
6430
6431 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6432 test->flags |= ACL_TEST_F_VOL_HDR;
6433 return 0;
6434}
6435
Willy Tarreau33a7e692007-06-10 19:45:56 +02006436static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006437acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6438 struct acl_expr *expr, struct acl_test *test)
6439{
6440 struct http_txn *txn = l7;
6441
Willy Tarreaub6866442008-07-14 23:54:42 +02006442 if (!txn)
6443 return 0;
6444
Willy Tarreau655dce92009-11-08 13:10:58 +01006445 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006446 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006447
Willy Tarreauc11416f2007-06-17 16:58:38 +02006448 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6449 /* ensure the indexes are not affected */
6450 return 0;
6451
6452 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6453}
6454
6455static int
6456acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6457 struct acl_expr *expr, struct acl_test *test)
6458{
6459 struct http_txn *txn = l7;
6460
Willy Tarreaub6866442008-07-14 23:54:42 +02006461 if (!txn)
6462 return 0;
6463
Willy Tarreau655dce92009-11-08 13:10:58 +01006464 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006465 return 0;
6466
6467 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6468}
6469
6470/* 6. Check on HTTP header count. The number of occurrences is returned.
6471 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6472 */
6473static int
6474acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006475 struct acl_expr *expr, struct acl_test *test)
6476{
6477 struct http_txn *txn = l7;
6478 struct hdr_idx *idx = &txn->hdr_idx;
6479 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006480 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006481
Willy Tarreaub6866442008-07-14 23:54:42 +02006482 if (!txn)
6483 return 0;
6484
Willy Tarreau33a7e692007-06-10 19:45:56 +02006485 ctx.idx = 0;
6486 cnt = 0;
6487 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6488 cnt++;
6489
6490 test->i = cnt;
6491 test->flags = ACL_TEST_F_VOL_HDR;
6492 return 1;
6493}
6494
Willy Tarreauc11416f2007-06-17 16:58:38 +02006495static int
6496acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6497 struct acl_expr *expr, struct acl_test *test)
6498{
6499 struct http_txn *txn = l7;
6500
Willy Tarreaub6866442008-07-14 23:54:42 +02006501 if (!txn)
6502 return 0;
6503
Willy Tarreau655dce92009-11-08 13:10:58 +01006504 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006505 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006506
Willy Tarreauc11416f2007-06-17 16:58:38 +02006507 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6508 /* ensure the indexes are not affected */
6509 return 0;
6510
6511 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6512}
6513
6514static int
6515acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6516 struct acl_expr *expr, struct acl_test *test)
6517{
6518 struct http_txn *txn = l7;
6519
Willy Tarreaub6866442008-07-14 23:54:42 +02006520 if (!txn)
6521 return 0;
6522
Willy Tarreau655dce92009-11-08 13:10:58 +01006523 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006524 return 0;
6525
6526 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6527}
6528
Willy Tarreau33a7e692007-06-10 19:45:56 +02006529/* 7. Check on HTTP header's integer value. The integer value is returned.
6530 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006531 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006532 */
6533static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006534acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006535 struct acl_expr *expr, struct acl_test *test)
6536{
6537 struct http_txn *txn = l7;
6538 struct hdr_idx *idx = &txn->hdr_idx;
6539 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006540
Willy Tarreaub6866442008-07-14 23:54:42 +02006541 if (!txn)
6542 return 0;
6543
Willy Tarreau33a7e692007-06-10 19:45:56 +02006544 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6545 /* search for header from the beginning */
6546 ctx->idx = 0;
6547
Willy Tarreau33a7e692007-06-10 19:45:56 +02006548 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6549 test->flags |= ACL_TEST_F_FETCH_MORE;
6550 test->flags |= ACL_TEST_F_VOL_HDR;
6551 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6552 return 1;
6553 }
6554
6555 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6556 test->flags |= ACL_TEST_F_VOL_HDR;
6557 return 0;
6558}
6559
Willy Tarreauc11416f2007-06-17 16:58:38 +02006560static int
6561acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6562 struct acl_expr *expr, struct acl_test *test)
6563{
6564 struct http_txn *txn = l7;
6565
Willy Tarreaub6866442008-07-14 23:54:42 +02006566 if (!txn)
6567 return 0;
6568
Willy Tarreau655dce92009-11-08 13:10:58 +01006569 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006570 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006571
Willy Tarreauc11416f2007-06-17 16:58:38 +02006572 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6573 /* ensure the indexes are not affected */
6574 return 0;
6575
6576 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6577}
6578
6579static int
6580acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6581 struct acl_expr *expr, struct acl_test *test)
6582{
6583 struct http_txn *txn = l7;
6584
Willy Tarreaub6866442008-07-14 23:54:42 +02006585 if (!txn)
6586 return 0;
6587
Willy Tarreau655dce92009-11-08 13:10:58 +01006588 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006589 return 0;
6590
6591 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6592}
6593
Willy Tarreau106f9792009-09-19 07:54:16 +02006594/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6595 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6596 */
6597static int
6598acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6599 struct acl_expr *expr, struct acl_test *test)
6600{
6601 struct http_txn *txn = l7;
6602 struct hdr_idx *idx = &txn->hdr_idx;
6603 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6604
6605 if (!txn)
6606 return 0;
6607
6608 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6609 /* search for header from the beginning */
6610 ctx->idx = 0;
6611
6612 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6613 test->flags |= ACL_TEST_F_FETCH_MORE;
6614 test->flags |= ACL_TEST_F_VOL_HDR;
6615 /* Same optimization as url_ip */
6616 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6617 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6618 test->ptr = (void *)&l4->srv_addr.sin_addr;
6619 test->i = AF_INET;
6620 return 1;
6621 }
6622
6623 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6624 test->flags |= ACL_TEST_F_VOL_HDR;
6625 return 0;
6626}
6627
6628static int
6629acl_fetch_chdr_ip(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
6634 if (!txn)
6635 return 0;
6636
Willy Tarreau655dce92009-11-08 13:10:58 +01006637 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006638 return 0;
6639
6640 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6641 /* ensure the indexes are not affected */
6642 return 0;
6643
6644 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6645}
6646
6647static int
6648acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6649 struct acl_expr *expr, struct acl_test *test)
6650{
6651 struct http_txn *txn = l7;
6652
6653 if (!txn)
6654 return 0;
6655
Willy Tarreau655dce92009-11-08 13:10:58 +01006656 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006657 return 0;
6658
6659 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6660}
6661
Willy Tarreau737b0c12007-06-10 21:28:46 +02006662/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6663 * the first '/' after the possible hostname, and ends before the possible '?'.
6664 */
6665static int
6666acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6667 struct acl_expr *expr, struct acl_test *test)
6668{
6669 struct http_txn *txn = l7;
6670 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006671
Willy Tarreaub6866442008-07-14 23:54:42 +02006672 if (!txn)
6673 return 0;
6674
Willy Tarreau655dce92009-11-08 13:10:58 +01006675 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006676 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006677
Willy Tarreauc11416f2007-06-17 16:58:38 +02006678 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6679 /* ensure the indexes are not affected */
6680 return 0;
6681
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006682 end = txn->req.sol - txn->req.som + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006683 ptr = http_get_path(txn);
6684 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006685 return 0;
6686
6687 /* OK, we got the '/' ! */
6688 test->ptr = ptr;
6689
6690 while (ptr < end && *ptr != '?')
6691 ptr++;
6692
6693 test->len = ptr - test->ptr;
6694
6695 /* we do not need to set READ_ONLY because the data is in a buffer */
6696 test->flags = ACL_TEST_F_VOL_1ST;
6697 return 1;
6698}
6699
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006700static int
6701acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6702 struct acl_expr *expr, struct acl_test *test)
6703{
6704 struct buffer *req = s->req;
6705 struct http_txn *txn = &s->txn;
6706 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006707
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006708 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6709 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6710 */
6711
6712 if (!s || !req)
6713 return 0;
6714
Willy Tarreau655dce92009-11-08 13:10:58 +01006715 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006716 /* Already decoded as OK */
6717 test->flags |= ACL_TEST_F_SET_RES_PASS;
6718 return 1;
6719 }
6720
6721 /* Try to decode HTTP request */
6722 if (likely(req->lr < req->r))
6723 http_msg_analyzer(req, msg, &txn->hdr_idx);
6724
Willy Tarreau655dce92009-11-08 13:10:58 +01006725 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006726 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6727 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6728 return 1;
6729 }
6730 /* wait for final state */
6731 test->flags |= ACL_TEST_F_MAY_CHANGE;
6732 return 0;
6733 }
6734
6735 /* OK we got a valid HTTP request. We have some minor preparation to
6736 * perform so that further checks can rely on HTTP tests.
6737 */
6738 msg->sol = req->data + msg->som;
6739 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6740 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6741 s->flags |= SN_REDIRECTABLE;
6742
6743 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6744 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6745 return 1;
6746 }
6747
6748 test->flags |= ACL_TEST_F_SET_RES_PASS;
6749 return 1;
6750}
6751
Willy Tarreau8797c062007-05-07 00:55:35 +02006752
6753/************************************************************************/
6754/* All supported keywords must be declared here. */
6755/************************************************************************/
6756
6757/* Note: must not be declared <const> as its list will be overwritten */
6758static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006759 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6760
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006761 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6762 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6763 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6764 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006765
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006766 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6767 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6768 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6769 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6770 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6771 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6772 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6773 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6774 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006775
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006776 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6777 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6778 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6779 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6780 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6781 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6782 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6783 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6784 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6785 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006786 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006787
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006788 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6789 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6790 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6791 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6792 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6793 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6794 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6795 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6796 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006797 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006798
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006799 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6800 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6801 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6802 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6803 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6804 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6805 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006806
Willy Tarreauf3d25982007-05-08 22:45:09 +02006807 { NULL, NULL, NULL, NULL },
6808
6809#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006810 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6811 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6812 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6813 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6814 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6815 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6816 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6817
Willy Tarreau8797c062007-05-07 00:55:35 +02006818 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6819 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6820 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6821 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6822 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6823 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6824 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6825 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6826
6827 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6828 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6829 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6830 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6831 { NULL, NULL, NULL, NULL },
6832#endif
6833}};
6834
6835
6836__attribute__((constructor))
6837static void __http_protocol_init(void)
6838{
6839 acl_register_keywords(&acl_kws);
6840}
6841
6842
Willy Tarreau58f10d72006-12-04 02:26:12 +01006843/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006844 * Local variables:
6845 * c-indent-level: 8
6846 * c-basic-offset: 8
6847 * End:
6848 */