blob: c9d4124835e1ca8c506ae0d18b505613e1fd5a02 [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 Tarreau68085d82010-01-18 14:54:04 +0100455/* Find the end of the header value contained between <s> and <e>. See RFC2616,
456 * par 2.2 for more information. Note that it requires a valid header to return
457 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200458 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100459char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200460{
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
Willy Tarreau68085d82010-01-18 14:54:04 +0100479 * 1 when it finds a value, and 0 when there is no more. It is designed to work
480 * with headers defined as comma-separated lists. As a special case, if ctx->val
481 * is NULL when searching for a new values of a header, the current header is
482 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200483 */
484int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100485 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200486 struct hdr_ctx *ctx)
487{
Willy Tarreau68085d82010-01-18 14:54:04 +0100488 char *eol, *sov;
489 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200490
Willy Tarreau68085d82010-01-18 14:54:04 +0100491 cur_idx = ctx->idx;
492 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200493 /* We have previously returned a value, let's search
494 * another one on the same line.
495 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200496 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100497 ctx->del = ctx->val + ctx->vlen;
498 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200499 eol = sol + idx->v[cur_idx].len;
500
501 if (sov >= eol)
502 /* no more values in this header */
503 goto next_hdr;
504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 /* values remaining for this header, skip the comma but save it
506 * for later use (eg: for header deletion).
507 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 sov++;
509 while (sov < eol && http_is_lws[(unsigned char)*sov])
510 sov++;
511
512 goto return_hdr;
513 }
514
515 /* first request for this header */
516 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100517 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 while (cur_idx) {
520 eol = sol + idx->v[cur_idx].len;
521
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200522 if (len == 0) {
523 /* No argument was passed, we want any header.
524 * To achieve this, we simply build a fake request. */
525 while (sol + len < eol && sol[len] != ':')
526 len++;
527 name = sol;
528 }
529
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 if ((len < eol - sol) &&
531 (sol[len] == ':') &&
532 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100533 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200534 sov = sol + len + 1;
535 while (sov < eol && http_is_lws[(unsigned char)*sov])
536 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100537
Willy Tarreau33a7e692007-06-10 19:45:56 +0200538 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100539 ctx->prev = old_idx;
540 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 ctx->idx = cur_idx;
542 ctx->val = sov - sol;
543
544 eol = find_hdr_value_end(sov, eol);
545 ctx->vlen = eol - sov;
546 return 1;
547 }
548 next_hdr:
549 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100550 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200551 cur_idx = idx->v[cur_idx].next;
552 }
553 return 0;
554}
555
556int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 struct hdr_ctx *ctx)
559{
560 return http_find_header2(name, strlen(name), sol, idx, ctx);
561}
562
Willy Tarreau68085d82010-01-18 14:54:04 +0100563/* Remove one value of a header. This only works on a <ctx> returned by one of
564 * the http_find_header functions. The value is removed, as well as surrounding
565 * commas if any. If the removed value was alone, the whole header is removed.
566 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
567 * message <msg>. The new index is returned. If it is zero, it means there is
568 * no more header, so any processing may stop. The ctx is always left in a form
569 * that can be handled by http_find_header2() to find next occurrence.
570 */
571int http_remove_header2(struct http_msg *msg, struct buffer *buf,
572 struct hdr_idx *idx, struct hdr_ctx *ctx)
573{
574 int cur_idx = ctx->idx;
575 char *sol = ctx->line;
576 struct hdr_idx_elem *hdr;
577 int delta, skip_comma;
578
579 if (!cur_idx)
580 return 0;
581
582 hdr = &idx->v[cur_idx];
583 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
584 /* This was the only value of the header, we must now remove it entirely. */
585 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
586 http_msg_move_end(msg, delta);
587 idx->used--;
588 hdr->len = 0; /* unused entry */
589 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
590 ctx->idx = ctx->prev; /* walk back to the end of previous header */
591 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
592 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
593 ctx->vlen = 0;
594 return ctx->idx;
595 }
596
597 /* This was not the only value of this header. We have to remove between
598 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
599 * of the list, we remove the last separator.
600 */
601
602 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
603 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
604 sol + ctx->val + ctx->vlen + skip_comma,
605 NULL, 0);
606 hdr->len += delta;
607 http_msg_move_end(msg, delta);
608 ctx->val = ctx->del;
609 ctx->vlen = 0;
610 return ctx->idx;
611}
612
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100613/* This function handles a server error at the stream interface level. The
614 * stream interface is assumed to be already in a closed state. An optional
615 * message is copied into the input buffer, and an HTTP status code stored.
616 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100617 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100619static void http_server_error(struct session *t, struct stream_interface *si,
620 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100622 buffer_auto_read(si->ob);
623 buffer_abort(si->ob);
624 buffer_auto_close(si->ob);
625 buffer_erase(si->ob);
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100626 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200627 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100628 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100629 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100630 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100631 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 }
633 if (!(t->flags & SN_ERR_MASK))
634 t->flags |= err;
635 if (!(t->flags & SN_FINST_MASK))
636 t->flags |= finst;
637}
638
Willy Tarreau80587432006-12-24 17:47:20 +0100639/* This function returns the appropriate error location for the given session
640 * and message.
641 */
642
643struct chunk *error_message(struct session *s, int msgnum)
644{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200645 if (s->be->errmsg[msgnum].str)
646 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100647 else if (s->fe->errmsg[msgnum].str)
648 return &s->fe->errmsg[msgnum];
649 else
650 return &http_err_chunks[msgnum];
651}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652
Willy Tarreau53b6c742006-12-17 13:37:46 +0100653/*
654 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
655 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
656 */
657static http_meth_t find_http_meth(const char *str, const int len)
658{
659 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100660 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100661
662 m = ((unsigned)*str - 'A');
663
664 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100665 for (h = http_methods[m]; h->len > 0; h++) {
666 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100667 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100668 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100669 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100670 };
671 return HTTP_METH_OTHER;
672 }
673 return HTTP_METH_NONE;
674
675}
676
Willy Tarreau21d2af32008-02-14 20:25:24 +0100677/* Parse the URI from the given transaction (which is assumed to be in request
678 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
679 * It is returned otherwise.
680 */
681static char *
682http_get_path(struct http_txn *txn)
683{
684 char *ptr, *end;
685
Willy Tarreau962c3f42010-01-10 00:15:35 +0100686 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100687 end = ptr + txn->req.sl.rq.u_l;
688
689 if (ptr >= end)
690 return NULL;
691
692 /* RFC2616, par. 5.1.2 :
693 * Request-URI = "*" | absuri | abspath | authority
694 */
695
696 if (*ptr == '*')
697 return NULL;
698
699 if (isalpha((unsigned char)*ptr)) {
700 /* this is a scheme as described by RFC3986, par. 3.1 */
701 ptr++;
702 while (ptr < end &&
703 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
704 ptr++;
705 /* skip '://' */
706 if (ptr == end || *ptr++ != ':')
707 return NULL;
708 if (ptr == end || *ptr++ != '/')
709 return NULL;
710 if (ptr == end || *ptr++ != '/')
711 return NULL;
712 }
713 /* skip [user[:passwd]@]host[:[port]] */
714
715 while (ptr < end && *ptr != '/')
716 ptr++;
717
718 if (ptr == end)
719 return NULL;
720
721 /* OK, we got the '/' ! */
722 return ptr;
723}
724
Willy Tarreauefb453c2008-10-26 20:49:47 +0100725/* Returns a 302 for a redirectable request. This may only be called just after
726 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
727 * left unchanged and will follow normal proxy processing.
728 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100730{
731 struct http_txn *txn;
732 struct chunk rdr;
733 char *path;
734 int len;
735
736 /* 1: create the response header */
737 rdr.len = strlen(HTTP_302);
738 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100739 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740 memcpy(rdr.str, HTTP_302, rdr.len);
741
742 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200743 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100744 return;
745
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100746 /* special prefix "/" means don't change URL */
747 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
748 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
749 rdr.len += s->srv->rdr_len;
750 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751
752 /* 3: add the request URI */
753 txn = &s->txn;
754 path = http_get_path(txn);
755 if (!path)
756 return;
757
Willy Tarreau962c3f42010-01-10 00:15:35 +0100758 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200759 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760 return;
761
762 memcpy(rdr.str + rdr.len, path, len);
763 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100764 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
765 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100766
767 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100768 si->shutr(si);
769 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 si->err_type = SI_ET_NONE;
771 si->err_loc = NULL;
772 si->state = SI_ST_CLO;
773
774 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100775 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776
777 /* FIXME: we should increase a counter of redirects per server and per backend. */
778 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100779 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780}
781
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100782/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100783 * that the server side is closed. Note that err_type is actually a
784 * bitmask, where almost only aborts may be cumulated with other
785 * values. We consider that aborted operations are more important
786 * than timeouts or errors due to the fact that nobody else in the
787 * logs might explain incomplete retries. All others should avoid
788 * being cumulated. It should normally not be possible to have multiple
789 * aborts at once, but just in case, the first one in sequence is reported.
790 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100791void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100793 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100794
795 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100796 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
797 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100798 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100799 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
800 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100801 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100802 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
803 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100805 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
806 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100808 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
809 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100810 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100811 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
812 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100814 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
815 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816}
817
Willy Tarreau42250582007-04-01 01:30:43 +0200818extern const char sess_term_cond[8];
819extern const char sess_fin_state[8];
820extern const char *monthname[12];
821const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
822const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
823 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
824 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200825struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200826struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100827
Emeric Brun3a058f32009-06-30 18:26:00 +0200828void http_sess_clflog(struct session *s)
829{
830 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
831 struct proxy *fe = s->fe;
832 struct proxy *be = s->be;
833 struct proxy *prx_log;
834 struct http_txn *txn = &s->txn;
835 int tolog, level, err;
836 char *uri, *h;
837 char *svid;
838 struct tm tm;
839 static char tmpline[MAX_SYSLOG_LEN];
840 int hdr;
841 size_t w;
842 int t_request;
843
844 prx_log = fe;
845 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
846 (s->conn_retries != be->conn_retries) ||
847 txn->status >= 500;
848
849 if (s->cli_addr.ss_family == AF_INET)
850 inet_ntop(AF_INET,
851 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
852 pn, sizeof(pn));
853 else
854 inet_ntop(AF_INET6,
855 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
856 pn, sizeof(pn));
857
858 get_gmtime(s->logs.accept_date.tv_sec, &tm);
859
860 /* FIXME: let's limit ourselves to frontend logging for now. */
861 tolog = fe->to_log;
862
863 h = tmpline;
864
865 w = snprintf(h, sizeof(tmpline),
866 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
867 pn,
868 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
869 tm.tm_hour, tm.tm_min, tm.tm_sec);
870 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
871 goto trunc;
872 h += w;
873
874 if (h >= tmpline + sizeof(tmpline) - 4)
875 goto trunc;
876
877 *(h++) = ' ';
878 *(h++) = '\"';
879 uri = txn->uri ? txn->uri : "<BADREQ>";
880 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
881 '#', url_encode_map, uri);
882 *(h++) = '\"';
883
884 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
885 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
886 goto trunc;
887 h += w;
888
889 if (h >= tmpline + sizeof(tmpline) - 9)
890 goto trunc;
891 memcpy(h, " \"-\" \"-\"", 8);
892 h += 8;
893
894 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
895 " %d %03d",
896 (s->cli_addr.ss_family == AF_INET) ?
897 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
898 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
899 (int)s->logs.accept_date.tv_usec/1000);
900 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
901 goto trunc;
902 h += w;
903
904 w = strlen(fe->id);
905 if (h >= tmpline + sizeof(tmpline) - 4 - w)
906 goto trunc;
907 *(h++) = ' ';
908 *(h++) = '\"';
909 memcpy(h, fe->id, w);
910 h += w;
911 *(h++) = '\"';
912
913 w = strlen(be->id);
914 if (h >= tmpline + sizeof(tmpline) - 4 - w)
915 goto trunc;
916 *(h++) = ' ';
917 *(h++) = '\"';
918 memcpy(h, be->id, w);
919 h += w;
920 *(h++) = '\"';
921
922 svid = (tolog & LW_SVID) ?
923 (s->data_source != DATA_SRC_STATS) ?
924 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
925
926 w = strlen(svid);
927 if (h >= tmpline + sizeof(tmpline) - 4 - w)
928 goto trunc;
929 *(h++) = ' ';
930 *(h++) = '\"';
931 memcpy(h, svid, w);
932 h += w;
933 *(h++) = '\"';
934
935 t_request = -1;
936 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
937 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
938 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
939 " %d %ld %ld %ld %ld",
940 t_request,
941 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
942 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
943 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
944 s->logs.t_close);
945 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
946 goto trunc;
947 h += w;
948
949 if (h >= tmpline + sizeof(tmpline) - 8)
950 goto trunc;
951 *(h++) = ' ';
952 *(h++) = '\"';
953 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
954 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
955 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
956 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
957 *(h++) = '\"';
958
959 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
960 " %d %d %d %d %d %ld %ld",
961 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
962 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
963 s->logs.srv_queue_size, s->logs.prx_queue_size);
964
965 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
966 goto trunc;
967 h += w;
968
969 if (txn->cli_cookie) {
970 w = strlen(txn->cli_cookie);
971 if (h >= tmpline + sizeof(tmpline) - 4 - w)
972 goto trunc;
973 *(h++) = ' ';
974 *(h++) = '\"';
975 memcpy(h, txn->cli_cookie, w);
976 h += w;
977 *(h++) = '\"';
978 } else {
979 if (h >= tmpline + sizeof(tmpline) - 5)
980 goto trunc;
981 memcpy(h, " \"-\"", 4);
982 h += 4;
983 }
984
985 if (txn->srv_cookie) {
986 w = strlen(txn->srv_cookie);
987 if (h >= tmpline + sizeof(tmpline) - 4 - w)
988 goto trunc;
989 *(h++) = ' ';
990 *(h++) = '\"';
991 memcpy(h, txn->srv_cookie, w);
992 h += w;
993 *(h++) = '\"';
994 } else {
995 if (h >= tmpline + sizeof(tmpline) - 5)
996 goto trunc;
997 memcpy(h, " \"-\"", 4);
998 h += 4;
999 }
1000
1001 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1002 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1003 if (h >= sizeof (tmpline) + tmpline - 4)
1004 goto trunc;
1005 *(h++) = ' ';
1006 *(h++) = '\"';
1007 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1008 '#', hdr_encode_map, txn->req.cap[hdr]);
1009 *(h++) = '\"';
1010 }
1011 }
1012
1013 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1014 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1015 if (h >= sizeof (tmpline) + tmpline - 4)
1016 goto trunc;
1017 *(h++) = ' ';
1018 *(h++) = '\"';
1019 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1020 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1021 *(h++) = '\"';
1022 }
1023 }
1024
1025trunc:
1026 *h = '\0';
1027
1028 level = LOG_INFO;
1029 if (err && (fe->options2 & PR_O2_LOGERRORS))
1030 level = LOG_ERR;
1031
1032 send_log(prx_log, level, "%s\n", tmpline);
1033
1034 s->logs.logwait = 0;
1035}
1036
Willy Tarreau42250582007-04-01 01:30:43 +02001037/*
1038 * send a log for the session when we have enough info about it.
1039 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001041void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001042{
1043 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1044 struct proxy *fe = s->fe;
1045 struct proxy *be = s->be;
1046 struct proxy *prx_log;
1047 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001048 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001049 char *uri, *h;
1050 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001051 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001052 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001053 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001054 int hdr;
1055
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001056 /* if we don't want to log normal traffic, return now */
1057 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1058 (s->conn_retries != be->conn_retries) ||
1059 txn->status >= 500;
1060 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1061 return;
1062
Willy Tarreau42250582007-04-01 01:30:43 +02001063 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1064 return;
1065 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066
Emeric Brun3a058f32009-06-30 18:26:00 +02001067 if (prx_log->options2 & PR_O2_CLFLOG)
1068 return http_sess_clflog(s);
1069
Willy Tarreau42250582007-04-01 01:30:43 +02001070 if (s->cli_addr.ss_family == AF_INET)
1071 inet_ntop(AF_INET,
1072 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1073 pn, sizeof(pn));
1074 else
1075 inet_ntop(AF_INET6,
1076 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1077 pn, sizeof(pn));
1078
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001079 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001080
1081 /* FIXME: let's limit ourselves to frontend logging for now. */
1082 tolog = fe->to_log;
1083
1084 h = tmpline;
1085 if (fe->to_log & LW_REQHDR &&
1086 txn->req.cap &&
1087 (h < tmpline + sizeof(tmpline) - 10)) {
1088 *(h++) = ' ';
1089 *(h++) = '{';
1090 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1091 if (hdr)
1092 *(h++) = '|';
1093 if (txn->req.cap[hdr] != NULL)
1094 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1095 '#', hdr_encode_map, txn->req.cap[hdr]);
1096 }
1097 *(h++) = '}';
1098 }
1099
1100 if (fe->to_log & LW_RSPHDR &&
1101 txn->rsp.cap &&
1102 (h < tmpline + sizeof(tmpline) - 7)) {
1103 *(h++) = ' ';
1104 *(h++) = '{';
1105 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1106 if (hdr)
1107 *(h++) = '|';
1108 if (txn->rsp.cap[hdr] != NULL)
1109 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1110 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1111 }
1112 *(h++) = '}';
1113 }
1114
1115 if (h < tmpline + sizeof(tmpline) - 4) {
1116 *(h++) = ' ';
1117 *(h++) = '"';
1118 uri = txn->uri ? txn->uri : "<BADREQ>";
1119 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1120 '#', url_encode_map, uri);
1121 *(h++) = '"';
1122 }
1123 *h = '\0';
1124
1125 svid = (tolog & LW_SVID) ?
1126 (s->data_source != DATA_SRC_STATS) ?
1127 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1128
Willy Tarreau70089872008-06-13 21:12:51 +02001129 t_request = -1;
1130 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1131 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1132
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001133 level = LOG_INFO;
1134 if (err && (fe->options2 & PR_O2_LOGERRORS))
1135 level = LOG_ERR;
1136
1137 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001138 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001139 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1140 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001141 pn,
1142 (s->cli_addr.ss_family == AF_INET) ?
1143 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1144 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001145 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001146 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001147 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001148 t_request,
1149 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001150 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1151 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1152 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1153 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001154 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001155 txn->cli_cookie ? txn->cli_cookie : "-",
1156 txn->srv_cookie ? txn->srv_cookie : "-",
1157 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1158 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1159 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1160 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1161 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001162 (s->flags & SN_REDISP)?"+":"",
1163 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001164 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1165
1166 s->logs.logwait = 0;
1167}
1168
Willy Tarreau117f59e2007-03-04 18:17:17 +01001169
1170/*
1171 * Capture headers from message starting at <som> according to header list
1172 * <cap_hdr>, and fill the <idx> structure appropriately.
1173 */
1174void capture_headers(char *som, struct hdr_idx *idx,
1175 char **cap, struct cap_hdr *cap_hdr)
1176{
1177 char *eol, *sol, *col, *sov;
1178 int cur_idx;
1179 struct cap_hdr *h;
1180 int len;
1181
1182 sol = som + hdr_idx_first_pos(idx);
1183 cur_idx = hdr_idx_first_idx(idx);
1184
1185 while (cur_idx) {
1186 eol = sol + idx->v[cur_idx].len;
1187
1188 col = sol;
1189 while (col < eol && *col != ':')
1190 col++;
1191
1192 sov = col + 1;
1193 while (sov < eol && http_is_lws[(unsigned char)*sov])
1194 sov++;
1195
1196 for (h = cap_hdr; h; h = h->next) {
1197 if ((h->namelen == col - sol) &&
1198 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1199 if (cap[h->index] == NULL)
1200 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001201 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202
1203 if (cap[h->index] == NULL) {
1204 Alert("HTTP capture : out of memory.\n");
1205 continue;
1206 }
1207
1208 len = eol - sov;
1209 if (len > h->len)
1210 len = h->len;
1211
1212 memcpy(cap[h->index], sov, len);
1213 cap[h->index][len]=0;
1214 }
1215 }
1216 sol = eol + idx->v[cur_idx].cr + 1;
1217 cur_idx = idx->v[cur_idx].next;
1218 }
1219}
1220
1221
Willy Tarreau42250582007-04-01 01:30:43 +02001222/* either we find an LF at <ptr> or we jump to <bad>.
1223 */
1224#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1225
1226/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1227 * otherwise to <http_msg_ood> with <state> set to <st>.
1228 */
1229#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1230 ptr++; \
1231 if (likely(ptr < end)) \
1232 goto good; \
1233 else { \
1234 state = (st); \
1235 goto http_msg_ood; \
1236 } \
1237 } while (0)
1238
1239
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001241 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001242 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1243 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1244 * will give undefined results.
1245 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1246 * and that msg->sol points to the beginning of the response.
1247 * If a complete line is found (which implies that at least one CR or LF is
1248 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1249 * returned indicating an incomplete line (which does not mean that parts have
1250 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1251 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1252 * upon next call.
1253 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001254 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001255 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1256 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001257 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001259const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1260 unsigned int state, const char *ptr, const char *end,
1261 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001262{
Willy Tarreau8973c702007-01-21 23:58:29 +01001263 switch (state) {
1264 http_msg_rpver:
1265 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001266 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001267 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1268
1269 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001270 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1272 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001273 state = HTTP_MSG_ERROR;
1274 break;
1275
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 http_msg_rpver_sp:
1277 case HTTP_MSG_RPVER_SP:
1278 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001279 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001280 goto http_msg_rpcode;
1281 }
1282 if (likely(HTTP_IS_SPHT(*ptr)))
1283 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1284 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001285 state = HTTP_MSG_ERROR;
1286 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001287
1288 http_msg_rpcode:
1289 case HTTP_MSG_RPCODE:
1290 if (likely(!HTTP_IS_LWS(*ptr)))
1291 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1292
1293 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001294 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1296 }
1297
1298 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001299 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 http_msg_rsp_reason:
1301 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001302 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 msg->sl.st.r_l = 0;
1304 goto http_msg_rpline_eol;
1305
1306 http_msg_rpcode_sp:
1307 case HTTP_MSG_RPCODE_SP:
1308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001309 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 goto http_msg_rpreason;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1314 /* so it's a CR/LF, so there is no reason phrase */
1315 goto http_msg_rsp_reason;
1316
1317 http_msg_rpreason:
1318 case HTTP_MSG_RPREASON:
1319 if (likely(!HTTP_IS_CRLF(*ptr)))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001321 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 http_msg_rpline_eol:
1323 /* We have seen the end of line. Note that we do not
1324 * necessarily have the \n yet, but at least we know that we
1325 * have EITHER \r OR \n, otherwise the response would not be
1326 * complete. We can then record the response length and return
1327 * to the caller which will be able to register it.
1328 */
1329 msg->sl.st.l = ptr - msg->sol;
1330 return ptr;
1331
1332#ifdef DEBUG_FULL
1333 default:
1334 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1335 exit(1);
1336#endif
1337 }
1338
1339 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001340 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 if (ret_state)
1342 *ret_state = state;
1343 if (ret_ptr)
1344 *ret_ptr = (char *)ptr;
1345 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001346}
1347
1348
1349/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 * This function parses a request line between <ptr> and <end>, starting with
1351 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1352 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1353 * will give undefined results.
1354 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1355 * and that msg->sol points to the beginning of the request.
1356 * If a complete line is found (which implies that at least one CR or LF is
1357 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1358 * returned indicating an incomplete line (which does not mean that parts have
1359 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1360 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1361 * upon next call.
1362 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001363 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1365 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001366 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001367 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001368const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1369 unsigned int state, const char *ptr, const char *end,
1370 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 switch (state) {
1373 http_msg_rqmeth:
1374 case HTTP_MSG_RQMETH:
1375 if (likely(HTTP_IS_TOKEN(*ptr)))
1376 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001379 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1381 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001382
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 if (likely(HTTP_IS_CRLF(*ptr))) {
1384 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001385 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001387 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001389 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001391 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 msg->sl.rq.v_l = 0;
1393 goto http_msg_rqline_eol;
1394 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001395 state = HTTP_MSG_ERROR;
1396 break;
1397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 http_msg_rqmeth_sp:
1399 case HTTP_MSG_RQMETH_SP:
1400 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001401 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 goto http_msg_rquri;
1403 }
1404 if (likely(HTTP_IS_SPHT(*ptr)))
1405 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1406 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1407 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 http_msg_rquri:
1410 case HTTP_MSG_RQURI:
1411 if (likely(!HTTP_IS_LWS(*ptr)))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001415 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1417 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1420 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_rquri_sp:
1423 case HTTP_MSG_RQURI_SP:
1424 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001425 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 goto http_msg_rqver;
1427 }
1428 if (likely(HTTP_IS_SPHT(*ptr)))
1429 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1430 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1431 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 http_msg_rqver:
1434 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001435 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001437
1438 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001439 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001440 http_msg_rqline_eol:
1441 /* We have seen the end of line. Note that we do not
1442 * necessarily have the \n yet, but at least we know that we
1443 * have EITHER \r OR \n, otherwise the request would not be
1444 * complete. We can then record the request length and return
1445 * to the caller which will be able to register it.
1446 */
1447 msg->sl.rq.l = ptr - msg->sol;
1448 return ptr;
1449 }
1450
1451 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001452 state = HTTP_MSG_ERROR;
1453 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455#ifdef DEBUG_FULL
1456 default:
1457 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1458 exit(1);
1459#endif
1460 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001463 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 if (ret_state)
1465 *ret_state = state;
1466 if (ret_ptr)
1467 *ret_ptr = (char *)ptr;
1468 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001470
1471
Willy Tarreau8973c702007-01-21 23:58:29 +01001472/*
1473 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001474 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001475 * when data are missing and recalled at the exact same location with no
1476 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001477 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001478 * fields. Note that msg->som and msg->sol will be initialized after completing
1479 * the first state, so that none of the msg pointers has to be initialized
1480 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1483{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001484 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001487 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 ptr = buf->lr;
1489 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 if (unlikely(ptr >= end))
1492 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001495 /*
1496 * First, states that are specific to the response only.
1497 * We check them first so that request and headers are
1498 * closer to each other (accessed more often).
1499 */
1500 http_msg_rpbefore:
1501 case HTTP_MSG_RPBEFORE:
1502 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001503 /* we have a start of message, but we have to check
1504 * first if we need to remove some CRLF. We can only
1505 * do this when send_max=0.
1506 */
1507 char *beg = buf->w + buf->send_max;
1508 if (beg >= buf->data + buf->size)
1509 beg -= buf->size;
1510 if (unlikely(ptr != beg)) {
1511 if (buf->send_max)
1512 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001513 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001514 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001515 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001516 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001517 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 hdr_idx_init(idx);
1519 state = HTTP_MSG_RPVER;
1520 goto http_msg_rpver;
1521 }
1522
1523 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1524 goto http_msg_invalid;
1525
1526 if (unlikely(*ptr == '\n'))
1527 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1529 /* stop here */
1530
1531 http_msg_rpbefore_cr:
1532 case HTTP_MSG_RPBEFORE_CR:
1533 EXPECT_LF_HERE(ptr, http_msg_invalid);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1535 /* stop here */
1536
1537 http_msg_rpver:
1538 case HTTP_MSG_RPVER:
1539 case HTTP_MSG_RPVER_SP:
1540 case HTTP_MSG_RPCODE:
1541 case HTTP_MSG_RPCODE_SP:
1542 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001543 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001544 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001545 if (unlikely(!ptr))
1546 return;
1547
1548 /* we have a full response and we know that we have either a CR
1549 * or an LF at <ptr>.
1550 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001551 //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 +01001552 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1553
1554 msg->sol = ptr;
1555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1557 goto http_msg_rpline_end;
1558
1559 http_msg_rpline_end:
1560 case HTTP_MSG_RPLINE_END:
1561 /* msg->sol must point to the first of CR or LF. */
1562 EXPECT_LF_HERE(ptr, http_msg_invalid);
1563 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1564 /* stop here */
1565
1566 /*
1567 * Second, states that are specific to the request only
1568 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 http_msg_rqbefore:
1570 case HTTP_MSG_RQBEFORE:
1571 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001572 /* we have a start of message, but we have to check
1573 * first if we need to remove some CRLF. We can only
1574 * do this when send_max=0.
1575 */
1576 char *beg = buf->w + buf->send_max;
1577 if (beg >= buf->data + buf->size)
1578 beg -= buf->size;
1579 if (likely(ptr != beg)) {
1580 if (buf->send_max)
1581 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001582 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001583 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001585 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001586 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001587 /* we will need this when keep-alive will be supported
1588 hdr_idx_init(idx);
1589 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001590 state = HTTP_MSG_RQMETH;
1591 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1595 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (unlikely(*ptr == '\n'))
1598 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1599 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 http_msg_rqbefore_cr:
1603 case HTTP_MSG_RQBEFORE_CR:
1604 EXPECT_LF_HERE(ptr, http_msg_invalid);
1605 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001606 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 http_msg_rqmeth:
1609 case HTTP_MSG_RQMETH:
1610 case HTTP_MSG_RQMETH_SP:
1611 case HTTP_MSG_RQURI:
1612 case HTTP_MSG_RQURI_SP:
1613 case HTTP_MSG_RQVER:
1614 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001615 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 if (unlikely(!ptr))
1617 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 /* we have a full request and we know that we have either a CR
1620 * or an LF at <ptr>.
1621 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001622 //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 +01001623 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 msg->sol = ptr;
1626 if (likely(*ptr == '\r'))
1627 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 http_msg_rqline_end:
1631 case HTTP_MSG_RQLINE_END:
1632 /* check for HTTP/0.9 request : no version information available.
1633 * msg->sol must point to the first of CR or LF.
1634 */
1635 if (unlikely(msg->sl.rq.v_l == 0))
1636 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 EXPECT_LF_HERE(ptr, http_msg_invalid);
1639 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001640 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001641
Willy Tarreau8973c702007-01-21 23:58:29 +01001642 /*
1643 * Common states below
1644 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 http_msg_hdr_first:
1646 case HTTP_MSG_HDR_FIRST:
1647 msg->sol = ptr;
1648 if (likely(!HTTP_IS_CRLF(*ptr))) {
1649 goto http_msg_hdr_name;
1650 }
1651
1652 if (likely(*ptr == '\r'))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1654 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 http_msg_hdr_name:
1657 case HTTP_MSG_HDR_NAME:
1658 /* assumes msg->sol points to the first char */
1659 if (likely(HTTP_IS_TOKEN(*ptr)))
1660 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 if (likely(*ptr == ':')) {
1663 msg->col = ptr - buf->data;
1664 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001666
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001667 if (likely(msg->err_pos < -1) || *ptr == '\n')
1668 goto http_msg_invalid;
1669
1670 if (msg->err_pos == -1) /* capture error pointer */
1671 msg->err_pos = ptr - buf->data; /* >= 0 now */
1672
1673 /* and we still accept this non-token character */
1674 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 http_msg_hdr_l1_sp:
1677 case HTTP_MSG_HDR_L1_SP:
1678 /* assumes msg->sol points to the first char and msg->col to the colon */
1679 if (likely(HTTP_IS_SPHT(*ptr)))
1680 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 /* header value can be basically anything except CR/LF */
1683 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 if (likely(!HTTP_IS_CRLF(*ptr))) {
1686 goto http_msg_hdr_val;
1687 }
1688
1689 if (likely(*ptr == '\r'))
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1691 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 http_msg_hdr_l1_lf:
1694 case HTTP_MSG_HDR_L1_LF:
1695 EXPECT_LF_HERE(ptr, http_msg_invalid);
1696 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 http_msg_hdr_l1_lws:
1699 case HTTP_MSG_HDR_L1_LWS:
1700 if (likely(HTTP_IS_SPHT(*ptr))) {
1701 /* replace HT,CR,LF with spaces */
1702 for (; buf->data+msg->sov < ptr; msg->sov++)
1703 buf->data[msg->sov] = ' ';
1704 goto http_msg_hdr_l1_sp;
1705 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001706 /* we had a header consisting only in spaces ! */
1707 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 goto http_msg_complete_header;
1709
1710 http_msg_hdr_val:
1711 case HTTP_MSG_HDR_VAL:
1712 /* assumes msg->sol points to the first char, msg->col to the
1713 * colon, and msg->sov points to the first character of the
1714 * value.
1715 */
1716 if (likely(!HTTP_IS_CRLF(*ptr)))
1717 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 msg->eol = ptr;
1720 /* Note: we could also copy eol into ->eoh so that we have the
1721 * real header end in case it ends with lots of LWS, but is this
1722 * really needed ?
1723 */
1724 if (likely(*ptr == '\r'))
1725 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1726 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 http_msg_hdr_l2_lf:
1729 case HTTP_MSG_HDR_L2_LF:
1730 EXPECT_LF_HERE(ptr, http_msg_invalid);
1731 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 http_msg_hdr_l2_lws:
1734 case HTTP_MSG_HDR_L2_LWS:
1735 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1736 /* LWS: replace HT,CR,LF with spaces */
1737 for (; msg->eol < ptr; msg->eol++)
1738 *msg->eol = ' ';
1739 goto http_msg_hdr_val;
1740 }
1741 http_msg_complete_header:
1742 /*
1743 * It was a new header, so the last one is finished.
1744 * Assumes msg->sol points to the first char, msg->col to the
1745 * colon, msg->sov points to the first character of the value
1746 * and msg->eol to the first CR or LF so we know how the line
1747 * ends. We insert last header into the index.
1748 */
1749 /*
1750 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1751 write(2, msg->sol, msg->eol-msg->sol);
1752 fprintf(stderr,"\n");
1753 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001754
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1756 idx, idx->tail) < 0))
1757 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 msg->sol = ptr;
1760 if (likely(!HTTP_IS_CRLF(*ptr))) {
1761 goto http_msg_hdr_name;
1762 }
1763
1764 if (likely(*ptr == '\r'))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1766 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 http_msg_last_lf:
1769 case HTTP_MSG_LAST_LF:
1770 /* Assumes msg->sol points to the first of either CR or LF */
1771 EXPECT_LF_HERE(ptr, http_msg_invalid);
1772 ptr++;
1773 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001774 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001776 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001777 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 return;
1779#ifdef DEBUG_FULL
1780 default:
1781 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1782 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001783#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 }
1785 http_msg_ood:
1786 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001787 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 buf->lr = ptr;
1789 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 http_msg_invalid:
1792 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001793 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001794 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 return;
1796}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001797
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001798/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1799 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1800 * nothing is done and 1 is returned.
1801 */
1802static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1803{
1804 int delta;
1805 char *cur_end;
1806
1807 if (msg->sl.rq.v_l != 0)
1808 return 1;
1809
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001810 cur_end = msg->sol + msg->sl.rq.l;
1811 delta = 0;
1812
1813 if (msg->sl.rq.u_l == 0) {
1814 /* if no URI was set, add "/" */
1815 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1816 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001817 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001818 }
1819 /* add HTTP version */
1820 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001821 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001822 cur_end += delta;
1823 cur_end = (char *)http_parse_reqline(msg, req->data,
1824 HTTP_MSG_RQMETH,
1825 msg->sol, cur_end + 1,
1826 NULL, NULL);
1827 if (unlikely(!cur_end))
1828 return 0;
1829
1830 /* we have a full HTTP/1.0 request now and we know that
1831 * we have either a CR or an LF at <ptr>.
1832 */
1833 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1834 return 1;
1835}
1836
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001837/* Parse the Connection: header of an HTTP request, looking for both "close"
1838 * and "keep-alive" values. If a buffer is provided and we already know that
1839 * some headers may safely be removed, we remove them now. The <to_del> flags
1840 * are used for that :
1841 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1842 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1843 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1844 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1845 * harmless combinations may be removed. Do not call that after changes have
1846 * been processed. If unused, the buffer can be NULL, and no data will be
1847 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001848 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001850{
Willy Tarreau5b154472009-12-21 20:11:07 +01001851 struct hdr_ctx ctx;
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001854 return;
1855
Willy Tarreau5b154472009-12-21 20:11:07 +01001856 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001857 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01001858 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1860 txn->flags |= TX_HDR_CONN_KAL;
1861 if ((to_del & 2) && buf)
1862 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1863 else
1864 txn->flags |= TX_CON_KAL_SET;
1865 }
1866 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1867 txn->flags |= TX_HDR_CONN_CLO;
1868 if ((to_del & 1) && buf)
1869 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1870 else
1871 txn->flags |= TX_CON_CLO_SET;
1872 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001873 }
1874
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001875 txn->flags |= TX_HDR_CONN_PRS;
1876 return;
1877}
Willy Tarreau5b154472009-12-21 20:11:07 +01001878
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001879/* Apply desired changes on the Connection: header. Values may be removed and/or
1880 * added depending on the <wanted> flags, which are exclusively composed of
1881 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1882 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1883 */
1884void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1885{
1886 struct hdr_ctx ctx;
1887
1888 ctx.idx = 0;
1889
1890 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
1891 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1892 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1893 if (wanted & TX_CON_KAL_SET)
1894 txn->flags |= TX_CON_KAL_SET;
1895 else
1896 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001897 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1899 if (wanted & TX_CON_CLO_SET)
1900 txn->flags |= TX_CON_CLO_SET;
1901 else
1902 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001903 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001904 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905
1906 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1907 return;
1908
1909 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1910 txn->flags |= TX_CON_CLO_SET;
1911 http_header_add_tail2(buf, msg, &txn->hdr_idx, "Connection: close", 17);
1912 }
1913
1914 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1915 txn->flags |= TX_CON_KAL_SET;
1916 http_header_add_tail2(buf, msg, &txn->hdr_idx, "Connection: keep-alive", 22);
1917 }
1918 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001919}
1920
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1922 * first byte of body, and increments msg->sov by the number of bytes parsed,
1923 * so that we know we can forward between ->som and ->sov. Note that due to
1924 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1925 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001928 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001930{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 char *ptr = buf->lr;
1932 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001933 unsigned int chunk = 0;
1934
1935 /* The chunk size is in the following form, though we are only
1936 * interested in the size and CRLF :
1937 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1938 */
1939 while (1) {
1940 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001942 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001944 if (c < 0) /* not a hex digit anymore */
1945 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 if (++ptr >= end)
1947 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001948 if (chunk & 0xF000000) /* overflow will occur */
1949 return -1;
1950 chunk = (chunk << 4) + c;
1951 }
1952
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 /* empty size not allowed */
1954 if (ptr == buf->lr)
1955 return -1;
1956
1957 while (http_is_spht[(unsigned char)*ptr]) {
1958 if (++ptr >= end)
1959 ptr = buf->data;
1960 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001961 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001962 }
1963
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 /* Up to there, we know that at least one byte is present at *ptr. Check
1965 * for the end of chunk size.
1966 */
1967 while (1) {
1968 if (likely(HTTP_IS_CRLF(*ptr))) {
1969 /* we now have a CR or an LF at ptr */
1970 if (likely(*ptr == '\r')) {
1971 if (++ptr >= end)
1972 ptr = buf->data;
1973 if (ptr == buf->r)
1974 return 0;
1975 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001976
Willy Tarreaud98cf932009-12-27 22:54:55 +01001977 if (*ptr != '\n')
1978 return -1;
1979 if (++ptr >= end)
1980 ptr = buf->data;
1981 /* done */
1982 break;
1983 }
1984 else if (*ptr == ';') {
1985 /* chunk extension, ends at next CRLF */
1986 if (++ptr >= end)
1987 ptr = buf->data;
1988 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001989 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990
1991 while (!HTTP_IS_CRLF(*ptr)) {
1992 if (++ptr >= end)
1993 ptr = buf->data;
1994 if (ptr == buf->r)
1995 return 0;
1996 }
1997 /* we have a CRLF now, loop above */
1998 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002000 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002001 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002002 }
2003
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 /* OK we found our CRLF and now <ptr> points to the next byte,
2005 * which may or may not be present. We save that into ->lr and
2006 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002007 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 msg->sov += ptr - buf->lr;
2009 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002010 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
2013}
2014
Willy Tarreaud98cf932009-12-27 22:54:55 +01002015/* This function skips trailers in the buffer <buf> associated with HTTP
2016 * message <msg>. The first visited position is buf->lr. If the end of
2017 * the trailers is found, it is automatically scheduled to be forwarded,
2018 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2019 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002020 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2021 * zero. If a parse error is encountered, the function returns < 0 and does not
2022 * change anything except maybe buf->lr and msg->sov. Note that the message
2023 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2024 * which implies that all non-trailers data have already been scheduled for
2025 * forwarding, and that the difference between msg->som and msg->sov exactly
2026 * matches the length of trailers already parsed and not forwarded. It is also
2027 * important to note that this function is designed to be able to parse wrapped
2028 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029 */
2030int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2031{
2032 /* we have buf->lr which points to next line. Look for CRLF. */
2033 while (1) {
2034 char *p1 = NULL, *p2 = NULL;
2035 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002036 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037
2038 /* scan current line and stop at LF or CRLF */
2039 while (1) {
2040 if (ptr == buf->r)
2041 return 0;
2042
2043 if (*ptr == '\n') {
2044 if (!p1)
2045 p1 = ptr;
2046 p2 = ptr;
2047 break;
2048 }
2049
2050 if (*ptr == '\r') {
2051 if (p1)
2052 return -1;
2053 p1 = ptr;
2054 }
2055
2056 ptr++;
2057 if (ptr >= buf->data + buf->size)
2058 ptr = buf->data;
2059 }
2060
2061 /* after LF; point to beginning of next line */
2062 p2++;
2063 if (p2 >= buf->data + buf->size)
2064 p2 = buf->data;
2065
Willy Tarreau638cd022010-01-03 07:42:04 +01002066 bytes = p2 - buf->lr;
2067 if (bytes < 0)
2068 bytes += buf->size;
2069
2070 /* schedule this line for forwarding */
2071 msg->sov += bytes;
2072 if (msg->sov >= buf->size)
2073 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002074
Willy Tarreau638cd022010-01-03 07:42:04 +01002075 if (p1 == buf->lr) {
2076 /* LF/CRLF at beginning of line => end of trailers at p2.
2077 * Everything was scheduled for forwarding, there's nothing
2078 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002079 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002080 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 msg->msg_state = HTTP_MSG_DONE;
2082 return 1;
2083 }
2084 /* OK, next line then */
2085 buf->lr = p2;
2086 }
2087}
2088
2089/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2090 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2091 * ->som, buf->lr in order to include this part into the next forwarding phase.
2092 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2093 * not enough data are available, the function does not change anything and
2094 * returns zero. If a parse error is encountered, the function returns < 0 and
2095 * does not change anything. Note: this function is designed to parse wrapped
2096 * CRLF at the end of the buffer.
2097 */
2098int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2099{
2100 char *ptr;
2101 int bytes;
2102
2103 /* NB: we'll check data availabilty at the end. It's not a
2104 * problem because whatever we match first will be checked
2105 * against the correct length.
2106 */
2107 bytes = 1;
2108 ptr = buf->lr;
2109 if (*ptr == '\r') {
2110 bytes++;
2111 ptr++;
2112 if (ptr >= buf->data + buf->size)
2113 ptr = buf->data;
2114 }
2115
2116 if (buf->l < bytes)
2117 return 0;
2118
2119 if (*ptr != '\n')
2120 return -1;
2121
2122 ptr++;
2123 if (ptr >= buf->data + buf->size)
2124 ptr = buf->data;
2125 buf->lr = ptr;
2126 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2127 msg->sov = ptr - buf->data;
2128 msg->som = msg->sov - bytes;
2129 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2130 return 1;
2131}
Willy Tarreau5b154472009-12-21 20:11:07 +01002132
Willy Tarreau83e3af02009-12-28 17:39:57 +01002133void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2134{
2135 char *end = buf->data + buf->size;
2136 int off = buf->data + buf->size - buf->w;
2137
2138 /* two possible cases :
2139 * - the buffer is in one contiguous block, we move it in-place
2140 * - the buffer is in two blocks, we move it via the trash
2141 */
2142 if (buf->l) {
2143 int block1 = buf->l;
2144 int block2 = 0;
2145 if (buf->r <= buf->w) {
2146 /* non-contiguous block */
2147 block1 = buf->data + buf->size - buf->w;
2148 block2 = buf->r - buf->data;
2149 }
2150 if (block2)
2151 memcpy(trash, buf->data, block2);
2152 memmove(buf->data, buf->w, block1);
2153 if (block2)
2154 memcpy(buf->data + block1, trash, block2);
2155 }
2156
2157 /* adjust all known pointers */
2158 buf->w = buf->data;
2159 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2160 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2161 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2162 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2163
2164 /* adjust relative pointers */
2165 msg->som = 0;
2166 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2167 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2168 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2169
Willy Tarreau83e3af02009-12-28 17:39:57 +01002170 if (msg->err_pos >= 0) {
2171 msg->err_pos += off;
2172 if (msg->err_pos >= buf->size)
2173 msg->err_pos -= buf->size;
2174 }
2175
2176 buf->flags &= ~BF_FULL;
2177 if (buf->l >= buffer_max_len(buf))
2178 buf->flags |= BF_FULL;
2179}
2180
Willy Tarreaud787e662009-07-07 10:14:51 +02002181/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2182 * processing can continue on next analysers, or zero if it either needs more
2183 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2184 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2185 * when it has nothing left to do, and may remove any analyser when it wants to
2186 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002187 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002188int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002189{
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 /*
2191 * We will parse the partial (or complete) lines.
2192 * We will check the request syntax, and also join multi-line
2193 * headers. An index of all the lines will be elaborated while
2194 * parsing.
2195 *
2196 * For the parsing, we use a 28 states FSM.
2197 *
2198 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002199 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002200 * req->data + msg->eoh = end of processed headers / start of current one
2201 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002202 * req->lr = first non-visited byte
2203 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002204 *
2205 * At end of parsing, we may perform a capture of the error (if any), and
2206 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2207 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2208 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002209 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002210
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002212 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002213 struct http_txn *txn = &s->txn;
2214 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002215 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002216
Willy Tarreau6bf17362009-02-24 10:48:35 +01002217 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2218 now_ms, __FUNCTION__,
2219 s,
2220 req,
2221 req->rex, req->wex,
2222 req->flags,
2223 req->l,
2224 req->analysers);
2225
Willy Tarreau52a0c602009-08-16 22:45:38 +02002226 /* we're speaking HTTP here, so let's speak HTTP to the client */
2227 s->srv_error = http_return_srv_error;
2228
Willy Tarreau83e3af02009-12-28 17:39:57 +01002229 /* There's a protected area at the end of the buffer for rewriting
2230 * purposes. We don't want to start to parse the request if the
2231 * protected area is affected, because we may have to move processed
2232 * data later, which is much more complicated.
2233 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002234 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002235 if ((txn->flags & TX_NOT_FIRST) &&
2236 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002237 req->r < req->lr ||
2238 req->r > req->data + req->size - global.tune.maxrewrite)) {
2239 if (req->send_max) {
2240 /* some data has still not left the buffer, wake us once that's done */
2241 buffer_dont_connect(req);
2242 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2243 return 0;
2244 }
2245 if (req->l <= req->size - global.tune.maxrewrite)
2246 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002247 }
2248
Willy Tarreau065e8332010-01-08 00:30:20 +01002249 /* Note that we have the same problem with the response ; we
2250 * may want to send a redirect, error or anything which requires
2251 * some spare space. So we'll ensure that we have at least
2252 * maxrewrite bytes available in the response buffer before
2253 * processing that one. This will only affect pipelined
2254 * keep-alive requests.
2255 */
2256 if ((txn->flags & TX_NOT_FIRST) &&
2257 unlikely((s->rep->flags & BF_FULL) ||
2258 s->rep->r < s->rep->lr ||
2259 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2260 if (s->rep->send_max) {
2261 /* don't let a connection request be initiated */
2262 buffer_dont_connect(req);
2263 return 0;
2264 }
2265 }
2266
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002267 if (likely(req->lr < req->r))
2268 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002269 }
2270
Willy Tarreau59234e92008-11-30 23:51:27 +01002271 /* 1: we might have to print this header in debug mode */
2272 if (unlikely((global.mode & MODE_DEBUG) &&
2273 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002274 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002276
Willy Tarreau962c3f42010-01-10 00:15:35 +01002277 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 eol = sol + msg->sl.rq.l;
2279 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002280
Willy Tarreau59234e92008-11-30 23:51:27 +01002281 sol += hdr_idx_first_pos(&txn->hdr_idx);
2282 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002283
Willy Tarreau59234e92008-11-30 23:51:27 +01002284 while (cur_idx) {
2285 eol = sol + txn->hdr_idx.v[cur_idx].len;
2286 debug_hdr("clihdr", s, sol, eol);
2287 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2288 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002289 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002290 }
2291
Willy Tarreau58f10d72006-12-04 02:26:12 +01002292
Willy Tarreau59234e92008-11-30 23:51:27 +01002293 /*
2294 * Now we quickly check if we have found a full valid request.
2295 * If not so, we check the FD and buffer states before leaving.
2296 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002297 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002298 * requests are checked first. When waiting for a second request
2299 * on a keep-alive session, if we encounter and error, close, t/o,
2300 * we note the error in the session flags but don't set any state.
2301 * Since the error will be noted there, it will not be counted by
2302 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002303 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002304
Willy Tarreau655dce92009-11-08 13:10:58 +01002305 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002306 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002307 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002308 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2310 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002311
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 /* 1: Since we are in header mode, if there's no space
2313 * left for headers, we won't be able to free more
2314 * later, so the session will never terminate. We
2315 * must terminate it now.
2316 */
2317 if (unlikely(req->flags & BF_FULL)) {
2318 /* FIXME: check if URI is set and return Status
2319 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002320 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 goto return_bad_req;
2322 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002323
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 /* 2: have we encountered a read error ? */
2325 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002326 if (!(s->flags & SN_ERR_MASK))
2327 s->flags |= SN_ERR_CLICL;
2328
Willy Tarreaufcffa692010-01-10 14:21:19 +01002329 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002330 goto failed_keep_alive;
2331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002333 if (msg->err_pos >= 0)
2334 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 msg->msg_state = HTTP_MSG_ERROR;
2336 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002337
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002338 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002339 if (s->listener->counters)
2340 s->listener->counters->failed_req++;
2341
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 if (!(s->flags & SN_FINST_MASK))
2343 s->flags |= SN_FINST_R;
2344 return 0;
2345 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 /* 3: has the read timeout expired ? */
2348 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002349 if (!(s->flags & SN_ERR_MASK))
2350 s->flags |= SN_ERR_CLITO;
2351
Willy Tarreaufcffa692010-01-10 14:21:19 +01002352 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002353 goto failed_keep_alive;
2354
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002356 if (msg->err_pos >= 0)
2357 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 txn->status = 408;
2359 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2360 msg->msg_state = HTTP_MSG_ERROR;
2361 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002362
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002363 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002364 if (s->listener->counters)
2365 s->listener->counters->failed_req++;
2366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 if (!(s->flags & SN_FINST_MASK))
2368 s->flags |= SN_FINST_R;
2369 return 0;
2370 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002371
Willy Tarreau59234e92008-11-30 23:51:27 +01002372 /* 4: have we encountered a close ? */
2373 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002374 if (!(s->flags & SN_ERR_MASK))
2375 s->flags |= SN_ERR_CLICL;
2376
Willy Tarreaufcffa692010-01-10 14:21:19 +01002377 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002378 goto failed_keep_alive;
2379
Willy Tarreau4076a152009-04-02 15:18:36 +02002380 if (msg->err_pos >= 0)
2381 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 txn->status = 400;
2383 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2384 msg->msg_state = HTTP_MSG_ERROR;
2385 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002386
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002387 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002388 if (s->listener->counters)
2389 s->listener->counters->failed_req++;
2390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 if (!(s->flags & SN_FINST_MASK))
2392 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002393 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002394 }
2395
Willy Tarreau520d95e2009-09-19 21:04:57 +02002396 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002397 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2398
Willy Tarreaufcffa692010-01-10 14:21:19 +01002399 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2400 /* If the client starts to talk, let's fall back to
2401 * request timeout processing.
2402 */
2403 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002404 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002405 }
2406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002408 if (!tick_isset(req->analyse_exp)) {
2409 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2410 (txn->flags & TX_WAIT_NEXT_RQ) &&
2411 tick_isset(s->be->timeout.httpka))
2412 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2413 else
2414 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002416
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 /* we're not ready yet */
2418 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002419
2420 failed_keep_alive:
2421 /* Here we process low-level errors for keep-alive requests. In
2422 * short, if the request is not the first one and it experiences
2423 * a timeout, read error or shutdown, we just silently close so
2424 * that the client can try again.
2425 */
2426 txn->status = 0;
2427 msg->msg_state = HTTP_MSG_RQBEFORE;
2428 req->analysers = 0;
2429 s->logs.logwait = 0;
Willy Tarreau148d0992010-01-10 10:21:21 +01002430 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002431 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002433
Willy Tarreaud787e662009-07-07 10:14:51 +02002434 /* OK now we have a complete HTTP request with indexed headers. Let's
2435 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002436 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2437 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2438 * points to the CRLF of the request line. req->lr points to the first
2439 * byte after the last LF. msg->col and msg->sov point to the first
2440 * byte of data. msg->eol cannot be trusted because it may have been
2441 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002442 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002443
Willy Tarreaub16a5742010-01-10 14:46:16 +01002444 if (txn->flags & TX_WAIT_NEXT_RQ) {
2445 /* kill the pending keep-alive timeout */
2446 txn->flags &= ~TX_WAIT_NEXT_RQ;
2447 req->analyse_exp = TICK_ETERNITY;
2448 }
2449
2450
Willy Tarreaud787e662009-07-07 10:14:51 +02002451 /* Maybe we found in invalid header name while we were configured not
2452 * to block on that, so we have to capture it now.
2453 */
2454 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002455 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2456
Willy Tarreau59234e92008-11-30 23:51:27 +01002457 /*
2458 * 1: identify the method
2459 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002460 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002461
2462 /* we can make use of server redirect on GET and HEAD */
2463 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2464 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002465
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 /*
2467 * 2: check if the URI matches the monitor_uri.
2468 * We have to do this for every request which gets in, because
2469 * the monitor-uri is defined by the frontend.
2470 */
2471 if (unlikely((s->fe->monitor_uri_len != 0) &&
2472 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002473 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002474 s->fe->monitor_uri,
2475 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002476 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002477 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002478 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002479 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002480
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002482
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002484 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2485 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002486
Willy Tarreau59234e92008-11-30 23:51:27 +01002487 ret = acl_pass(ret);
2488 if (cond->pol == ACL_COND_UNLESS)
2489 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002490
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 if (ret) {
2492 /* we fail this request, let's return 503 service unavail */
2493 txn->status = 503;
2494 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2495 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002496 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002497 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002498
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 /* nothing to fail, let's reply normaly */
2500 txn->status = 200;
2501 stream_int_retnclose(req->prod, &http_200_chunk);
2502 goto return_prx_cond;
2503 }
2504
2505 /*
2506 * 3: Maybe we have to copy the original REQURI for the logs ?
2507 * Note: we cannot log anymore if the request has been
2508 * classified as invalid.
2509 */
2510 if (unlikely(s->logs.logwait & LW_REQ)) {
2511 /* we have a complete HTTP request that we must log */
2512 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2513 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002514
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 if (urilen >= REQURI_LEN)
2516 urilen = REQURI_LEN - 1;
2517 memcpy(txn->uri, &req->data[msg->som], urilen);
2518 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002519
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 if (!(s->logs.logwait &= ~LW_REQ))
2521 s->do_log(s);
2522 } else {
2523 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002524 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 }
Willy Tarreau06619262006-12-17 08:37:22 +01002526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002528 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2529 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002530
Willy Tarreau5b154472009-12-21 20:11:07 +01002531 /* ... and check if the request is HTTP/1.1 or above */
2532 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002533 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2534 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2535 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002536 txn->flags |= TX_REQ_VER_11;
2537
2538 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002539 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002540
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002541 /* transfer length unknown*/
2542 txn->flags &= ~TX_REQ_XFER_LEN;
2543
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 /* 5: we may need to capture headers */
2545 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002546 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002548
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002549 /* 6: determine the transfer-length.
2550 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2551 * the presence of a message-body in a REQUEST and its transfer length
2552 * must be determined that way (in order of precedence) :
2553 * 1. The presence of a message-body in a request is signaled by the
2554 * inclusion of a Content-Length or Transfer-Encoding header field
2555 * in the request's header fields. When a request message contains
2556 * both a message-body of non-zero length and a method that does
2557 * not define any semantics for that request message-body, then an
2558 * origin server SHOULD either ignore the message-body or respond
2559 * with an appropriate error message (e.g., 413). A proxy or
2560 * gateway, when presented the same request, SHOULD either forward
2561 * the request inbound with the message- body or ignore the
2562 * message-body when determining a response.
2563 *
2564 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2565 * and the "chunked" transfer-coding (Section 6.2) is used, the
2566 * transfer-length is defined by the use of this transfer-coding.
2567 * If a Transfer-Encoding header field is present and the "chunked"
2568 * transfer-coding is not present, the transfer-length is defined
2569 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002570 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002571 * 3. If a Content-Length header field is present, its decimal value in
2572 * OCTETs represents both the entity-length and the transfer-length.
2573 * If a message is received with both a Transfer-Encoding header
2574 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002575 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002576 * 4. By the server closing the connection. (Closing the connection
2577 * cannot be used to indicate the end of a request body, since that
2578 * would leave no possibility for the server to send back a response.)
2579 *
2580 * Whenever a transfer-coding is applied to a message-body, the set of
2581 * transfer-codings MUST include "chunked", unless the message indicates
2582 * it is terminated by closing the connection. When the "chunked"
2583 * transfer-coding is used, it MUST be the last transfer-coding applied
2584 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002585 */
2586
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002587 /* CONNECT sets a tunnel and ignores everything else */
2588 if (txn->meth == HTTP_METH_CONNECT)
2589 goto skip_xfer_len;
2590
2591 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002592 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002593 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002594 while ((txn->flags & TX_REQ_VER_11) &&
2595 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002596 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2597 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2598 else if (txn->flags & TX_REQ_TE_CHNK) {
2599 /* bad transfer-encoding (chunked followed by something else) */
2600 use_close_only = 1;
2601 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2602 break;
2603 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002604 }
2605
Willy Tarreau32b47f42009-10-18 20:55:02 +02002606 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002607 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002608 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2609 signed long long cl;
2610
2611 if (!ctx.vlen)
2612 goto return_bad_req;
2613
2614 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2615 goto return_bad_req; /* parse failure */
2616
2617 if (cl < 0)
2618 goto return_bad_req;
2619
2620 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2621 goto return_bad_req; /* already specified, was different */
2622
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002623 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 msg->hdr_content_len = cl;
2625 }
2626
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002627 /* bodyless requests have a known length */
2628 if (!use_close_only)
2629 txn->flags |= TX_REQ_XFER_LEN;
2630
2631 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002632 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002633 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002634 req->analyse_exp = TICK_ETERNITY;
2635 return 1;
2636
2637 return_bad_req:
2638 /* We centralize bad requests processing here */
2639 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2640 /* we detected a parsing error. We want to archive this request
2641 * in the dedicated proxy area for later troubleshooting.
2642 */
2643 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2644 }
2645
2646 txn->req.msg_state = HTTP_MSG_ERROR;
2647 txn->status = 400;
2648 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002649
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002650 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002651 if (s->listener->counters)
2652 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002653
2654 return_prx_cond:
2655 if (!(s->flags & SN_ERR_MASK))
2656 s->flags |= SN_ERR_PRXCOND;
2657 if (!(s->flags & SN_FINST_MASK))
2658 s->flags |= SN_FINST_R;
2659
2660 req->analysers = 0;
2661 req->analyse_exp = TICK_ETERNITY;
2662 return 0;
2663}
2664
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002665/* This stream analyser runs all HTTP request processing which is common to
2666 * frontends and backends, which means blocking ACLs, filters, connection-close,
2667 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002668 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002669 * either needs more data or wants to immediately abort the request (eg: deny,
2670 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002671 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002672int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002673{
Willy Tarreaud787e662009-07-07 10:14:51 +02002674 struct http_txn *txn = &s->txn;
2675 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002676 struct acl_cond *cond;
2677 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002678 struct wordlist *wl;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002679 int del_ka, del_cl;
Willy Tarreaud787e662009-07-07 10:14:51 +02002680
Willy Tarreau655dce92009-11-08 13:10:58 +01002681 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002682 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002683 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002684 return 0;
2685 }
2686
Willy Tarreau3a816292009-07-07 10:55:49 +02002687 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002688 req->analyse_exp = TICK_ETERNITY;
2689
2690 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2691 now_ms, __FUNCTION__,
2692 s,
2693 req,
2694 req->rex, req->wex,
2695 req->flags,
2696 req->l,
2697 req->analysers);
2698
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002699 /* first check whether we have some ACLs set to block this request */
2700 list_for_each_entry(cond, &px->block_cond, list) {
2701 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002702
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002703 ret = acl_pass(ret);
2704 if (cond->pol == ACL_COND_UNLESS)
2705 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002706
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002707 if (ret) {
2708 txn->status = 403;
2709 /* let's log the request time */
2710 s->logs.tv_request = now;
2711 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2712 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002713 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002715
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002716 /* try headers filters */
2717 if (px->req_exp != NULL) {
2718 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2719 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002720
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 /* has the request been denied ? */
2722 if (txn->flags & TX_CLDENY) {
2723 /* no need to go further */
2724 txn->status = 403;
2725 /* let's log the request time */
2726 s->logs.tv_request = now;
2727 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2728 goto return_prx_cond;
2729 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002730
2731 /* When a connection is tarpitted, we use the tarpit timeout,
2732 * which may be the same as the connect timeout if unspecified.
2733 * If unset, then set it to zero because we really want it to
2734 * eventually expire. We build the tarpit as an analyser.
2735 */
2736 if (txn->flags & TX_CLTARPIT) {
2737 buffer_erase(s->req);
2738 /* wipe the request out so that we can drop the connection early
2739 * if the client closes first.
2740 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002741 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002742 req->analysers = 0; /* remove switching rules etc... */
2743 req->analysers |= AN_REQ_HTTP_TARPIT;
2744 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2745 if (!req->analyse_exp)
2746 req->analyse_exp = tick_add(now_ms, 0);
2747 return 1;
2748 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002749 }
Willy Tarreau06619262006-12-17 08:37:22 +01002750
Willy Tarreau5b154472009-12-21 20:11:07 +01002751 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2752 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002753 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2754 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002755 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2756 * avoid to redo the same work if FE and BE have the same settings (common).
2757 * The method consists in checking if options changed between the two calls
2758 * (implying that either one is non-null, or one of them is non-null and we
2759 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002760 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002761
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002762 del_cl = del_ka = 0;
2763
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002765 ((!(txn->flags & TX_HDR_CONN_PRS) &&
2766 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2767 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2768 (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 +01002769 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002770
Willy Tarreau5b154472009-12-21 20:11:07 +01002771 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2772 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002773 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2774 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002775 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002776 tmp = TX_CON_WANT_CLO;
2777
Willy Tarreau5b154472009-12-21 20:11:07 +01002778 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2779 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002780
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002781 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2782 /* parse the Connection header and possibly clean it */
2783 int to_del = 0;
2784 if ((txn->flags & TX_REQ_VER_11) ||
2785 (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)
2786 to_del |= 2; /* remove "keep-alive" */
2787 if (!(txn->flags & TX_REQ_VER_11))
2788 to_del |= 1; /* remove "close" */
2789 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002790 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002791
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002792 /* check if client or config asks for explicit close in KAL/SCL */
2793 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2794 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2795 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2796 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2797 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
2798 !(txn->flags & TX_REQ_XFER_LEN))) /* no length known => close */
2799 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2800 }
Willy Tarreau78599912009-10-17 20:12:21 +02002801
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002802 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002803 list_for_each_entry(wl, &px->req_add, list) {
2804 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002805 goto return_bad_req;
2806 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002807
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002808 /* check if stats URI was requested, and if an auth is needed */
2809 if (px->uri_auth != NULL &&
2810 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2811 /* we have to check the URI and auth for this request.
2812 * FIXME!!! that one is rather dangerous, we want to
2813 * make it follow standard rules (eg: clear req->analysers).
2814 */
2815 if (stats_check_uri_auth(s, px)) {
2816 req->analysers = 0;
2817 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002819 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002820
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002821 /* check whether we have some ACLs set to redirect this request */
2822 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002823 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002824
Willy Tarreauf285f542010-01-03 20:03:03 +01002825 if (rule->cond) {
2826 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2827 ret = acl_pass(ret);
2828 if (rule->cond->pol == ACL_COND_UNLESS)
2829 ret = !ret;
2830 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002831
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002832 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002833 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002834 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002835
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002836 /* build redirect message */
2837 switch(rule->code) {
2838 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002839 msg_fmt = HTTP_303;
2840 break;
2841 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002842 msg_fmt = HTTP_301;
2843 break;
2844 case 302:
2845 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002846 msg_fmt = HTTP_302;
2847 break;
2848 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002849
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002850 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002851 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002852
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002853 switch(rule->type) {
2854 case REDIRECT_TYPE_PREFIX: {
2855 const char *path;
2856 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002857
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002858 path = http_get_path(txn);
2859 /* build message using path */
2860 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002861 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002862 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2863 int qs = 0;
2864 while (qs < pathlen) {
2865 if (path[qs] == '?') {
2866 pathlen = qs;
2867 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002868 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002869 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002870 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002871 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002872 } else {
2873 path = "/";
2874 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002875 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002876
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002877 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002878 goto return_bad_req;
2879
2880 /* add prefix. Note that if prefix == "/", we don't want to
2881 * add anything, otherwise it makes it hard for the user to
2882 * configure a self-redirection.
2883 */
2884 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002885 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2886 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002887 }
2888
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002889 /* add path */
2890 memcpy(rdr.str + rdr.len, path, pathlen);
2891 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01002892
2893 /* append a slash at the end of the location is needed and missing */
2894 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
2895 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2896 if (rdr.len > rdr.size - 5)
2897 goto return_bad_req;
2898 rdr.str[rdr.len] = '/';
2899 rdr.len++;
2900 }
2901
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002902 break;
2903 }
2904 case REDIRECT_TYPE_LOCATION:
2905 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002906 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002907 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002908
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002909 /* add location */
2910 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2911 rdr.len += rule->rdr_len;
2912 break;
2913 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002914
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002915 if (rule->cookie_len) {
2916 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2917 rdr.len += 14;
2918 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2919 rdr.len += rule->cookie_len;
2920 memcpy(rdr.str + rdr.len, "\r\n", 2);
2921 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002922 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002923
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002924 /* add end of headers and the keep-alive/close status.
2925 * We may choose to set keep-alive if the Location begins
2926 * with a slash, because the client will come back to the
2927 * same server.
2928 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002929 txn->status = rule->code;
2930 /* let's log the request time */
2931 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002932
2933 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2934 (txn->flags & TX_REQ_XFER_LEN) &&
2935 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2936 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2937 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2938 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01002939 if (!(txn->flags & TX_REQ_VER_11)) {
2940 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
2941 rdr.len += 24;
2942 }
2943 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2944 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002945 buffer_write(req->prod->ob, rdr.str, rdr.len);
2946 /* "eat" the request */
2947 buffer_ignore(req, msg->sov - msg->som);
2948 msg->som = msg->sov;
2949 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002950 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2951 txn->req.msg_state = HTTP_MSG_CLOSED;
2952 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002953 break;
2954 } else {
2955 /* keep-alive not possible */
2956 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2957 rdr.len += 23;
Willy Tarreau148d0992010-01-10 10:21:21 +01002958 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002959 goto return_prx_cond;
2960 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002961 }
2962 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002963
Willy Tarreau2be39392010-01-03 17:24:51 +01002964 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2965 * If this happens, then the data will not come immediately, so we must
2966 * send all what we have without waiting. Note that due to the small gain
2967 * in waiting for the body of the request, it's easier to simply put the
2968 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2969 * itself once used.
2970 */
2971 req->flags |= BF_SEND_DONTWAIT;
2972
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002973 /* that's OK for us now, let's move on to next analysers */
2974 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002975
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002976 return_bad_req:
2977 /* We centralize bad requests processing here */
2978 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2979 /* we detected a parsing error. We want to archive this request
2980 * in the dedicated proxy area for later troubleshooting.
2981 */
2982 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2983 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002984
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002985 txn->req.msg_state = HTTP_MSG_ERROR;
2986 txn->status = 400;
2987 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002988
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002989 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002990 if (s->listener->counters)
2991 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002992
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002993 return_prx_cond:
2994 if (!(s->flags & SN_ERR_MASK))
2995 s->flags |= SN_ERR_PRXCOND;
2996 if (!(s->flags & SN_FINST_MASK))
2997 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002998
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999 req->analysers = 0;
3000 req->analyse_exp = TICK_ETERNITY;
3001 return 0;
3002}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003003
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003004/* This function performs all the processing enabled for the current request.
3005 * It returns 1 if the processing can continue on next analysers, or zero if it
3006 * needs more data, encounters an error, or wants to immediately abort the
3007 * request. It relies on buffers flags, and updates s->req->analysers.
3008 */
3009int http_process_request(struct session *s, struct buffer *req, int an_bit)
3010{
3011 struct http_txn *txn = &s->txn;
3012 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003013
Willy Tarreau655dce92009-11-08 13:10:58 +01003014 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003015 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003016 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003017 return 0;
3018 }
3019
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3021 now_ms, __FUNCTION__,
3022 s,
3023 req,
3024 req->rex, req->wex,
3025 req->flags,
3026 req->l,
3027 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003028
Willy Tarreau59234e92008-11-30 23:51:27 +01003029 /*
3030 * Right now, we know that we have processed the entire headers
3031 * and that unwanted requests have been filtered out. We can do
3032 * whatever we want with the remaining request. Also, now we
3033 * may have separate values for ->fe, ->be.
3034 */
Willy Tarreau06619262006-12-17 08:37:22 +01003035
Willy Tarreau59234e92008-11-30 23:51:27 +01003036 /*
3037 * If HTTP PROXY is set we simply get remote server address
3038 * parsing incoming request.
3039 */
3040 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003041 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003042 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003043
Willy Tarreau59234e92008-11-30 23:51:27 +01003044 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003045 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003046 * Note that doing so might move headers in the request, but
3047 * the fields will stay coherent and the URI will not move.
3048 * This should only be performed in the backend.
3049 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003050 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003051 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3052 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003053
Willy Tarreau59234e92008-11-30 23:51:27 +01003054 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003055 * 8: the appsession cookie was looked up very early in 1.2,
3056 * so let's do the same now.
3057 */
3058
3059 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003060 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003061 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003062 }
3063
3064 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003065 * 9: add X-Forwarded-For if either the frontend or the backend
3066 * asks for it.
3067 */
3068 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3069 if (s->cli_addr.ss_family == AF_INET) {
3070 /* Add an X-Forwarded-For header unless the source IP is
3071 * in the 'except' network range.
3072 */
3073 if ((!s->fe->except_mask.s_addr ||
3074 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3075 != s->fe->except_net.s_addr) &&
3076 (!s->be->except_mask.s_addr ||
3077 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3078 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003079 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 unsigned char *pn;
3081 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003082
3083 /* Note: we rely on the backend to get the header name to be used for
3084 * x-forwarded-for, because the header is really meant for the backends.
3085 * However, if the backend did not specify any option, we have to rely
3086 * on the frontend's header name.
3087 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003088 if (s->be->fwdfor_hdr_len) {
3089 len = s->be->fwdfor_hdr_len;
3090 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003091 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003092 len = s->fe->fwdfor_hdr_len;
3093 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003094 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003095 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003096
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003097 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003098 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003099 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003100 }
3101 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003102 else if (s->cli_addr.ss_family == AF_INET6) {
3103 /* FIXME: for the sake of completeness, we should also support
3104 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003105 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 int len;
3107 char pn[INET6_ADDRSTRLEN];
3108 inet_ntop(AF_INET6,
3109 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3110 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003111
Willy Tarreau59234e92008-11-30 23:51:27 +01003112 /* Note: we rely on the backend to get the header name to be used for
3113 * x-forwarded-for, because the header is really meant for the backends.
3114 * However, if the backend did not specify any option, we have to rely
3115 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003116 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003117 if (s->be->fwdfor_hdr_len) {
3118 len = s->be->fwdfor_hdr_len;
3119 memcpy(trash, s->be->fwdfor_hdr_name, len);
3120 } else {
3121 len = s->fe->fwdfor_hdr_len;
3122 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003123 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003124 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003125
Willy Tarreau59234e92008-11-30 23:51:27 +01003126 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003127 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003128 goto return_bad_req;
3129 }
3130 }
3131
3132 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003133 * 10: add X-Original-To if either the frontend or the backend
3134 * asks for it.
3135 */
3136 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3137
3138 /* FIXME: don't know if IPv6 can handle that case too. */
3139 if (s->cli_addr.ss_family == AF_INET) {
3140 /* Add an X-Original-To header unless the destination IP is
3141 * in the 'except' network range.
3142 */
3143 if (!(s->flags & SN_FRT_ADDR_SET))
3144 get_frt_addr(s);
3145
3146 if ((!s->fe->except_mask_to.s_addr ||
3147 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3148 != s->fe->except_to.s_addr) &&
3149 (!s->be->except_mask_to.s_addr ||
3150 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3151 != s->be->except_to.s_addr)) {
3152 int len;
3153 unsigned char *pn;
3154 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3155
3156 /* Note: we rely on the backend to get the header name to be used for
3157 * x-original-to, because the header is really meant for the backends.
3158 * However, if the backend did not specify any option, we have to rely
3159 * on the frontend's header name.
3160 */
3161 if (s->be->orgto_hdr_len) {
3162 len = s->be->orgto_hdr_len;
3163 memcpy(trash, s->be->orgto_hdr_name, len);
3164 } else {
3165 len = s->fe->orgto_hdr_len;
3166 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003167 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003168 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3169
3170 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003171 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003172 goto return_bad_req;
3173 }
3174 }
3175 }
3176
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003177 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3178 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3179 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3180 unsigned int want_flags = 0;
3181
3182 if (txn->flags & TX_REQ_VER_11) {
3183 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3184 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3185 want_flags |= TX_CON_CLO_SET;
3186 } else {
3187 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
3188 want_flags |= TX_CON_KAL_SET;
3189 }
3190
3191 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3192 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003193 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003194
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003195
Willy Tarreau522d6c02009-12-06 18:49:18 +01003196 /* If we have no server assigned yet and we're balancing on url_param
3197 * with a POST request, we may be interested in checking the body for
3198 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003199 */
3200 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3201 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003202 s->be->url_param_post_limit != 0 &&
3203 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003204 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003205 buffer_dont_connect(req);
3206 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003207 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003208
Willy Tarreaud98cf932009-12-27 22:54:55 +01003209 if (txn->flags & TX_REQ_XFER_LEN)
3210 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003211
Willy Tarreau59234e92008-11-30 23:51:27 +01003212 /*************************************************************
3213 * OK, that's finished for the headers. We have done what we *
3214 * could. Let's switch to the DATA state. *
3215 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003216 req->analyse_exp = TICK_ETERNITY;
3217 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003218
Willy Tarreau59234e92008-11-30 23:51:27 +01003219 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003220 /* OK let's go on with the BODY now */
3221 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003222
Willy Tarreau59234e92008-11-30 23:51:27 +01003223 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003224 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003225 /* we detected a parsing error. We want to archive this request
3226 * in the dedicated proxy area for later troubleshooting.
3227 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003228 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003229 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003230
Willy Tarreau59234e92008-11-30 23:51:27 +01003231 txn->req.msg_state = HTTP_MSG_ERROR;
3232 txn->status = 400;
3233 req->analysers = 0;
3234 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003235
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003236 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003237 if (s->listener->counters)
3238 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003239
Willy Tarreau59234e92008-11-30 23:51:27 +01003240 if (!(s->flags & SN_ERR_MASK))
3241 s->flags |= SN_ERR_PRXCOND;
3242 if (!(s->flags & SN_FINST_MASK))
3243 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003244 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003245}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003246
Willy Tarreau60b85b02008-11-30 23:28:40 +01003247/* This function is an analyser which processes the HTTP tarpit. It always
3248 * returns zero, at the beginning because it prevents any other processing
3249 * from occurring, and at the end because it terminates the request.
3250 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003251int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003252{
3253 struct http_txn *txn = &s->txn;
3254
3255 /* This connection is being tarpitted. The CLIENT side has
3256 * already set the connect expiration date to the right
3257 * timeout. We just have to check that the client is still
3258 * there and that the timeout has not expired.
3259 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003260 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003261 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3262 !tick_is_expired(req->analyse_exp, now_ms))
3263 return 0;
3264
3265 /* We will set the queue timer to the time spent, just for
3266 * logging purposes. We fake a 500 server error, so that the
3267 * attacker will not suspect his connection has been tarpitted.
3268 * It will not cause trouble to the logs because we can exclude
3269 * the tarpitted connections by filtering on the 'PT' status flags.
3270 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003271 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3272
3273 txn->status = 500;
3274 if (req->flags != BF_READ_ERROR)
3275 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3276
3277 req->analysers = 0;
3278 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003279
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003280 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003281 if (s->listener->counters)
3282 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003283
Willy Tarreau60b85b02008-11-30 23:28:40 +01003284 if (!(s->flags & SN_ERR_MASK))
3285 s->flags |= SN_ERR_PRXCOND;
3286 if (!(s->flags & SN_FINST_MASK))
3287 s->flags |= SN_FINST_T;
3288 return 0;
3289}
3290
Willy Tarreaud34af782008-11-30 23:36:37 +01003291/* This function is an analyser which processes the HTTP request body. It looks
3292 * for parameters to be used for the load balancing algorithm (url_param). It
3293 * must only be called after the standard HTTP request processing has occurred,
3294 * because it expects the request to be parsed. It returns zero if it needs to
3295 * read more data, or 1 once it has completed its analysis.
3296 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003297int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003298{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003299 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003300 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003301 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003302
3303 /* We have to parse the HTTP request body to find any required data.
3304 * "balance url_param check_post" should have been the only way to get
3305 * into this. We were brought here after HTTP header analysis, so all
3306 * related structures are ready.
3307 */
3308
Willy Tarreau522d6c02009-12-06 18:49:18 +01003309 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3310 goto missing_data;
3311
3312 if (msg->msg_state < HTTP_MSG_100_SENT) {
3313 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3314 * send an HTTP/1.1 100 Continue intermediate response.
3315 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003316 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003317 struct hdr_ctx ctx;
3318 ctx.idx = 0;
3319 /* Expect is allowed in 1.1, look for it */
3320 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3321 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3322 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3323 }
3324 }
3325 msg->msg_state = HTTP_MSG_100_SENT;
3326 }
3327
3328 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003329 /* we have msg->col and msg->sov which both point to the first
3330 * byte of message body. msg->som still points to the beginning
3331 * of the message. We must save the body in req->lr because it
3332 * survives buffer re-alignments.
3333 */
3334 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003335 if (txn->flags & TX_REQ_TE_CHNK)
3336 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3337 else
3338 msg->msg_state = HTTP_MSG_DATA;
3339 }
3340
3341 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003342 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003343 * set ->sov and ->lr to point to the body and switch to DATA or
3344 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003345 */
3346 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003347
Willy Tarreau115acb92009-12-26 13:56:06 +01003348 if (!ret)
3349 goto missing_data;
3350 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003351 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003352 }
3353
Willy Tarreaud98cf932009-12-27 22:54:55 +01003354 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003355 * We have the first non-header byte in msg->col, which is either the
3356 * beginning of the chunk size or of the data. The first data byte is in
3357 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3358 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003359 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003360
3361 if (msg->hdr_content_len < limit)
3362 limit = msg->hdr_content_len;
3363
Willy Tarreau7c96f672009-12-27 22:47:25 +01003364 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003365 goto http_end;
3366
3367 missing_data:
3368 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003369 if (req->flags & BF_FULL)
3370 goto return_bad_req;
3371
Willy Tarreau522d6c02009-12-06 18:49:18 +01003372 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3373 txn->status = 408;
3374 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3375 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003376 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003377
3378 /* we get here if we need to wait for more data */
3379 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003380 /* Not enough data. We'll re-use the http-request
3381 * timeout here. Ideally, we should set the timeout
3382 * relative to the accept() date. We just set the
3383 * request timeout once at the beginning of the
3384 * request.
3385 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003386 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003387 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003388 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003389 return 0;
3390 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003391
3392 http_end:
3393 /* The situation will not evolve, so let's give up on the analysis. */
3394 s->logs.tv_request = now; /* update the request timer to reflect full request */
3395 req->analysers &= ~an_bit;
3396 req->analyse_exp = TICK_ETERNITY;
3397 return 1;
3398
3399 return_bad_req: /* let's centralize all bad requests */
3400 txn->req.msg_state = HTTP_MSG_ERROR;
3401 txn->status = 400;
3402 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3403
3404 return_err_msg:
3405 req->analysers = 0;
3406 s->fe->counters.failed_req++;
3407 if (s->listener->counters)
3408 s->listener->counters->failed_req++;
3409
3410 if (!(s->flags & SN_ERR_MASK))
3411 s->flags |= SN_ERR_PRXCOND;
3412 if (!(s->flags & SN_FINST_MASK))
3413 s->flags |= SN_FINST_R;
3414 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003415}
3416
Willy Tarreau610ecce2010-01-04 21:15:02 +01003417/* Terminate current transaction and prepare a new one. This is very tricky
3418 * right now but it works.
3419 */
3420void http_end_txn_clean_session(struct session *s)
3421{
3422 /* FIXME: We need a more portable way of releasing a backend's and a
3423 * server's connections. We need a safer way to reinitialize buffer
3424 * flags. We also need a more accurate method for computing per-request
3425 * data.
3426 */
3427 http_silent_debug(__LINE__, s);
3428
3429 s->req->cons->flags |= SI_FL_NOLINGER;
3430 s->req->cons->shutr(s->req->cons);
3431 s->req->cons->shutw(s->req->cons);
3432
3433 http_silent_debug(__LINE__, s);
3434
3435 if (s->flags & SN_BE_ASSIGNED)
3436 s->be->beconn--;
3437
3438 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3439 session_process_counters(s);
3440
3441 if (s->txn.status) {
3442 int n;
3443
3444 n = s->txn.status / 100;
3445 if (n < 1 || n > 5)
3446 n = 0;
3447
3448 if (s->fe->mode == PR_MODE_HTTP)
3449 s->fe->counters.p.http.rsp[n]++;
3450
3451 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3452 (s->be->mode == PR_MODE_HTTP))
3453 s->be->counters.p.http.rsp[n]++;
3454 }
3455
3456 /* don't count other requests' data */
3457 s->logs.bytes_in -= s->req->l - s->req->send_max;
3458 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3459
3460 /* let's do a final log if we need it */
3461 if (s->logs.logwait &&
3462 !(s->flags & SN_MONITOR) &&
3463 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3464 s->do_log(s);
3465 }
3466
3467 s->logs.accept_date = date; /* user-visible date for logging */
3468 s->logs.tv_accept = now; /* corrected date for internal use */
3469 tv_zero(&s->logs.tv_request);
3470 s->logs.t_queue = -1;
3471 s->logs.t_connect = -1;
3472 s->logs.t_data = -1;
3473 s->logs.t_close = 0;
3474 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3475 s->logs.srv_queue_size = 0; /* we will get this number soon */
3476
3477 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3478 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3479
3480 if (s->pend_pos)
3481 pendconn_free(s->pend_pos);
3482
3483 if (s->srv) {
3484 if (s->flags & SN_CURR_SESS) {
3485 s->flags &= ~SN_CURR_SESS;
3486 s->srv->cur_sess--;
3487 }
3488 if (may_dequeue_tasks(s->srv, s->be))
3489 process_srv_queue(s->srv);
3490 }
3491
3492 if (unlikely(s->srv_conn))
3493 sess_change_server(s, NULL);
3494 s->srv = NULL;
3495
3496 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3497 s->req->cons->fd = -1; /* just to help with debugging */
3498 s->req->cons->err_type = SI_ET_NONE;
3499 s->req->cons->err_loc = NULL;
3500 s->req->cons->exp = TICK_ETERNITY;
3501 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003502 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3503 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003504 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3505 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3506 s->txn.meth = 0;
3507 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003508 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003509 if (s->be->options2 & PR_O2_INDEPSTR)
3510 s->req->cons->flags |= SI_FL_INDEP_STR;
3511
3512 /* if the request buffer is not empty, it means we're
3513 * about to process another request, so send pending
3514 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003515 * Just don't do this if the buffer is close to be full,
3516 * because the request will wait for it to flush a little
3517 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003518 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003519 if (s->req->l > s->req->send_max) {
3520 if (s->rep->send_max &&
3521 !(s->rep->flags & BF_FULL) &&
3522 s->rep->lr <= s->rep->r &&
3523 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3524 s->rep->flags |= BF_EXPECT_MORE;
3525 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003526
3527 /* we're removing the analysers, we MUST re-enable events detection */
3528 buffer_auto_read(s->req);
3529 buffer_auto_close(s->req);
3530 buffer_auto_read(s->rep);
3531 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003532
3533 /* make ->lr point to the first non-forwarded byte */
3534 s->req->lr = s->req->w + s->req->send_max;
3535 if (s->req->lr >= s->req->data + s->req->size)
3536 s->req->lr -= s->req->size;
3537 s->rep->lr = s->rep->w + s->rep->send_max;
3538 if (s->rep->lr >= s->rep->data + s->rep->size)
3539 s->rep->lr -= s->req->size;
3540
3541 s->req->analysers |= s->fe->fe_req_ana;
3542 s->rep->analysers = 0;
3543
3544 http_silent_debug(__LINE__, s);
3545}
3546
3547
3548/* This function updates the request state machine according to the response
3549 * state machine and buffer flags. It returns 1 if it changes anything (flag
3550 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3551 * it is only used to find when a request/response couple is complete. Both
3552 * this function and its equivalent should loop until both return zero. It
3553 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3554 */
3555int http_sync_req_state(struct session *s)
3556{
3557 struct buffer *buf = s->req;
3558 struct http_txn *txn = &s->txn;
3559 unsigned int old_flags = buf->flags;
3560 unsigned int old_state = txn->req.msg_state;
3561
3562 http_silent_debug(__LINE__, s);
3563 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3564 return 0;
3565
3566 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003567 /* No need to read anymore, the request was completely parsed.
3568 * We can shut the read side unless we want to abort_on_close.
3569 */
3570 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3571 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003572
3573 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3574 goto wait_other_side;
3575
3576 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3577 /* The server has not finished to respond, so we
3578 * don't want to move in order not to upset it.
3579 */
3580 goto wait_other_side;
3581 }
3582
3583 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3584 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003585 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003586 txn->req.msg_state = HTTP_MSG_TUNNEL;
3587 goto wait_other_side;
3588 }
3589
3590 /* When we get here, it means that both the request and the
3591 * response have finished receiving. Depending on the connection
3592 * mode, we'll have to wait for the last bytes to leave in either
3593 * direction, and sometimes for a close to be effective.
3594 */
3595
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003596 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3597 /* Server-close mode : queue a connection close to the server */
3598 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003599 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003600 }
3601 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3602 /* Option forceclose is set, or either side wants to close,
3603 * let's enforce it now that we're not expecting any new
3604 * data to come. The caller knows the session is complete
3605 * once both states are CLOSED.
3606 */
3607 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003608 buffer_shutr_now(buf);
3609 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003610 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003611 }
3612 else {
3613 /* The last possible modes are keep-alive and tunnel. Since tunnel
3614 * mode does not set the body analyser, we can't reach this place
3615 * in tunnel mode, so we're left with keep-alive only.
3616 * This mode is currently not implemented, we switch to tunnel mode.
3617 */
3618 buffer_auto_read(buf);
3619 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003620 }
3621
3622 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3623 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003624 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3625
Willy Tarreau610ecce2010-01-04 21:15:02 +01003626 if (!(buf->flags & BF_OUT_EMPTY)) {
3627 txn->req.msg_state = HTTP_MSG_CLOSING;
3628 goto http_msg_closing;
3629 }
3630 else {
3631 txn->req.msg_state = HTTP_MSG_CLOSED;
3632 goto http_msg_closed;
3633 }
3634 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003635 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003636 }
3637
3638 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3639 http_msg_closing:
3640 /* nothing else to forward, just waiting for the output buffer
3641 * to be empty and for the shutw_now to take effect.
3642 */
3643 if (buf->flags & BF_OUT_EMPTY) {
3644 txn->req.msg_state = HTTP_MSG_CLOSED;
3645 goto http_msg_closed;
3646 }
3647 else if (buf->flags & BF_SHUTW) {
3648 txn->req.msg_state = HTTP_MSG_ERROR;
3649 goto wait_other_side;
3650 }
3651 }
3652
3653 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3654 http_msg_closed:
3655 goto wait_other_side;
3656 }
3657
3658 wait_other_side:
3659 http_silent_debug(__LINE__, s);
3660 return txn->req.msg_state != old_state || buf->flags != old_flags;
3661}
3662
3663
3664/* This function updates the response state machine according to the request
3665 * state machine and buffer flags. It returns 1 if it changes anything (flag
3666 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3667 * it is only used to find when a request/response couple is complete. Both
3668 * this function and its equivalent should loop until both return zero. It
3669 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3670 */
3671int http_sync_res_state(struct session *s)
3672{
3673 struct buffer *buf = s->rep;
3674 struct http_txn *txn = &s->txn;
3675 unsigned int old_flags = buf->flags;
3676 unsigned int old_state = txn->rsp.msg_state;
3677
3678 http_silent_debug(__LINE__, s);
3679 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3680 return 0;
3681
3682 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3683 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003684 * still monitor the server connection for a possible close
3685 * while the request is being uploaded, so we don't disable
3686 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003687 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003688 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003689
3690 if (txn->req.msg_state == HTTP_MSG_ERROR)
3691 goto wait_other_side;
3692
3693 if (txn->req.msg_state < HTTP_MSG_DONE) {
3694 /* The client seems to still be sending data, probably
3695 * because we got an error response during an upload.
3696 * We have the choice of either breaking the connection
3697 * or letting it pass through. Let's do the later.
3698 */
3699 goto wait_other_side;
3700 }
3701
3702 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3703 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003704 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3706 goto wait_other_side;
3707 }
3708
3709 /* When we get here, it means that both the request and the
3710 * response have finished receiving. Depending on the connection
3711 * mode, we'll have to wait for the last bytes to leave in either
3712 * direction, and sometimes for a close to be effective.
3713 */
3714
3715 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3716 /* Server-close mode : shut read and wait for the request
3717 * side to close its output buffer. The caller will detect
3718 * when we're in DONE and the other is in CLOSED and will
3719 * catch that for the final cleanup.
3720 */
3721 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3722 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003723 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003724 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3725 /* Option forceclose is set, or either side wants to close,
3726 * let's enforce it now that we're not expecting any new
3727 * data to come. The caller knows the session is complete
3728 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003730 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3731 buffer_shutr_now(buf);
3732 buffer_shutw_now(buf);
3733 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003734 }
3735 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003736 /* The last possible modes are keep-alive and tunnel. Since tunnel
3737 * mode does not set the body analyser, we can't reach this place
3738 * in tunnel mode, so we're left with keep-alive only.
3739 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003740 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003741 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743 }
3744
3745 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3746 /* if we've just closed an output, let's switch */
3747 if (!(buf->flags & BF_OUT_EMPTY)) {
3748 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3749 goto http_msg_closing;
3750 }
3751 else {
3752 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3753 goto http_msg_closed;
3754 }
3755 }
3756 goto wait_other_side;
3757 }
3758
3759 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3760 http_msg_closing:
3761 /* nothing else to forward, just waiting for the output buffer
3762 * to be empty and for the shutw_now to take effect.
3763 */
3764 if (buf->flags & BF_OUT_EMPTY) {
3765 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3766 goto http_msg_closed;
3767 }
3768 else if (buf->flags & BF_SHUTW) {
3769 txn->rsp.msg_state = HTTP_MSG_ERROR;
3770 goto wait_other_side;
3771 }
3772 }
3773
3774 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3775 http_msg_closed:
3776 /* drop any pending data */
3777 buffer_ignore(buf, buf->l - buf->send_max);
3778 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003779 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780 goto wait_other_side;
3781 }
3782
3783 wait_other_side:
3784 http_silent_debug(__LINE__, s);
3785 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3786}
3787
3788
3789/* Resync the request and response state machines. Return 1 if either state
3790 * changes.
3791 */
3792int http_resync_states(struct session *s)
3793{
3794 struct http_txn *txn = &s->txn;
3795 int old_req_state = txn->req.msg_state;
3796 int old_res_state = txn->rsp.msg_state;
3797
3798 http_silent_debug(__LINE__, s);
3799 http_sync_req_state(s);
3800 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003801 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802 if (!http_sync_res_state(s))
3803 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003804 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 if (!http_sync_req_state(s))
3806 break;
3807 }
3808 http_silent_debug(__LINE__, s);
3809 /* OK, both state machines agree on a compatible state.
3810 * There are a few cases we're interested in :
3811 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3812 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3813 * directions, so let's simply disable both analysers.
3814 * - HTTP_MSG_CLOSED on the response only means we must abort the
3815 * request.
3816 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3817 * with server-close mode means we've completed one request and we
3818 * must re-initialize the server connection.
3819 */
3820
3821 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3822 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3823 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3824 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3825 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003826 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003827 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003828 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003829 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003830 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003831 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003832 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3833 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3834 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003835 s->rep->analysers = 0;
3836 buffer_auto_close(s->rep);
3837 buffer_auto_read(s->rep);
3838 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003839 buffer_abort(s->req);
3840 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003841 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003842 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003843 }
3844 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3845 txn->rsp.msg_state == HTTP_MSG_DONE &&
3846 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3847 /* server-close: terminate this server connection and
3848 * reinitialize a fresh-new transaction.
3849 */
3850 http_end_txn_clean_session(s);
3851 }
3852
3853 http_silent_debug(__LINE__, s);
3854 return txn->req.msg_state != old_req_state ||
3855 txn->rsp.msg_state != old_res_state;
3856}
3857
Willy Tarreaud98cf932009-12-27 22:54:55 +01003858/* This function is an analyser which forwards request body (including chunk
3859 * sizes if any). It is called as soon as we must forward, even if we forward
3860 * zero byte. The only situation where it must not be called is when we're in
3861 * tunnel mode and we want to forward till the close. It's used both to forward
3862 * remaining data and to resync after end of body. It expects the msg_state to
3863 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3864 * read more data, or 1 once we can go on with next request or end the session.
3865 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3866 * bytes of pending data + the headers if not already done (between som and sov).
3867 * It eventually adjusts som to match sov after the data in between have been sent.
3868 */
3869int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3870{
3871 struct http_txn *txn = &s->txn;
3872 struct http_msg *msg = &s->txn.req;
3873
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003874 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3875 return 0;
3876
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003877 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3878 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3879 /* Output closed while we were sending data. We must abort. */
3880 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003881 buffer_auto_read(req);
3882 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003883 req->analysers &= ~an_bit;
3884 return 1;
3885 }
3886
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003887 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003888
3889 /* Note that we don't have to send 100-continue back because we don't
3890 * need the data to complete our job, and it's up to the server to
3891 * decide whether to return 100, 417 or anything else in return of
3892 * an "Expect: 100-continue" header.
3893 */
3894
3895 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3896 /* we have msg->col and msg->sov which both point to the first
3897 * byte of message body. msg->som still points to the beginning
3898 * of the message. We must save the body in req->lr because it
3899 * survives buffer re-alignments.
3900 */
3901 req->lr = req->data + msg->sov;
3902 if (txn->flags & TX_REQ_TE_CHNK)
3903 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3904 else {
3905 msg->msg_state = HTTP_MSG_DATA;
3906 }
3907 }
3908
Willy Tarreaud98cf932009-12-27 22:54:55 +01003909 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003911 /* we may have some data pending */
3912 if (msg->hdr_content_len || msg->som != msg->sov) {
3913 int bytes = msg->sov - msg->som;
3914 if (bytes < 0) /* sov may have wrapped at the end */
3915 bytes += req->size;
3916 buffer_forward(req, bytes + msg->hdr_content_len);
3917 msg->hdr_content_len = 0; /* don't forward that again */
3918 msg->som = msg->sov;
3919 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003920
Willy Tarreaucaabe412010-01-03 23:08:28 +01003921 if (msg->msg_state == HTTP_MSG_DATA) {
3922 /* must still forward */
3923 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003924 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003925
3926 /* nothing left to forward */
3927 if (txn->flags & TX_REQ_TE_CHNK)
3928 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003930 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003931 }
3932 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003933 /* read the chunk size and assign it to ->hdr_content_len, then
3934 * set ->sov and ->lr to point to the body and switch to DATA or
3935 * TRAILERS state.
3936 */
3937 int ret = http_parse_chunk_size(req, msg);
3938
3939 if (!ret)
3940 goto missing_data;
3941 else if (ret < 0)
3942 goto return_bad_req;
3943 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003944 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003945 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3946 /* we want the CRLF after the data */
3947 int ret;
3948
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003949 req->lr = req->w + req->send_max;
3950 if (req->lr >= req->data + req->size)
3951 req->lr -= req->size;
3952
Willy Tarreaud98cf932009-12-27 22:54:55 +01003953 ret = http_skip_chunk_crlf(req, msg);
3954
3955 if (ret == 0)
3956 goto missing_data;
3957 else if (ret < 0)
3958 goto return_bad_req;
3959 /* we're in MSG_CHUNK_SIZE now */
3960 }
3961 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3962 int ret = http_forward_trailers(req, msg);
3963
3964 if (ret == 0)
3965 goto missing_data;
3966 else if (ret < 0)
3967 goto return_bad_req;
3968 /* we're in HTTP_MSG_DONE now */
3969 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003970 else {
3971 /* other states, DONE...TUNNEL */
3972 if (http_resync_states(s)) {
3973 /* some state changes occurred, maybe the analyser
3974 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003975 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003976 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3977 goto return_bad_req;
3978 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003979 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003981 }
3982 }
3983
Willy Tarreaud98cf932009-12-27 22:54:55 +01003984 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003985 /* stop waiting for data if the input is closed before the end */
3986 if (req->flags & BF_SHUTR)
3987 goto return_bad_req;
3988
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003989 /* waiting for the last bits to leave the buffer */
3990 if (req->flags & BF_SHUTW)
3991 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003992
3993 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003994 return 0;
3995
Willy Tarreaud98cf932009-12-27 22:54:55 +01003996 return_bad_req: /* let's centralize all bad requests */
3997 txn->req.msg_state = HTTP_MSG_ERROR;
3998 txn->status = 400;
3999 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004000 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004001 req->analysers = 0;
4002 s->fe->counters.failed_req++;
4003 if (s->listener->counters)
4004 s->listener->counters->failed_req++;
4005
4006 if (!(s->flags & SN_ERR_MASK))
4007 s->flags |= SN_ERR_PRXCOND;
4008 if (!(s->flags & SN_FINST_MASK))
4009 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004011 return 0;
4012}
4013
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004014/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4015 * processing can continue on next analysers, or zero if it either needs more
4016 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4017 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4018 * when it has nothing left to do, and may remove any analyser when it wants to
4019 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004020 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004021int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004022{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004023 struct http_txn *txn = &s->txn;
4024 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004025 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004026 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004027 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004028 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004029
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004030 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 +02004031 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004032 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004033 rep,
4034 rep->rex, rep->wex,
4035 rep->flags,
4036 rep->l,
4037 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004039 /*
4040 * Now parse the partial (or complete) lines.
4041 * We will check the response syntax, and also join multi-line
4042 * headers. An index of all the lines will be elaborated while
4043 * parsing.
4044 *
4045 * For the parsing, we use a 28 states FSM.
4046 *
4047 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004048 * rep->data + msg->som = beginning of response
4049 * rep->data + msg->eoh = end of processed headers / start of current one
4050 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004051 * rep->lr = first non-visited byte
4052 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004053 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004054 */
4055
Willy Tarreau83e3af02009-12-28 17:39:57 +01004056 /* There's a protected area at the end of the buffer for rewriting
4057 * purposes. We don't want to start to parse the request if the
4058 * protected area is affected, because we may have to move processed
4059 * data later, which is much more complicated.
4060 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004061 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4062 if (unlikely((rep->flags & BF_FULL) ||
4063 rep->r < rep->lr ||
4064 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4065 if (rep->send_max) {
4066 /* some data has still not left the buffer, wake us once that's done */
4067 buffer_dont_close(rep);
4068 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4069 return 0;
4070 }
4071 if (rep->l <= rep->size - global.tune.maxrewrite)
4072 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004073 }
4074
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004075 if (likely(rep->lr < rep->r))
4076 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004077 }
4078
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004079 /* 1: we might have to print this header in debug mode */
4080 if (unlikely((global.mode & MODE_DEBUG) &&
4081 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004082 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004083 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004084
Willy Tarreau962c3f42010-01-10 00:15:35 +01004085 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004086 eol = sol + msg->sl.rq.l;
4087 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004089 sol += hdr_idx_first_pos(&txn->hdr_idx);
4090 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004091
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004092 while (cur_idx) {
4093 eol = sol + txn->hdr_idx.v[cur_idx].len;
4094 debug_hdr("srvhdr", s, sol, eol);
4095 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4096 cur_idx = txn->hdr_idx.v[cur_idx].next;
4097 }
4098 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004099
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004100 /*
4101 * Now we quickly check if we have found a full valid response.
4102 * If not so, we check the FD and buffer states before leaving.
4103 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004104 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004105 * responses are checked first.
4106 *
4107 * Depending on whether the client is still there or not, we
4108 * may send an error response back or not. Note that normally
4109 * we should only check for HTTP status there, and check I/O
4110 * errors somewhere else.
4111 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004112
Willy Tarreau655dce92009-11-08 13:10:58 +01004113 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004114 /* Invalid response */
4115 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4116 /* we detected a parsing error. We want to archive this response
4117 * in the dedicated proxy area for later troubleshooting.
4118 */
4119 hdr_response_bad:
4120 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4121 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4122
4123 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004124 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004125 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004126 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4127 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004128
Willy Tarreau90deb182010-01-07 00:20:41 +01004129 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004130 rep->analysers = 0;
4131 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004132 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004133 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4134
4135 if (!(s->flags & SN_ERR_MASK))
4136 s->flags |= SN_ERR_PRXCOND;
4137 if (!(s->flags & SN_FINST_MASK))
4138 s->flags |= SN_FINST_H;
4139
4140 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004141 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004142
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004143 /* too large response does not fit in buffer. */
4144 else if (rep->flags & BF_FULL) {
4145 goto hdr_response_bad;
4146 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004147
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004148 /* read error */
4149 else if (rep->flags & BF_READ_ERROR) {
4150 if (msg->err_pos >= 0)
4151 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004153 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004154 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004155 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004156 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4157 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004158
Willy Tarreau90deb182010-01-07 00:20:41 +01004159 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004160 rep->analysers = 0;
4161 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004162 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004163 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004164
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004165 if (!(s->flags & SN_ERR_MASK))
4166 s->flags |= SN_ERR_SRVCL;
4167 if (!(s->flags & SN_FINST_MASK))
4168 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004169 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004170 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004171
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004172 /* read timeout : return a 504 to the client. */
4173 else if (rep->flags & BF_READ_TIMEOUT) {
4174 if (msg->err_pos >= 0)
4175 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004176
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004177 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004178 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004179 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004180 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4181 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004182
Willy Tarreau90deb182010-01-07 00:20:41 +01004183 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004184 rep->analysers = 0;
4185 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004186 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004187 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004189 if (!(s->flags & SN_ERR_MASK))
4190 s->flags |= SN_ERR_SRVTO;
4191 if (!(s->flags & SN_FINST_MASK))
4192 s->flags |= SN_FINST_H;
4193 return 0;
4194 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004195
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004196 /* close from server */
4197 else if (rep->flags & BF_SHUTR) {
4198 if (msg->err_pos >= 0)
4199 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004200
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004201 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004202 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004203 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004204 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4205 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004206
Willy Tarreau90deb182010-01-07 00:20:41 +01004207 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004208 rep->analysers = 0;
4209 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004210 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004211 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004212
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004213 if (!(s->flags & SN_ERR_MASK))
4214 s->flags |= SN_ERR_SRVCL;
4215 if (!(s->flags & SN_FINST_MASK))
4216 s->flags |= SN_FINST_H;
4217 return 0;
4218 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004219
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004220 /* write error to client (we don't send any message then) */
4221 else if (rep->flags & BF_WRITE_ERROR) {
4222 if (msg->err_pos >= 0)
4223 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004224
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004225 s->be->counters.failed_resp++;
4226 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004227 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004228
4229 if (!(s->flags & SN_ERR_MASK))
4230 s->flags |= SN_ERR_CLICL;
4231 if (!(s->flags & SN_FINST_MASK))
4232 s->flags |= SN_FINST_H;
4233
4234 /* process_session() will take care of the error */
4235 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004236 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004237
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004238 buffer_dont_close(rep);
4239 return 0;
4240 }
4241
4242 /* More interesting part now : we know that we have a complete
4243 * response which at least looks like HTTP. We have an indicator
4244 * of each header's length, so we can parse them quickly.
4245 */
4246
4247 if (unlikely(msg->err_pos >= 0))
4248 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4249
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004250 /*
4251 * 1: get the status code
4252 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004253 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004254 if (n < 1 || n > 5)
4255 n = 0;
4256 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004257
Willy Tarreau5b154472009-12-21 20:11:07 +01004258 /* check if the response is HTTP/1.1 or above */
4259 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004260 ((msg->sol[5] > '1') ||
4261 ((msg->sol[5] == '1') &&
4262 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004263 txn->flags |= TX_RES_VER_11;
4264
4265 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004266 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004267
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004268 /* transfer length unknown*/
4269 txn->flags &= ~TX_RES_XFER_LEN;
4270
Willy Tarreau962c3f42010-01-10 00:15:35 +01004271 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004272
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004273 if (txn->status >= 100 && txn->status < 500)
4274 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4275 else
4276 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4277
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004278 /*
4279 * 2: check for cacheability.
4280 */
4281
4282 switch (txn->status) {
4283 case 200:
4284 case 203:
4285 case 206:
4286 case 300:
4287 case 301:
4288 case 410:
4289 /* RFC2616 @13.4:
4290 * "A response received with a status code of
4291 * 200, 203, 206, 300, 301 or 410 MAY be stored
4292 * by a cache (...) unless a cache-control
4293 * directive prohibits caching."
4294 *
4295 * RFC2616 @9.5: POST method :
4296 * "Responses to this method are not cacheable,
4297 * unless the response includes appropriate
4298 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004299 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004300 if (likely(txn->meth != HTTP_METH_POST) &&
4301 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4302 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4303 break;
4304 default:
4305 break;
4306 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004307
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004308 /*
4309 * 3: we may need to capture headers
4310 */
4311 s->logs.logwait &= ~LW_RESP;
4312 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004313 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004314 txn->rsp.cap, s->fe->rsp_cap);
4315
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004316 /* 4: determine the transfer-length.
4317 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4318 * the presence of a message-body in a RESPONSE and its transfer length
4319 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004320 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004321 * All responses to the HEAD request method MUST NOT include a
4322 * message-body, even though the presence of entity-header fields
4323 * might lead one to believe they do. All 1xx (informational), 204
4324 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4325 * message-body. All other responses do include a message-body,
4326 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004327 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004328 * 1. Any response which "MUST NOT" include a message-body (such as the
4329 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4330 * always terminated by the first empty line after the header fields,
4331 * regardless of the entity-header fields present in the message.
4332 *
4333 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4334 * the "chunked" transfer-coding (Section 6.2) is used, the
4335 * transfer-length is defined by the use of this transfer-coding.
4336 * If a Transfer-Encoding header field is present and the "chunked"
4337 * transfer-coding is not present, the transfer-length is defined by
4338 * the sender closing the connection.
4339 *
4340 * 3. If a Content-Length header field is present, its decimal value in
4341 * OCTETs represents both the entity-length and the transfer-length.
4342 * If a message is received with both a Transfer-Encoding header
4343 * field and a Content-Length header field, the latter MUST be ignored.
4344 *
4345 * 4. If the message uses the media type "multipart/byteranges", and
4346 * the transfer-length is not otherwise specified, then this self-
4347 * delimiting media type defines the transfer-length. This media
4348 * type MUST NOT be used unless the sender knows that the recipient
4349 * can parse it; the presence in a request of a Range header with
4350 * multiple byte-range specifiers from a 1.1 client implies that the
4351 * client can parse multipart/byteranges responses.
4352 *
4353 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004354 */
4355
4356 /* Skip parsing if no content length is possible. The response flags
4357 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004358 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004359 * FIXME: should we parse anyway and return an error on chunked encoding ?
4360 */
4361 if (txn->meth == HTTP_METH_HEAD ||
4362 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004363 txn->status == 204 || txn->status == 304) {
4364 txn->flags |= TX_RES_XFER_LEN;
4365 goto skip_content_length;
4366 }
4367
4368 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004369 goto skip_content_length;
4370
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004371 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004372 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004373 while ((txn->flags & TX_RES_VER_11) &&
4374 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004375 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4376 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4377 else if (txn->flags & TX_RES_TE_CHNK) {
4378 /* bad transfer-encoding (chunked followed by something else) */
4379 use_close_only = 1;
4380 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4381 break;
4382 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004383 }
4384
4385 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4386 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004387 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004388 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4389 signed long long cl;
4390
4391 if (!ctx.vlen)
4392 goto hdr_response_bad;
4393
4394 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4395 goto hdr_response_bad; /* parse failure */
4396
4397 if (cl < 0)
4398 goto hdr_response_bad;
4399
4400 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4401 goto hdr_response_bad; /* already specified, was different */
4402
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004403 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004404 msg->hdr_content_len = cl;
4405 }
4406
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004407 /* FIXME: we should also implement the multipart/byterange method.
4408 * For now on, we resort to close mode in this case (unknown length).
4409 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004410skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004411
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004412 /* end of job, return OK */
4413 rep->analysers &= ~an_bit;
4414 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004415 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004416 return 1;
4417}
4418
4419/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004420 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4421 * and updates t->rep->analysers. It might make sense to explode it into several
4422 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004423 */
4424int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4425{
4426 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004427 struct http_msg *msg = &txn->rsp;
4428 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004429 struct wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004430
4431 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4432 now_ms, __FUNCTION__,
4433 t,
4434 rep,
4435 rep->rex, rep->wex,
4436 rep->flags,
4437 rep->l,
4438 rep->analysers);
4439
Willy Tarreau655dce92009-11-08 13:10:58 +01004440 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004441 return 0;
4442
4443 rep->analysers &= ~an_bit;
4444 rep->analyse_exp = TICK_ETERNITY;
4445
Willy Tarreau5b154472009-12-21 20:11:07 +01004446 /* Now we have to check if we need to modify the Connection header.
4447 * This is more difficult on the response than it is on the request,
4448 * because we can have two different HTTP versions and we don't know
4449 * how the client will interprete a response. For instance, let's say
4450 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4451 * HTTP/1.1 response without any header. Maybe it will bound itself to
4452 * HTTP/1.0 because it only knows about it, and will consider the lack
4453 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4454 * the lack of header as a keep-alive. Thus we will use two flags
4455 * indicating how a request MAY be understood by the client. In case
4456 * of multiple possibilities, we'll fix the header to be explicit. If
4457 * ambiguous cases such as both close and keepalive are seen, then we
4458 * will fall back to explicit close. Note that we won't take risks with
4459 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004460 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004461 */
4462
4463 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004464 (txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004465 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4466 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004467 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004468
Willy Tarreau60466522010-01-18 19:08:45 +01004469 /* on unknown transfer length, we must close */
4470 if (!(txn->flags & TX_RES_XFER_LEN) &&
4471 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4472 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004473
Willy Tarreau60466522010-01-18 19:08:45 +01004474 /* now adjust header transformations depending on current state */
4475 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4476 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4477 to_del |= 2; /* remove "keep-alive" on any response */
4478 if (!(txn->flags & TX_RES_VER_11))
4479 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004480 }
Willy Tarreau60466522010-01-18 19:08:45 +01004481 else { /* SCL / KAL */
4482 to_del |= 1; /* remove "close" on any response */
4483 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4484 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004485 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004486
Willy Tarreau60466522010-01-18 19:08:45 +01004487 /* Parse and remove some headers from the connection header */
4488 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004489
Willy Tarreau60466522010-01-18 19:08:45 +01004490 /* Some keep-alive responses are converted to Server-close if
4491 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004492 */
Willy Tarreau60466522010-01-18 19:08:45 +01004493 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4494 if ((txn->flags & TX_HDR_CONN_CLO) ||
4495 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4496 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004497 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004498 }
4499
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004500 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004501 /*
4502 * 3: we will have to evaluate the filters.
4503 * As opposed to version 1.2, now they will be evaluated in the
4504 * filters order and not in the header order. This means that
4505 * each filter has to be validated among all headers.
4506 *
4507 * Filters are tried with ->be first, then with ->fe if it is
4508 * different from ->be.
4509 */
4510
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004511 cur_proxy = t->be;
4512 while (1) {
4513 struct proxy *rule_set = cur_proxy;
4514
4515 /* try headers filters */
4516 if (rule_set->rsp_exp != NULL) {
4517 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4518 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004519 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004520 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004521 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4522 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004523 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004524 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004525 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004526 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004527 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004528 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004529 if (!(t->flags & SN_ERR_MASK))
4530 t->flags |= SN_ERR_PRXCOND;
4531 if (!(t->flags & SN_FINST_MASK))
4532 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004533 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004534 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004535 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004536
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004537 /* has the response been denied ? */
4538 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004539 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004540 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004541
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004542 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004543 if (t->listener->counters)
4544 t->listener->counters->denied_resp++;
4545
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004546 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004547 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004548
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004549 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004550 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004551 if (txn->status < 200)
4552 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004553 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004554 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004555 }
4556
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004557 /* check whether we're already working on the frontend */
4558 if (cur_proxy == t->fe)
4559 break;
4560 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004561 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004562
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004563 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004564 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4565 * in which case this is not the right response, and we're waiting for the
4566 * next one. Let's allow this response to go to the client and wait for the
4567 * next one.
4568 */
4569 if (txn->status < 200) {
4570 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004571 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004572 msg->msg_state = HTTP_MSG_RPBEFORE;
4573 txn->status = 0;
4574 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4575 return 1;
4576 }
4577
4578 /* we don't have any 1xx status code now */
4579
4580 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004581 * 4: check for server cookie.
4582 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004583 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4584 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004586
Willy Tarreaubaaee002006-06-26 02:48:02 +02004587
Willy Tarreaua15645d2007-03-18 16:22:39 +01004588 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004590 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004591 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004592 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004593
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004594 /*
4595 * 6: add server cookie in the response if needed
4596 */
4597 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004598 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004599 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004600
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004601 /* the server is known, it's not the one the client requested, we have to
4602 * insert a set-cookie here, except if we want to insert only on POST
4603 * requests and this one isn't. Note that servers which don't have cookies
4604 * (eg: some backup servers) will return a full cookie removal request.
4605 */
4606 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4607 t->be->cookie_name,
4608 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004609
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004610 if (t->be->cookie_domain)
4611 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004612
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004614 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004615 goto return_bad_resp;
4616 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004617
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004618 /* Here, we will tell an eventual cache on the client side that we don't
4619 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4620 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4621 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4622 */
4623 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004624
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004625 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4626
4627 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004628 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004629 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004630 }
4631 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004632
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004633 /*
4634 * 7: check if result will be cacheable with a cookie.
4635 * We'll block the response if security checks have caught
4636 * nasty things such as a cacheable cookie.
4637 */
4638 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4639 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004640 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004641
4642 /* we're in presence of a cacheable response containing
4643 * a set-cookie header. We'll block it as requested by
4644 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004645 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004646 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004647 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004648
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004649 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004650 if (t->listener->counters)
4651 t->listener->counters->denied_resp++;
4652
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004653 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4654 t->be->id, t->srv?t->srv->id:"<dispatch>");
4655 send_log(t->be, LOG_ALERT,
4656 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4657 t->be->id, t->srv?t->srv->id:"<dispatch>");
4658 goto return_srv_prx_502;
4659 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004660
4661 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004662 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004663 */
Willy Tarreau60466522010-01-18 19:08:45 +01004664 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4665 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4666 unsigned int want_flags = 0;
4667
4668 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4669 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4670 /* we want a keep-alive response here. Keep-alive header
4671 * required if either side is not 1.1.
4672 */
4673 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4674 want_flags |= TX_CON_KAL_SET;
4675 }
4676 else {
4677 /* we want a close response here. Close header required if
4678 * the server is 1.1, regardless of the client.
4679 */
4680 if (txn->flags & TX_RES_VER_11)
4681 want_flags |= TX_CON_CLO_SET;
4682 }
4683
4684 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4685 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004686 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004687
Willy Tarreaud98cf932009-12-27 22:54:55 +01004688 if (txn->flags & TX_RES_XFER_LEN)
4689 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004690
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004691 /*************************************************************
4692 * OK, that's finished for the headers. We have done what we *
4693 * could. Let's switch to the DATA state. *
4694 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004695
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004696 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004697
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004698 /* if the user wants to log as soon as possible, without counting
4699 * bytes from the server, then this is the right moment. We have
4700 * to temporarily assign bytes_out to log what we currently have.
4701 */
4702 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4703 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4704 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004705 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004706 t->logs.bytes_out = 0;
4707 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004708
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004709 /* Note: we must not try to cheat by jumping directly to DATA,
4710 * otherwise we would not let the client side wake up.
4711 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004712
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004713 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004714 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004715 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004716}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004717
Willy Tarreaud98cf932009-12-27 22:54:55 +01004718/* This function is an analyser which forwards response body (including chunk
4719 * sizes if any). It is called as soon as we must forward, even if we forward
4720 * zero byte. The only situation where it must not be called is when we're in
4721 * tunnel mode and we want to forward till the close. It's used both to forward
4722 * remaining data and to resync after end of body. It expects the msg_state to
4723 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4724 * read more data, or 1 once we can go on with next request or end the session.
4725 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4726 * bytes of pending data + the headers if not already done (between som and sov).
4727 * It eventually adjusts som to match sov after the data in between have been sent.
4728 */
4729int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4730{
4731 struct http_txn *txn = &s->txn;
4732 struct http_msg *msg = &s->txn.rsp;
4733
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004734 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4735 return 0;
4736
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004737 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004738 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004739 !s->req->analysers) {
4740 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4741 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004742 buffer_auto_read(res);
4743 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004744 res->analysers &= ~an_bit;
4745 return 1;
4746 }
4747
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004748 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004749
Willy Tarreaud98cf932009-12-27 22:54:55 +01004750 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4751 /* we have msg->col and msg->sov which both point to the first
4752 * byte of message body. msg->som still points to the beginning
4753 * of the message. We must save the body in req->lr because it
4754 * survives buffer re-alignments.
4755 */
4756 res->lr = res->data + msg->sov;
4757 if (txn->flags & TX_RES_TE_CHNK)
4758 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4759 else {
4760 msg->msg_state = HTTP_MSG_DATA;
4761 }
4762 }
4763
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004765 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004766 /* we may have some data pending */
4767 if (msg->hdr_content_len || msg->som != msg->sov) {
4768 int bytes = msg->sov - msg->som;
4769 if (bytes < 0) /* sov may have wrapped at the end */
4770 bytes += res->size;
4771 buffer_forward(res, bytes + msg->hdr_content_len);
4772 msg->hdr_content_len = 0; /* don't forward that again */
4773 msg->som = msg->sov;
4774 }
4775
Willy Tarreaucaabe412010-01-03 23:08:28 +01004776 if (msg->msg_state == HTTP_MSG_DATA) {
4777 /* must still forward */
4778 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004779 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004780
4781 /* nothing left to forward */
4782 if (txn->flags & TX_RES_TE_CHNK)
4783 msg->msg_state = HTTP_MSG_DATA_CRLF;
4784 else
4785 msg->msg_state = HTTP_MSG_DONE;
4786 }
4787 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004788 /* read the chunk size and assign it to ->hdr_content_len, then
4789 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4790 */
4791 int ret = http_parse_chunk_size(res, msg);
4792
4793 if (!ret)
4794 goto missing_data;
4795 else if (ret < 0)
4796 goto return_bad_res;
4797 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004798 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004799 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4800 /* we want the CRLF after the data */
4801 int ret;
4802
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004803 res->lr = res->w + res->send_max;
4804 if (res->lr >= res->data + res->size)
4805 res->lr -= res->size;
4806
Willy Tarreaud98cf932009-12-27 22:54:55 +01004807 ret = http_skip_chunk_crlf(res, msg);
4808
4809 if (!ret)
4810 goto missing_data;
4811 else if (ret < 0)
4812 goto return_bad_res;
4813 /* we're in MSG_CHUNK_SIZE now */
4814 }
4815 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4816 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004817
Willy Tarreaud98cf932009-12-27 22:54:55 +01004818 if (ret == 0)
4819 goto missing_data;
4820 else if (ret < 0)
4821 goto return_bad_res;
4822 /* we're in HTTP_MSG_DONE now */
4823 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004824 else {
4825 /* other states, DONE...TUNNEL */
4826 if (http_resync_states(s)) {
4827 http_silent_debug(__LINE__, s);
4828 /* some state changes occurred, maybe the analyser
4829 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004830 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4832 goto return_bad_res;
4833 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004834 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004835 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004836 }
4837 }
4838
Willy Tarreaud98cf932009-12-27 22:54:55 +01004839 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004840 /* stop waiting for data if the input is closed before the end */
4841 if (res->flags & BF_SHUTR)
4842 goto return_bad_res;
4843
Willy Tarreau610ecce2010-01-04 21:15:02 +01004844 if (!s->req->analysers)
4845 goto return_bad_res;
4846
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847 /* forward the chunk size as well as any pending data */
4848 if (msg->hdr_content_len || msg->som != msg->sov) {
4849 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4850 msg->hdr_content_len = 0; /* don't forward that again */
4851 msg->som = msg->sov;
4852 }
4853
Willy Tarreaud98cf932009-12-27 22:54:55 +01004854 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856 return 0;
4857
4858 return_bad_res: /* let's centralize all bad resuests */
4859 txn->rsp.msg_state = HTTP_MSG_ERROR;
4860 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01004861 /* don't send any error message as we're in the body */
4862 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004863 res->analysers = 0;
4864 s->be->counters.failed_resp++;
4865 if (s->srv) {
4866 s->srv->counters.failed_resp++;
4867 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4868 }
4869
4870 if (!(s->flags & SN_ERR_MASK))
4871 s->flags |= SN_ERR_PRXCOND;
4872 if (!(s->flags & SN_FINST_MASK))
4873 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004874 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004875 return 0;
4876}
4877
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004878/* Iterate the same filter through all request headers.
4879 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004880 * Since it can manage the switch to another backend, it updates the per-proxy
4881 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004882 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004883int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004884{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004885 char term;
4886 char *cur_ptr, *cur_end, *cur_next;
4887 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004888 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004889 struct hdr_idx_elem *cur_hdr;
4890 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004892 last_hdr = 0;
4893
Willy Tarreau962c3f42010-01-10 00:15:35 +01004894 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 old_idx = 0;
4896
4897 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004898 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004899 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004900 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004901 (exp->action == ACT_ALLOW ||
4902 exp->action == ACT_DENY ||
4903 exp->action == ACT_TARPIT))
4904 return 0;
4905
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004906 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004907 if (!cur_idx)
4908 break;
4909
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004910 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004911 cur_ptr = cur_next;
4912 cur_end = cur_ptr + cur_hdr->len;
4913 cur_next = cur_end + cur_hdr->cr + 1;
4914
4915 /* Now we have one header between cur_ptr and cur_end,
4916 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004917 */
4918
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004919 /* The annoying part is that pattern matching needs
4920 * that we modify the contents to null-terminate all
4921 * strings before testing them.
4922 */
4923
4924 term = *cur_end;
4925 *cur_end = '\0';
4926
4927 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4928 switch (exp->action) {
4929 case ACT_SETBE:
4930 /* It is not possible to jump a second time.
4931 * FIXME: should we return an HTTP/500 here so that
4932 * the admin knows there's a problem ?
4933 */
4934 if (t->be != t->fe)
4935 break;
4936
4937 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004938 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004939 last_hdr = 1;
4940 break;
4941
4942 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004943 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004944 last_hdr = 1;
4945 break;
4946
4947 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004948 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004949 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004950
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004951 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004952 if (t->listener->counters)
4953 t->listener->counters->denied_resp++;
4954
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004955 break;
4956
4957 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004958 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004959 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004960
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004961 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004962 if (t->listener->counters)
4963 t->listener->counters->denied_resp++;
4964
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004965 break;
4966
4967 case ACT_REPLACE:
4968 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4969 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4970 /* FIXME: if the user adds a newline in the replacement, the
4971 * index will not be recalculated for now, and the new line
4972 * will not be counted as a new header.
4973 */
4974
4975 cur_end += delta;
4976 cur_next += delta;
4977 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004978 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004979 break;
4980
4981 case ACT_REMOVE:
4982 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4983 cur_next += delta;
4984
Willy Tarreaufa355d42009-11-29 18:12:29 +01004985 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004986 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4987 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004988 cur_hdr->len = 0;
4989 cur_end = NULL; /* null-term has been rewritten */
4990 break;
4991
4992 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004993 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004994 if (cur_end)
4995 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004997 /* keep the link from this header to next one in case of later
4998 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004999 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005000 old_idx = cur_idx;
5001 }
5002 return 0;
5003}
5004
5005
5006/* Apply the filter to the request line.
5007 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5008 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005009 * Since it can manage the switch to another backend, it updates the per-proxy
5010 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005011 */
5012int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5013{
5014 char term;
5015 char *cur_ptr, *cur_end;
5016 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005017 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005018 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005019
Willy Tarreau58f10d72006-12-04 02:26:12 +01005020
Willy Tarreau3d300592007-03-18 18:34:41 +01005021 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005022 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005023 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005024 (exp->action == ACT_ALLOW ||
5025 exp->action == ACT_DENY ||
5026 exp->action == ACT_TARPIT))
5027 return 0;
5028 else if (exp->action == ACT_REMOVE)
5029 return 0;
5030
5031 done = 0;
5032
Willy Tarreau962c3f42010-01-10 00:15:35 +01005033 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005034 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005035
5036 /* Now we have the request line between cur_ptr and cur_end */
5037
5038 /* The annoying part is that pattern matching needs
5039 * that we modify the contents to null-terminate all
5040 * strings before testing them.
5041 */
5042
5043 term = *cur_end;
5044 *cur_end = '\0';
5045
5046 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5047 switch (exp->action) {
5048 case ACT_SETBE:
5049 /* It is not possible to jump a second time.
5050 * FIXME: should we return an HTTP/500 here so that
5051 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005052 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005053 if (t->be != t->fe)
5054 break;
5055
5056 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005057 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005058 done = 1;
5059 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005061 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005062 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005063 done = 1;
5064 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005066 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005067 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005068
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005069 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005070 if (t->listener->counters)
5071 t->listener->counters->denied_resp++;
5072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005073 done = 1;
5074 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005076 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005077 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005078
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005079 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005080 if (t->listener->counters)
5081 t->listener->counters->denied_resp++;
5082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005083 done = 1;
5084 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005086 case ACT_REPLACE:
5087 *cur_end = term; /* restore the string terminator */
5088 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5089 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5090 /* FIXME: if the user adds a newline in the replacement, the
5091 * index will not be recalculated for now, and the new line
5092 * will not be counted as a new header.
5093 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005094
Willy Tarreaufa355d42009-11-29 18:12:29 +01005095 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005096 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005097 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005098 HTTP_MSG_RQMETH,
5099 cur_ptr, cur_end + 1,
5100 NULL, NULL);
5101 if (unlikely(!cur_end))
5102 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005104 /* we have a full request and we know that we have either a CR
5105 * or an LF at <ptr>.
5106 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005107 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5108 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005109 /* there is no point trying this regex on headers */
5110 return 1;
5111 }
5112 }
5113 *cur_end = term; /* restore the string terminator */
5114 return done;
5115}
Willy Tarreau97de6242006-12-27 17:18:38 +01005116
Willy Tarreau58f10d72006-12-04 02:26:12 +01005117
Willy Tarreau58f10d72006-12-04 02:26:12 +01005118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005119/*
5120 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5121 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005122 * unparsable request. Since it can manage the switch to another backend, it
5123 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005124 */
5125int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5126{
Willy Tarreau3d300592007-03-18 18:34:41 +01005127 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005128 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005129 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005130 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005132 /*
5133 * The interleaving of transformations and verdicts
5134 * makes it difficult to decide to continue or stop
5135 * the evaluation.
5136 */
5137
Willy Tarreau3d300592007-03-18 18:34:41 +01005138 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005139 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5140 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5141 exp = exp->next;
5142 continue;
5143 }
5144
5145 /* Apply the filter to the request line. */
5146 ret = apply_filter_to_req_line(t, req, exp);
5147 if (unlikely(ret < 0))
5148 return -1;
5149
5150 if (likely(ret == 0)) {
5151 /* The filter did not match the request, it can be
5152 * iterated through all headers.
5153 */
5154 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005155 }
5156 exp = exp->next;
5157 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005158 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005159}
5160
5161
Willy Tarreaua15645d2007-03-18 16:22:39 +01005162
Willy Tarreau58f10d72006-12-04 02:26:12 +01005163/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005164 * Try to retrieve the server associated to the appsession.
5165 * If the server is found, it's assigned to the session.
5166 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005167void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005168 struct http_txn *txn = &t->txn;
5169 appsess *asession = NULL;
5170 char *sessid_temp = NULL;
5171
Cyril Bontéb21570a2009-11-29 20:04:48 +01005172 if (len > t->be->appsession_len) {
5173 len = t->be->appsession_len;
5174 }
5175
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005176 if (t->be->options2 & PR_O2_AS_REQL) {
5177 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005178 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005179 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005180 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005181 }
5182
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005183 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005184 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5185 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5186 return;
5187 }
5188
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005189 memcpy(txn->sessid, buf, len);
5190 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005191 }
5192
5193 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5194 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5195 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5196 return;
5197 }
5198
Cyril Bontéb21570a2009-11-29 20:04:48 +01005199 memcpy(sessid_temp, buf, len);
5200 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005201
5202 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5203 /* free previously allocated memory */
5204 pool_free2(apools.sessid, sessid_temp);
5205
5206 if (asession != NULL) {
5207 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5208 if (!(t->be->options2 & PR_O2_AS_REQL))
5209 asession->request_count++;
5210
5211 if (asession->serverid != NULL) {
5212 struct server *srv = t->be->srv;
5213 while (srv) {
5214 if (strcmp(srv->id, asession->serverid) == 0) {
5215 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5216 /* we found the server and it's usable */
5217 txn->flags &= ~TX_CK_MASK;
5218 txn->flags |= TX_CK_VALID;
5219 t->flags |= SN_DIRECT | SN_ASSIGNED;
5220 t->srv = srv;
5221 break;
5222 } else {
5223 txn->flags &= ~TX_CK_MASK;
5224 txn->flags |= TX_CK_DOWN;
5225 }
5226 }
5227 srv = srv->next;
5228 }
5229 }
5230 }
5231}
5232
5233/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005234 * Manage client-side cookie. It can impact performance by about 2% so it is
5235 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005236 */
5237void manage_client_side_cookies(struct session *t, struct buffer *req)
5238{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005239 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005240 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005241 char *del_colon, *del_cookie, *colon;
5242 int app_cookies;
5243
Willy Tarreau58f10d72006-12-04 02:26:12 +01005244 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005245 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246
Willy Tarreau2a324282006-12-05 00:05:46 +01005247 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005248 * we start with the start line.
5249 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005250 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005251 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005252
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005253 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005254 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005255 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005257 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258 cur_ptr = cur_next;
5259 cur_end = cur_ptr + cur_hdr->len;
5260 cur_next = cur_end + cur_hdr->cr + 1;
5261
5262 /* We have one full header between cur_ptr and cur_end, and the
5263 * next header starts at cur_next. We're only interested in
5264 * "Cookie:" headers.
5265 */
5266
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005267 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5268 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005269 old_idx = cur_idx;
5270 continue;
5271 }
5272
5273 /* Now look for cookies. Conforming to RFC2109, we have to support
5274 * attributes whose name begin with a '$', and associate them with
5275 * the right cookie, if we want to delete this cookie.
5276 * So there are 3 cases for each cookie read :
5277 * 1) it's a special attribute, beginning with a '$' : ignore it.
5278 * 2) it's a server id cookie that we *MAY* want to delete : save
5279 * some pointers on it (last semi-colon, beginning of cookie...)
5280 * 3) it's an application cookie : we *MAY* have to delete a previous
5281 * "special" cookie.
5282 * At the end of loop, if a "special" cookie remains, we may have to
5283 * remove it. If no application cookie persists in the header, we
5284 * *MUST* delete it
5285 */
5286
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005287 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005288
Willy Tarreau58f10d72006-12-04 02:26:12 +01005289 /* del_cookie == NULL => nothing to be deleted */
5290 del_colon = del_cookie = NULL;
5291 app_cookies = 0;
5292
5293 while (p1 < cur_end) {
5294 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005295 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005296 while (p1 < cur_end) {
5297 if (*p1 == ';' || *p1 == ',')
5298 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005299 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005300 break;
5301 p1++;
5302 }
5303
5304 if (p1 == cur_end)
5305 break;
5306
5307 /* p1 is at the beginning of the cookie name */
5308 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005309 while (p2 < cur_end && *p2 != '=') {
5310 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5311 /* oops, the cookie name was truncated, resync */
5312 p1 = p2;
5313 goto resync_name;
5314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005315 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005317
5318 if (p2 == cur_end)
5319 break;
5320
5321 p3 = p2 + 1; /* skips the '=' sign */
5322 if (p3 == cur_end)
5323 break;
5324
Willy Tarreau305ae852010-01-03 19:45:54 +01005325 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5326 p5 = p4 = p3;
5327 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5328 if (!isspace((unsigned char)*p5))
5329 p4 = p5 + 1;
5330 p5++;
5331 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005332
5333 /* here, we have the cookie name between p1 and p2,
5334 * and its value between p3 and p4.
5335 * we can process it :
5336 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005337 * Cookie: NAME=VALUE ;
5338 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005339 * | || || +--> p4
5340 * | || |+-------> p3
5341 * | || +--------> p2
5342 * | |+------------> p1
5343 * | +-------------> colon
5344 * +--------------------> cur_ptr
5345 */
5346
5347 if (*p1 == '$') {
5348 /* skip this one */
5349 }
5350 else {
5351 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005352 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005353 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005354 (p4 - p1 >= t->fe->capture_namelen) &&
5355 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005356 int log_len = p4 - p1;
5357
Willy Tarreau086b3b42007-05-13 21:45:51 +02005358 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005359 Alert("HTTP logging : out of memory.\n");
5360 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005361 if (log_len > t->fe->capture_len)
5362 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005363 memcpy(txn->cli_cookie, p1, log_len);
5364 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005365 }
5366 }
5367
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005368 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5369 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005370 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005371 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005372 char *delim;
5373
5374 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5375 * have the server ID betweek p3 and delim, and the original cookie between
5376 * delim+1 and p4. Otherwise, delim==p4 :
5377 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005378 * Cookie: NAME=SRV~VALUE ;
5379 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005380 * | || || | +--> p4
5381 * | || || +--------> delim
5382 * | || |+-----------> p3
5383 * | || +------------> p2
5384 * | |+----------------> p1
5385 * | +-----------------> colon
5386 * +------------------------> cur_ptr
5387 */
5388
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005389 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005390 for (delim = p3; delim < p4; delim++)
5391 if (*delim == COOKIE_DELIM)
5392 break;
5393 }
5394 else
5395 delim = p4;
5396
5397
5398 /* Here, we'll look for the first running server which supports the cookie.
5399 * This allows to share a same cookie between several servers, for example
5400 * to dedicate backup servers to specific servers only.
5401 * However, to prevent clients from sticking to cookie-less backup server
5402 * when they have incidentely learned an empty cookie, we simply ignore
5403 * empty cookies and mark them as invalid.
5404 */
5405 if (delim == p3)
5406 srv = NULL;
5407
5408 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005409 if (srv->cookie && (srv->cklen == delim - p3) &&
5410 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005411 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005412 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005413 txn->flags &= ~TX_CK_MASK;
5414 txn->flags |= TX_CK_VALID;
5415 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005416 t->srv = srv;
5417 break;
5418 } else {
5419 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005420 txn->flags &= ~TX_CK_MASK;
5421 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005422 }
5423 }
5424 srv = srv->next;
5425 }
5426
Willy Tarreau3d300592007-03-18 18:34:41 +01005427 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005428 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005429 txn->flags &= ~TX_CK_MASK;
5430 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005431 }
5432
5433 /* depending on the cookie mode, we may have to either :
5434 * - delete the complete cookie if we're in insert+indirect mode, so that
5435 * the server never sees it ;
5436 * - remove the server id from the cookie value, and tag the cookie as an
5437 * application cookie so that it does not get accidentely removed later,
5438 * if we're in cookie prefix mode
5439 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005440 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005441 int delta; /* negative */
5442
5443 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5444 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005445 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005446 cur_end += delta;
5447 cur_next += delta;
5448 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005449 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005450
5451 del_cookie = del_colon = NULL;
5452 app_cookies++; /* protect the header from deletion */
5453 }
5454 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005455 (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 +01005456 del_cookie = p1;
5457 del_colon = colon;
5458 }
5459 } else {
5460 /* now we know that we must keep this cookie since it's
5461 * not ours. But if we wanted to delete our cookie
5462 * earlier, we cannot remove the complete header, but we
5463 * can remove the previous block itself.
5464 */
5465 app_cookies++;
5466
5467 if (del_cookie != NULL) {
5468 int delta; /* negative */
5469
5470 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5471 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005472 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005473 cur_end += delta;
5474 cur_next += delta;
5475 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005476 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005477 del_cookie = del_colon = NULL;
5478 }
5479 }
5480
Cyril Bontéb21570a2009-11-29 20:04:48 +01005481 if (t->be->appsession_name != NULL) {
5482 int cmp_len, value_len;
5483 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005484
Cyril Bontéb21570a2009-11-29 20:04:48 +01005485 if (t->be->options2 & PR_O2_AS_PFX) {
5486 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5487 value_begin = p1 + t->be->appsession_name_len;
5488 value_len = p4 - p1 - t->be->appsession_name_len;
5489 } else {
5490 cmp_len = p2 - p1;
5491 value_begin = p3;
5492 value_len = p4 - p3;
5493 }
5494
5495 /* let's see if the cookie is our appcookie */
5496 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5497 /* Cool... it's the right one */
5498 manage_client_side_appsession(t, value_begin, value_len);
5499 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005500#if defined(DEBUG_HASH)
5501 Alert("manage_client_side_cookies\n");
5502 appsession_hash_dump(&(t->be->htbl_proxy));
5503#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005504 }/* end if ((t->proxy->appsession_name != NULL) ... */
5505 }
5506
5507 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005508 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005509 } /* while (p1 < cur_end) */
5510
5511 /* There's no more cookie on this line.
5512 * We may have marked the last one(s) for deletion.
5513 * We must do this now in two ways :
5514 * - if there is no app cookie, we simply delete the header ;
5515 * - if there are app cookies, we must delete the end of the
5516 * string properly, including the colon/semi-colon before
5517 * the cookie name.
5518 */
5519 if (del_cookie != NULL) {
5520 int delta;
5521 if (app_cookies) {
5522 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5523 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005524 cur_hdr->len += delta;
5525 } else {
5526 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005527
5528 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005529 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5530 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005531 cur_hdr->len = 0;
5532 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005533 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005534 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005535 }
5536
5537 /* keep the link from this header to next one */
5538 old_idx = cur_idx;
5539 } /* end of cookie processing on this header */
5540}
5541
5542
Willy Tarreaua15645d2007-03-18 16:22:39 +01005543/* Iterate the same filter through all response headers contained in <rtr>.
5544 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5545 */
5546int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5547{
5548 char term;
5549 char *cur_ptr, *cur_end, *cur_next;
5550 int cur_idx, old_idx, last_hdr;
5551 struct http_txn *txn = &t->txn;
5552 struct hdr_idx_elem *cur_hdr;
5553 int len, delta;
5554
5555 last_hdr = 0;
5556
Willy Tarreau962c3f42010-01-10 00:15:35 +01005557 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005558 old_idx = 0;
5559
5560 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005561 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005562 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005563 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005564 (exp->action == ACT_ALLOW ||
5565 exp->action == ACT_DENY))
5566 return 0;
5567
5568 cur_idx = txn->hdr_idx.v[old_idx].next;
5569 if (!cur_idx)
5570 break;
5571
5572 cur_hdr = &txn->hdr_idx.v[cur_idx];
5573 cur_ptr = cur_next;
5574 cur_end = cur_ptr + cur_hdr->len;
5575 cur_next = cur_end + cur_hdr->cr + 1;
5576
5577 /* Now we have one header between cur_ptr and cur_end,
5578 * and the next header starts at cur_next.
5579 */
5580
5581 /* The annoying part is that pattern matching needs
5582 * that we modify the contents to null-terminate all
5583 * strings before testing them.
5584 */
5585
5586 term = *cur_end;
5587 *cur_end = '\0';
5588
5589 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5590 switch (exp->action) {
5591 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005592 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005593 last_hdr = 1;
5594 break;
5595
5596 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005597 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005598 last_hdr = 1;
5599 break;
5600
5601 case ACT_REPLACE:
5602 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5603 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5604 /* FIXME: if the user adds a newline in the replacement, the
5605 * index will not be recalculated for now, and the new line
5606 * will not be counted as a new header.
5607 */
5608
5609 cur_end += delta;
5610 cur_next += delta;
5611 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005612 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005613 break;
5614
5615 case ACT_REMOVE:
5616 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5617 cur_next += delta;
5618
Willy Tarreaufa355d42009-11-29 18:12:29 +01005619 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005620 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5621 txn->hdr_idx.used--;
5622 cur_hdr->len = 0;
5623 cur_end = NULL; /* null-term has been rewritten */
5624 break;
5625
5626 }
5627 }
5628 if (cur_end)
5629 *cur_end = term; /* restore the string terminator */
5630
5631 /* keep the link from this header to next one in case of later
5632 * removal of next header.
5633 */
5634 old_idx = cur_idx;
5635 }
5636 return 0;
5637}
5638
5639
5640/* Apply the filter to the status line in the response buffer <rtr>.
5641 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5642 * or -1 if a replacement resulted in an invalid status line.
5643 */
5644int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5645{
5646 char term;
5647 char *cur_ptr, *cur_end;
5648 int done;
5649 struct http_txn *txn = &t->txn;
5650 int len, delta;
5651
5652
Willy Tarreau3d300592007-03-18 18:34:41 +01005653 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005654 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005655 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005656 (exp->action == ACT_ALLOW ||
5657 exp->action == ACT_DENY))
5658 return 0;
5659 else if (exp->action == ACT_REMOVE)
5660 return 0;
5661
5662 done = 0;
5663
Willy Tarreau962c3f42010-01-10 00:15:35 +01005664 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005665 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5666
5667 /* Now we have the status line between cur_ptr and cur_end */
5668
5669 /* The annoying part is that pattern matching needs
5670 * that we modify the contents to null-terminate all
5671 * strings before testing them.
5672 */
5673
5674 term = *cur_end;
5675 *cur_end = '\0';
5676
5677 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5678 switch (exp->action) {
5679 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005680 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005681 done = 1;
5682 break;
5683
5684 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005685 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005686 done = 1;
5687 break;
5688
5689 case ACT_REPLACE:
5690 *cur_end = term; /* restore the string terminator */
5691 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5692 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5693 /* FIXME: if the user adds a newline in the replacement, the
5694 * index will not be recalculated for now, and the new line
5695 * will not be counted as a new header.
5696 */
5697
Willy Tarreaufa355d42009-11-29 18:12:29 +01005698 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005699 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005700 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005701 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005702 cur_ptr, cur_end + 1,
5703 NULL, NULL);
5704 if (unlikely(!cur_end))
5705 return -1;
5706
5707 /* we have a full respnse and we know that we have either a CR
5708 * or an LF at <ptr>.
5709 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005710 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005711 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5712 /* there is no point trying this regex on headers */
5713 return 1;
5714 }
5715 }
5716 *cur_end = term; /* restore the string terminator */
5717 return done;
5718}
5719
5720
5721
5722/*
5723 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5724 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5725 * unparsable response.
5726 */
5727int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5728{
Willy Tarreau3d300592007-03-18 18:34:41 +01005729 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005730 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005731 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005732 int ret;
5733
5734 /*
5735 * The interleaving of transformations and verdicts
5736 * makes it difficult to decide to continue or stop
5737 * the evaluation.
5738 */
5739
Willy Tarreau3d300592007-03-18 18:34:41 +01005740 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005741 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5742 exp->action == ACT_PASS)) {
5743 exp = exp->next;
5744 continue;
5745 }
5746
5747 /* Apply the filter to the status line. */
5748 ret = apply_filter_to_sts_line(t, rtr, exp);
5749 if (unlikely(ret < 0))
5750 return -1;
5751
5752 if (likely(ret == 0)) {
5753 /* The filter did not match the response, it can be
5754 * iterated through all headers.
5755 */
5756 apply_filter_to_resp_headers(t, rtr, exp);
5757 }
5758 exp = exp->next;
5759 }
5760 return 0;
5761}
5762
5763
5764
5765/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005766 * Manage server-side cookies. It can impact performance by about 2% so it is
5767 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005768 */
5769void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5770{
5771 struct http_txn *txn = &t->txn;
5772 char *p1, *p2, *p3, *p4;
5773
Willy Tarreaua15645d2007-03-18 16:22:39 +01005774 char *cur_ptr, *cur_end, *cur_next;
5775 int cur_idx, old_idx, delta;
5776
Willy Tarreaua15645d2007-03-18 16:22:39 +01005777 /* Iterate through the headers.
5778 * we start with the start line.
5779 */
5780 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005781 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005782
5783 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5784 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005785 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005786
5787 cur_hdr = &txn->hdr_idx.v[cur_idx];
5788 cur_ptr = cur_next;
5789 cur_end = cur_ptr + cur_hdr->len;
5790 cur_next = cur_end + cur_hdr->cr + 1;
5791
5792 /* We have one full header between cur_ptr and cur_end, and the
5793 * next header starts at cur_next. We're only interested in
5794 * "Cookie:" headers.
5795 */
5796
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005797 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5798 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005799 old_idx = cur_idx;
5800 continue;
5801 }
5802
5803 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005804 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005805
5806
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005807 /* maybe we only wanted to see if there was a set-cookie. Note that
5808 * the cookie capture is declared in the fronend.
5809 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005810 if (t->be->cookie_name == NULL &&
5811 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005812 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005813 return;
5814
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005815 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005816
5817 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005818 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5819 break;
5820
5821 /* p1 is at the beginning of the cookie name */
5822 p2 = p1;
5823
5824 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5825 p2++;
5826
5827 if (p2 == cur_end || *p2 == ';') /* next cookie */
5828 break;
5829
5830 p3 = p2 + 1; /* skip the '=' sign */
5831 if (p3 == cur_end)
5832 break;
5833
5834 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005835 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005836 p4++;
5837
5838 /* here, we have the cookie name between p1 and p2,
5839 * and its value between p3 and p4.
5840 * we can process it.
5841 */
5842
5843 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005844 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005845 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005846 (p4 - p1 >= t->fe->capture_namelen) &&
5847 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005848 int log_len = p4 - p1;
5849
Willy Tarreau086b3b42007-05-13 21:45:51 +02005850 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005851 Alert("HTTP logging : out of memory.\n");
5852 }
5853
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005854 if (log_len > t->fe->capture_len)
5855 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005856 memcpy(txn->srv_cookie, p1, log_len);
5857 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005858 }
5859
5860 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005861 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5862 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005863 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005864 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005865
5866 /* If the cookie is in insert mode on a known server, we'll delete
5867 * this occurrence because we'll insert another one later.
5868 * We'll delete it too if the "indirect" option is set and we're in
5869 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005870 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5871 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005872 /* this header must be deleted */
5873 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5874 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5875 txn->hdr_idx.used--;
5876 cur_hdr->len = 0;
5877 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005878 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879
Willy Tarreau3d300592007-03-18 18:34:41 +01005880 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005881 }
5882 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005883 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005884 /* replace bytes p3->p4 with the cookie name associated
5885 * with this server since we know it.
5886 */
5887 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5888 cur_hdr->len += delta;
5889 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005890 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891
Willy Tarreau3d300592007-03-18 18:34:41 +01005892 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005893 }
5894 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005895 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005896 /* insert the cookie name associated with this server
5897 * before existing cookie, and insert a delimitor between them..
5898 */
5899 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5900 cur_hdr->len += delta;
5901 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005902 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005903
5904 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005905 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906 }
5907 }
5908 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005909 else if (t->be->appsession_name != NULL) {
5910 int cmp_len, value_len;
5911 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005912
Cyril Bontéb21570a2009-11-29 20:04:48 +01005913 if (t->be->options2 & PR_O2_AS_PFX) {
5914 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5915 value_begin = p1 + t->be->appsession_name_len;
5916 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5917 } else {
5918 cmp_len = p2 - p1;
5919 value_begin = p3;
5920 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005921 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005922
5923 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5924 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005925 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005926 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005927 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01005928 }
5929 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005930 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005931 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5932 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5933 return;
5934 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005935 memcpy(txn->sessid, value_begin, value_len);
5936 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005937 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005938 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5940 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005941 /* keep the link from this header to next one */
5942 old_idx = cur_idx;
5943 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005944
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005945 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005946 appsess *asession = NULL;
5947 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005948 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005949 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01005950 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005951 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5952 Alert("Not enough Memory process_srv():asession:calloc().\n");
5953 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5954 return;
5955 }
5956 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5957 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5958 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005959 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005960 return;
5961 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005962 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005963 asession->sessid[t->be->appsession_len] = 0;
5964
Willy Tarreau1fac7532010-01-09 19:23:06 +01005965 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005966 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5967 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5968 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005969 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005970 return;
5971 }
5972 asession->serverid[0] = '\0';
5973 memcpy(asession->serverid, t->srv->id, server_id_len);
5974
5975 asession->request_count = 0;
5976 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5977 }
5978
5979 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5980 asession->request_count++;
5981 }
5982
5983#if defined(DEBUG_HASH)
5984 Alert("manage_server_side_cookies\n");
5985 appsession_hash_dump(&(t->be->htbl_proxy));
5986#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005987}
5988
5989
5990
5991/*
5992 * Check if response is cacheable or not. Updates t->flags.
5993 */
5994void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5995{
5996 struct http_txn *txn = &t->txn;
5997 char *p1, *p2;
5998
5999 char *cur_ptr, *cur_end, *cur_next;
6000 int cur_idx;
6001
Willy Tarreau5df51872007-11-25 16:20:08 +01006002 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006003 return;
6004
6005 /* Iterate through the headers.
6006 * we start with the start line.
6007 */
6008 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006009 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006010
6011 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6012 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006013 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006014
6015 cur_hdr = &txn->hdr_idx.v[cur_idx];
6016 cur_ptr = cur_next;
6017 cur_end = cur_ptr + cur_hdr->len;
6018 cur_next = cur_end + cur_hdr->cr + 1;
6019
6020 /* We have one full header between cur_ptr and cur_end, and the
6021 * next header starts at cur_next. We're only interested in
6022 * "Cookie:" headers.
6023 */
6024
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006025 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6026 if (val) {
6027 if ((cur_end - (cur_ptr + val) >= 8) &&
6028 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6029 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6030 return;
6031 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006032 }
6033
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006034 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6035 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006036 continue;
6037
6038 /* OK, right now we know we have a cache-control header at cur_ptr */
6039
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006040 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006041
6042 if (p1 >= cur_end) /* no more info */
6043 continue;
6044
6045 /* p1 is at the beginning of the value */
6046 p2 = p1;
6047
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006048 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006049 p2++;
6050
6051 /* we have a complete value between p1 and p2 */
6052 if (p2 < cur_end && *p2 == '=') {
6053 /* we have something of the form no-cache="set-cookie" */
6054 if ((cur_end - p1 >= 21) &&
6055 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6056 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006057 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006058 continue;
6059 }
6060
6061 /* OK, so we know that either p2 points to the end of string or to a comma */
6062 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6063 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6064 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6065 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006066 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006067 return;
6068 }
6069
6070 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006071 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006072 continue;
6073 }
6074 }
6075}
6076
6077
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078/*
6079 * Try to retrieve a known appsession in the URI, then the associated server.
6080 * If the server is found, it's assigned to the session.
6081 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006082void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006083{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006084 char *end_params, *first_param, *cur_param, *next_param;
6085 char separator;
6086 int value_len;
6087
6088 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006090 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006091 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006093 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006094
Cyril Bontéb21570a2009-11-29 20:04:48 +01006095 first_param = NULL;
6096 switch (mode) {
6097 case PR_O2_AS_M_PP:
6098 first_param = memchr(begin, ';', len);
6099 break;
6100 case PR_O2_AS_M_QS:
6101 first_param = memchr(begin, '?', len);
6102 break;
6103 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006104
Cyril Bontéb21570a2009-11-29 20:04:48 +01006105 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006106 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006107 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108
Cyril Bontéb21570a2009-11-29 20:04:48 +01006109 switch (mode) {
6110 case PR_O2_AS_M_PP:
6111 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6112 end_params = (char *) begin + len;
6113 }
6114 separator = ';';
6115 break;
6116 case PR_O2_AS_M_QS:
6117 end_params = (char *) begin + len;
6118 separator = '&';
6119 break;
6120 default:
6121 /* unknown mode, shouldn't happen */
6122 return;
6123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006124
Cyril Bontéb21570a2009-11-29 20:04:48 +01006125 cur_param = next_param = end_params;
6126 while (cur_param > first_param) {
6127 cur_param--;
6128 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6129 /* let's see if this is the appsession parameter */
6130 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6131 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6132 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6133 /* Cool... it's the right one */
6134 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6135 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6136 if (value_len > 0) {
6137 manage_client_side_appsession(t, cur_param, value_len);
6138 }
6139 break;
6140 }
6141 next_param = cur_param;
6142 }
6143 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006145 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006146 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006147#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006148}
6149
6150
Willy Tarreaub2513902006-12-17 14:52:38 +01006151/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006152 * In a GET or HEAD request, check if the requested URI matches the stats uri
6153 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006154 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006155 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006156 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006157 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006158 *
6159 * Returns 1 if the session's state changes, otherwise 0.
6160 */
6161int stats_check_uri_auth(struct session *t, struct proxy *backend)
6162{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006163 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006164 struct uri_auth *uri_auth = backend->uri_auth;
6165 struct user_auth *user;
6166 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006167 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006168
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006169 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006171 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006172 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006173 return 0;
6174
Willy Tarreau962c3f42010-01-10 00:15:35 +01006175 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006176
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006177 /* the URI is in h */
6178 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006179 return 0;
6180
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006181 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006182 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006183 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006184 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006185 break;
6186 }
6187 h++;
6188 }
6189
6190 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006191 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6192 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006193 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006194 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006195 break;
6196 }
6197 h++;
6198 }
6199 }
6200
Willy Tarreau962c3f42010-01-10 00:15:35 +01006201 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6202 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006203 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006204 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006205 break;
6206 }
6207 h++;
6208 }
6209
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006210 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6211
Willy Tarreaub2513902006-12-17 14:52:38 +01006212 /* we are in front of a interceptable URI. Let's check
6213 * if there's an authentication and if it's valid.
6214 */
6215 user = uri_auth->users;
6216 if (!user) {
6217 /* no user auth required, it's OK */
6218 authenticated = 1;
6219 } else {
6220 authenticated = 0;
6221
6222 /* a user list is defined, we have to check.
6223 * skip 21 chars for "Authorization: Basic ".
6224 */
6225
6226 /* FIXME: this should move to an earlier place */
6227 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006228 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006229 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6230 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006231 if (len > 14 &&
6232 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006233 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006234 break;
6235 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006236 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006237 }
6238
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006239 if (txn->auth_hdr.len < 21 ||
6240 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006241 user = NULL;
6242
6243 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006244 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6245 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006246 user->user_pwd, user->user_len)) {
6247 authenticated = 1;
6248 break;
6249 }
6250 user = user->next;
6251 }
6252 }
6253
6254 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006255 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006256
6257 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006258 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6259 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006260 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006261 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006262 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006263 if (!(t->flags & SN_ERR_MASK))
6264 t->flags |= SN_ERR_PRXCOND;
6265 if (!(t->flags & SN_FINST_MASK))
6266 t->flags |= SN_FINST_R;
6267 return 1;
6268 }
6269
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006270 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006271 * data.
6272 */
Willy Tarreau70089872008-06-13 21:12:51 +02006273 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006274 t->data_source = DATA_SRC_STATS;
6275 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006276 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006277 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6278 t->rep->prod->private = t;
6279 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006280 return 1;
6281}
6282
Willy Tarreau4076a152009-04-02 15:18:36 +02006283/*
6284 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006285 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6286 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006287 */
6288void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6289 struct buffer *buf, struct http_msg *msg,
6290 struct proxy *other_end)
6291{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006292 es->len = buf->r - (buf->data + msg->som);
6293 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006294 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006295 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006296 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006297 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006298 es->when = date; // user-visible date
6299 es->sid = s->uniq_id;
6300 es->srv = s->srv;
6301 es->oe = other_end;
6302 es->src = s->cli_addr;
6303}
Willy Tarreaub2513902006-12-17 14:52:38 +01006304
Willy Tarreaubaaee002006-06-26 02:48:02 +02006305/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006306 * Print a debug line with a header
6307 */
6308void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6309{
6310 int len, max;
6311 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006312 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006313 max = end - start;
6314 UBOUND(max, sizeof(trash) - len - 1);
6315 len += strlcpy2(trash + len, start, max + 1);
6316 trash[len++] = '\n';
6317 write(1, trash, len);
6318}
6319
Willy Tarreau0937bc42009-12-22 15:03:09 +01006320/*
6321 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6322 * the required fields are properly allocated and that we only need to (re)init
6323 * them. This should be used before processing any new request.
6324 */
6325void http_init_txn(struct session *s)
6326{
6327 struct http_txn *txn = &s->txn;
6328 struct proxy *fe = s->fe;
6329
6330 txn->flags = 0;
6331 txn->status = -1;
6332
6333 txn->req.sol = txn->req.eol = NULL;
6334 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6335 txn->rsp.sol = txn->rsp.eol = NULL;
6336 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6337 txn->req.hdr_content_len = 0LL;
6338 txn->rsp.hdr_content_len = 0LL;
6339 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6340 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6341 chunk_reset(&txn->auth_hdr);
6342
6343 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6344 if (fe->options2 & PR_O2_REQBUG_OK)
6345 txn->req.err_pos = -1; /* let buggy requests pass */
6346
Willy Tarreau46023632010-01-07 22:51:47 +01006347 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006348 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6349
Willy Tarreau46023632010-01-07 22:51:47 +01006350 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006351 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6352
6353 if (txn->hdr_idx.v)
6354 hdr_idx_init(&txn->hdr_idx);
6355}
6356
6357/* to be used at the end of a transaction */
6358void http_end_txn(struct session *s)
6359{
6360 struct http_txn *txn = &s->txn;
6361
6362 /* these ones will have been dynamically allocated */
6363 pool_free2(pool2_requri, txn->uri);
6364 pool_free2(pool2_capture, txn->cli_cookie);
6365 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006366 pool_free2(apools.sessid, txn->sessid);
6367 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006368 txn->uri = NULL;
6369 txn->srv_cookie = NULL;
6370 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006371
6372 if (txn->req.cap) {
6373 struct cap_hdr *h;
6374 for (h = s->fe->req_cap; h; h = h->next)
6375 pool_free2(h->pool, txn->req.cap[h->index]);
6376 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6377 }
6378
6379 if (txn->rsp.cap) {
6380 struct cap_hdr *h;
6381 for (h = s->fe->rsp_cap; h; h = h->next)
6382 pool_free2(h->pool, txn->rsp.cap[h->index]);
6383 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6384 }
6385
Willy Tarreau0937bc42009-12-22 15:03:09 +01006386}
6387
6388/* to be used at the end of a transaction to prepare a new one */
6389void http_reset_txn(struct session *s)
6390{
6391 http_end_txn(s);
6392 http_init_txn(s);
6393
6394 s->be = s->fe;
6395 s->req->analysers = s->listener->analysers;
6396 s->logs.logwait = s->fe->to_log;
6397 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006398 /* re-init store persistence */
6399 s->store_count = 0;
6400
Willy Tarreau0937bc42009-12-22 15:03:09 +01006401 s->pend_pos = NULL;
6402 s->conn_retries = s->be->conn_retries;
6403
6404 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6405
6406 s->req->rto = s->fe->timeout.client;
6407 s->req->wto = s->be->timeout.server;
6408 s->req->cto = s->be->timeout.connect;
6409
6410 s->rep->rto = s->be->timeout.server;
6411 s->rep->wto = s->fe->timeout.client;
6412 s->rep->cto = TICK_ETERNITY;
6413
6414 s->req->rex = TICK_ETERNITY;
6415 s->req->wex = TICK_ETERNITY;
6416 s->req->analyse_exp = TICK_ETERNITY;
6417 s->rep->rex = TICK_ETERNITY;
6418 s->rep->wex = TICK_ETERNITY;
6419 s->rep->analyse_exp = TICK_ETERNITY;
6420}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006421
Willy Tarreau8797c062007-05-07 00:55:35 +02006422/************************************************************************/
6423/* The code below is dedicated to ACL parsing and matching */
6424/************************************************************************/
6425
6426
6427
6428
6429/* 1. Check on METHOD
6430 * We use the pre-parsed method if it is known, and store its number as an
6431 * integer. If it is unknown, we use the pointer and the length.
6432 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006433static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006434{
6435 int len, meth;
6436
Willy Tarreauae8b7962007-06-09 23:10:04 +02006437 len = strlen(*text);
6438 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006439
6440 pattern->val.i = meth;
6441 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006442 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006443 if (!pattern->ptr.str)
6444 return 0;
6445 pattern->len = len;
6446 }
6447 return 1;
6448}
6449
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006450static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006451acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6452 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006453{
6454 int meth;
6455 struct http_txn *txn = l7;
6456
Willy Tarreaub6866442008-07-14 23:54:42 +02006457 if (!txn)
6458 return 0;
6459
Willy Tarreau655dce92009-11-08 13:10:58 +01006460 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006461 return 0;
6462
Willy Tarreau8797c062007-05-07 00:55:35 +02006463 meth = txn->meth;
6464 test->i = meth;
6465 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006466 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6467 /* ensure the indexes are not affected */
6468 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006469 test->len = txn->req.sl.rq.m_l;
6470 test->ptr = txn->req.sol;
6471 }
6472 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6473 return 1;
6474}
6475
6476static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6477{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006478 int icase;
6479
Willy Tarreau8797c062007-05-07 00:55:35 +02006480 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006481 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006482
6483 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006484 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006485
6486 /* Other method, we must compare the strings */
6487 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006488 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006489
6490 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6491 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6492 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006493 return ACL_PAT_FAIL;
6494 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006495}
6496
6497/* 2. Check on Request/Status Version
6498 * We simply compare strings here.
6499 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006500static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006501{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006502 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006503 if (!pattern->ptr.str)
6504 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006505 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006506 return 1;
6507}
6508
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006509static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006510acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6511 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006512{
6513 struct http_txn *txn = l7;
6514 char *ptr;
6515 int len;
6516
Willy Tarreaub6866442008-07-14 23:54:42 +02006517 if (!txn)
6518 return 0;
6519
Willy Tarreau655dce92009-11-08 13:10:58 +01006520 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006521 return 0;
6522
Willy Tarreau8797c062007-05-07 00:55:35 +02006523 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006524 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006525
6526 while ((len-- > 0) && (*ptr++ != '/'));
6527 if (len <= 0)
6528 return 0;
6529
6530 test->ptr = ptr;
6531 test->len = len;
6532
6533 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6534 return 1;
6535}
6536
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006537static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006538acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6539 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006540{
6541 struct http_txn *txn = l7;
6542 char *ptr;
6543 int len;
6544
Willy Tarreaub6866442008-07-14 23:54:42 +02006545 if (!txn)
6546 return 0;
6547
Willy Tarreau655dce92009-11-08 13:10:58 +01006548 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006549 return 0;
6550
Willy Tarreau8797c062007-05-07 00:55:35 +02006551 len = txn->rsp.sl.st.v_l;
6552 ptr = txn->rsp.sol;
6553
6554 while ((len-- > 0) && (*ptr++ != '/'));
6555 if (len <= 0)
6556 return 0;
6557
6558 test->ptr = ptr;
6559 test->len = len;
6560
6561 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6562 return 1;
6563}
6564
6565/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006566static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006567acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6568 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006569{
6570 struct http_txn *txn = l7;
6571 char *ptr;
6572 int len;
6573
Willy Tarreaub6866442008-07-14 23:54:42 +02006574 if (!txn)
6575 return 0;
6576
Willy Tarreau655dce92009-11-08 13:10:58 +01006577 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006578 return 0;
6579
Willy Tarreau8797c062007-05-07 00:55:35 +02006580 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006581 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006582
6583 test->i = __strl2ui(ptr, len);
6584 test->flags = ACL_TEST_F_VOL_1ST;
6585 return 1;
6586}
6587
6588/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006589static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006590acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6591 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006592{
6593 struct http_txn *txn = l7;
6594
Willy Tarreaub6866442008-07-14 23:54:42 +02006595 if (!txn)
6596 return 0;
6597
Willy Tarreau655dce92009-11-08 13:10:58 +01006598 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006599 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006600
Willy Tarreauc11416f2007-06-17 16:58:38 +02006601 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6602 /* ensure the indexes are not affected */
6603 return 0;
6604
Willy Tarreau8797c062007-05-07 00:55:35 +02006605 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006606 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006607
Willy Tarreauf3d25982007-05-08 22:45:09 +02006608 /* we do not need to set READ_ONLY because the data is in a buffer */
6609 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006610 return 1;
6611}
6612
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006613static int
6614acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6615 struct acl_expr *expr, struct acl_test *test)
6616{
6617 struct http_txn *txn = l7;
6618
Willy Tarreaub6866442008-07-14 23:54:42 +02006619 if (!txn)
6620 return 0;
6621
Willy Tarreau655dce92009-11-08 13:10:58 +01006622 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006623 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006624
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006625 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6626 /* ensure the indexes are not affected */
6627 return 0;
6628
6629 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006630 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006631 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6632 test->i = AF_INET;
6633
6634 /*
6635 * If we are parsing url in frontend space, we prepare backend stage
6636 * to not parse again the same url ! optimization lazyness...
6637 */
6638 if (px->options & PR_O_HTTP_PROXY)
6639 l4->flags |= SN_ADDR_SET;
6640
6641 test->flags = ACL_TEST_F_READ_ONLY;
6642 return 1;
6643}
6644
6645static int
6646acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6647 struct acl_expr *expr, struct acl_test *test)
6648{
6649 struct http_txn *txn = l7;
6650
Willy Tarreaub6866442008-07-14 23:54:42 +02006651 if (!txn)
6652 return 0;
6653
Willy Tarreau655dce92009-11-08 13:10:58 +01006654 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006655 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006656
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006657 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6658 /* ensure the indexes are not affected */
6659 return 0;
6660
6661 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006662 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006663 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6664
6665 if (px->options & PR_O_HTTP_PROXY)
6666 l4->flags |= SN_ADDR_SET;
6667
6668 test->flags = ACL_TEST_F_READ_ONLY;
6669 return 1;
6670}
6671
Willy Tarreauc11416f2007-06-17 16:58:38 +02006672/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6673 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6674 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006675static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006676acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006677 struct acl_expr *expr, struct acl_test *test)
6678{
6679 struct http_txn *txn = l7;
6680 struct hdr_idx *idx = &txn->hdr_idx;
6681 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006682
Willy Tarreaub6866442008-07-14 23:54:42 +02006683 if (!txn)
6684 return 0;
6685
Willy Tarreau33a7e692007-06-10 19:45:56 +02006686 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6687 /* search for header from the beginning */
6688 ctx->idx = 0;
6689
Willy Tarreau33a7e692007-06-10 19:45:56 +02006690 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6691 test->flags |= ACL_TEST_F_FETCH_MORE;
6692 test->flags |= ACL_TEST_F_VOL_HDR;
6693 test->len = ctx->vlen;
6694 test->ptr = (char *)ctx->line + ctx->val;
6695 return 1;
6696 }
6697
6698 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6699 test->flags |= ACL_TEST_F_VOL_HDR;
6700 return 0;
6701}
6702
Willy Tarreau33a7e692007-06-10 19:45:56 +02006703static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006704acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6705 struct acl_expr *expr, struct acl_test *test)
6706{
6707 struct http_txn *txn = l7;
6708
Willy Tarreaub6866442008-07-14 23:54:42 +02006709 if (!txn)
6710 return 0;
6711
Willy Tarreau655dce92009-11-08 13:10:58 +01006712 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006713 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006714
Willy Tarreauc11416f2007-06-17 16:58:38 +02006715 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6716 /* ensure the indexes are not affected */
6717 return 0;
6718
6719 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6720}
6721
6722static int
6723acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6724 struct acl_expr *expr, struct acl_test *test)
6725{
6726 struct http_txn *txn = l7;
6727
Willy Tarreaub6866442008-07-14 23:54:42 +02006728 if (!txn)
6729 return 0;
6730
Willy Tarreau655dce92009-11-08 13:10:58 +01006731 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006732 return 0;
6733
6734 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6735}
6736
6737/* 6. Check on HTTP header count. The number of occurrences is returned.
6738 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6739 */
6740static int
6741acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006742 struct acl_expr *expr, struct acl_test *test)
6743{
6744 struct http_txn *txn = l7;
6745 struct hdr_idx *idx = &txn->hdr_idx;
6746 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006747 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006748
Willy Tarreaub6866442008-07-14 23:54:42 +02006749 if (!txn)
6750 return 0;
6751
Willy Tarreau33a7e692007-06-10 19:45:56 +02006752 ctx.idx = 0;
6753 cnt = 0;
6754 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6755 cnt++;
6756
6757 test->i = cnt;
6758 test->flags = ACL_TEST_F_VOL_HDR;
6759 return 1;
6760}
6761
Willy Tarreauc11416f2007-06-17 16:58:38 +02006762static int
6763acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6764 struct acl_expr *expr, struct acl_test *test)
6765{
6766 struct http_txn *txn = l7;
6767
Willy Tarreaub6866442008-07-14 23:54:42 +02006768 if (!txn)
6769 return 0;
6770
Willy Tarreau655dce92009-11-08 13:10:58 +01006771 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006772 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006773
Willy Tarreauc11416f2007-06-17 16:58:38 +02006774 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6775 /* ensure the indexes are not affected */
6776 return 0;
6777
6778 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6779}
6780
6781static int
6782acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6783 struct acl_expr *expr, struct acl_test *test)
6784{
6785 struct http_txn *txn = l7;
6786
Willy Tarreaub6866442008-07-14 23:54:42 +02006787 if (!txn)
6788 return 0;
6789
Willy Tarreau655dce92009-11-08 13:10:58 +01006790 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006791 return 0;
6792
6793 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6794}
6795
Willy Tarreau33a7e692007-06-10 19:45:56 +02006796/* 7. Check on HTTP header's integer value. The integer value is returned.
6797 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006798 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006799 */
6800static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006801acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006802 struct acl_expr *expr, struct acl_test *test)
6803{
6804 struct http_txn *txn = l7;
6805 struct hdr_idx *idx = &txn->hdr_idx;
6806 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006807
Willy Tarreaub6866442008-07-14 23:54:42 +02006808 if (!txn)
6809 return 0;
6810
Willy Tarreau33a7e692007-06-10 19:45:56 +02006811 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6812 /* search for header from the beginning */
6813 ctx->idx = 0;
6814
Willy Tarreau33a7e692007-06-10 19:45:56 +02006815 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6816 test->flags |= ACL_TEST_F_FETCH_MORE;
6817 test->flags |= ACL_TEST_F_VOL_HDR;
6818 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6819 return 1;
6820 }
6821
6822 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6823 test->flags |= ACL_TEST_F_VOL_HDR;
6824 return 0;
6825}
6826
Willy Tarreauc11416f2007-06-17 16:58:38 +02006827static int
6828acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6829 struct acl_expr *expr, struct acl_test *test)
6830{
6831 struct http_txn *txn = l7;
6832
Willy Tarreaub6866442008-07-14 23:54:42 +02006833 if (!txn)
6834 return 0;
6835
Willy Tarreau655dce92009-11-08 13:10:58 +01006836 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006837 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006838
Willy Tarreauc11416f2007-06-17 16:58:38 +02006839 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6840 /* ensure the indexes are not affected */
6841 return 0;
6842
6843 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6844}
6845
6846static int
6847acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6848 struct acl_expr *expr, struct acl_test *test)
6849{
6850 struct http_txn *txn = l7;
6851
Willy Tarreaub6866442008-07-14 23:54:42 +02006852 if (!txn)
6853 return 0;
6854
Willy Tarreau655dce92009-11-08 13:10:58 +01006855 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006856 return 0;
6857
6858 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6859}
6860
Willy Tarreau106f9792009-09-19 07:54:16 +02006861/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6862 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6863 */
6864static int
6865acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6866 struct acl_expr *expr, struct acl_test *test)
6867{
6868 struct http_txn *txn = l7;
6869 struct hdr_idx *idx = &txn->hdr_idx;
6870 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6871
6872 if (!txn)
6873 return 0;
6874
6875 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6876 /* search for header from the beginning */
6877 ctx->idx = 0;
6878
6879 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6880 test->flags |= ACL_TEST_F_FETCH_MORE;
6881 test->flags |= ACL_TEST_F_VOL_HDR;
6882 /* Same optimization as url_ip */
6883 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6884 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6885 test->ptr = (void *)&l4->srv_addr.sin_addr;
6886 test->i = AF_INET;
6887 return 1;
6888 }
6889
6890 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6891 test->flags |= ACL_TEST_F_VOL_HDR;
6892 return 0;
6893}
6894
6895static int
6896acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6897 struct acl_expr *expr, struct acl_test *test)
6898{
6899 struct http_txn *txn = l7;
6900
6901 if (!txn)
6902 return 0;
6903
Willy Tarreau655dce92009-11-08 13:10:58 +01006904 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006905 return 0;
6906
6907 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6908 /* ensure the indexes are not affected */
6909 return 0;
6910
6911 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6912}
6913
6914static int
6915acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6916 struct acl_expr *expr, struct acl_test *test)
6917{
6918 struct http_txn *txn = l7;
6919
6920 if (!txn)
6921 return 0;
6922
Willy Tarreau655dce92009-11-08 13:10:58 +01006923 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006924 return 0;
6925
6926 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6927}
6928
Willy Tarreau737b0c12007-06-10 21:28:46 +02006929/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6930 * the first '/' after the possible hostname, and ends before the possible '?'.
6931 */
6932static int
6933acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6934 struct acl_expr *expr, struct acl_test *test)
6935{
6936 struct http_txn *txn = l7;
6937 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006938
Willy Tarreaub6866442008-07-14 23:54:42 +02006939 if (!txn)
6940 return 0;
6941
Willy Tarreau655dce92009-11-08 13:10:58 +01006942 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006943 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006944
Willy Tarreauc11416f2007-06-17 16:58:38 +02006945 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6946 /* ensure the indexes are not affected */
6947 return 0;
6948
Willy Tarreau962c3f42010-01-10 00:15:35 +01006949 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006950 ptr = http_get_path(txn);
6951 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006952 return 0;
6953
6954 /* OK, we got the '/' ! */
6955 test->ptr = ptr;
6956
6957 while (ptr < end && *ptr != '?')
6958 ptr++;
6959
6960 test->len = ptr - test->ptr;
6961
6962 /* we do not need to set READ_ONLY because the data is in a buffer */
6963 test->flags = ACL_TEST_F_VOL_1ST;
6964 return 1;
6965}
6966
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006967static int
6968acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6969 struct acl_expr *expr, struct acl_test *test)
6970{
6971 struct buffer *req = s->req;
6972 struct http_txn *txn = &s->txn;
6973 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006974
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006975 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6976 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6977 */
6978
6979 if (!s || !req)
6980 return 0;
6981
Willy Tarreau655dce92009-11-08 13:10:58 +01006982 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006983 /* Already decoded as OK */
6984 test->flags |= ACL_TEST_F_SET_RES_PASS;
6985 return 1;
6986 }
6987
6988 /* Try to decode HTTP request */
6989 if (likely(req->lr < req->r))
6990 http_msg_analyzer(req, msg, &txn->hdr_idx);
6991
Willy Tarreau655dce92009-11-08 13:10:58 +01006992 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006993 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6994 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6995 return 1;
6996 }
6997 /* wait for final state */
6998 test->flags |= ACL_TEST_F_MAY_CHANGE;
6999 return 0;
7000 }
7001
7002 /* OK we got a valid HTTP request. We have some minor preparation to
7003 * perform so that further checks can rely on HTTP tests.
7004 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007005 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007006 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7007 s->flags |= SN_REDIRECTABLE;
7008
7009 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7010 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7011 return 1;
7012 }
7013
7014 test->flags |= ACL_TEST_F_SET_RES_PASS;
7015 return 1;
7016}
7017
Willy Tarreau8797c062007-05-07 00:55:35 +02007018
7019/************************************************************************/
7020/* All supported keywords must be declared here. */
7021/************************************************************************/
7022
7023/* Note: must not be declared <const> as its list will be overwritten */
7024static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007025 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7026
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007027 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7028 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7029 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7030 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007031
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007032 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7033 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7034 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7035 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7036 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7037 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7038 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7039 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7040 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007041
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007042 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7043 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7044 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7045 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7046 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7047 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7048 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7049 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7050 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7051 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007052 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007053
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007054 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7055 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7056 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7057 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7058 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7059 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7060 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7061 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7062 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007063 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007064
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007065 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7066 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7067 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7068 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7069 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7070 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7071 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007072
Willy Tarreauf3d25982007-05-08 22:45:09 +02007073 { NULL, NULL, NULL, NULL },
7074
7075#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007076 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7077 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7078 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7079 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7080 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7081 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7082 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7083
Willy Tarreau8797c062007-05-07 00:55:35 +02007084 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7085 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7086 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7087 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7088 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7089 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7090 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7091 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7092
7093 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7094 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7095 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7096 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7097 { NULL, NULL, NULL, NULL },
7098#endif
7099}};
7100
7101
7102__attribute__((constructor))
7103static void __http_protocol_init(void)
7104{
7105 acl_register_keywords(&acl_kws);
7106}
7107
7108
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007110 * Local variables:
7111 * c-indent-level: 8
7112 * c-basic-offset: 8
7113 * End:
7114 */