blob: b2dd232ceef7415a343538bc17ac16a8297e94ab [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 Tarreau88d349d2010-01-25 12:15:43 +0100764
765 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
766 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
767 rdr.len += 29;
768 } else {
769 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
770 rdr.len += 23;
771 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772
773 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100774 si->shutr(si);
775 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 si->err_type = SI_ET_NONE;
777 si->err_loc = NULL;
778 si->state = SI_ST_CLO;
779
780 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100781 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100782
783 /* FIXME: we should increase a counter of redirects per server and per backend. */
784 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100785 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100786}
787
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100788/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 * that the server side is closed. Note that err_type is actually a
790 * bitmask, where almost only aborts may be cumulated with other
791 * values. We consider that aborted operations are more important
792 * than timeouts or errors due to the fact that nobody else in the
793 * logs might explain incomplete retries. All others should avoid
794 * being cumulated. It should normally not be possible to have multiple
795 * aborts at once, but just in case, the first one in sequence is reported.
796 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100797void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100798{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100799 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800
801 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100802 http_server_error(s, si, SN_ERR_CLICL, 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_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100805 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
806 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100808 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
809 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100810 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100811 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
812 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100814 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
815 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100817 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
818 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100819 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
821 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822}
823
Willy Tarreau42250582007-04-01 01:30:43 +0200824extern const char sess_term_cond[8];
825extern const char sess_fin_state[8];
826extern const char *monthname[12];
827const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
828const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
829 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
830 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200831struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200832struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100833
Emeric Brun3a058f32009-06-30 18:26:00 +0200834void http_sess_clflog(struct session *s)
835{
836 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
837 struct proxy *fe = s->fe;
838 struct proxy *be = s->be;
839 struct proxy *prx_log;
840 struct http_txn *txn = &s->txn;
841 int tolog, level, err;
842 char *uri, *h;
843 char *svid;
844 struct tm tm;
845 static char tmpline[MAX_SYSLOG_LEN];
846 int hdr;
847 size_t w;
848 int t_request;
849
850 prx_log = fe;
851 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
852 (s->conn_retries != be->conn_retries) ||
853 txn->status >= 500;
854
855 if (s->cli_addr.ss_family == AF_INET)
856 inet_ntop(AF_INET,
857 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
858 pn, sizeof(pn));
859 else
860 inet_ntop(AF_INET6,
861 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
862 pn, sizeof(pn));
863
864 get_gmtime(s->logs.accept_date.tv_sec, &tm);
865
866 /* FIXME: let's limit ourselves to frontend logging for now. */
867 tolog = fe->to_log;
868
869 h = tmpline;
870
871 w = snprintf(h, sizeof(tmpline),
872 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
873 pn,
874 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
875 tm.tm_hour, tm.tm_min, tm.tm_sec);
876 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
877 goto trunc;
878 h += w;
879
880 if (h >= tmpline + sizeof(tmpline) - 4)
881 goto trunc;
882
883 *(h++) = ' ';
884 *(h++) = '\"';
885 uri = txn->uri ? txn->uri : "<BADREQ>";
886 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
887 '#', url_encode_map, uri);
888 *(h++) = '\"';
889
890 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
891 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
892 goto trunc;
893 h += w;
894
895 if (h >= tmpline + sizeof(tmpline) - 9)
896 goto trunc;
897 memcpy(h, " \"-\" \"-\"", 8);
898 h += 8;
899
900 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
901 " %d %03d",
902 (s->cli_addr.ss_family == AF_INET) ?
903 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
904 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
905 (int)s->logs.accept_date.tv_usec/1000);
906 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
907 goto trunc;
908 h += w;
909
910 w = strlen(fe->id);
911 if (h >= tmpline + sizeof(tmpline) - 4 - w)
912 goto trunc;
913 *(h++) = ' ';
914 *(h++) = '\"';
915 memcpy(h, fe->id, w);
916 h += w;
917 *(h++) = '\"';
918
919 w = strlen(be->id);
920 if (h >= tmpline + sizeof(tmpline) - 4 - w)
921 goto trunc;
922 *(h++) = ' ';
923 *(h++) = '\"';
924 memcpy(h, be->id, w);
925 h += w;
926 *(h++) = '\"';
927
928 svid = (tolog & LW_SVID) ?
929 (s->data_source != DATA_SRC_STATS) ?
930 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
931
932 w = strlen(svid);
933 if (h >= tmpline + sizeof(tmpline) - 4 - w)
934 goto trunc;
935 *(h++) = ' ';
936 *(h++) = '\"';
937 memcpy(h, svid, w);
938 h += w;
939 *(h++) = '\"';
940
941 t_request = -1;
942 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
943 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
944 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
945 " %d %ld %ld %ld %ld",
946 t_request,
947 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
948 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
949 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
950 s->logs.t_close);
951 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
952 goto trunc;
953 h += w;
954
955 if (h >= tmpline + sizeof(tmpline) - 8)
956 goto trunc;
957 *(h++) = ' ';
958 *(h++) = '\"';
959 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
960 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
961 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
962 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
963 *(h++) = '\"';
964
965 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
966 " %d %d %d %d %d %ld %ld",
967 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
968 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
969 s->logs.srv_queue_size, s->logs.prx_queue_size);
970
971 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
972 goto trunc;
973 h += w;
974
975 if (txn->cli_cookie) {
976 w = strlen(txn->cli_cookie);
977 if (h >= tmpline + sizeof(tmpline) - 4 - w)
978 goto trunc;
979 *(h++) = ' ';
980 *(h++) = '\"';
981 memcpy(h, txn->cli_cookie, w);
982 h += w;
983 *(h++) = '\"';
984 } else {
985 if (h >= tmpline + sizeof(tmpline) - 5)
986 goto trunc;
987 memcpy(h, " \"-\"", 4);
988 h += 4;
989 }
990
991 if (txn->srv_cookie) {
992 w = strlen(txn->srv_cookie);
993 if (h >= tmpline + sizeof(tmpline) - 4 - w)
994 goto trunc;
995 *(h++) = ' ';
996 *(h++) = '\"';
997 memcpy(h, txn->srv_cookie, w);
998 h += w;
999 *(h++) = '\"';
1000 } else {
1001 if (h >= tmpline + sizeof(tmpline) - 5)
1002 goto trunc;
1003 memcpy(h, " \"-\"", 4);
1004 h += 4;
1005 }
1006
1007 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1008 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1009 if (h >= sizeof (tmpline) + tmpline - 4)
1010 goto trunc;
1011 *(h++) = ' ';
1012 *(h++) = '\"';
1013 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1014 '#', hdr_encode_map, txn->req.cap[hdr]);
1015 *(h++) = '\"';
1016 }
1017 }
1018
1019 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1020 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1021 if (h >= sizeof (tmpline) + tmpline - 4)
1022 goto trunc;
1023 *(h++) = ' ';
1024 *(h++) = '\"';
1025 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1026 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1027 *(h++) = '\"';
1028 }
1029 }
1030
1031trunc:
1032 *h = '\0';
1033
1034 level = LOG_INFO;
1035 if (err && (fe->options2 & PR_O2_LOGERRORS))
1036 level = LOG_ERR;
1037
1038 send_log(prx_log, level, "%s\n", tmpline);
1039
1040 s->logs.logwait = 0;
1041}
1042
Willy Tarreau42250582007-04-01 01:30:43 +02001043/*
1044 * send a log for the session when we have enough info about it.
1045 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001046 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001047void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001048{
1049 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1050 struct proxy *fe = s->fe;
1051 struct proxy *be = s->be;
1052 struct proxy *prx_log;
1053 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001054 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001055 char *uri, *h;
1056 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001057 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001058 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001059 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001060 int hdr;
1061
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001062 /* if we don't want to log normal traffic, return now */
1063 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1064 (s->conn_retries != be->conn_retries) ||
1065 txn->status >= 500;
1066 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1067 return;
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1070 return;
1071 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072
Emeric Brun3a058f32009-06-30 18:26:00 +02001073 if (prx_log->options2 & PR_O2_CLFLOG)
1074 return http_sess_clflog(s);
1075
Willy Tarreau42250582007-04-01 01:30:43 +02001076 if (s->cli_addr.ss_family == AF_INET)
1077 inet_ntop(AF_INET,
1078 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1079 pn, sizeof(pn));
1080 else
1081 inet_ntop(AF_INET6,
1082 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1083 pn, sizeof(pn));
1084
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001085 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001086
1087 /* FIXME: let's limit ourselves to frontend logging for now. */
1088 tolog = fe->to_log;
1089
1090 h = tmpline;
1091 if (fe->to_log & LW_REQHDR &&
1092 txn->req.cap &&
1093 (h < tmpline + sizeof(tmpline) - 10)) {
1094 *(h++) = ' ';
1095 *(h++) = '{';
1096 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1097 if (hdr)
1098 *(h++) = '|';
1099 if (txn->req.cap[hdr] != NULL)
1100 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1101 '#', hdr_encode_map, txn->req.cap[hdr]);
1102 }
1103 *(h++) = '}';
1104 }
1105
1106 if (fe->to_log & LW_RSPHDR &&
1107 txn->rsp.cap &&
1108 (h < tmpline + sizeof(tmpline) - 7)) {
1109 *(h++) = ' ';
1110 *(h++) = '{';
1111 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1112 if (hdr)
1113 *(h++) = '|';
1114 if (txn->rsp.cap[hdr] != NULL)
1115 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1116 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1117 }
1118 *(h++) = '}';
1119 }
1120
1121 if (h < tmpline + sizeof(tmpline) - 4) {
1122 *(h++) = ' ';
1123 *(h++) = '"';
1124 uri = txn->uri ? txn->uri : "<BADREQ>";
1125 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1126 '#', url_encode_map, uri);
1127 *(h++) = '"';
1128 }
1129 *h = '\0';
1130
1131 svid = (tolog & LW_SVID) ?
1132 (s->data_source != DATA_SRC_STATS) ?
1133 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1134
Willy Tarreau70089872008-06-13 21:12:51 +02001135 t_request = -1;
1136 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1137 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1138
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001139 level = LOG_INFO;
1140 if (err && (fe->options2 & PR_O2_LOGERRORS))
1141 level = LOG_ERR;
1142
1143 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001144 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001145 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1146 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001147 pn,
1148 (s->cli_addr.ss_family == AF_INET) ?
1149 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1150 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001151 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001152 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001153 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001154 t_request,
1155 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001156 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1157 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1158 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1159 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001160 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001161 txn->cli_cookie ? txn->cli_cookie : "-",
1162 txn->srv_cookie ? txn->srv_cookie : "-",
1163 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1164 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1165 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1166 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1167 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001168 (s->flags & SN_REDISP)?"+":"",
1169 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001170 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1171
1172 s->logs.logwait = 0;
1173}
1174
Willy Tarreau117f59e2007-03-04 18:17:17 +01001175
1176/*
1177 * Capture headers from message starting at <som> according to header list
1178 * <cap_hdr>, and fill the <idx> structure appropriately.
1179 */
1180void capture_headers(char *som, struct hdr_idx *idx,
1181 char **cap, struct cap_hdr *cap_hdr)
1182{
1183 char *eol, *sol, *col, *sov;
1184 int cur_idx;
1185 struct cap_hdr *h;
1186 int len;
1187
1188 sol = som + hdr_idx_first_pos(idx);
1189 cur_idx = hdr_idx_first_idx(idx);
1190
1191 while (cur_idx) {
1192 eol = sol + idx->v[cur_idx].len;
1193
1194 col = sol;
1195 while (col < eol && *col != ':')
1196 col++;
1197
1198 sov = col + 1;
1199 while (sov < eol && http_is_lws[(unsigned char)*sov])
1200 sov++;
1201
1202 for (h = cap_hdr; h; h = h->next) {
1203 if ((h->namelen == col - sol) &&
1204 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1205 if (cap[h->index] == NULL)
1206 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001207 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001208
1209 if (cap[h->index] == NULL) {
1210 Alert("HTTP capture : out of memory.\n");
1211 continue;
1212 }
1213
1214 len = eol - sov;
1215 if (len > h->len)
1216 len = h->len;
1217
1218 memcpy(cap[h->index], sov, len);
1219 cap[h->index][len]=0;
1220 }
1221 }
1222 sol = eol + idx->v[cur_idx].cr + 1;
1223 cur_idx = idx->v[cur_idx].next;
1224 }
1225}
1226
1227
Willy Tarreau42250582007-04-01 01:30:43 +02001228/* either we find an LF at <ptr> or we jump to <bad>.
1229 */
1230#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1231
1232/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1233 * otherwise to <http_msg_ood> with <state> set to <st>.
1234 */
1235#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1236 ptr++; \
1237 if (likely(ptr < end)) \
1238 goto good; \
1239 else { \
1240 state = (st); \
1241 goto http_msg_ood; \
1242 } \
1243 } while (0)
1244
1245
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001247 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1249 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1250 * will give undefined results.
1251 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1252 * and that msg->sol points to the beginning of the response.
1253 * If a complete line is found (which implies that at least one CR or LF is
1254 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1255 * returned indicating an incomplete line (which does not mean that parts have
1256 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1257 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1258 * upon next call.
1259 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001260 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001261 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1262 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001263 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001265const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1266 unsigned int state, const char *ptr, const char *end,
1267 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001268{
Willy Tarreau8973c702007-01-21 23:58:29 +01001269 switch (state) {
1270 http_msg_rpver:
1271 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001272 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001273 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1274
1275 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001276 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001277 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1278 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001279 state = HTTP_MSG_ERROR;
1280 break;
1281
Willy Tarreau8973c702007-01-21 23:58:29 +01001282 http_msg_rpver_sp:
1283 case HTTP_MSG_RPVER_SP:
1284 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001285 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001286 goto http_msg_rpcode;
1287 }
1288 if (likely(HTTP_IS_SPHT(*ptr)))
1289 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1290 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001291 state = HTTP_MSG_ERROR;
1292 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001293
1294 http_msg_rpcode:
1295 case HTTP_MSG_RPCODE:
1296 if (likely(!HTTP_IS_LWS(*ptr)))
1297 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1298
1299 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001300 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1302 }
1303
1304 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001305 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 http_msg_rsp_reason:
1307 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001308 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 msg->sl.st.r_l = 0;
1310 goto http_msg_rpline_eol;
1311
1312 http_msg_rpcode_sp:
1313 case HTTP_MSG_RPCODE_SP:
1314 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001315 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 goto http_msg_rpreason;
1317 }
1318 if (likely(HTTP_IS_SPHT(*ptr)))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1320 /* so it's a CR/LF, so there is no reason phrase */
1321 goto http_msg_rsp_reason;
1322
1323 http_msg_rpreason:
1324 case HTTP_MSG_RPREASON:
1325 if (likely(!HTTP_IS_CRLF(*ptr)))
1326 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001327 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 http_msg_rpline_eol:
1329 /* We have seen the end of line. Note that we do not
1330 * necessarily have the \n yet, but at least we know that we
1331 * have EITHER \r OR \n, otherwise the response would not be
1332 * complete. We can then record the response length and return
1333 * to the caller which will be able to register it.
1334 */
1335 msg->sl.st.l = ptr - msg->sol;
1336 return ptr;
1337
1338#ifdef DEBUG_FULL
1339 default:
1340 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1341 exit(1);
1342#endif
1343 }
1344
1345 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001346 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 if (ret_state)
1348 *ret_state = state;
1349 if (ret_ptr)
1350 *ret_ptr = (char *)ptr;
1351 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001352}
1353
1354
1355/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 * This function parses a request line between <ptr> and <end>, starting with
1357 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1358 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1359 * will give undefined results.
1360 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1361 * and that msg->sol points to the beginning of the request.
1362 * If a complete line is found (which implies that at least one CR or LF is
1363 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1364 * returned indicating an incomplete line (which does not mean that parts have
1365 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1366 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1367 * upon next call.
1368 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001369 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1371 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001372 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001373 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001374const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1375 unsigned int state, const char *ptr, const char *end,
1376 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 switch (state) {
1379 http_msg_rqmeth:
1380 case HTTP_MSG_RQMETH:
1381 if (likely(HTTP_IS_TOKEN(*ptr)))
1382 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (likely(HTTP_IS_SPHT(*ptr))) {
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 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1387 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (likely(HTTP_IS_CRLF(*ptr))) {
1390 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001391 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001393 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001395 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001397 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 msg->sl.rq.v_l = 0;
1399 goto http_msg_rqline_eol;
1400 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001401 state = HTTP_MSG_ERROR;
1402 break;
1403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 http_msg_rqmeth_sp:
1405 case HTTP_MSG_RQMETH_SP:
1406 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001407 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 goto http_msg_rquri;
1409 }
1410 if (likely(HTTP_IS_SPHT(*ptr)))
1411 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1412 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1413 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 http_msg_rquri:
1416 case HTTP_MSG_RQURI:
1417 if (likely(!HTTP_IS_LWS(*ptr)))
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001421 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1426 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 http_msg_rquri_sp:
1429 case HTTP_MSG_RQURI_SP:
1430 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001431 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 goto http_msg_rqver;
1433 }
1434 if (likely(HTTP_IS_SPHT(*ptr)))
1435 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1436 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1437 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 http_msg_rqver:
1440 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001441 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001443
1444 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001445 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001446 http_msg_rqline_eol:
1447 /* We have seen the end of line. Note that we do not
1448 * necessarily have the \n yet, but at least we know that we
1449 * have EITHER \r OR \n, otherwise the request would not be
1450 * complete. We can then record the request length and return
1451 * to the caller which will be able to register it.
1452 */
1453 msg->sl.rq.l = ptr - msg->sol;
1454 return ptr;
1455 }
1456
1457 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001458 state = HTTP_MSG_ERROR;
1459 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461#ifdef DEBUG_FULL
1462 default:
1463 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1464 exit(1);
1465#endif
1466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001469 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 if (ret_state)
1471 *ret_state = state;
1472 if (ret_ptr)
1473 *ret_ptr = (char *)ptr;
1474 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001476
1477
Willy Tarreau8973c702007-01-21 23:58:29 +01001478/*
1479 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 * when data are missing and recalled at the exact same location with no
1482 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001483 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001484 * fields. Note that msg->som and msg->sol will be initialized after completing
1485 * the first state, so that none of the msg pointers has to be initialized
1486 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1489{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001490 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001492
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001493 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 ptr = buf->lr;
1495 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (unlikely(ptr >= end))
1498 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 /*
1502 * First, states that are specific to the response only.
1503 * We check them first so that request and headers are
1504 * closer to each other (accessed more often).
1505 */
1506 http_msg_rpbefore:
1507 case HTTP_MSG_RPBEFORE:
1508 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001509 /* we have a start of message, but we have to check
1510 * first if we need to remove some CRLF. We can only
1511 * do this when send_max=0.
1512 */
1513 char *beg = buf->w + buf->send_max;
1514 if (beg >= buf->data + buf->size)
1515 beg -= buf->size;
1516 if (unlikely(ptr != beg)) {
1517 if (buf->send_max)
1518 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001519 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001520 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001522 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001523 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 hdr_idx_init(idx);
1525 state = HTTP_MSG_RPVER;
1526 goto http_msg_rpver;
1527 }
1528
1529 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1530 goto http_msg_invalid;
1531
1532 if (unlikely(*ptr == '\n'))
1533 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1535 /* stop here */
1536
1537 http_msg_rpbefore_cr:
1538 case HTTP_MSG_RPBEFORE_CR:
1539 EXPECT_LF_HERE(ptr, http_msg_invalid);
1540 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1541 /* stop here */
1542
1543 http_msg_rpver:
1544 case HTTP_MSG_RPVER:
1545 case HTTP_MSG_RPVER_SP:
1546 case HTTP_MSG_RPCODE:
1547 case HTTP_MSG_RPCODE_SP:
1548 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001549 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001550 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001551 if (unlikely(!ptr))
1552 return;
1553
1554 /* we have a full response and we know that we have either a CR
1555 * or an LF at <ptr>.
1556 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001557 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001558 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1559
1560 msg->sol = ptr;
1561 if (likely(*ptr == '\r'))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1563 goto http_msg_rpline_end;
1564
1565 http_msg_rpline_end:
1566 case HTTP_MSG_RPLINE_END:
1567 /* msg->sol must point to the first of CR or LF. */
1568 EXPECT_LF_HERE(ptr, http_msg_invalid);
1569 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1570 /* stop here */
1571
1572 /*
1573 * Second, states that are specific to the request only
1574 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 http_msg_rqbefore:
1576 case HTTP_MSG_RQBEFORE:
1577 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001578 /* we have a start of message, but we have to check
1579 * first if we need to remove some CRLF. We can only
1580 * do this when send_max=0.
1581 */
1582 char *beg = buf->w + buf->send_max;
1583 if (beg >= buf->data + buf->size)
1584 beg -= buf->size;
1585 if (likely(ptr != beg)) {
1586 if (buf->send_max)
1587 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001588 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001589 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001591 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001592 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001593 /* we will need this when keep-alive will be supported
1594 hdr_idx_init(idx);
1595 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001596 state = HTTP_MSG_RQMETH;
1597 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1601 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (unlikely(*ptr == '\n'))
1604 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1605 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
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_rqbefore_cr:
1609 case HTTP_MSG_RQBEFORE_CR:
1610 EXPECT_LF_HERE(ptr, http_msg_invalid);
1611 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 http_msg_rqmeth:
1615 case HTTP_MSG_RQMETH:
1616 case HTTP_MSG_RQMETH_SP:
1617 case HTTP_MSG_RQURI:
1618 case HTTP_MSG_RQURI_SP:
1619 case HTTP_MSG_RQVER:
1620 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001621 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 if (unlikely(!ptr))
1623 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 /* we have a full request and we know that we have either a CR
1626 * or an LF at <ptr>.
1627 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001628 //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 +01001629 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 msg->sol = ptr;
1632 if (likely(*ptr == '\r'))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 http_msg_rqline_end:
1637 case HTTP_MSG_RQLINE_END:
1638 /* check for HTTP/0.9 request : no version information available.
1639 * msg->sol must point to the first of CR or LF.
1640 */
1641 if (unlikely(msg->sl.rq.v_l == 0))
1642 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 EXPECT_LF_HERE(ptr, http_msg_invalid);
1645 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8973c702007-01-21 23:58:29 +01001648 /*
1649 * Common states below
1650 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 http_msg_hdr_first:
1652 case HTTP_MSG_HDR_FIRST:
1653 msg->sol = ptr;
1654 if (likely(!HTTP_IS_CRLF(*ptr))) {
1655 goto http_msg_hdr_name;
1656 }
1657
1658 if (likely(*ptr == '\r'))
1659 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1660 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 http_msg_hdr_name:
1663 case HTTP_MSG_HDR_NAME:
1664 /* assumes msg->sol points to the first char */
1665 if (likely(HTTP_IS_TOKEN(*ptr)))
1666 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 if (likely(*ptr == ':')) {
1669 msg->col = ptr - buf->data;
1670 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1671 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001672
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001673 if (likely(msg->err_pos < -1) || *ptr == '\n')
1674 goto http_msg_invalid;
1675
1676 if (msg->err_pos == -1) /* capture error pointer */
1677 msg->err_pos = ptr - buf->data; /* >= 0 now */
1678
1679 /* and we still accept this non-token character */
1680 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 http_msg_hdr_l1_sp:
1683 case HTTP_MSG_HDR_L1_SP:
1684 /* assumes msg->sol points to the first char and msg->col to the colon */
1685 if (likely(HTTP_IS_SPHT(*ptr)))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 /* header value can be basically anything except CR/LF */
1689 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001690
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 if (likely(!HTTP_IS_CRLF(*ptr))) {
1692 goto http_msg_hdr_val;
1693 }
1694
1695 if (likely(*ptr == '\r'))
1696 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1697 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 http_msg_hdr_l1_lf:
1700 case HTTP_MSG_HDR_L1_LF:
1701 EXPECT_LF_HERE(ptr, http_msg_invalid);
1702 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001703
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 http_msg_hdr_l1_lws:
1705 case HTTP_MSG_HDR_L1_LWS:
1706 if (likely(HTTP_IS_SPHT(*ptr))) {
1707 /* replace HT,CR,LF with spaces */
1708 for (; buf->data+msg->sov < ptr; msg->sov++)
1709 buf->data[msg->sov] = ' ';
1710 goto http_msg_hdr_l1_sp;
1711 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001712 /* we had a header consisting only in spaces ! */
1713 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 goto http_msg_complete_header;
1715
1716 http_msg_hdr_val:
1717 case HTTP_MSG_HDR_VAL:
1718 /* assumes msg->sol points to the first char, msg->col to the
1719 * colon, and msg->sov points to the first character of the
1720 * value.
1721 */
1722 if (likely(!HTTP_IS_CRLF(*ptr)))
1723 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 msg->eol = ptr;
1726 /* Note: we could also copy eol into ->eoh so that we have the
1727 * real header end in case it ends with lots of LWS, but is this
1728 * really needed ?
1729 */
1730 if (likely(*ptr == '\r'))
1731 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1732 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 http_msg_hdr_l2_lf:
1735 case HTTP_MSG_HDR_L2_LF:
1736 EXPECT_LF_HERE(ptr, http_msg_invalid);
1737 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 http_msg_hdr_l2_lws:
1740 case HTTP_MSG_HDR_L2_LWS:
1741 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1742 /* LWS: replace HT,CR,LF with spaces */
1743 for (; msg->eol < ptr; msg->eol++)
1744 *msg->eol = ' ';
1745 goto http_msg_hdr_val;
1746 }
1747 http_msg_complete_header:
1748 /*
1749 * It was a new header, so the last one is finished.
1750 * Assumes msg->sol points to the first char, msg->col to the
1751 * colon, msg->sov points to the first character of the value
1752 * and msg->eol to the first CR or LF so we know how the line
1753 * ends. We insert last header into the index.
1754 */
1755 /*
1756 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1757 write(2, msg->sol, msg->eol-msg->sol);
1758 fprintf(stderr,"\n");
1759 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1762 idx, idx->tail) < 0))
1763 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001764
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 msg->sol = ptr;
1766 if (likely(!HTTP_IS_CRLF(*ptr))) {
1767 goto http_msg_hdr_name;
1768 }
1769
1770 if (likely(*ptr == '\r'))
1771 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1772 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 http_msg_last_lf:
1775 case HTTP_MSG_LAST_LF:
1776 /* Assumes msg->sol points to the first of either CR or LF */
1777 EXPECT_LF_HERE(ptr, http_msg_invalid);
1778 ptr++;
1779 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001780 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001781 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001782 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001783 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 return;
1785#ifdef DEBUG_FULL
1786 default:
1787 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1788 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001789#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 }
1791 http_msg_ood:
1792 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001793 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 buf->lr = ptr;
1795 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 http_msg_invalid:
1798 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001799 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001800 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 return;
1802}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001803
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001804/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1805 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1806 * nothing is done and 1 is returned.
1807 */
1808static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1809{
1810 int delta;
1811 char *cur_end;
1812
1813 if (msg->sl.rq.v_l != 0)
1814 return 1;
1815
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001816 cur_end = msg->sol + msg->sl.rq.l;
1817 delta = 0;
1818
1819 if (msg->sl.rq.u_l == 0) {
1820 /* if no URI was set, add "/" */
1821 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1822 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001823 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001824 }
1825 /* add HTTP version */
1826 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001827 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001828 cur_end += delta;
1829 cur_end = (char *)http_parse_reqline(msg, req->data,
1830 HTTP_MSG_RQMETH,
1831 msg->sol, cur_end + 1,
1832 NULL, NULL);
1833 if (unlikely(!cur_end))
1834 return 0;
1835
1836 /* we have a full HTTP/1.0 request now and we know that
1837 * we have either a CR or an LF at <ptr>.
1838 */
1839 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1840 return 1;
1841}
1842
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001843/* Parse the Connection: header of an HTTP request, looking for both "close"
1844 * and "keep-alive" values. If a buffer is provided and we already know that
1845 * some headers may safely be removed, we remove them now. The <to_del> flags
1846 * are used for that :
1847 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1848 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1849 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1850 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1851 * harmless combinations may be removed. Do not call that after changes have
1852 * been processed. If unused, the buffer can be NULL, and no data will be
1853 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001854 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001855void 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 +01001856{
Willy Tarreau5b154472009-12-21 20:11:07 +01001857 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001858 const char *hdr_val = "Connection";
1859 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001860
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001861 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001862 return;
1863
Willy Tarreau88d349d2010-01-25 12:15:43 +01001864 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1865 hdr_val = "Proxy-Connection";
1866 hdr_len = 16;
1867 }
1868
Willy Tarreau5b154472009-12-21 20:11:07 +01001869 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001871 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1873 txn->flags |= TX_HDR_CONN_KAL;
1874 if ((to_del & 2) && buf)
1875 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1876 else
1877 txn->flags |= TX_CON_KAL_SET;
1878 }
1879 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1880 txn->flags |= TX_HDR_CONN_CLO;
1881 if ((to_del & 1) && buf)
1882 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1883 else
1884 txn->flags |= TX_CON_CLO_SET;
1885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
1887
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001888 txn->flags |= TX_HDR_CONN_PRS;
1889 return;
1890}
Willy Tarreau5b154472009-12-21 20:11:07 +01001891
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892/* Apply desired changes on the Connection: header. Values may be removed and/or
1893 * added depending on the <wanted> flags, which are exclusively composed of
1894 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1895 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1896 */
1897void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1898{
1899 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001900 const char *hdr_val = "Connection";
1901 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001902
1903 ctx.idx = 0;
1904
Willy Tarreau88d349d2010-01-25 12:15:43 +01001905
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection";
1908 hdr_len = 16;
1909 }
1910
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001912 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1914 if (wanted & TX_CON_KAL_SET)
1915 txn->flags |= TX_CON_KAL_SET;
1916 else
1917 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001918 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001919 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1920 if (wanted & TX_CON_CLO_SET)
1921 txn->flags |= TX_CON_CLO_SET;
1922 else
1923 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001924 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001925 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001926
1927 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1928 return;
1929
1930 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1931 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001932 hdr_val = "Connection: close";
1933 hdr_len = 17;
1934 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1935 hdr_val = "Proxy-Connection: close";
1936 hdr_len = 23;
1937 }
1938 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001939 }
1940
1941 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1942 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001943 hdr_val = "Connection: keep-alive";
1944 hdr_len = 22;
1945 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1946 hdr_val = "Proxy-Connection: keep-alive";
1947 hdr_len = 28;
1948 }
1949 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001950 }
1951 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001952}
1953
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1955 * first byte of body, and increments msg->sov by the number of bytes parsed,
1956 * so that we know we can forward between ->som and ->sov. Note that due to
1957 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1958 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001960 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001961 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001963{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 char *ptr = buf->lr;
1965 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001966 unsigned int chunk = 0;
1967
1968 /* The chunk size is in the following form, though we are only
1969 * interested in the size and CRLF :
1970 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1971 */
1972 while (1) {
1973 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001975 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001977 if (c < 0) /* not a hex digit anymore */
1978 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 if (++ptr >= end)
1980 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001981 if (chunk & 0xF000000) /* overflow will occur */
1982 return -1;
1983 chunk = (chunk << 4) + c;
1984 }
1985
Willy Tarreaud98cf932009-12-27 22:54:55 +01001986 /* empty size not allowed */
1987 if (ptr == buf->lr)
1988 return -1;
1989
1990 while (http_is_spht[(unsigned char)*ptr]) {
1991 if (++ptr >= end)
1992 ptr = buf->data;
1993 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001995 }
1996
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 /* Up to there, we know that at least one byte is present at *ptr. Check
1998 * for the end of chunk size.
1999 */
2000 while (1) {
2001 if (likely(HTTP_IS_CRLF(*ptr))) {
2002 /* we now have a CR or an LF at ptr */
2003 if (likely(*ptr == '\r')) {
2004 if (++ptr >= end)
2005 ptr = buf->data;
2006 if (ptr == buf->r)
2007 return 0;
2008 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002009
Willy Tarreaud98cf932009-12-27 22:54:55 +01002010 if (*ptr != '\n')
2011 return -1;
2012 if (++ptr >= end)
2013 ptr = buf->data;
2014 /* done */
2015 break;
2016 }
2017 else if (*ptr == ';') {
2018 /* chunk extension, ends at next CRLF */
2019 if (++ptr >= end)
2020 ptr = buf->data;
2021 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002022 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002023
2024 while (!HTTP_IS_CRLF(*ptr)) {
2025 if (++ptr >= end)
2026 ptr = buf->data;
2027 if (ptr == buf->r)
2028 return 0;
2029 }
2030 /* we have a CRLF now, loop above */
2031 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002032 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002034 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002035 }
2036
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 /* OK we found our CRLF and now <ptr> points to the next byte,
2038 * which may or may not be present. We save that into ->lr and
2039 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002040 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041 msg->sov += ptr - buf->lr;
2042 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002043 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002044 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002045 return 1;
2046}
2047
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048/* This function skips trailers in the buffer <buf> associated with HTTP
2049 * message <msg>. The first visited position is buf->lr. If the end of
2050 * the trailers is found, it is automatically scheduled to be forwarded,
2051 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2052 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002053 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2054 * zero. If a parse error is encountered, the function returns < 0 and does not
2055 * change anything except maybe buf->lr and msg->sov. Note that the message
2056 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2057 * which implies that all non-trailers data have already been scheduled for
2058 * forwarding, and that the difference between msg->som and msg->sov exactly
2059 * matches the length of trailers already parsed and not forwarded. It is also
2060 * important to note that this function is designed to be able to parse wrapped
2061 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 */
2063int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2064{
2065 /* we have buf->lr which points to next line. Look for CRLF. */
2066 while (1) {
2067 char *p1 = NULL, *p2 = NULL;
2068 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002069 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070
2071 /* scan current line and stop at LF or CRLF */
2072 while (1) {
2073 if (ptr == buf->r)
2074 return 0;
2075
2076 if (*ptr == '\n') {
2077 if (!p1)
2078 p1 = ptr;
2079 p2 = ptr;
2080 break;
2081 }
2082
2083 if (*ptr == '\r') {
2084 if (p1)
2085 return -1;
2086 p1 = ptr;
2087 }
2088
2089 ptr++;
2090 if (ptr >= buf->data + buf->size)
2091 ptr = buf->data;
2092 }
2093
2094 /* after LF; point to beginning of next line */
2095 p2++;
2096 if (p2 >= buf->data + buf->size)
2097 p2 = buf->data;
2098
Willy Tarreau638cd022010-01-03 07:42:04 +01002099 bytes = p2 - buf->lr;
2100 if (bytes < 0)
2101 bytes += buf->size;
2102
2103 /* schedule this line for forwarding */
2104 msg->sov += bytes;
2105 if (msg->sov >= buf->size)
2106 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107
Willy Tarreau638cd022010-01-03 07:42:04 +01002108 if (p1 == buf->lr) {
2109 /* LF/CRLF at beginning of line => end of trailers at p2.
2110 * Everything was scheduled for forwarding, there's nothing
2111 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002112 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002113 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002114 msg->msg_state = HTTP_MSG_DONE;
2115 return 1;
2116 }
2117 /* OK, next line then */
2118 buf->lr = p2;
2119 }
2120}
2121
2122/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2123 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2124 * ->som, buf->lr in order to include this part into the next forwarding phase.
2125 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2126 * not enough data are available, the function does not change anything and
2127 * returns zero. If a parse error is encountered, the function returns < 0 and
2128 * does not change anything. Note: this function is designed to parse wrapped
2129 * CRLF at the end of the buffer.
2130 */
2131int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2132{
2133 char *ptr;
2134 int bytes;
2135
2136 /* NB: we'll check data availabilty at the end. It's not a
2137 * problem because whatever we match first will be checked
2138 * against the correct length.
2139 */
2140 bytes = 1;
2141 ptr = buf->lr;
2142 if (*ptr == '\r') {
2143 bytes++;
2144 ptr++;
2145 if (ptr >= buf->data + buf->size)
2146 ptr = buf->data;
2147 }
2148
2149 if (buf->l < bytes)
2150 return 0;
2151
2152 if (*ptr != '\n')
2153 return -1;
2154
2155 ptr++;
2156 if (ptr >= buf->data + buf->size)
2157 ptr = buf->data;
2158 buf->lr = ptr;
2159 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2160 msg->sov = ptr - buf->data;
2161 msg->som = msg->sov - bytes;
2162 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2163 return 1;
2164}
Willy Tarreau5b154472009-12-21 20:11:07 +01002165
Willy Tarreau83e3af02009-12-28 17:39:57 +01002166void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2167{
2168 char *end = buf->data + buf->size;
2169 int off = buf->data + buf->size - buf->w;
2170
2171 /* two possible cases :
2172 * - the buffer is in one contiguous block, we move it in-place
2173 * - the buffer is in two blocks, we move it via the trash
2174 */
2175 if (buf->l) {
2176 int block1 = buf->l;
2177 int block2 = 0;
2178 if (buf->r <= buf->w) {
2179 /* non-contiguous block */
2180 block1 = buf->data + buf->size - buf->w;
2181 block2 = buf->r - buf->data;
2182 }
2183 if (block2)
2184 memcpy(trash, buf->data, block2);
2185 memmove(buf->data, buf->w, block1);
2186 if (block2)
2187 memcpy(buf->data + block1, trash, block2);
2188 }
2189
2190 /* adjust all known pointers */
2191 buf->w = buf->data;
2192 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2193 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2194 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2195 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2196
2197 /* adjust relative pointers */
2198 msg->som = 0;
2199 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2200 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2201 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2202
Willy Tarreau83e3af02009-12-28 17:39:57 +01002203 if (msg->err_pos >= 0) {
2204 msg->err_pos += off;
2205 if (msg->err_pos >= buf->size)
2206 msg->err_pos -= buf->size;
2207 }
2208
2209 buf->flags &= ~BF_FULL;
2210 if (buf->l >= buffer_max_len(buf))
2211 buf->flags |= BF_FULL;
2212}
2213
Willy Tarreaud787e662009-07-07 10:14:51 +02002214/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2215 * processing can continue on next analysers, or zero if it either needs more
2216 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2217 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2218 * when it has nothing left to do, and may remove any analyser when it wants to
2219 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002220 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002221int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002222{
Willy Tarreau59234e92008-11-30 23:51:27 +01002223 /*
2224 * We will parse the partial (or complete) lines.
2225 * We will check the request syntax, and also join multi-line
2226 * headers. An index of all the lines will be elaborated while
2227 * parsing.
2228 *
2229 * For the parsing, we use a 28 states FSM.
2230 *
2231 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002232 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002233 * req->data + msg->eoh = end of processed headers / start of current one
2234 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002235 * req->lr = first non-visited byte
2236 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002237 *
2238 * At end of parsing, we may perform a capture of the error (if any), and
2239 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2240 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2241 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002242 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002243
Willy Tarreau59234e92008-11-30 23:51:27 +01002244 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002245 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 struct http_txn *txn = &s->txn;
2247 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002248 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002249
Willy Tarreau6bf17362009-02-24 10:48:35 +01002250 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2251 now_ms, __FUNCTION__,
2252 s,
2253 req,
2254 req->rex, req->wex,
2255 req->flags,
2256 req->l,
2257 req->analysers);
2258
Willy Tarreau52a0c602009-08-16 22:45:38 +02002259 /* we're speaking HTTP here, so let's speak HTTP to the client */
2260 s->srv_error = http_return_srv_error;
2261
Willy Tarreau83e3af02009-12-28 17:39:57 +01002262 /* There's a protected area at the end of the buffer for rewriting
2263 * purposes. We don't want to start to parse the request if the
2264 * protected area is affected, because we may have to move processed
2265 * data later, which is much more complicated.
2266 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002267 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002268 if ((txn->flags & TX_NOT_FIRST) &&
2269 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002270 req->r < req->lr ||
2271 req->r > req->data + req->size - global.tune.maxrewrite)) {
2272 if (req->send_max) {
2273 /* some data has still not left the buffer, wake us once that's done */
2274 buffer_dont_connect(req);
2275 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2276 return 0;
2277 }
2278 if (req->l <= req->size - global.tune.maxrewrite)
2279 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002280 }
2281
Willy Tarreau065e8332010-01-08 00:30:20 +01002282 /* Note that we have the same problem with the response ; we
2283 * may want to send a redirect, error or anything which requires
2284 * some spare space. So we'll ensure that we have at least
2285 * maxrewrite bytes available in the response buffer before
2286 * processing that one. This will only affect pipelined
2287 * keep-alive requests.
2288 */
2289 if ((txn->flags & TX_NOT_FIRST) &&
2290 unlikely((s->rep->flags & BF_FULL) ||
2291 s->rep->r < s->rep->lr ||
2292 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2293 if (s->rep->send_max) {
2294 /* don't let a connection request be initiated */
2295 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002296 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002297 return 0;
2298 }
2299 }
2300
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002301 if (likely(req->lr < req->r))
2302 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002303 }
2304
Willy Tarreau59234e92008-11-30 23:51:27 +01002305 /* 1: we might have to print this header in debug mode */
2306 if (unlikely((global.mode & MODE_DEBUG) &&
2307 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002308 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau962c3f42010-01-10 00:15:35 +01002311 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 eol = sol + msg->sl.rq.l;
2313 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002314
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 sol += hdr_idx_first_pos(&txn->hdr_idx);
2316 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002317
Willy Tarreau59234e92008-11-30 23:51:27 +01002318 while (cur_idx) {
2319 eol = sol + txn->hdr_idx.v[cur_idx].len;
2320 debug_hdr("clihdr", s, sol, eol);
2321 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2322 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002323 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 }
2325
Willy Tarreau58f10d72006-12-04 02:26:12 +01002326
Willy Tarreau59234e92008-11-30 23:51:27 +01002327 /*
2328 * Now we quickly check if we have found a full valid request.
2329 * If not so, we check the FD and buffer states before leaving.
2330 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002331 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002332 * requests are checked first. When waiting for a second request
2333 * on a keep-alive session, if we encounter and error, close, t/o,
2334 * we note the error in the session flags but don't set any state.
2335 * Since the error will be noted there, it will not be counted by
2336 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002338
Willy Tarreau655dce92009-11-08 13:10:58 +01002339 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002340 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2344 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 /* 1: Since we are in header mode, if there's no space
2347 * left for headers, we won't be able to free more
2348 * later, so the session will never terminate. We
2349 * must terminate it now.
2350 */
2351 if (unlikely(req->flags & BF_FULL)) {
2352 /* FIXME: check if URI is set and return Status
2353 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002354 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 goto return_bad_req;
2356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 /* 2: have we encountered a read error ? */
2359 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002360 if (!(s->flags & SN_ERR_MASK))
2361 s->flags |= SN_ERR_CLICL;
2362
Willy Tarreaufcffa692010-01-10 14:21:19 +01002363 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002364 goto failed_keep_alive;
2365
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002367 if (msg->err_pos >= 0)
2368 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 msg->msg_state = HTTP_MSG_ERROR;
2370 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002371
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002372 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002373 if (s->listener->counters)
2374 s->listener->counters->failed_req++;
2375
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 if (!(s->flags & SN_FINST_MASK))
2377 s->flags |= SN_FINST_R;
2378 return 0;
2379 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 /* 3: has the read timeout expired ? */
2382 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002383 if (!(s->flags & SN_ERR_MASK))
2384 s->flags |= SN_ERR_CLITO;
2385
Willy Tarreaufcffa692010-01-10 14:21:19 +01002386 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002387 goto failed_keep_alive;
2388
Willy Tarreau59234e92008-11-30 23:51:27 +01002389 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002390 if (msg->err_pos >= 0)
2391 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002392 txn->status = 408;
2393 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2394 msg->msg_state = HTTP_MSG_ERROR;
2395 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002396
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002397 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002398 if (s->listener->counters)
2399 s->listener->counters->failed_req++;
2400
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 if (!(s->flags & SN_FINST_MASK))
2402 s->flags |= SN_FINST_R;
2403 return 0;
2404 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002405
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 /* 4: have we encountered a close ? */
2407 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002408 if (!(s->flags & SN_ERR_MASK))
2409 s->flags |= SN_ERR_CLICL;
2410
Willy Tarreaufcffa692010-01-10 14:21:19 +01002411 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002412 goto failed_keep_alive;
2413
Willy Tarreau4076a152009-04-02 15:18:36 +02002414 if (msg->err_pos >= 0)
2415 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 txn->status = 400;
2417 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2418 msg->msg_state = HTTP_MSG_ERROR;
2419 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002420
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002421 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002422 if (s->listener->counters)
2423 s->listener->counters->failed_req++;
2424
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 if (!(s->flags & SN_FINST_MASK))
2426 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002427 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002428 }
2429
Willy Tarreau520d95e2009-09-19 21:04:57 +02002430 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002431 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002432 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002433
Willy Tarreaufcffa692010-01-10 14:21:19 +01002434 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2435 /* If the client starts to talk, let's fall back to
2436 * request timeout processing.
2437 */
2438 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002439 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002440 }
2441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002443 if (!tick_isset(req->analyse_exp)) {
2444 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2445 (txn->flags & TX_WAIT_NEXT_RQ) &&
2446 tick_isset(s->be->timeout.httpka))
2447 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2448 else
2449 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002451
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 /* we're not ready yet */
2453 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002454
2455 failed_keep_alive:
2456 /* Here we process low-level errors for keep-alive requests. In
2457 * short, if the request is not the first one and it experiences
2458 * a timeout, read error or shutdown, we just silently close so
2459 * that the client can try again.
2460 */
2461 txn->status = 0;
2462 msg->msg_state = HTTP_MSG_RQBEFORE;
2463 req->analysers = 0;
2464 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002465 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002466 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002467 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002469
Willy Tarreaud787e662009-07-07 10:14:51 +02002470 /* OK now we have a complete HTTP request with indexed headers. Let's
2471 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002472 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2473 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2474 * points to the CRLF of the request line. req->lr points to the first
2475 * byte after the last LF. msg->col and msg->sov point to the first
2476 * byte of data. msg->eol cannot be trusted because it may have been
2477 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002478 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002479
Willy Tarreaub16a5742010-01-10 14:46:16 +01002480 if (txn->flags & TX_WAIT_NEXT_RQ) {
2481 /* kill the pending keep-alive timeout */
2482 txn->flags &= ~TX_WAIT_NEXT_RQ;
2483 req->analyse_exp = TICK_ETERNITY;
2484 }
2485
2486
Willy Tarreaud787e662009-07-07 10:14:51 +02002487 /* Maybe we found in invalid header name while we were configured not
2488 * to block on that, so we have to capture it now.
2489 */
2490 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002491 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /*
2494 * 1: identify the method
2495 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002496 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002497
2498 /* we can make use of server redirect on GET and HEAD */
2499 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2500 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 /*
2503 * 2: check if the URI matches the monitor_uri.
2504 * We have to do this for every request which gets in, because
2505 * the monitor-uri is defined by the frontend.
2506 */
2507 if (unlikely((s->fe->monitor_uri_len != 0) &&
2508 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002509 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 s->fe->monitor_uri,
2511 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002512 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002514 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002520 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2521 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 ret = acl_pass(ret);
2524 if (cond->pol == ACL_COND_UNLESS)
2525 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 if (ret) {
2528 /* we fail this request, let's return 503 service unavail */
2529 txn->status = 503;
2530 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2531 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002532 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002533 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002534
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 /* nothing to fail, let's reply normaly */
2536 txn->status = 200;
2537 stream_int_retnclose(req->prod, &http_200_chunk);
2538 goto return_prx_cond;
2539 }
2540
2541 /*
2542 * 3: Maybe we have to copy the original REQURI for the logs ?
2543 * Note: we cannot log anymore if the request has been
2544 * classified as invalid.
2545 */
2546 if (unlikely(s->logs.logwait & LW_REQ)) {
2547 /* we have a complete HTTP request that we must log */
2548 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2549 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002550
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 if (urilen >= REQURI_LEN)
2552 urilen = REQURI_LEN - 1;
2553 memcpy(txn->uri, &req->data[msg->som], urilen);
2554 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002555
Willy Tarreau59234e92008-11-30 23:51:27 +01002556 if (!(s->logs.logwait &= ~LW_REQ))
2557 s->do_log(s);
2558 } else {
2559 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002560 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 }
Willy Tarreau06619262006-12-17 08:37:22 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002564 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2565 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002566
Willy Tarreau5b154472009-12-21 20:11:07 +01002567 /* ... and check if the request is HTTP/1.1 or above */
2568 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002569 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2570 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2571 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002572 txn->flags |= TX_REQ_VER_11;
2573
2574 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002575 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002576
Willy Tarreau88d349d2010-01-25 12:15:43 +01002577 /* if the frontend has "option http-use-proxy-header", we'll check if
2578 * we have what looks like a proxied connection instead of a connection,
2579 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2580 * Note that this is *not* RFC-compliant, however browsers and proxies
2581 * happen to do that despite being non-standard :-(
2582 * We consider that a request not beginning with either '/' or '*' is
2583 * a proxied connection, which covers both "scheme://location" and
2584 * CONNECT ip:port.
2585 */
2586 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2587 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2588 txn->flags |= TX_USE_PX_CONN;
2589
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002590 /* transfer length unknown*/
2591 txn->flags &= ~TX_REQ_XFER_LEN;
2592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /* 5: we may need to capture headers */
2594 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002595 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002597
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002598 /* 6: determine the transfer-length.
2599 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2600 * the presence of a message-body in a REQUEST and its transfer length
2601 * must be determined that way (in order of precedence) :
2602 * 1. The presence of a message-body in a request is signaled by the
2603 * inclusion of a Content-Length or Transfer-Encoding header field
2604 * in the request's header fields. When a request message contains
2605 * both a message-body of non-zero length and a method that does
2606 * not define any semantics for that request message-body, then an
2607 * origin server SHOULD either ignore the message-body or respond
2608 * with an appropriate error message (e.g., 413). A proxy or
2609 * gateway, when presented the same request, SHOULD either forward
2610 * the request inbound with the message- body or ignore the
2611 * message-body when determining a response.
2612 *
2613 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2614 * and the "chunked" transfer-coding (Section 6.2) is used, the
2615 * transfer-length is defined by the use of this transfer-coding.
2616 * If a Transfer-Encoding header field is present and the "chunked"
2617 * transfer-coding is not present, the transfer-length is defined
2618 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002619 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002620 * 3. If a Content-Length header field is present, its decimal value in
2621 * OCTETs represents both the entity-length and the transfer-length.
2622 * If a message is received with both a Transfer-Encoding header
2623 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002625 * 4. By the server closing the connection. (Closing the connection
2626 * cannot be used to indicate the end of a request body, since that
2627 * would leave no possibility for the server to send back a response.)
2628 *
2629 * Whenever a transfer-coding is applied to a message-body, the set of
2630 * transfer-codings MUST include "chunked", unless the message indicates
2631 * it is terminated by closing the connection. When the "chunked"
2632 * transfer-coding is used, it MUST be the last transfer-coding applied
2633 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002634 */
2635
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002636 /* CONNECT sets a tunnel and ignores everything else */
2637 if (txn->meth == HTTP_METH_CONNECT)
2638 goto skip_xfer_len;
2639
2640 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002641 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002642 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002643 while ((txn->flags & TX_REQ_VER_11) &&
2644 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002645 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2646 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2647 else if (txn->flags & TX_REQ_TE_CHNK) {
2648 /* bad transfer-encoding (chunked followed by something else) */
2649 use_close_only = 1;
2650 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2651 break;
2652 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002653 }
2654
Willy Tarreau32b47f42009-10-18 20:55:02 +02002655 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002656 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002657 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2658 signed long long cl;
2659
2660 if (!ctx.vlen)
2661 goto return_bad_req;
2662
2663 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2664 goto return_bad_req; /* parse failure */
2665
2666 if (cl < 0)
2667 goto return_bad_req;
2668
2669 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2670 goto return_bad_req; /* already specified, was different */
2671
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002672 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002673 msg->hdr_content_len = cl;
2674 }
2675
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002676 /* bodyless requests have a known length */
2677 if (!use_close_only)
2678 txn->flags |= TX_REQ_XFER_LEN;
2679
2680 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002681 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002682 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002683 req->analyse_exp = TICK_ETERNITY;
2684 return 1;
2685
2686 return_bad_req:
2687 /* We centralize bad requests processing here */
2688 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2689 /* we detected a parsing error. We want to archive this request
2690 * in the dedicated proxy area for later troubleshooting.
2691 */
2692 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2693 }
2694
2695 txn->req.msg_state = HTTP_MSG_ERROR;
2696 txn->status = 400;
2697 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002698
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002699 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002700 if (s->listener->counters)
2701 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002702
2703 return_prx_cond:
2704 if (!(s->flags & SN_ERR_MASK))
2705 s->flags |= SN_ERR_PRXCOND;
2706 if (!(s->flags & SN_FINST_MASK))
2707 s->flags |= SN_FINST_R;
2708
2709 req->analysers = 0;
2710 req->analyse_exp = TICK_ETERNITY;
2711 return 0;
2712}
2713
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714/* This stream analyser runs all HTTP request processing which is common to
2715 * frontends and backends, which means blocking ACLs, filters, connection-close,
2716 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002717 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002718 * either needs more data or wants to immediately abort the request (eg: deny,
2719 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002720 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002721int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002722{
Willy Tarreaud787e662009-07-07 10:14:51 +02002723 struct http_txn *txn = &s->txn;
2724 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002725 struct acl_cond *cond;
2726 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002727 struct cond_wordlist *wl;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002728 int del_ka, del_cl;
Willy Tarreaud787e662009-07-07 10:14:51 +02002729
Willy Tarreau655dce92009-11-08 13:10:58 +01002730 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002731 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002732 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002733 return 0;
2734 }
2735
Willy Tarreau3a816292009-07-07 10:55:49 +02002736 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002737 req->analyse_exp = TICK_ETERNITY;
2738
2739 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2740 now_ms, __FUNCTION__,
2741 s,
2742 req,
2743 req->rex, req->wex,
2744 req->flags,
2745 req->l,
2746 req->analysers);
2747
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748 /* first check whether we have some ACLs set to block this request */
2749 list_for_each_entry(cond, &px->block_cond, list) {
2750 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 ret = acl_pass(ret);
2753 if (cond->pol == ACL_COND_UNLESS)
2754 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002755
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 if (ret) {
2757 txn->status = 403;
2758 /* let's log the request time */
2759 s->logs.tv_request = now;
2760 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2761 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002764
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 /* try headers filters */
2766 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002767 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002768 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* has the request been denied ? */
2771 if (txn->flags & TX_CLDENY) {
2772 /* no need to go further */
2773 txn->status = 403;
2774 /* let's log the request time */
2775 s->logs.tv_request = now;
2776 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2777 goto return_prx_cond;
2778 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002779
2780 /* When a connection is tarpitted, we use the tarpit timeout,
2781 * which may be the same as the connect timeout if unspecified.
2782 * If unset, then set it to zero because we really want it to
2783 * eventually expire. We build the tarpit as an analyser.
2784 */
2785 if (txn->flags & TX_CLTARPIT) {
2786 buffer_erase(s->req);
2787 /* wipe the request out so that we can drop the connection early
2788 * if the client closes first.
2789 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002790 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002791 req->analysers = 0; /* remove switching rules etc... */
2792 req->analysers |= AN_REQ_HTTP_TARPIT;
2793 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2794 if (!req->analyse_exp)
2795 req->analyse_exp = tick_add(now_ms, 0);
2796 return 1;
2797 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798 }
Willy Tarreau06619262006-12-17 08:37:22 +01002799
Willy Tarreau5b154472009-12-21 20:11:07 +01002800 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2801 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002802 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2803 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002804 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2805 * avoid to redo the same work if FE and BE have the same settings (common).
2806 * The method consists in checking if options changed between the two calls
2807 * (implying that either one is non-null, or one of them is non-null and we
2808 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002809 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002810
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002811 del_cl = del_ka = 0;
2812
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002813 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002814 ((!(txn->flags & TX_HDR_CONN_PRS) &&
2815 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2816 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2817 (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 +01002818 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002819
Willy Tarreau5b154472009-12-21 20:11:07 +01002820 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2821 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002822 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2823 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002824 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002825 tmp = TX_CON_WANT_CLO;
2826
Willy Tarreau5b154472009-12-21 20:11:07 +01002827 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2828 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002829
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002830 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2831 /* parse the Connection header and possibly clean it */
2832 int to_del = 0;
2833 if ((txn->flags & TX_REQ_VER_11) ||
2834 (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)
2835 to_del |= 2; /* remove "keep-alive" */
2836 if (!(txn->flags & TX_REQ_VER_11))
2837 to_del |= 1; /* remove "close" */
2838 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002839 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002840
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002841 /* check if client or config asks for explicit close in KAL/SCL */
2842 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2843 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2844 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2845 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2846 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002847 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2848 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002849 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2850 }
Willy Tarreau78599912009-10-17 20:12:21 +02002851
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002852 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002853 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01002854 if (wl->cond) {
2855 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2856 ret = acl_pass(ret);
2857 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2858 ret = !ret;
2859 if (!ret)
2860 continue;
2861 }
2862
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002863 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002864 goto return_bad_req;
2865 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002866
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002867 /* check if stats URI was requested, and if an auth is needed */
2868 if (px->uri_auth != NULL &&
2869 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2870 /* we have to check the URI and auth for this request.
2871 * FIXME!!! that one is rather dangerous, we want to
2872 * make it follow standard rules (eg: clear req->analysers).
2873 */
2874 if (stats_check_uri_auth(s, px)) {
2875 req->analysers = 0;
2876 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002878 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002879
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002880 /* check whether we have some ACLs set to redirect this request */
2881 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002882 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002883
Willy Tarreauf285f542010-01-03 20:03:03 +01002884 if (rule->cond) {
2885 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2886 ret = acl_pass(ret);
2887 if (rule->cond->pol == ACL_COND_UNLESS)
2888 ret = !ret;
2889 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002890
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002891 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002892 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002894
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002895 /* build redirect message */
2896 switch(rule->code) {
2897 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002898 msg_fmt = HTTP_303;
2899 break;
2900 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002901 msg_fmt = HTTP_301;
2902 break;
2903 case 302:
2904 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002905 msg_fmt = HTTP_302;
2906 break;
2907 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002908
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002909 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002910 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002911
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002912 switch(rule->type) {
2913 case REDIRECT_TYPE_PREFIX: {
2914 const char *path;
2915 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002916
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002917 path = http_get_path(txn);
2918 /* build message using path */
2919 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002920 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002921 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2922 int qs = 0;
2923 while (qs < pathlen) {
2924 if (path[qs] == '?') {
2925 pathlen = qs;
2926 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002927 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002928 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002929 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002930 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002931 } else {
2932 path = "/";
2933 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002934 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002935
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002936 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002937 goto return_bad_req;
2938
2939 /* add prefix. Note that if prefix == "/", we don't want to
2940 * add anything, otherwise it makes it hard for the user to
2941 * configure a self-redirection.
2942 */
2943 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002944 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2945 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002946 }
2947
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002948 /* add path */
2949 memcpy(rdr.str + rdr.len, path, pathlen);
2950 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01002951
2952 /* append a slash at the end of the location is needed and missing */
2953 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
2954 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2955 if (rdr.len > rdr.size - 5)
2956 goto return_bad_req;
2957 rdr.str[rdr.len] = '/';
2958 rdr.len++;
2959 }
2960
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002961 break;
2962 }
2963 case REDIRECT_TYPE_LOCATION:
2964 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002965 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002967
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002968 /* add location */
2969 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2970 rdr.len += rule->rdr_len;
2971 break;
2972 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002973
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002974 if (rule->cookie_len) {
2975 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2976 rdr.len += 14;
2977 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2978 rdr.len += rule->cookie_len;
2979 memcpy(rdr.str + rdr.len, "\r\n", 2);
2980 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002981 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002982
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002983 /* add end of headers and the keep-alive/close status.
2984 * We may choose to set keep-alive if the Location begins
2985 * with a slash, because the client will come back to the
2986 * same server.
2987 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002988 txn->status = rule->code;
2989 /* let's log the request time */
2990 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002991
2992 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2993 (txn->flags & TX_REQ_XFER_LEN) &&
2994 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2995 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2996 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2997 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01002998 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01002999 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3000 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3001 rdr.len += 30;
3002 } else {
3003 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3004 rdr.len += 24;
3005 }
Willy Tarreau75661452010-01-10 10:35:01 +01003006 }
3007 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3008 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003009 buffer_write(req->prod->ob, rdr.str, rdr.len);
3010 /* "eat" the request */
3011 buffer_ignore(req, msg->sov - msg->som);
3012 msg->som = msg->sov;
3013 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003014 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3015 txn->req.msg_state = HTTP_MSG_CLOSED;
3016 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003017 break;
3018 } else {
3019 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003020 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3021 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3022 rdr.len += 29;
3023 } else {
3024 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3025 rdr.len += 23;
3026 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003027 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003028 goto return_prx_cond;
3029 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003030 }
3031 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003032
Willy Tarreau2be39392010-01-03 17:24:51 +01003033 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3034 * If this happens, then the data will not come immediately, so we must
3035 * send all what we have without waiting. Note that due to the small gain
3036 * in waiting for the body of the request, it's easier to simply put the
3037 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3038 * itself once used.
3039 */
3040 req->flags |= BF_SEND_DONTWAIT;
3041
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 /* that's OK for us now, let's move on to next analysers */
3043 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 return_bad_req:
3046 /* We centralize bad requests processing here */
3047 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3048 /* we detected a parsing error. We want to archive this request
3049 * in the dedicated proxy area for later troubleshooting.
3050 */
3051 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3052 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003053
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 txn->req.msg_state = HTTP_MSG_ERROR;
3055 txn->status = 400;
3056 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003057
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003058 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003059 if (s->listener->counters)
3060 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003061
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003062 return_prx_cond:
3063 if (!(s->flags & SN_ERR_MASK))
3064 s->flags |= SN_ERR_PRXCOND;
3065 if (!(s->flags & SN_FINST_MASK))
3066 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003067
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003068 req->analysers = 0;
3069 req->analyse_exp = TICK_ETERNITY;
3070 return 0;
3071}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073/* This function performs all the processing enabled for the current request.
3074 * It returns 1 if the processing can continue on next analysers, or zero if it
3075 * needs more data, encounters an error, or wants to immediately abort the
3076 * request. It relies on buffers flags, and updates s->req->analysers.
3077 */
3078int http_process_request(struct session *s, struct buffer *req, int an_bit)
3079{
3080 struct http_txn *txn = &s->txn;
3081 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003082
Willy Tarreau655dce92009-11-08 13:10:58 +01003083 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003084 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003085 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003086 return 0;
3087 }
3088
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3090 now_ms, __FUNCTION__,
3091 s,
3092 req,
3093 req->rex, req->wex,
3094 req->flags,
3095 req->l,
3096 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003097
Willy Tarreau59234e92008-11-30 23:51:27 +01003098 /*
3099 * Right now, we know that we have processed the entire headers
3100 * and that unwanted requests have been filtered out. We can do
3101 * whatever we want with the remaining request. Also, now we
3102 * may have separate values for ->fe, ->be.
3103 */
Willy Tarreau06619262006-12-17 08:37:22 +01003104
Willy Tarreau59234e92008-11-30 23:51:27 +01003105 /*
3106 * If HTTP PROXY is set we simply get remote server address
3107 * parsing incoming request.
3108 */
3109 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003110 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003111 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003112
Willy Tarreau59234e92008-11-30 23:51:27 +01003113 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003114 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003115 * Note that doing so might move headers in the request, but
3116 * the fields will stay coherent and the URI will not move.
3117 * This should only be performed in the backend.
3118 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003119 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003120 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3121 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003122
Willy Tarreau59234e92008-11-30 23:51:27 +01003123 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003124 * 8: the appsession cookie was looked up very early in 1.2,
3125 * so let's do the same now.
3126 */
3127
3128 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003129 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003130 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003131 }
3132
3133 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003134 * 9: add X-Forwarded-For if either the frontend or the backend
3135 * asks for it.
3136 */
3137 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3138 if (s->cli_addr.ss_family == AF_INET) {
3139 /* Add an X-Forwarded-For header unless the source IP is
3140 * in the 'except' network range.
3141 */
3142 if ((!s->fe->except_mask.s_addr ||
3143 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3144 != s->fe->except_net.s_addr) &&
3145 (!s->be->except_mask.s_addr ||
3146 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3147 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003148 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003149 unsigned char *pn;
3150 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003151
3152 /* Note: we rely on the backend to get the header name to be used for
3153 * x-forwarded-for, because the header is really meant for the backends.
3154 * However, if the backend did not specify any option, we have to rely
3155 * on the frontend's header name.
3156 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003157 if (s->be->fwdfor_hdr_len) {
3158 len = s->be->fwdfor_hdr_len;
3159 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003160 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003161 len = s->fe->fwdfor_hdr_len;
3162 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003163 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003164 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003165
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003166 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003167 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003168 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003169 }
3170 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003171 else if (s->cli_addr.ss_family == AF_INET6) {
3172 /* FIXME: for the sake of completeness, we should also support
3173 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003174 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003175 int len;
3176 char pn[INET6_ADDRSTRLEN];
3177 inet_ntop(AF_INET6,
3178 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3179 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003180
Willy Tarreau59234e92008-11-30 23:51:27 +01003181 /* Note: we rely on the backend to get the header name to be used for
3182 * x-forwarded-for, because the header is really meant for the backends.
3183 * However, if the backend did not specify any option, we have to rely
3184 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003185 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003186 if (s->be->fwdfor_hdr_len) {
3187 len = s->be->fwdfor_hdr_len;
3188 memcpy(trash, s->be->fwdfor_hdr_name, len);
3189 } else {
3190 len = s->fe->fwdfor_hdr_len;
3191 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003192 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003193 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003194
Willy Tarreau59234e92008-11-30 23:51:27 +01003195 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003196 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003197 goto return_bad_req;
3198 }
3199 }
3200
3201 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003202 * 10: add X-Original-To if either the frontend or the backend
3203 * asks for it.
3204 */
3205 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3206
3207 /* FIXME: don't know if IPv6 can handle that case too. */
3208 if (s->cli_addr.ss_family == AF_INET) {
3209 /* Add an X-Original-To header unless the destination IP is
3210 * in the 'except' network range.
3211 */
3212 if (!(s->flags & SN_FRT_ADDR_SET))
3213 get_frt_addr(s);
3214
3215 if ((!s->fe->except_mask_to.s_addr ||
3216 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3217 != s->fe->except_to.s_addr) &&
3218 (!s->be->except_mask_to.s_addr ||
3219 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3220 != s->be->except_to.s_addr)) {
3221 int len;
3222 unsigned char *pn;
3223 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3224
3225 /* Note: we rely on the backend to get the header name to be used for
3226 * x-original-to, because the header is really meant for the backends.
3227 * However, if the backend did not specify any option, we have to rely
3228 * on the frontend's header name.
3229 */
3230 if (s->be->orgto_hdr_len) {
3231 len = s->be->orgto_hdr_len;
3232 memcpy(trash, s->be->orgto_hdr_name, len);
3233 } else {
3234 len = s->fe->orgto_hdr_len;
3235 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003236 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003237 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3238
3239 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003240 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003241 goto return_bad_req;
3242 }
3243 }
3244 }
3245
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003246 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3247 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3248 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3249 unsigned int want_flags = 0;
3250
3251 if (txn->flags & TX_REQ_VER_11) {
3252 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3253 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3254 want_flags |= TX_CON_CLO_SET;
3255 } else {
3256 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
3257 want_flags |= TX_CON_KAL_SET;
3258 }
3259
3260 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3261 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003262 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003263
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003264
Willy Tarreau522d6c02009-12-06 18:49:18 +01003265 /* If we have no server assigned yet and we're balancing on url_param
3266 * with a POST request, we may be interested in checking the body for
3267 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003268 */
3269 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3270 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003271 s->be->url_param_post_limit != 0 &&
3272 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003273 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003274 buffer_dont_connect(req);
3275 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003277
Willy Tarreaud98cf932009-12-27 22:54:55 +01003278 if (txn->flags & TX_REQ_XFER_LEN)
3279 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003280
Willy Tarreau59234e92008-11-30 23:51:27 +01003281 /*************************************************************
3282 * OK, that's finished for the headers. We have done what we *
3283 * could. Let's switch to the DATA state. *
3284 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003285 req->analyse_exp = TICK_ETERNITY;
3286 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003287
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003289 /* OK let's go on with the BODY now */
3290 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003291
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003293 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003294 /* we detected a parsing error. We want to archive this request
3295 * in the dedicated proxy area for later troubleshooting.
3296 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003297 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003298 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003299
Willy Tarreau59234e92008-11-30 23:51:27 +01003300 txn->req.msg_state = HTTP_MSG_ERROR;
3301 txn->status = 400;
3302 req->analysers = 0;
3303 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003304
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003305 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003306 if (s->listener->counters)
3307 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003308
Willy Tarreau59234e92008-11-30 23:51:27 +01003309 if (!(s->flags & SN_ERR_MASK))
3310 s->flags |= SN_ERR_PRXCOND;
3311 if (!(s->flags & SN_FINST_MASK))
3312 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003313 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003314}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003315
Willy Tarreau60b85b02008-11-30 23:28:40 +01003316/* This function is an analyser which processes the HTTP tarpit. It always
3317 * returns zero, at the beginning because it prevents any other processing
3318 * from occurring, and at the end because it terminates the request.
3319 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003320int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003321{
3322 struct http_txn *txn = &s->txn;
3323
3324 /* This connection is being tarpitted. The CLIENT side has
3325 * already set the connect expiration date to the right
3326 * timeout. We just have to check that the client is still
3327 * there and that the timeout has not expired.
3328 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003329 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003330 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3331 !tick_is_expired(req->analyse_exp, now_ms))
3332 return 0;
3333
3334 /* We will set the queue timer to the time spent, just for
3335 * logging purposes. We fake a 500 server error, so that the
3336 * attacker will not suspect his connection has been tarpitted.
3337 * It will not cause trouble to the logs because we can exclude
3338 * the tarpitted connections by filtering on the 'PT' status flags.
3339 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003340 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3341
3342 txn->status = 500;
3343 if (req->flags != BF_READ_ERROR)
3344 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3345
3346 req->analysers = 0;
3347 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003348
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003349 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003350 if (s->listener->counters)
3351 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003352
Willy Tarreau60b85b02008-11-30 23:28:40 +01003353 if (!(s->flags & SN_ERR_MASK))
3354 s->flags |= SN_ERR_PRXCOND;
3355 if (!(s->flags & SN_FINST_MASK))
3356 s->flags |= SN_FINST_T;
3357 return 0;
3358}
3359
Willy Tarreaud34af782008-11-30 23:36:37 +01003360/* This function is an analyser which processes the HTTP request body. It looks
3361 * for parameters to be used for the load balancing algorithm (url_param). It
3362 * must only be called after the standard HTTP request processing has occurred,
3363 * because it expects the request to be parsed. It returns zero if it needs to
3364 * read more data, or 1 once it has completed its analysis.
3365 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003366int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003367{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003368 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003369 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003370 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003371
3372 /* We have to parse the HTTP request body to find any required data.
3373 * "balance url_param check_post" should have been the only way to get
3374 * into this. We were brought here after HTTP header analysis, so all
3375 * related structures are ready.
3376 */
3377
Willy Tarreau522d6c02009-12-06 18:49:18 +01003378 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3379 goto missing_data;
3380
3381 if (msg->msg_state < HTTP_MSG_100_SENT) {
3382 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3383 * send an HTTP/1.1 100 Continue intermediate response.
3384 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003385 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003386 struct hdr_ctx ctx;
3387 ctx.idx = 0;
3388 /* Expect is allowed in 1.1, look for it */
3389 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3390 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3391 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3392 }
3393 }
3394 msg->msg_state = HTTP_MSG_100_SENT;
3395 }
3396
3397 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003398 /* we have msg->col and msg->sov which both point to the first
3399 * byte of message body. msg->som still points to the beginning
3400 * of the message. We must save the body in req->lr because it
3401 * survives buffer re-alignments.
3402 */
3403 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003404 if (txn->flags & TX_REQ_TE_CHNK)
3405 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3406 else
3407 msg->msg_state = HTTP_MSG_DATA;
3408 }
3409
3410 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003411 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003412 * set ->sov and ->lr to point to the body and switch to DATA or
3413 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003414 */
3415 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003416
Willy Tarreau115acb92009-12-26 13:56:06 +01003417 if (!ret)
3418 goto missing_data;
3419 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003420 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003421 }
3422
Willy Tarreaud98cf932009-12-27 22:54:55 +01003423 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003424 * We have the first non-header byte in msg->col, which is either the
3425 * beginning of the chunk size or of the data. The first data byte is in
3426 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3427 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003428 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003429
3430 if (msg->hdr_content_len < limit)
3431 limit = msg->hdr_content_len;
3432
Willy Tarreau7c96f672009-12-27 22:47:25 +01003433 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003434 goto http_end;
3435
3436 missing_data:
3437 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003438 if (req->flags & BF_FULL)
3439 goto return_bad_req;
3440
Willy Tarreau522d6c02009-12-06 18:49:18 +01003441 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3442 txn->status = 408;
3443 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3444 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003445 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003446
3447 /* we get here if we need to wait for more data */
3448 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003449 /* Not enough data. We'll re-use the http-request
3450 * timeout here. Ideally, we should set the timeout
3451 * relative to the accept() date. We just set the
3452 * request timeout once at the beginning of the
3453 * request.
3454 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003455 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003456 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003457 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003458 return 0;
3459 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003460
3461 http_end:
3462 /* The situation will not evolve, so let's give up on the analysis. */
3463 s->logs.tv_request = now; /* update the request timer to reflect full request */
3464 req->analysers &= ~an_bit;
3465 req->analyse_exp = TICK_ETERNITY;
3466 return 1;
3467
3468 return_bad_req: /* let's centralize all bad requests */
3469 txn->req.msg_state = HTTP_MSG_ERROR;
3470 txn->status = 400;
3471 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3472
3473 return_err_msg:
3474 req->analysers = 0;
3475 s->fe->counters.failed_req++;
3476 if (s->listener->counters)
3477 s->listener->counters->failed_req++;
3478
3479 if (!(s->flags & SN_ERR_MASK))
3480 s->flags |= SN_ERR_PRXCOND;
3481 if (!(s->flags & SN_FINST_MASK))
3482 s->flags |= SN_FINST_R;
3483 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003484}
3485
Willy Tarreau610ecce2010-01-04 21:15:02 +01003486/* Terminate current transaction and prepare a new one. This is very tricky
3487 * right now but it works.
3488 */
3489void http_end_txn_clean_session(struct session *s)
3490{
3491 /* FIXME: We need a more portable way of releasing a backend's and a
3492 * server's connections. We need a safer way to reinitialize buffer
3493 * flags. We also need a more accurate method for computing per-request
3494 * data.
3495 */
3496 http_silent_debug(__LINE__, s);
3497
3498 s->req->cons->flags |= SI_FL_NOLINGER;
3499 s->req->cons->shutr(s->req->cons);
3500 s->req->cons->shutw(s->req->cons);
3501
3502 http_silent_debug(__LINE__, s);
3503
3504 if (s->flags & SN_BE_ASSIGNED)
3505 s->be->beconn--;
3506
3507 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3508 session_process_counters(s);
3509
3510 if (s->txn.status) {
3511 int n;
3512
3513 n = s->txn.status / 100;
3514 if (n < 1 || n > 5)
3515 n = 0;
3516
3517 if (s->fe->mode == PR_MODE_HTTP)
3518 s->fe->counters.p.http.rsp[n]++;
3519
3520 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3521 (s->be->mode == PR_MODE_HTTP))
3522 s->be->counters.p.http.rsp[n]++;
3523 }
3524
3525 /* don't count other requests' data */
3526 s->logs.bytes_in -= s->req->l - s->req->send_max;
3527 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3528
3529 /* let's do a final log if we need it */
3530 if (s->logs.logwait &&
3531 !(s->flags & SN_MONITOR) &&
3532 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3533 s->do_log(s);
3534 }
3535
3536 s->logs.accept_date = date; /* user-visible date for logging */
3537 s->logs.tv_accept = now; /* corrected date for internal use */
3538 tv_zero(&s->logs.tv_request);
3539 s->logs.t_queue = -1;
3540 s->logs.t_connect = -1;
3541 s->logs.t_data = -1;
3542 s->logs.t_close = 0;
3543 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3544 s->logs.srv_queue_size = 0; /* we will get this number soon */
3545
3546 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3547 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3548
3549 if (s->pend_pos)
3550 pendconn_free(s->pend_pos);
3551
3552 if (s->srv) {
3553 if (s->flags & SN_CURR_SESS) {
3554 s->flags &= ~SN_CURR_SESS;
3555 s->srv->cur_sess--;
3556 }
3557 if (may_dequeue_tasks(s->srv, s->be))
3558 process_srv_queue(s->srv);
3559 }
3560
3561 if (unlikely(s->srv_conn))
3562 sess_change_server(s, NULL);
3563 s->srv = NULL;
3564
3565 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3566 s->req->cons->fd = -1; /* just to help with debugging */
3567 s->req->cons->err_type = SI_ET_NONE;
3568 s->req->cons->err_loc = NULL;
3569 s->req->cons->exp = TICK_ETERNITY;
3570 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003571 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3572 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 Tarreau4de91492010-01-22 19:10:05 +01003573 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003574 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3575 s->txn.meth = 0;
3576 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003577 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003578 if (s->be->options2 & PR_O2_INDEPSTR)
3579 s->req->cons->flags |= SI_FL_INDEP_STR;
3580
3581 /* if the request buffer is not empty, it means we're
3582 * about to process another request, so send pending
3583 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003584 * Just don't do this if the buffer is close to be full,
3585 * because the request will wait for it to flush a little
3586 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003587 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003588 if (s->req->l > s->req->send_max) {
3589 if (s->rep->send_max &&
3590 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003591 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3592 s->rep->flags |= BF_EXPECT_MORE;
3593 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003594
3595 /* we're removing the analysers, we MUST re-enable events detection */
3596 buffer_auto_read(s->req);
3597 buffer_auto_close(s->req);
3598 buffer_auto_read(s->rep);
3599 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003600
3601 /* make ->lr point to the first non-forwarded byte */
3602 s->req->lr = s->req->w + s->req->send_max;
3603 if (s->req->lr >= s->req->data + s->req->size)
3604 s->req->lr -= s->req->size;
3605 s->rep->lr = s->rep->w + s->rep->send_max;
3606 if (s->rep->lr >= s->rep->data + s->rep->size)
3607 s->rep->lr -= s->req->size;
3608
3609 s->req->analysers |= s->fe->fe_req_ana;
3610 s->rep->analysers = 0;
3611
3612 http_silent_debug(__LINE__, s);
3613}
3614
3615
3616/* This function updates the request state machine according to the response
3617 * state machine and buffer flags. It returns 1 if it changes anything (flag
3618 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3619 * it is only used to find when a request/response couple is complete. Both
3620 * this function and its equivalent should loop until both return zero. It
3621 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3622 */
3623int http_sync_req_state(struct session *s)
3624{
3625 struct buffer *buf = s->req;
3626 struct http_txn *txn = &s->txn;
3627 unsigned int old_flags = buf->flags;
3628 unsigned int old_state = txn->req.msg_state;
3629
3630 http_silent_debug(__LINE__, s);
3631 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3632 return 0;
3633
3634 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003635 /* No need to read anymore, the request was completely parsed.
3636 * We can shut the read side unless we want to abort_on_close.
3637 */
3638 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3639 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003640
3641 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3642 goto wait_other_side;
3643
3644 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3645 /* The server has not finished to respond, so we
3646 * don't want to move in order not to upset it.
3647 */
3648 goto wait_other_side;
3649 }
3650
3651 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3652 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003653 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003654 txn->req.msg_state = HTTP_MSG_TUNNEL;
3655 goto wait_other_side;
3656 }
3657
3658 /* When we get here, it means that both the request and the
3659 * response have finished receiving. Depending on the connection
3660 * mode, we'll have to wait for the last bytes to leave in either
3661 * direction, and sometimes for a close to be effective.
3662 */
3663
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003664 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3665 /* Server-close mode : queue a connection close to the server */
3666 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003667 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003668 }
3669 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3670 /* Option forceclose is set, or either side wants to close,
3671 * let's enforce it now that we're not expecting any new
3672 * data to come. The caller knows the session is complete
3673 * once both states are CLOSED.
3674 */
3675 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003676 buffer_shutr_now(buf);
3677 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003678 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003679 }
3680 else {
3681 /* The last possible modes are keep-alive and tunnel. Since tunnel
3682 * mode does not set the body analyser, we can't reach this place
3683 * in tunnel mode, so we're left with keep-alive only.
3684 * This mode is currently not implemented, we switch to tunnel mode.
3685 */
3686 buffer_auto_read(buf);
3687 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003688 }
3689
3690 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3691 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003692 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3693
Willy Tarreau610ecce2010-01-04 21:15:02 +01003694 if (!(buf->flags & BF_OUT_EMPTY)) {
3695 txn->req.msg_state = HTTP_MSG_CLOSING;
3696 goto http_msg_closing;
3697 }
3698 else {
3699 txn->req.msg_state = HTTP_MSG_CLOSED;
3700 goto http_msg_closed;
3701 }
3702 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003703 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003704 }
3705
3706 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3707 http_msg_closing:
3708 /* nothing else to forward, just waiting for the output buffer
3709 * to be empty and for the shutw_now to take effect.
3710 */
3711 if (buf->flags & BF_OUT_EMPTY) {
3712 txn->req.msg_state = HTTP_MSG_CLOSED;
3713 goto http_msg_closed;
3714 }
3715 else if (buf->flags & BF_SHUTW) {
3716 txn->req.msg_state = HTTP_MSG_ERROR;
3717 goto wait_other_side;
3718 }
3719 }
3720
3721 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3722 http_msg_closed:
3723 goto wait_other_side;
3724 }
3725
3726 wait_other_side:
3727 http_silent_debug(__LINE__, s);
3728 return txn->req.msg_state != old_state || buf->flags != old_flags;
3729}
3730
3731
3732/* This function updates the response state machine according to the request
3733 * state machine and buffer flags. It returns 1 if it changes anything (flag
3734 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3735 * it is only used to find when a request/response couple is complete. Both
3736 * this function and its equivalent should loop until both return zero. It
3737 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3738 */
3739int http_sync_res_state(struct session *s)
3740{
3741 struct buffer *buf = s->rep;
3742 struct http_txn *txn = &s->txn;
3743 unsigned int old_flags = buf->flags;
3744 unsigned int old_state = txn->rsp.msg_state;
3745
3746 http_silent_debug(__LINE__, s);
3747 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3748 return 0;
3749
3750 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3751 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003752 * still monitor the server connection for a possible close
3753 * while the request is being uploaded, so we don't disable
3754 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003755 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003756 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003757
3758 if (txn->req.msg_state == HTTP_MSG_ERROR)
3759 goto wait_other_side;
3760
3761 if (txn->req.msg_state < HTTP_MSG_DONE) {
3762 /* The client seems to still be sending data, probably
3763 * because we got an error response during an upload.
3764 * We have the choice of either breaking the connection
3765 * or letting it pass through. Let's do the later.
3766 */
3767 goto wait_other_side;
3768 }
3769
3770 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3771 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003772 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3774 goto wait_other_side;
3775 }
3776
3777 /* When we get here, it means that both the request and the
3778 * response have finished receiving. Depending on the connection
3779 * mode, we'll have to wait for the last bytes to leave in either
3780 * direction, and sometimes for a close to be effective.
3781 */
3782
3783 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3784 /* Server-close mode : shut read and wait for the request
3785 * side to close its output buffer. The caller will detect
3786 * when we're in DONE and the other is in CLOSED and will
3787 * catch that for the final cleanup.
3788 */
3789 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3790 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003791 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003792 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3793 /* Option forceclose is set, or either side wants to close,
3794 * let's enforce it now that we're not expecting any new
3795 * data to come. The caller knows the session is complete
3796 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003797 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003798 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3799 buffer_shutr_now(buf);
3800 buffer_shutw_now(buf);
3801 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802 }
3803 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003804 /* The last possible modes are keep-alive and tunnel. Since tunnel
3805 * mode does not set the body analyser, we can't reach this place
3806 * in tunnel mode, so we're left with keep-alive only.
3807 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003808 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003809 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 }
3812
3813 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3814 /* if we've just closed an output, let's switch */
3815 if (!(buf->flags & BF_OUT_EMPTY)) {
3816 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3817 goto http_msg_closing;
3818 }
3819 else {
3820 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3821 goto http_msg_closed;
3822 }
3823 }
3824 goto wait_other_side;
3825 }
3826
3827 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3828 http_msg_closing:
3829 /* nothing else to forward, just waiting for the output buffer
3830 * to be empty and for the shutw_now to take effect.
3831 */
3832 if (buf->flags & BF_OUT_EMPTY) {
3833 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3834 goto http_msg_closed;
3835 }
3836 else if (buf->flags & BF_SHUTW) {
3837 txn->rsp.msg_state = HTTP_MSG_ERROR;
3838 goto wait_other_side;
3839 }
3840 }
3841
3842 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3843 http_msg_closed:
3844 /* drop any pending data */
3845 buffer_ignore(buf, buf->l - buf->send_max);
3846 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003847 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003848 goto wait_other_side;
3849 }
3850
3851 wait_other_side:
3852 http_silent_debug(__LINE__, s);
3853 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3854}
3855
3856
3857/* Resync the request and response state machines. Return 1 if either state
3858 * changes.
3859 */
3860int http_resync_states(struct session *s)
3861{
3862 struct http_txn *txn = &s->txn;
3863 int old_req_state = txn->req.msg_state;
3864 int old_res_state = txn->rsp.msg_state;
3865
3866 http_silent_debug(__LINE__, s);
3867 http_sync_req_state(s);
3868 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003869 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003870 if (!http_sync_res_state(s))
3871 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003872 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003873 if (!http_sync_req_state(s))
3874 break;
3875 }
3876 http_silent_debug(__LINE__, s);
3877 /* OK, both state machines agree on a compatible state.
3878 * There are a few cases we're interested in :
3879 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3880 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3881 * directions, so let's simply disable both analysers.
3882 * - HTTP_MSG_CLOSED on the response only means we must abort the
3883 * request.
3884 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3885 * with server-close mode means we've completed one request and we
3886 * must re-initialize the server connection.
3887 */
3888
3889 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3890 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3891 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3892 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3893 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003894 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003895 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003897 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003898 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003899 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003900 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3901 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3902 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003903 s->rep->analysers = 0;
3904 buffer_auto_close(s->rep);
3905 buffer_auto_read(s->rep);
3906 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003907 buffer_abort(s->req);
3908 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003909 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003911 }
3912 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3913 txn->rsp.msg_state == HTTP_MSG_DONE &&
3914 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3915 /* server-close: terminate this server connection and
3916 * reinitialize a fresh-new transaction.
3917 */
3918 http_end_txn_clean_session(s);
3919 }
3920
3921 http_silent_debug(__LINE__, s);
3922 return txn->req.msg_state != old_req_state ||
3923 txn->rsp.msg_state != old_res_state;
3924}
3925
Willy Tarreaud98cf932009-12-27 22:54:55 +01003926/* This function is an analyser which forwards request body (including chunk
3927 * sizes if any). It is called as soon as we must forward, even if we forward
3928 * zero byte. The only situation where it must not be called is when we're in
3929 * tunnel mode and we want to forward till the close. It's used both to forward
3930 * remaining data and to resync after end of body. It expects the msg_state to
3931 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3932 * read more data, or 1 once we can go on with next request or end the session.
3933 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3934 * bytes of pending data + the headers if not already done (between som and sov).
3935 * It eventually adjusts som to match sov after the data in between have been sent.
3936 */
3937int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3938{
3939 struct http_txn *txn = &s->txn;
3940 struct http_msg *msg = &s->txn.req;
3941
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003942 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3943 return 0;
3944
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003945 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3946 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3947 /* Output closed while we were sending data. We must abort. */
3948 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003949 buffer_auto_read(req);
3950 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003951 req->analysers &= ~an_bit;
3952 return 1;
3953 }
3954
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003955 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003956
3957 /* Note that we don't have to send 100-continue back because we don't
3958 * need the data to complete our job, and it's up to the server to
3959 * decide whether to return 100, 417 or anything else in return of
3960 * an "Expect: 100-continue" header.
3961 */
3962
3963 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3964 /* we have msg->col and msg->sov which both point to the first
3965 * byte of message body. msg->som still points to the beginning
3966 * of the message. We must save the body in req->lr because it
3967 * survives buffer re-alignments.
3968 */
3969 req->lr = req->data + msg->sov;
3970 if (txn->flags & TX_REQ_TE_CHNK)
3971 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3972 else {
3973 msg->msg_state = HTTP_MSG_DATA;
3974 }
3975 }
3976
Willy Tarreaud98cf932009-12-27 22:54:55 +01003977 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003978 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003979 /* we may have some data pending */
3980 if (msg->hdr_content_len || msg->som != msg->sov) {
3981 int bytes = msg->sov - msg->som;
3982 if (bytes < 0) /* sov may have wrapped at the end */
3983 bytes += req->size;
3984 buffer_forward(req, bytes + msg->hdr_content_len);
3985 msg->hdr_content_len = 0; /* don't forward that again */
3986 msg->som = msg->sov;
3987 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003988
Willy Tarreaucaabe412010-01-03 23:08:28 +01003989 if (msg->msg_state == HTTP_MSG_DATA) {
3990 /* must still forward */
3991 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003992 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003993
3994 /* nothing left to forward */
3995 if (txn->flags & TX_REQ_TE_CHNK)
3996 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003998 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003999 }
4000 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004001 /* read the chunk size and assign it to ->hdr_content_len, then
4002 * set ->sov and ->lr to point to the body and switch to DATA or
4003 * TRAILERS state.
4004 */
4005 int ret = http_parse_chunk_size(req, msg);
4006
4007 if (!ret)
4008 goto missing_data;
4009 else if (ret < 0)
4010 goto return_bad_req;
4011 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004012 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004013 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4014 /* we want the CRLF after the data */
4015 int ret;
4016
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004017 req->lr = req->w + req->send_max;
4018 if (req->lr >= req->data + req->size)
4019 req->lr -= req->size;
4020
Willy Tarreaud98cf932009-12-27 22:54:55 +01004021 ret = http_skip_chunk_crlf(req, msg);
4022
4023 if (ret == 0)
4024 goto missing_data;
4025 else if (ret < 0)
4026 goto return_bad_req;
4027 /* we're in MSG_CHUNK_SIZE now */
4028 }
4029 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4030 int ret = http_forward_trailers(req, msg);
4031
4032 if (ret == 0)
4033 goto missing_data;
4034 else if (ret < 0)
4035 goto return_bad_req;
4036 /* we're in HTTP_MSG_DONE now */
4037 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 else {
4039 /* other states, DONE...TUNNEL */
4040 if (http_resync_states(s)) {
4041 /* some state changes occurred, maybe the analyser
4042 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004043 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4045 goto return_bad_req;
4046 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004047 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004049 }
4050 }
4051
Willy Tarreaud98cf932009-12-27 22:54:55 +01004052 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004053 /* stop waiting for data if the input is closed before the end */
4054 if (req->flags & BF_SHUTR)
4055 goto return_bad_req;
4056
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004057 /* waiting for the last bits to leave the buffer */
4058 if (req->flags & BF_SHUTW)
4059 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060
4061 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004062 return 0;
4063
Willy Tarreaud98cf932009-12-27 22:54:55 +01004064 return_bad_req: /* let's centralize all bad requests */
4065 txn->req.msg_state = HTTP_MSG_ERROR;
4066 txn->status = 400;
4067 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004068 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 +01004069 req->analysers = 0;
4070 s->fe->counters.failed_req++;
4071 if (s->listener->counters)
4072 s->listener->counters->failed_req++;
4073
4074 if (!(s->flags & SN_ERR_MASK))
4075 s->flags |= SN_ERR_PRXCOND;
4076 if (!(s->flags & SN_FINST_MASK))
4077 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004079 return 0;
4080}
4081
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004082/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4083 * processing can continue on next analysers, or zero if it either needs more
4084 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4085 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4086 * when it has nothing left to do, and may remove any analyser when it wants to
4087 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004088 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004089int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004090{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004091 struct http_txn *txn = &s->txn;
4092 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004093 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004094 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004095 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004096 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004097
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004098 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 +02004099 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004100 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004101 rep,
4102 rep->rex, rep->wex,
4103 rep->flags,
4104 rep->l,
4105 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004106
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004107 /*
4108 * Now parse the partial (or complete) lines.
4109 * We will check the response syntax, and also join multi-line
4110 * headers. An index of all the lines will be elaborated while
4111 * parsing.
4112 *
4113 * For the parsing, we use a 28 states FSM.
4114 *
4115 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004116 * rep->data + msg->som = beginning of response
4117 * rep->data + msg->eoh = end of processed headers / start of current one
4118 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004119 * rep->lr = first non-visited byte
4120 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004121 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004122 */
4123
Willy Tarreau83e3af02009-12-28 17:39:57 +01004124 /* There's a protected area at the end of the buffer for rewriting
4125 * purposes. We don't want to start to parse the request if the
4126 * protected area is affected, because we may have to move processed
4127 * data later, which is much more complicated.
4128 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004129 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4130 if (unlikely((rep->flags & BF_FULL) ||
4131 rep->r < rep->lr ||
4132 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4133 if (rep->send_max) {
4134 /* some data has still not left the buffer, wake us once that's done */
4135 buffer_dont_close(rep);
4136 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4137 return 0;
4138 }
4139 if (rep->l <= rep->size - global.tune.maxrewrite)
4140 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004141 }
4142
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004143 if (likely(rep->lr < rep->r))
4144 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004145 }
4146
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004147 /* 1: we might have to print this header in debug mode */
4148 if (unlikely((global.mode & MODE_DEBUG) &&
4149 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004150 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004151 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004152
Willy Tarreau962c3f42010-01-10 00:15:35 +01004153 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004154 eol = sol + msg->sl.rq.l;
4155 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004156
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004157 sol += hdr_idx_first_pos(&txn->hdr_idx);
4158 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004159
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004160 while (cur_idx) {
4161 eol = sol + txn->hdr_idx.v[cur_idx].len;
4162 debug_hdr("srvhdr", s, sol, eol);
4163 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4164 cur_idx = txn->hdr_idx.v[cur_idx].next;
4165 }
4166 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004167
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004168 /*
4169 * Now we quickly check if we have found a full valid response.
4170 * If not so, we check the FD and buffer states before leaving.
4171 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004172 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004173 * responses are checked first.
4174 *
4175 * Depending on whether the client is still there or not, we
4176 * may send an error response back or not. Note that normally
4177 * we should only check for HTTP status there, and check I/O
4178 * errors somewhere else.
4179 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004180
Willy Tarreau655dce92009-11-08 13:10:58 +01004181 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004182 /* Invalid response */
4183 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4184 /* we detected a parsing error. We want to archive this response
4185 * in the dedicated proxy area for later troubleshooting.
4186 */
4187 hdr_response_bad:
4188 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4189 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4190
4191 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004192 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004193 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004194 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4195 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004196
Willy Tarreau90deb182010-01-07 00:20:41 +01004197 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004198 rep->analysers = 0;
4199 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004200 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004201 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4202
4203 if (!(s->flags & SN_ERR_MASK))
4204 s->flags |= SN_ERR_PRXCOND;
4205 if (!(s->flags & SN_FINST_MASK))
4206 s->flags |= SN_FINST_H;
4207
4208 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004209 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004210
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004211 /* too large response does not fit in buffer. */
4212 else if (rep->flags & BF_FULL) {
4213 goto hdr_response_bad;
4214 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004215
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004216 /* read error */
4217 else if (rep->flags & BF_READ_ERROR) {
4218 if (msg->err_pos >= 0)
4219 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004220
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004221 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004222 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004223 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004224 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4225 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004226
Willy Tarreau90deb182010-01-07 00:20:41 +01004227 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004228 rep->analysers = 0;
4229 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004230 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004231 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004232
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004233 if (!(s->flags & SN_ERR_MASK))
4234 s->flags |= SN_ERR_SRVCL;
4235 if (!(s->flags & SN_FINST_MASK))
4236 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004237 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004238 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004239
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004240 /* read timeout : return a 504 to the client. */
4241 else if (rep->flags & BF_READ_TIMEOUT) {
4242 if (msg->err_pos >= 0)
4243 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004244
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004245 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004246 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004247 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004248 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4249 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004250
Willy Tarreau90deb182010-01-07 00:20:41 +01004251 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004252 rep->analysers = 0;
4253 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004254 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004255 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004256
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004257 if (!(s->flags & SN_ERR_MASK))
4258 s->flags |= SN_ERR_SRVTO;
4259 if (!(s->flags & SN_FINST_MASK))
4260 s->flags |= SN_FINST_H;
4261 return 0;
4262 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004263
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004264 /* close from server */
4265 else if (rep->flags & BF_SHUTR) {
4266 if (msg->err_pos >= 0)
4267 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004268
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004269 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004270 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004271 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004272 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4273 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004274
Willy Tarreau90deb182010-01-07 00:20:41 +01004275 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004276 rep->analysers = 0;
4277 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004278 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004279 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004280
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004281 if (!(s->flags & SN_ERR_MASK))
4282 s->flags |= SN_ERR_SRVCL;
4283 if (!(s->flags & SN_FINST_MASK))
4284 s->flags |= SN_FINST_H;
4285 return 0;
4286 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004287
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004288 /* write error to client (we don't send any message then) */
4289 else if (rep->flags & BF_WRITE_ERROR) {
4290 if (msg->err_pos >= 0)
4291 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004292
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004293 s->be->counters.failed_resp++;
4294 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004295 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004296
4297 if (!(s->flags & SN_ERR_MASK))
4298 s->flags |= SN_ERR_CLICL;
4299 if (!(s->flags & SN_FINST_MASK))
4300 s->flags |= SN_FINST_H;
4301
4302 /* process_session() will take care of the error */
4303 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004304 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004305
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004306 buffer_dont_close(rep);
4307 return 0;
4308 }
4309
4310 /* More interesting part now : we know that we have a complete
4311 * response which at least looks like HTTP. We have an indicator
4312 * of each header's length, so we can parse them quickly.
4313 */
4314
4315 if (unlikely(msg->err_pos >= 0))
4316 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4317
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004318 /*
4319 * 1: get the status code
4320 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004321 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004322 if (n < 1 || n > 5)
4323 n = 0;
4324 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004325
Willy Tarreau5b154472009-12-21 20:11:07 +01004326 /* check if the response is HTTP/1.1 or above */
4327 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004328 ((msg->sol[5] > '1') ||
4329 ((msg->sol[5] == '1') &&
4330 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004331 txn->flags |= TX_RES_VER_11;
4332
4333 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004334 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 +01004335
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004336 /* transfer length unknown*/
4337 txn->flags &= ~TX_RES_XFER_LEN;
4338
Willy Tarreau962c3f42010-01-10 00:15:35 +01004339 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004340
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004341 if (txn->status >= 100 && txn->status < 500)
4342 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4343 else
4344 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4345
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004346 /*
4347 * 2: check for cacheability.
4348 */
4349
4350 switch (txn->status) {
4351 case 200:
4352 case 203:
4353 case 206:
4354 case 300:
4355 case 301:
4356 case 410:
4357 /* RFC2616 @13.4:
4358 * "A response received with a status code of
4359 * 200, 203, 206, 300, 301 or 410 MAY be stored
4360 * by a cache (...) unless a cache-control
4361 * directive prohibits caching."
4362 *
4363 * RFC2616 @9.5: POST method :
4364 * "Responses to this method are not cacheable,
4365 * unless the response includes appropriate
4366 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004367 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004368 if (likely(txn->meth != HTTP_METH_POST) &&
4369 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4370 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4371 break;
4372 default:
4373 break;
4374 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004375
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004376 /*
4377 * 3: we may need to capture headers
4378 */
4379 s->logs.logwait &= ~LW_RESP;
4380 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004381 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004382 txn->rsp.cap, s->fe->rsp_cap);
4383
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004384 /* 4: determine the transfer-length.
4385 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4386 * the presence of a message-body in a RESPONSE and its transfer length
4387 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004388 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004389 * All responses to the HEAD request method MUST NOT include a
4390 * message-body, even though the presence of entity-header fields
4391 * might lead one to believe they do. All 1xx (informational), 204
4392 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4393 * message-body. All other responses do include a message-body,
4394 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004395 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004396 * 1. Any response which "MUST NOT" include a message-body (such as the
4397 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4398 * always terminated by the first empty line after the header fields,
4399 * regardless of the entity-header fields present in the message.
4400 *
4401 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4402 * the "chunked" transfer-coding (Section 6.2) is used, the
4403 * transfer-length is defined by the use of this transfer-coding.
4404 * If a Transfer-Encoding header field is present and the "chunked"
4405 * transfer-coding is not present, the transfer-length is defined by
4406 * the sender closing the connection.
4407 *
4408 * 3. If a Content-Length header field is present, its decimal value in
4409 * OCTETs represents both the entity-length and the transfer-length.
4410 * If a message is received with both a Transfer-Encoding header
4411 * field and a Content-Length header field, the latter MUST be ignored.
4412 *
4413 * 4. If the message uses the media type "multipart/byteranges", and
4414 * the transfer-length is not otherwise specified, then this self-
4415 * delimiting media type defines the transfer-length. This media
4416 * type MUST NOT be used unless the sender knows that the recipient
4417 * can parse it; the presence in a request of a Range header with
4418 * multiple byte-range specifiers from a 1.1 client implies that the
4419 * client can parse multipart/byteranges responses.
4420 *
4421 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004422 */
4423
4424 /* Skip parsing if no content length is possible. The response flags
4425 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004426 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004427 * FIXME: should we parse anyway and return an error on chunked encoding ?
4428 */
4429 if (txn->meth == HTTP_METH_HEAD ||
4430 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004431 txn->status == 204 || txn->status == 304) {
4432 txn->flags |= TX_RES_XFER_LEN;
4433 goto skip_content_length;
4434 }
4435
4436 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004437 goto skip_content_length;
4438
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004439 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004440 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004441 while ((txn->flags & TX_RES_VER_11) &&
4442 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004443 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4444 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4445 else if (txn->flags & TX_RES_TE_CHNK) {
4446 /* bad transfer-encoding (chunked followed by something else) */
4447 use_close_only = 1;
4448 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4449 break;
4450 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004451 }
4452
4453 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4454 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004455 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004456 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4457 signed long long cl;
4458
4459 if (!ctx.vlen)
4460 goto hdr_response_bad;
4461
4462 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4463 goto hdr_response_bad; /* parse failure */
4464
4465 if (cl < 0)
4466 goto hdr_response_bad;
4467
4468 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4469 goto hdr_response_bad; /* already specified, was different */
4470
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004471 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004472 msg->hdr_content_len = cl;
4473 }
4474
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004475 /* FIXME: we should also implement the multipart/byterange method.
4476 * For now on, we resort to close mode in this case (unknown length).
4477 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004478skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004479
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480 /* end of job, return OK */
4481 rep->analysers &= ~an_bit;
4482 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004483 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004484 return 1;
4485}
4486
4487/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004488 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4489 * and updates t->rep->analysers. It might make sense to explode it into several
4490 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004491 */
4492int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4493{
4494 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004495 struct http_msg *msg = &txn->rsp;
4496 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004497 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498
4499 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4500 now_ms, __FUNCTION__,
4501 t,
4502 rep,
4503 rep->rex, rep->wex,
4504 rep->flags,
4505 rep->l,
4506 rep->analysers);
4507
Willy Tarreau655dce92009-11-08 13:10:58 +01004508 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 return 0;
4510
4511 rep->analysers &= ~an_bit;
4512 rep->analyse_exp = TICK_ETERNITY;
4513
Willy Tarreau5b154472009-12-21 20:11:07 +01004514 /* Now we have to check if we need to modify the Connection header.
4515 * This is more difficult on the response than it is on the request,
4516 * because we can have two different HTTP versions and we don't know
4517 * how the client will interprete a response. For instance, let's say
4518 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4519 * HTTP/1.1 response without any header. Maybe it will bound itself to
4520 * HTTP/1.0 because it only knows about it, and will consider the lack
4521 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4522 * the lack of header as a keep-alive. Thus we will use two flags
4523 * indicating how a request MAY be understood by the client. In case
4524 * of multiple possibilities, we'll fix the header to be explicit. If
4525 * ambiguous cases such as both close and keepalive are seen, then we
4526 * will fall back to explicit close. Note that we won't take risks with
4527 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004528 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004529 */
4530
4531 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004532 (txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004533 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4534 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004535 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004536
Willy Tarreau60466522010-01-18 19:08:45 +01004537 /* on unknown transfer length, we must close */
4538 if (!(txn->flags & TX_RES_XFER_LEN) &&
4539 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4540 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004541
Willy Tarreau60466522010-01-18 19:08:45 +01004542 /* now adjust header transformations depending on current state */
4543 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4544 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4545 to_del |= 2; /* remove "keep-alive" on any response */
4546 if (!(txn->flags & TX_RES_VER_11))
4547 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004548 }
Willy Tarreau60466522010-01-18 19:08:45 +01004549 else { /* SCL / KAL */
4550 to_del |= 1; /* remove "close" on any response */
4551 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4552 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004553 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004554
Willy Tarreau60466522010-01-18 19:08:45 +01004555 /* Parse and remove some headers from the connection header */
4556 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004557
Willy Tarreau60466522010-01-18 19:08:45 +01004558 /* Some keep-alive responses are converted to Server-close if
4559 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004560 */
Willy Tarreau60466522010-01-18 19:08:45 +01004561 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4562 if ((txn->flags & TX_HDR_CONN_CLO) ||
4563 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4564 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004565 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004566 }
4567
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004569 /*
4570 * 3: we will have to evaluate the filters.
4571 * As opposed to version 1.2, now they will be evaluated in the
4572 * filters order and not in the header order. This means that
4573 * each filter has to be validated among all headers.
4574 *
4575 * Filters are tried with ->be first, then with ->fe if it is
4576 * different from ->be.
4577 */
4578
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004579 cur_proxy = t->be;
4580 while (1) {
4581 struct proxy *rule_set = cur_proxy;
4582
4583 /* try headers filters */
4584 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004585 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004586 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004587 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004588 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004589 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4590 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004591 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004592 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004593 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004594 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004595 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004596 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004597 if (!(t->flags & SN_ERR_MASK))
4598 t->flags |= SN_ERR_PRXCOND;
4599 if (!(t->flags & SN_FINST_MASK))
4600 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004601 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004602 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004603 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004604
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004605 /* has the response been denied ? */
4606 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004607 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004608 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004609
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004610 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004611 if (t->listener->counters)
4612 t->listener->counters->denied_resp++;
4613
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004614 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004615 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004616
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004617 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004618 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004619 if (txn->status < 200)
4620 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004621 if (wl->cond) {
4622 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4623 ret = acl_pass(ret);
4624 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4625 ret = !ret;
4626 if (!ret)
4627 continue;
4628 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004629 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004630 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004631 }
4632
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004633 /* check whether we're already working on the frontend */
4634 if (cur_proxy == t->fe)
4635 break;
4636 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004637 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004638
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004639 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004640 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4641 * in which case this is not the right response, and we're waiting for the
4642 * next one. Let's allow this response to go to the client and wait for the
4643 * next one.
4644 */
4645 if (txn->status < 200) {
4646 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004647 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004648 msg->msg_state = HTTP_MSG_RPBEFORE;
4649 txn->status = 0;
4650 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4651 return 1;
4652 }
4653
4654 /* we don't have any 1xx status code now */
4655
4656 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004657 * 4: check for server cookie.
4658 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004659 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4660 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004661 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004662
Willy Tarreaubaaee002006-06-26 02:48:02 +02004663
Willy Tarreaua15645d2007-03-18 16:22:39 +01004664 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004665 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004666 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004667 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004668 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004669
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004670 /*
4671 * 6: add server cookie in the response if needed
4672 */
4673 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004674 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004675 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004676
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004677 /* the server is known, it's not the one the client requested, we have to
4678 * insert a set-cookie here, except if we want to insert only on POST
4679 * requests and this one isn't. Note that servers which don't have cookies
4680 * (eg: some backup servers) will return a full cookie removal request.
4681 */
4682 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4683 t->be->cookie_name,
4684 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004685
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004686 if (t->be->cookie_domain)
4687 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004688
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004689 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004690 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004691 goto return_bad_resp;
4692 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004693
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004694 /* Here, we will tell an eventual cache on the client side that we don't
4695 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4696 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4697 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4698 */
4699 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004700
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004701 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4702
4703 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004704 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004705 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004706 }
4707 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004708
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004709 /*
4710 * 7: check if result will be cacheable with a cookie.
4711 * We'll block the response if security checks have caught
4712 * nasty things such as a cacheable cookie.
4713 */
4714 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4715 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004716 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004717
4718 /* we're in presence of a cacheable response containing
4719 * a set-cookie header. We'll block it as requested by
4720 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004721 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004722 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004723 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004724
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004725 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004726 if (t->listener->counters)
4727 t->listener->counters->denied_resp++;
4728
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004729 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4730 t->be->id, t->srv?t->srv->id:"<dispatch>");
4731 send_log(t->be, LOG_ALERT,
4732 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4733 t->be->id, t->srv?t->srv->id:"<dispatch>");
4734 goto return_srv_prx_502;
4735 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004736
4737 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004738 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739 */
Willy Tarreau60466522010-01-18 19:08:45 +01004740 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4741 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4742 unsigned int want_flags = 0;
4743
4744 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4745 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4746 /* we want a keep-alive response here. Keep-alive header
4747 * required if either side is not 1.1.
4748 */
4749 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4750 want_flags |= TX_CON_KAL_SET;
4751 }
4752 else {
4753 /* we want a close response here. Close header required if
4754 * the server is 1.1, regardless of the client.
4755 */
4756 if (txn->flags & TX_RES_VER_11)
4757 want_flags |= TX_CON_CLO_SET;
4758 }
4759
4760 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4761 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004762 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 if (txn->flags & TX_RES_XFER_LEN)
4765 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004767 /*************************************************************
4768 * OK, that's finished for the headers. We have done what we *
4769 * could. Let's switch to the DATA state. *
4770 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004771
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004772 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004773
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004774 /* if the user wants to log as soon as possible, without counting
4775 * bytes from the server, then this is the right moment. We have
4776 * to temporarily assign bytes_out to log what we currently have.
4777 */
4778 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4779 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4780 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004781 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004782 t->logs.bytes_out = 0;
4783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004784
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004785 /* Note: we must not try to cheat by jumping directly to DATA,
4786 * otherwise we would not let the client side wake up.
4787 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004788
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004789 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004790 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004791 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004792}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004793
Willy Tarreaud98cf932009-12-27 22:54:55 +01004794/* This function is an analyser which forwards response body (including chunk
4795 * sizes if any). It is called as soon as we must forward, even if we forward
4796 * zero byte. The only situation where it must not be called is when we're in
4797 * tunnel mode and we want to forward till the close. It's used both to forward
4798 * remaining data and to resync after end of body. It expects the msg_state to
4799 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4800 * read more data, or 1 once we can go on with next request or end the session.
4801 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4802 * bytes of pending data + the headers if not already done (between som and sov).
4803 * It eventually adjusts som to match sov after the data in between have been sent.
4804 */
4805int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4806{
4807 struct http_txn *txn = &s->txn;
4808 struct http_msg *msg = &s->txn.rsp;
4809
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004810 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4811 return 0;
4812
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004813 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004814 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004815 !s->req->analysers) {
4816 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4817 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004818 buffer_auto_read(res);
4819 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004820 res->analysers &= ~an_bit;
4821 return 1;
4822 }
4823
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004824 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004825
Willy Tarreaud98cf932009-12-27 22:54:55 +01004826 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4827 /* we have msg->col and msg->sov which both point to the first
4828 * byte of message body. msg->som still points to the beginning
4829 * of the message. We must save the body in req->lr because it
4830 * survives buffer re-alignments.
4831 */
4832 res->lr = res->data + msg->sov;
4833 if (txn->flags & TX_RES_TE_CHNK)
4834 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4835 else {
4836 msg->msg_state = HTTP_MSG_DATA;
4837 }
4838 }
4839
Willy Tarreaud98cf932009-12-27 22:54:55 +01004840 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004841 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004842 /* we may have some data pending */
4843 if (msg->hdr_content_len || msg->som != msg->sov) {
4844 int bytes = msg->sov - msg->som;
4845 if (bytes < 0) /* sov may have wrapped at the end */
4846 bytes += res->size;
4847 buffer_forward(res, bytes + msg->hdr_content_len);
4848 msg->hdr_content_len = 0; /* don't forward that again */
4849 msg->som = msg->sov;
4850 }
4851
Willy Tarreaucaabe412010-01-03 23:08:28 +01004852 if (msg->msg_state == HTTP_MSG_DATA) {
4853 /* must still forward */
4854 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004855 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004856
4857 /* nothing left to forward */
4858 if (txn->flags & TX_RES_TE_CHNK)
4859 msg->msg_state = HTTP_MSG_DATA_CRLF;
4860 else
4861 msg->msg_state = HTTP_MSG_DONE;
4862 }
4863 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004864 /* read the chunk size and assign it to ->hdr_content_len, then
4865 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4866 */
4867 int ret = http_parse_chunk_size(res, msg);
4868
4869 if (!ret)
4870 goto missing_data;
4871 else if (ret < 0)
4872 goto return_bad_res;
4873 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004874 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004875 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4876 /* we want the CRLF after the data */
4877 int ret;
4878
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004879 res->lr = res->w + res->send_max;
4880 if (res->lr >= res->data + res->size)
4881 res->lr -= res->size;
4882
Willy Tarreaud98cf932009-12-27 22:54:55 +01004883 ret = http_skip_chunk_crlf(res, msg);
4884
4885 if (!ret)
4886 goto missing_data;
4887 else if (ret < 0)
4888 goto return_bad_res;
4889 /* we're in MSG_CHUNK_SIZE now */
4890 }
4891 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4892 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004893
Willy Tarreaud98cf932009-12-27 22:54:55 +01004894 if (ret == 0)
4895 goto missing_data;
4896 else if (ret < 0)
4897 goto return_bad_res;
4898 /* we're in HTTP_MSG_DONE now */
4899 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004900 else {
4901 /* other states, DONE...TUNNEL */
4902 if (http_resync_states(s)) {
4903 http_silent_debug(__LINE__, s);
4904 /* some state changes occurred, maybe the analyser
4905 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004906 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004907 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4908 goto return_bad_res;
4909 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004910 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004911 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004912 }
4913 }
4914
Willy Tarreaud98cf932009-12-27 22:54:55 +01004915 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004916 /* stop waiting for data if the input is closed before the end */
4917 if (res->flags & BF_SHUTR)
4918 goto return_bad_res;
4919
Willy Tarreau610ecce2010-01-04 21:15:02 +01004920 if (!s->req->analysers)
4921 goto return_bad_res;
4922
Willy Tarreaud98cf932009-12-27 22:54:55 +01004923 /* forward the chunk size as well as any pending data */
4924 if (msg->hdr_content_len || msg->som != msg->sov) {
4925 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4926 msg->hdr_content_len = 0; /* don't forward that again */
4927 msg->som = msg->sov;
4928 }
4929
Willy Tarreaud98cf932009-12-27 22:54:55 +01004930 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004932 return 0;
4933
4934 return_bad_res: /* let's centralize all bad resuests */
4935 txn->rsp.msg_state = HTTP_MSG_ERROR;
4936 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01004937 /* don't send any error message as we're in the body */
4938 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004939 res->analysers = 0;
4940 s->be->counters.failed_resp++;
4941 if (s->srv) {
4942 s->srv->counters.failed_resp++;
4943 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4944 }
4945
4946 if (!(s->flags & SN_ERR_MASK))
4947 s->flags |= SN_ERR_PRXCOND;
4948 if (!(s->flags & SN_FINST_MASK))
4949 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 return 0;
4952}
4953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954/* Iterate the same filter through all request headers.
4955 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004956 * Since it can manage the switch to another backend, it updates the per-proxy
4957 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004958 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004959int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004960{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004961 char term;
4962 char *cur_ptr, *cur_end, *cur_next;
4963 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004964 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004965 struct hdr_idx_elem *cur_hdr;
4966 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004968 last_hdr = 0;
4969
Willy Tarreau962c3f42010-01-10 00:15:35 +01004970 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004971 old_idx = 0;
4972
4973 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004974 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004975 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004976 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004977 (exp->action == ACT_ALLOW ||
4978 exp->action == ACT_DENY ||
4979 exp->action == ACT_TARPIT))
4980 return 0;
4981
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004982 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004983 if (!cur_idx)
4984 break;
4985
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004986 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004987 cur_ptr = cur_next;
4988 cur_end = cur_ptr + cur_hdr->len;
4989 cur_next = cur_end + cur_hdr->cr + 1;
4990
4991 /* Now we have one header between cur_ptr and cur_end,
4992 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004993 */
4994
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004995 /* The annoying part is that pattern matching needs
4996 * that we modify the contents to null-terminate all
4997 * strings before testing them.
4998 */
4999
5000 term = *cur_end;
5001 *cur_end = '\0';
5002
5003 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5004 switch (exp->action) {
5005 case ACT_SETBE:
5006 /* It is not possible to jump a second time.
5007 * FIXME: should we return an HTTP/500 here so that
5008 * the admin knows there's a problem ?
5009 */
5010 if (t->be != t->fe)
5011 break;
5012
5013 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005014 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005015 last_hdr = 1;
5016 break;
5017
5018 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005019 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005020 last_hdr = 1;
5021 break;
5022
5023 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005024 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005025 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005026
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005027 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005028 if (t->listener->counters)
5029 t->listener->counters->denied_resp++;
5030
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005031 break;
5032
5033 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005034 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005035 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005036
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005037 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005038 if (t->listener->counters)
5039 t->listener->counters->denied_resp++;
5040
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005041 break;
5042
5043 case ACT_REPLACE:
5044 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5045 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5046 /* FIXME: if the user adds a newline in the replacement, the
5047 * index will not be recalculated for now, and the new line
5048 * will not be counted as a new header.
5049 */
5050
5051 cur_end += delta;
5052 cur_next += delta;
5053 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005054 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005055 break;
5056
5057 case ACT_REMOVE:
5058 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5059 cur_next += delta;
5060
Willy Tarreaufa355d42009-11-29 18:12:29 +01005061 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005062 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5063 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005064 cur_hdr->len = 0;
5065 cur_end = NULL; /* null-term has been rewritten */
5066 break;
5067
5068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005069 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005070 if (cur_end)
5071 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005073 /* keep the link from this header to next one in case of later
5074 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005075 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005076 old_idx = cur_idx;
5077 }
5078 return 0;
5079}
5080
5081
5082/* Apply the filter to the request line.
5083 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5084 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005085 * Since it can manage the switch to another backend, it updates the per-proxy
5086 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005087 */
5088int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5089{
5090 char term;
5091 char *cur_ptr, *cur_end;
5092 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005093 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005094 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005095
Willy Tarreau58f10d72006-12-04 02:26:12 +01005096
Willy Tarreau3d300592007-03-18 18:34:41 +01005097 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005098 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005099 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005100 (exp->action == ACT_ALLOW ||
5101 exp->action == ACT_DENY ||
5102 exp->action == ACT_TARPIT))
5103 return 0;
5104 else if (exp->action == ACT_REMOVE)
5105 return 0;
5106
5107 done = 0;
5108
Willy Tarreau962c3f42010-01-10 00:15:35 +01005109 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005110 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005111
5112 /* Now we have the request line between cur_ptr and cur_end */
5113
5114 /* The annoying part is that pattern matching needs
5115 * that we modify the contents to null-terminate all
5116 * strings before testing them.
5117 */
5118
5119 term = *cur_end;
5120 *cur_end = '\0';
5121
5122 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5123 switch (exp->action) {
5124 case ACT_SETBE:
5125 /* It is not possible to jump a second time.
5126 * FIXME: should we return an HTTP/500 here so that
5127 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005128 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005129 if (t->be != t->fe)
5130 break;
5131
5132 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005133 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005134 done = 1;
5135 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005137 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005138 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005139 done = 1;
5140 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005142 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005143 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005144
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005145 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005146 if (t->listener->counters)
5147 t->listener->counters->denied_resp++;
5148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005149 done = 1;
5150 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005152 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005153 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005154
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005155 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005156 if (t->listener->counters)
5157 t->listener->counters->denied_resp++;
5158
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005159 done = 1;
5160 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005162 case ACT_REPLACE:
5163 *cur_end = term; /* restore the string terminator */
5164 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5165 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5166 /* FIXME: if the user adds a newline in the replacement, the
5167 * index will not be recalculated for now, and the new line
5168 * will not be counted as a new header.
5169 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005170
Willy Tarreaufa355d42009-11-29 18:12:29 +01005171 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005172 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005173 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005174 HTTP_MSG_RQMETH,
5175 cur_ptr, cur_end + 1,
5176 NULL, NULL);
5177 if (unlikely(!cur_end))
5178 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005180 /* we have a full request and we know that we have either a CR
5181 * or an LF at <ptr>.
5182 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005183 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5184 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005185 /* there is no point trying this regex on headers */
5186 return 1;
5187 }
5188 }
5189 *cur_end = term; /* restore the string terminator */
5190 return done;
5191}
Willy Tarreau97de6242006-12-27 17:18:38 +01005192
Willy Tarreau58f10d72006-12-04 02:26:12 +01005193
Willy Tarreau58f10d72006-12-04 02:26:12 +01005194
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005195/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005196 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005197 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005198 * unparsable request. Since it can manage the switch to another backend, it
5199 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005200 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005201int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005202{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005203 struct http_txn *txn = &s->txn;
5204 struct hdr_exp *exp;
5205
5206 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005207 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005208
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005209 /*
5210 * The interleaving of transformations and verdicts
5211 * makes it difficult to decide to continue or stop
5212 * the evaluation.
5213 */
5214
Willy Tarreau6c123b12010-01-28 20:22:06 +01005215 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5216 break;
5217
Willy Tarreau3d300592007-03-18 18:34:41 +01005218 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005219 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005220 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005221 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005222
5223 /* if this filter had a condition, evaluate it now and skip to
5224 * next filter if the condition does not match.
5225 */
5226 if (exp->cond) {
5227 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5228 ret = acl_pass(ret);
5229 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5230 ret = !ret;
5231
5232 if (!ret)
5233 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005234 }
5235
5236 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005237 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005238 if (unlikely(ret < 0))
5239 return -1;
5240
5241 if (likely(ret == 0)) {
5242 /* The filter did not match the request, it can be
5243 * iterated through all headers.
5244 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005245 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005247 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005248 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249}
5250
5251
Willy Tarreaua15645d2007-03-18 16:22:39 +01005252
Willy Tarreau58f10d72006-12-04 02:26:12 +01005253/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005254 * Try to retrieve the server associated to the appsession.
5255 * If the server is found, it's assigned to the session.
5256 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005257void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005258 struct http_txn *txn = &t->txn;
5259 appsess *asession = NULL;
5260 char *sessid_temp = NULL;
5261
Cyril Bontéb21570a2009-11-29 20:04:48 +01005262 if (len > t->be->appsession_len) {
5263 len = t->be->appsession_len;
5264 }
5265
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005266 if (t->be->options2 & PR_O2_AS_REQL) {
5267 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005268 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005269 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005270 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005271 }
5272
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005273 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005274 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5275 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5276 return;
5277 }
5278
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005279 memcpy(txn->sessid, buf, len);
5280 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005281 }
5282
5283 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5284 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5285 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5286 return;
5287 }
5288
Cyril Bontéb21570a2009-11-29 20:04:48 +01005289 memcpy(sessid_temp, buf, len);
5290 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005291
5292 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5293 /* free previously allocated memory */
5294 pool_free2(apools.sessid, sessid_temp);
5295
5296 if (asession != NULL) {
5297 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5298 if (!(t->be->options2 & PR_O2_AS_REQL))
5299 asession->request_count++;
5300
5301 if (asession->serverid != NULL) {
5302 struct server *srv = t->be->srv;
5303 while (srv) {
5304 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005305 if ((srv->state & SRV_RUNNING) ||
5306 (t->be->options & PR_O_PERSIST) ||
5307 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005308 /* we found the server and it's usable */
5309 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005310 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005311 t->flags |= SN_DIRECT | SN_ASSIGNED;
5312 t->srv = srv;
5313 break;
5314 } else {
5315 txn->flags &= ~TX_CK_MASK;
5316 txn->flags |= TX_CK_DOWN;
5317 }
5318 }
5319 srv = srv->next;
5320 }
5321 }
5322 }
5323}
5324
5325/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005326 * Manage client-side cookie. It can impact performance by about 2% so it is
5327 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005328 */
5329void manage_client_side_cookies(struct session *t, struct buffer *req)
5330{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005331 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005332 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005333 char *del_colon, *del_cookie, *colon;
5334 int app_cookies;
5335
Willy Tarreau58f10d72006-12-04 02:26:12 +01005336 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005337 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005338
Willy Tarreau2a324282006-12-05 00:05:46 +01005339 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005340 * we start with the start line.
5341 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005342 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005343 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005344
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005345 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005346 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005347 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005348
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005349 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005350 cur_ptr = cur_next;
5351 cur_end = cur_ptr + cur_hdr->len;
5352 cur_next = cur_end + cur_hdr->cr + 1;
5353
5354 /* We have one full header between cur_ptr and cur_end, and the
5355 * next header starts at cur_next. We're only interested in
5356 * "Cookie:" headers.
5357 */
5358
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005359 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5360 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005361 old_idx = cur_idx;
5362 continue;
5363 }
5364
5365 /* Now look for cookies. Conforming to RFC2109, we have to support
5366 * attributes whose name begin with a '$', and associate them with
5367 * the right cookie, if we want to delete this cookie.
5368 * So there are 3 cases for each cookie read :
5369 * 1) it's a special attribute, beginning with a '$' : ignore it.
5370 * 2) it's a server id cookie that we *MAY* want to delete : save
5371 * some pointers on it (last semi-colon, beginning of cookie...)
5372 * 3) it's an application cookie : we *MAY* have to delete a previous
5373 * "special" cookie.
5374 * At the end of loop, if a "special" cookie remains, we may have to
5375 * remove it. If no application cookie persists in the header, we
5376 * *MUST* delete it
5377 */
5378
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005379 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005380
Willy Tarreau58f10d72006-12-04 02:26:12 +01005381 /* del_cookie == NULL => nothing to be deleted */
5382 del_colon = del_cookie = NULL;
5383 app_cookies = 0;
5384
5385 while (p1 < cur_end) {
5386 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005387 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005388 while (p1 < cur_end) {
5389 if (*p1 == ';' || *p1 == ',')
5390 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005391 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005392 break;
5393 p1++;
5394 }
5395
5396 if (p1 == cur_end)
5397 break;
5398
5399 /* p1 is at the beginning of the cookie name */
5400 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005401 while (p2 < cur_end && *p2 != '=') {
5402 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5403 /* oops, the cookie name was truncated, resync */
5404 p1 = p2;
5405 goto resync_name;
5406 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005407 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005408 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005409
5410 if (p2 == cur_end)
5411 break;
5412
5413 p3 = p2 + 1; /* skips the '=' sign */
5414 if (p3 == cur_end)
5415 break;
5416
Willy Tarreau305ae852010-01-03 19:45:54 +01005417 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5418 p5 = p4 = p3;
5419 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5420 if (!isspace((unsigned char)*p5))
5421 p4 = p5 + 1;
5422 p5++;
5423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005424
5425 /* here, we have the cookie name between p1 and p2,
5426 * and its value between p3 and p4.
5427 * we can process it :
5428 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005429 * Cookie: NAME=VALUE ;
5430 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005431 * | || || +--> p4
5432 * | || |+-------> p3
5433 * | || +--------> p2
5434 * | |+------------> p1
5435 * | +-------------> colon
5436 * +--------------------> cur_ptr
5437 */
5438
5439 if (*p1 == '$') {
5440 /* skip this one */
5441 }
5442 else {
5443 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005444 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005445 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005446 (p4 - p1 >= t->fe->capture_namelen) &&
5447 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005448 int log_len = p4 - p1;
5449
Willy Tarreau086b3b42007-05-13 21:45:51 +02005450 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005451 Alert("HTTP logging : out of memory.\n");
5452 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005453 if (log_len > t->fe->capture_len)
5454 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005455 memcpy(txn->cli_cookie, p1, log_len);
5456 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005457 }
5458 }
5459
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005460 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5461 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005462 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005463 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005464 char *delim;
5465
5466 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5467 * have the server ID betweek p3 and delim, and the original cookie between
5468 * delim+1 and p4. Otherwise, delim==p4 :
5469 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005470 * Cookie: NAME=SRV~VALUE ;
5471 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005472 * | || || | +--> p4
5473 * | || || +--------> delim
5474 * | || |+-----------> p3
5475 * | || +------------> p2
5476 * | |+----------------> p1
5477 * | +-----------------> colon
5478 * +------------------------> cur_ptr
5479 */
5480
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005481 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005482 for (delim = p3; delim < p4; delim++)
5483 if (*delim == COOKIE_DELIM)
5484 break;
5485 }
5486 else
5487 delim = p4;
5488
5489
5490 /* Here, we'll look for the first running server which supports the cookie.
5491 * This allows to share a same cookie between several servers, for example
5492 * to dedicate backup servers to specific servers only.
5493 * However, to prevent clients from sticking to cookie-less backup server
5494 * when they have incidentely learned an empty cookie, we simply ignore
5495 * empty cookies and mark them as invalid.
5496 */
5497 if (delim == p3)
5498 srv = NULL;
5499
5500 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005501 if (srv->cookie && (srv->cklen == delim - p3) &&
5502 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005503 if ((srv->state & SRV_RUNNING) ||
5504 (t->be->options & PR_O_PERSIST) ||
5505 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005506 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005507 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005508 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005509 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005510 t->srv = srv;
5511 break;
5512 } else {
5513 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005514 txn->flags &= ~TX_CK_MASK;
5515 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005516 }
5517 }
5518 srv = srv->next;
5519 }
5520
Willy Tarreau3d300592007-03-18 18:34:41 +01005521 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005522 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005523 txn->flags &= ~TX_CK_MASK;
5524 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005525 }
5526
5527 /* depending on the cookie mode, we may have to either :
5528 * - delete the complete cookie if we're in insert+indirect mode, so that
5529 * the server never sees it ;
5530 * - remove the server id from the cookie value, and tag the cookie as an
5531 * application cookie so that it does not get accidentely removed later,
5532 * if we're in cookie prefix mode
5533 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005534 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005535 int delta; /* negative */
5536
5537 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5538 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005539 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005540 cur_end += delta;
5541 cur_next += delta;
5542 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005543 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005544
5545 del_cookie = del_colon = NULL;
5546 app_cookies++; /* protect the header from deletion */
5547 }
5548 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005549 (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 +01005550 del_cookie = p1;
5551 del_colon = colon;
5552 }
5553 } else {
5554 /* now we know that we must keep this cookie since it's
5555 * not ours. But if we wanted to delete our cookie
5556 * earlier, we cannot remove the complete header, but we
5557 * can remove the previous block itself.
5558 */
5559 app_cookies++;
5560
5561 if (del_cookie != NULL) {
5562 int delta; /* negative */
5563
5564 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5565 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005566 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005567 cur_end += delta;
5568 cur_next += delta;
5569 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005570 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005571 del_cookie = del_colon = NULL;
5572 }
5573 }
5574
Cyril Bontéb21570a2009-11-29 20:04:48 +01005575 if (t->be->appsession_name != NULL) {
5576 int cmp_len, value_len;
5577 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005578
Cyril Bontéb21570a2009-11-29 20:04:48 +01005579 if (t->be->options2 & PR_O2_AS_PFX) {
5580 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5581 value_begin = p1 + t->be->appsession_name_len;
5582 value_len = p4 - p1 - t->be->appsession_name_len;
5583 } else {
5584 cmp_len = p2 - p1;
5585 value_begin = p3;
5586 value_len = p4 - p3;
5587 }
5588
5589 /* let's see if the cookie is our appcookie */
5590 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5591 /* Cool... it's the right one */
5592 manage_client_side_appsession(t, value_begin, value_len);
5593 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005594#if defined(DEBUG_HASH)
5595 Alert("manage_client_side_cookies\n");
5596 appsession_hash_dump(&(t->be->htbl_proxy));
5597#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005598 }/* end if ((t->proxy->appsession_name != NULL) ... */
5599 }
5600
5601 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005602 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005603 } /* while (p1 < cur_end) */
5604
5605 /* There's no more cookie on this line.
5606 * We may have marked the last one(s) for deletion.
5607 * We must do this now in two ways :
5608 * - if there is no app cookie, we simply delete the header ;
5609 * - if there are app cookies, we must delete the end of the
5610 * string properly, including the colon/semi-colon before
5611 * the cookie name.
5612 */
5613 if (del_cookie != NULL) {
5614 int delta;
5615 if (app_cookies) {
5616 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5617 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005618 cur_hdr->len += delta;
5619 } else {
5620 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005621
5622 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005623 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5624 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005625 cur_hdr->len = 0;
5626 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005627 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005628 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005629 }
5630
5631 /* keep the link from this header to next one */
5632 old_idx = cur_idx;
5633 } /* end of cookie processing on this header */
5634}
5635
5636
Willy Tarreaua15645d2007-03-18 16:22:39 +01005637/* Iterate the same filter through all response headers contained in <rtr>.
5638 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5639 */
5640int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5641{
5642 char term;
5643 char *cur_ptr, *cur_end, *cur_next;
5644 int cur_idx, old_idx, last_hdr;
5645 struct http_txn *txn = &t->txn;
5646 struct hdr_idx_elem *cur_hdr;
5647 int len, delta;
5648
5649 last_hdr = 0;
5650
Willy Tarreau962c3f42010-01-10 00:15:35 +01005651 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005652 old_idx = 0;
5653
5654 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005655 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005656 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005657 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005658 (exp->action == ACT_ALLOW ||
5659 exp->action == ACT_DENY))
5660 return 0;
5661
5662 cur_idx = txn->hdr_idx.v[old_idx].next;
5663 if (!cur_idx)
5664 break;
5665
5666 cur_hdr = &txn->hdr_idx.v[cur_idx];
5667 cur_ptr = cur_next;
5668 cur_end = cur_ptr + cur_hdr->len;
5669 cur_next = cur_end + cur_hdr->cr + 1;
5670
5671 /* Now we have one header between cur_ptr and cur_end,
5672 * and the next header starts at cur_next.
5673 */
5674
5675 /* The annoying part is that pattern matching needs
5676 * that we modify the contents to null-terminate all
5677 * strings before testing them.
5678 */
5679
5680 term = *cur_end;
5681 *cur_end = '\0';
5682
5683 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5684 switch (exp->action) {
5685 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005686 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005687 last_hdr = 1;
5688 break;
5689
5690 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005691 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005692 last_hdr = 1;
5693 break;
5694
5695 case ACT_REPLACE:
5696 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5697 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5698 /* FIXME: if the user adds a newline in the replacement, the
5699 * index will not be recalculated for now, and the new line
5700 * will not be counted as a new header.
5701 */
5702
5703 cur_end += delta;
5704 cur_next += delta;
5705 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005706 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005707 break;
5708
5709 case ACT_REMOVE:
5710 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5711 cur_next += delta;
5712
Willy Tarreaufa355d42009-11-29 18:12:29 +01005713 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005714 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5715 txn->hdr_idx.used--;
5716 cur_hdr->len = 0;
5717 cur_end = NULL; /* null-term has been rewritten */
5718 break;
5719
5720 }
5721 }
5722 if (cur_end)
5723 *cur_end = term; /* restore the string terminator */
5724
5725 /* keep the link from this header to next one in case of later
5726 * removal of next header.
5727 */
5728 old_idx = cur_idx;
5729 }
5730 return 0;
5731}
5732
5733
5734/* Apply the filter to the status line in the response buffer <rtr>.
5735 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5736 * or -1 if a replacement resulted in an invalid status line.
5737 */
5738int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5739{
5740 char term;
5741 char *cur_ptr, *cur_end;
5742 int done;
5743 struct http_txn *txn = &t->txn;
5744 int len, delta;
5745
5746
Willy Tarreau3d300592007-03-18 18:34:41 +01005747 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005748 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005749 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005750 (exp->action == ACT_ALLOW ||
5751 exp->action == ACT_DENY))
5752 return 0;
5753 else if (exp->action == ACT_REMOVE)
5754 return 0;
5755
5756 done = 0;
5757
Willy Tarreau962c3f42010-01-10 00:15:35 +01005758 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005759 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5760
5761 /* Now we have the status line between cur_ptr and cur_end */
5762
5763 /* The annoying part is that pattern matching needs
5764 * that we modify the contents to null-terminate all
5765 * strings before testing them.
5766 */
5767
5768 term = *cur_end;
5769 *cur_end = '\0';
5770
5771 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5772 switch (exp->action) {
5773 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005774 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005775 done = 1;
5776 break;
5777
5778 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005779 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005780 done = 1;
5781 break;
5782
5783 case ACT_REPLACE:
5784 *cur_end = term; /* restore the string terminator */
5785 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5786 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5787 /* FIXME: if the user adds a newline in the replacement, the
5788 * index will not be recalculated for now, and the new line
5789 * will not be counted as a new header.
5790 */
5791
Willy Tarreaufa355d42009-11-29 18:12:29 +01005792 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005793 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005795 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005796 cur_ptr, cur_end + 1,
5797 NULL, NULL);
5798 if (unlikely(!cur_end))
5799 return -1;
5800
5801 /* we have a full respnse and we know that we have either a CR
5802 * or an LF at <ptr>.
5803 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005804 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005805 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5806 /* there is no point trying this regex on headers */
5807 return 1;
5808 }
5809 }
5810 *cur_end = term; /* restore the string terminator */
5811 return done;
5812}
5813
5814
5815
5816/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005817 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005818 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5819 * unparsable response.
5820 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005821int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005822{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005823 struct http_txn *txn = &s->txn;
5824 struct hdr_exp *exp;
5825
5826 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005827 int ret;
5828
5829 /*
5830 * The interleaving of transformations and verdicts
5831 * makes it difficult to decide to continue or stop
5832 * the evaluation.
5833 */
5834
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005835 if (txn->flags & TX_SVDENY)
5836 break;
5837
Willy Tarreau3d300592007-03-18 18:34:41 +01005838 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005839 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5840 exp->action == ACT_PASS)) {
5841 exp = exp->next;
5842 continue;
5843 }
5844
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005845 /* if this filter had a condition, evaluate it now and skip to
5846 * next filter if the condition does not match.
5847 */
5848 if (exp->cond) {
5849 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
5850 ret = acl_pass(ret);
5851 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5852 ret = !ret;
5853 if (!ret)
5854 continue;
5855 }
5856
Willy Tarreaua15645d2007-03-18 16:22:39 +01005857 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005858 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005859 if (unlikely(ret < 0))
5860 return -1;
5861
5862 if (likely(ret == 0)) {
5863 /* The filter did not match the response, it can be
5864 * iterated through all headers.
5865 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005866 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005867 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868 }
5869 return 0;
5870}
5871
5872
5873
5874/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005875 * Manage server-side cookies. It can impact performance by about 2% so it is
5876 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877 */
5878void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5879{
5880 struct http_txn *txn = &t->txn;
5881 char *p1, *p2, *p3, *p4;
5882
Willy Tarreaua15645d2007-03-18 16:22:39 +01005883 char *cur_ptr, *cur_end, *cur_next;
5884 int cur_idx, old_idx, delta;
5885
Willy Tarreaua15645d2007-03-18 16:22:39 +01005886 /* Iterate through the headers.
5887 * we start with the start line.
5888 */
5889 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005890 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891
5892 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5893 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005894 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005895
5896 cur_hdr = &txn->hdr_idx.v[cur_idx];
5897 cur_ptr = cur_next;
5898 cur_end = cur_ptr + cur_hdr->len;
5899 cur_next = cur_end + cur_hdr->cr + 1;
5900
5901 /* We have one full header between cur_ptr and cur_end, and the
5902 * next header starts at cur_next. We're only interested in
5903 * "Cookie:" headers.
5904 */
5905
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005906 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5907 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005908 old_idx = cur_idx;
5909 continue;
5910 }
5911
5912 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005913 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005914
5915
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005916 /* maybe we only wanted to see if there was a set-cookie. Note that
5917 * the cookie capture is declared in the fronend.
5918 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005919 if (t->be->cookie_name == NULL &&
5920 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005921 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005922 return;
5923
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005924 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005925
5926 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005927 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5928 break;
5929
5930 /* p1 is at the beginning of the cookie name */
5931 p2 = p1;
5932
5933 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5934 p2++;
5935
5936 if (p2 == cur_end || *p2 == ';') /* next cookie */
5937 break;
5938
5939 p3 = p2 + 1; /* skip the '=' sign */
5940 if (p3 == cur_end)
5941 break;
5942
5943 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005944 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005945 p4++;
5946
5947 /* here, we have the cookie name between p1 and p2,
5948 * and its value between p3 and p4.
5949 * we can process it.
5950 */
5951
5952 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005953 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005954 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005955 (p4 - p1 >= t->fe->capture_namelen) &&
5956 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005957 int log_len = p4 - p1;
5958
Willy Tarreau086b3b42007-05-13 21:45:51 +02005959 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005960 Alert("HTTP logging : out of memory.\n");
5961 }
5962
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005963 if (log_len > t->fe->capture_len)
5964 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005965 memcpy(txn->srv_cookie, p1, log_len);
5966 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005967 }
5968
5969 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005970 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5971 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005972 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005973 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005974
5975 /* If the cookie is in insert mode on a known server, we'll delete
5976 * this occurrence because we'll insert another one later.
5977 * We'll delete it too if the "indirect" option is set and we're in
5978 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005979 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5980 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005981 /* this header must be deleted */
5982 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5983 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5984 txn->hdr_idx.used--;
5985 cur_hdr->len = 0;
5986 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005987 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005988
Willy Tarreau3d300592007-03-18 18:34:41 +01005989 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005990 }
5991 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005992 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005993 /* replace bytes p3->p4 with the cookie name associated
5994 * with this server since we know it.
5995 */
5996 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5997 cur_hdr->len += delta;
5998 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005999 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006000
Willy Tarreau3d300592007-03-18 18:34:41 +01006001 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006002 }
6003 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006004 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006005 /* insert the cookie name associated with this server
6006 * before existing cookie, and insert a delimitor between them..
6007 */
6008 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6009 cur_hdr->len += delta;
6010 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006011 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006012
6013 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006014 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006015 }
6016 }
6017 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006018 else if (t->be->appsession_name != NULL) {
6019 int cmp_len, value_len;
6020 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006021
Cyril Bontéb21570a2009-11-29 20:04:48 +01006022 if (t->be->options2 & PR_O2_AS_PFX) {
6023 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6024 value_begin = p1 + t->be->appsession_name_len;
6025 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6026 } else {
6027 cmp_len = p2 - p1;
6028 value_begin = p3;
6029 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006030 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006031
6032 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
6033 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006034 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006035 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006036 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006037 }
6038 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006039 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006040 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6041 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6042 return;
6043 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006044 memcpy(txn->sessid, value_begin, value_len);
6045 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006046 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006047 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006048 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6049 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006050 /* keep the link from this header to next one */
6051 old_idx = cur_idx;
6052 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006053
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006054 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006055 appsess *asession = NULL;
6056 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006057 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006058 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006059 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006060 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6061 Alert("Not enough Memory process_srv():asession:calloc().\n");
6062 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6063 return;
6064 }
6065 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6066 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6067 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006068 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006069 return;
6070 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006071 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006072 asession->sessid[t->be->appsession_len] = 0;
6073
Willy Tarreau1fac7532010-01-09 19:23:06 +01006074 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006075 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6076 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6077 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006078 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006079 return;
6080 }
6081 asession->serverid[0] = '\0';
6082 memcpy(asession->serverid, t->srv->id, server_id_len);
6083
6084 asession->request_count = 0;
6085 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6086 }
6087
6088 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6089 asession->request_count++;
6090 }
6091
6092#if defined(DEBUG_HASH)
6093 Alert("manage_server_side_cookies\n");
6094 appsession_hash_dump(&(t->be->htbl_proxy));
6095#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006096}
6097
6098
6099
6100/*
6101 * Check if response is cacheable or not. Updates t->flags.
6102 */
6103void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6104{
6105 struct http_txn *txn = &t->txn;
6106 char *p1, *p2;
6107
6108 char *cur_ptr, *cur_end, *cur_next;
6109 int cur_idx;
6110
Willy Tarreau5df51872007-11-25 16:20:08 +01006111 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006112 return;
6113
6114 /* Iterate through the headers.
6115 * we start with the start line.
6116 */
6117 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006118 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006119
6120 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6121 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006122 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006123
6124 cur_hdr = &txn->hdr_idx.v[cur_idx];
6125 cur_ptr = cur_next;
6126 cur_end = cur_ptr + cur_hdr->len;
6127 cur_next = cur_end + cur_hdr->cr + 1;
6128
6129 /* We have one full header between cur_ptr and cur_end, and the
6130 * next header starts at cur_next. We're only interested in
6131 * "Cookie:" headers.
6132 */
6133
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006134 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6135 if (val) {
6136 if ((cur_end - (cur_ptr + val) >= 8) &&
6137 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6138 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6139 return;
6140 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006141 }
6142
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006143 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6144 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006145 continue;
6146
6147 /* OK, right now we know we have a cache-control header at cur_ptr */
6148
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006149 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006150
6151 if (p1 >= cur_end) /* no more info */
6152 continue;
6153
6154 /* p1 is at the beginning of the value */
6155 p2 = p1;
6156
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006157 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006158 p2++;
6159
6160 /* we have a complete value between p1 and p2 */
6161 if (p2 < cur_end && *p2 == '=') {
6162 /* we have something of the form no-cache="set-cookie" */
6163 if ((cur_end - p1 >= 21) &&
6164 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6165 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006166 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006167 continue;
6168 }
6169
6170 /* OK, so we know that either p2 points to the end of string or to a comma */
6171 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6172 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6173 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6174 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006175 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006176 return;
6177 }
6178
6179 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006180 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006181 continue;
6182 }
6183 }
6184}
6185
6186
Willy Tarreau58f10d72006-12-04 02:26:12 +01006187/*
6188 * Try to retrieve a known appsession in the URI, then the associated server.
6189 * If the server is found, it's assigned to the session.
6190 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006191void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006192{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006193 char *end_params, *first_param, *cur_param, *next_param;
6194 char separator;
6195 int value_len;
6196
6197 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006198
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006199 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006200 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006201 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006202 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006203
Cyril Bontéb21570a2009-11-29 20:04:48 +01006204 first_param = NULL;
6205 switch (mode) {
6206 case PR_O2_AS_M_PP:
6207 first_param = memchr(begin, ';', len);
6208 break;
6209 case PR_O2_AS_M_QS:
6210 first_param = memchr(begin, '?', len);
6211 break;
6212 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006213
Cyril Bontéb21570a2009-11-29 20:04:48 +01006214 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006215 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006216 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006217
Cyril Bontéb21570a2009-11-29 20:04:48 +01006218 switch (mode) {
6219 case PR_O2_AS_M_PP:
6220 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6221 end_params = (char *) begin + len;
6222 }
6223 separator = ';';
6224 break;
6225 case PR_O2_AS_M_QS:
6226 end_params = (char *) begin + len;
6227 separator = '&';
6228 break;
6229 default:
6230 /* unknown mode, shouldn't happen */
6231 return;
6232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233
Cyril Bontéb21570a2009-11-29 20:04:48 +01006234 cur_param = next_param = end_params;
6235 while (cur_param > first_param) {
6236 cur_param--;
6237 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6238 /* let's see if this is the appsession parameter */
6239 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6240 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6241 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6242 /* Cool... it's the right one */
6243 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6244 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6245 if (value_len > 0) {
6246 manage_client_side_appsession(t, cur_param, value_len);
6247 }
6248 break;
6249 }
6250 next_param = cur_param;
6251 }
6252 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006253#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006254 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006255 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257}
6258
6259
Willy Tarreaub2513902006-12-17 14:52:38 +01006260/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006261 * In a GET or HEAD request, check if the requested URI matches the stats uri
6262 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006263 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006264 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006265 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006266 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006267 *
6268 * Returns 1 if the session's state changes, otherwise 0.
6269 */
6270int stats_check_uri_auth(struct session *t, struct proxy *backend)
6271{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006272 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006273 struct uri_auth *uri_auth = backend->uri_auth;
6274 struct user_auth *user;
6275 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006276 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006277
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006278 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6279
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006280 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006281 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006282 return 0;
6283
Willy Tarreau962c3f42010-01-10 00:15:35 +01006284 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006285
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006286 /* the URI is in h */
6287 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006288 return 0;
6289
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006290 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006291 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006292 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006293 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006294 break;
6295 }
6296 h++;
6297 }
6298
6299 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006300 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6301 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006302 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006303 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006304 break;
6305 }
6306 h++;
6307 }
6308 }
6309
Willy Tarreau962c3f42010-01-10 00:15:35 +01006310 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6311 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006312 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006313 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006314 break;
6315 }
6316 h++;
6317 }
6318
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006319 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6320
Willy Tarreaub2513902006-12-17 14:52:38 +01006321 /* we are in front of a interceptable URI. Let's check
6322 * if there's an authentication and if it's valid.
6323 */
6324 user = uri_auth->users;
6325 if (!user) {
6326 /* no user auth required, it's OK */
6327 authenticated = 1;
6328 } else {
6329 authenticated = 0;
6330
6331 /* a user list is defined, we have to check.
6332 * skip 21 chars for "Authorization: Basic ".
6333 */
6334
6335 /* FIXME: this should move to an earlier place */
6336 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006337 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006338 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6339 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006340 if (len > 14 &&
6341 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006342 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006343 break;
6344 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006345 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006346 }
6347
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006348 if (txn->auth_hdr.len < 21 ||
6349 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006350 user = NULL;
6351
6352 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006353 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6354 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006355 user->user_pwd, user->user_len)) {
6356 authenticated = 1;
6357 break;
6358 }
6359 user = user->next;
6360 }
6361 }
6362
6363 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006364 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006365
6366 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006367 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6368 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006369 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006370 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006371 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006372 if (!(t->flags & SN_ERR_MASK))
6373 t->flags |= SN_ERR_PRXCOND;
6374 if (!(t->flags & SN_FINST_MASK))
6375 t->flags |= SN_FINST_R;
6376 return 1;
6377 }
6378
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006379 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006380 * data.
6381 */
Willy Tarreau70089872008-06-13 21:12:51 +02006382 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006383 t->data_source = DATA_SRC_STATS;
6384 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006385 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006386 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6387 t->rep->prod->private = t;
6388 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006389 return 1;
6390}
6391
Willy Tarreau4076a152009-04-02 15:18:36 +02006392/*
6393 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006394 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6395 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006396 */
6397void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6398 struct buffer *buf, struct http_msg *msg,
6399 struct proxy *other_end)
6400{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006401 es->len = buf->r - (buf->data + msg->som);
6402 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006403 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006404 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006405 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006406 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006407 es->when = date; // user-visible date
6408 es->sid = s->uniq_id;
6409 es->srv = s->srv;
6410 es->oe = other_end;
6411 es->src = s->cli_addr;
6412}
Willy Tarreaub2513902006-12-17 14:52:38 +01006413
Willy Tarreaubaaee002006-06-26 02:48:02 +02006414/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006415 * Print a debug line with a header
6416 */
6417void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6418{
6419 int len, max;
6420 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006421 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006422 max = end - start;
6423 UBOUND(max, sizeof(trash) - len - 1);
6424 len += strlcpy2(trash + len, start, max + 1);
6425 trash[len++] = '\n';
6426 write(1, trash, len);
6427}
6428
Willy Tarreau0937bc42009-12-22 15:03:09 +01006429/*
6430 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6431 * the required fields are properly allocated and that we only need to (re)init
6432 * them. This should be used before processing any new request.
6433 */
6434void http_init_txn(struct session *s)
6435{
6436 struct http_txn *txn = &s->txn;
6437 struct proxy *fe = s->fe;
6438
6439 txn->flags = 0;
6440 txn->status = -1;
6441
6442 txn->req.sol = txn->req.eol = NULL;
6443 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6444 txn->rsp.sol = txn->rsp.eol = NULL;
6445 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6446 txn->req.hdr_content_len = 0LL;
6447 txn->rsp.hdr_content_len = 0LL;
6448 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6449 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6450 chunk_reset(&txn->auth_hdr);
6451
6452 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6453 if (fe->options2 & PR_O2_REQBUG_OK)
6454 txn->req.err_pos = -1; /* let buggy requests pass */
6455
Willy Tarreau46023632010-01-07 22:51:47 +01006456 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006457 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6458
Willy Tarreau46023632010-01-07 22:51:47 +01006459 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006460 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6461
6462 if (txn->hdr_idx.v)
6463 hdr_idx_init(&txn->hdr_idx);
6464}
6465
6466/* to be used at the end of a transaction */
6467void http_end_txn(struct session *s)
6468{
6469 struct http_txn *txn = &s->txn;
6470
6471 /* these ones will have been dynamically allocated */
6472 pool_free2(pool2_requri, txn->uri);
6473 pool_free2(pool2_capture, txn->cli_cookie);
6474 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006475 pool_free2(apools.sessid, txn->sessid);
6476 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006477 txn->uri = NULL;
6478 txn->srv_cookie = NULL;
6479 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006480
6481 if (txn->req.cap) {
6482 struct cap_hdr *h;
6483 for (h = s->fe->req_cap; h; h = h->next)
6484 pool_free2(h->pool, txn->req.cap[h->index]);
6485 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6486 }
6487
6488 if (txn->rsp.cap) {
6489 struct cap_hdr *h;
6490 for (h = s->fe->rsp_cap; h; h = h->next)
6491 pool_free2(h->pool, txn->rsp.cap[h->index]);
6492 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6493 }
6494
Willy Tarreau0937bc42009-12-22 15:03:09 +01006495}
6496
6497/* to be used at the end of a transaction to prepare a new one */
6498void http_reset_txn(struct session *s)
6499{
6500 http_end_txn(s);
6501 http_init_txn(s);
6502
6503 s->be = s->fe;
6504 s->req->analysers = s->listener->analysers;
6505 s->logs.logwait = s->fe->to_log;
6506 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006507 /* re-init store persistence */
6508 s->store_count = 0;
6509
Willy Tarreau0937bc42009-12-22 15:03:09 +01006510 s->pend_pos = NULL;
6511 s->conn_retries = s->be->conn_retries;
6512
6513 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6514
Willy Tarreau739cfba2010-01-25 23:11:14 +01006515 /* We must trim any excess data from the response buffer, because we
6516 * may have blocked an invalid response from a server that we don't
6517 * want to accidentely forward once we disable the analysers, nor do
6518 * we want those data to come along with next response. A typical
6519 * example of such data would be from a buggy server responding to
6520 * a HEAD with some data, or sending more than the advertised
6521 * content-length.
6522 */
6523 if (unlikely(s->rep->l > s->rep->send_max)) {
6524 s->rep->l = s->rep->send_max;
6525 s->rep->r = s->rep->w + s->rep->l;
6526 if (s->rep->r >= s->rep->data + s->rep->size)
6527 s->rep->r -= s->rep->size;
6528 }
6529
Willy Tarreau0937bc42009-12-22 15:03:09 +01006530 s->req->rto = s->fe->timeout.client;
6531 s->req->wto = s->be->timeout.server;
6532 s->req->cto = s->be->timeout.connect;
6533
6534 s->rep->rto = s->be->timeout.server;
6535 s->rep->wto = s->fe->timeout.client;
6536 s->rep->cto = TICK_ETERNITY;
6537
6538 s->req->rex = TICK_ETERNITY;
6539 s->req->wex = TICK_ETERNITY;
6540 s->req->analyse_exp = TICK_ETERNITY;
6541 s->rep->rex = TICK_ETERNITY;
6542 s->rep->wex = TICK_ETERNITY;
6543 s->rep->analyse_exp = TICK_ETERNITY;
6544}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545
Willy Tarreau8797c062007-05-07 00:55:35 +02006546/************************************************************************/
6547/* The code below is dedicated to ACL parsing and matching */
6548/************************************************************************/
6549
6550
6551
6552
6553/* 1. Check on METHOD
6554 * We use the pre-parsed method if it is known, and store its number as an
6555 * integer. If it is unknown, we use the pointer and the length.
6556 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006557static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006558{
6559 int len, meth;
6560
Willy Tarreauae8b7962007-06-09 23:10:04 +02006561 len = strlen(*text);
6562 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006563
6564 pattern->val.i = meth;
6565 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006566 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006567 if (!pattern->ptr.str)
6568 return 0;
6569 pattern->len = len;
6570 }
6571 return 1;
6572}
6573
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006574static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006575acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6576 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006577{
6578 int meth;
6579 struct http_txn *txn = l7;
6580
Willy Tarreaub6866442008-07-14 23:54:42 +02006581 if (!txn)
6582 return 0;
6583
Willy Tarreau655dce92009-11-08 13:10:58 +01006584 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006585 return 0;
6586
Willy Tarreau8797c062007-05-07 00:55:35 +02006587 meth = txn->meth;
6588 test->i = meth;
6589 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006590 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6591 /* ensure the indexes are not affected */
6592 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006593 test->len = txn->req.sl.rq.m_l;
6594 test->ptr = txn->req.sol;
6595 }
6596 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6597 return 1;
6598}
6599
6600static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6601{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006602 int icase;
6603
Willy Tarreau8797c062007-05-07 00:55:35 +02006604 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006605 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006606
6607 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006608 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006609
6610 /* Other method, we must compare the strings */
6611 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006612 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006613
6614 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6615 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6616 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006617 return ACL_PAT_FAIL;
6618 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006619}
6620
6621/* 2. Check on Request/Status Version
6622 * We simply compare strings here.
6623 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006624static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006625{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006626 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006627 if (!pattern->ptr.str)
6628 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006629 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006630 return 1;
6631}
6632
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006633static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006634acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6635 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006636{
6637 struct http_txn *txn = l7;
6638 char *ptr;
6639 int len;
6640
Willy Tarreaub6866442008-07-14 23:54:42 +02006641 if (!txn)
6642 return 0;
6643
Willy Tarreau655dce92009-11-08 13:10:58 +01006644 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006645 return 0;
6646
Willy Tarreau8797c062007-05-07 00:55:35 +02006647 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006648 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006649
6650 while ((len-- > 0) && (*ptr++ != '/'));
6651 if (len <= 0)
6652 return 0;
6653
6654 test->ptr = ptr;
6655 test->len = len;
6656
6657 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6658 return 1;
6659}
6660
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006661static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006662acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6663 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006664{
6665 struct http_txn *txn = l7;
6666 char *ptr;
6667 int len;
6668
Willy Tarreaub6866442008-07-14 23:54:42 +02006669 if (!txn)
6670 return 0;
6671
Willy Tarreau655dce92009-11-08 13:10:58 +01006672 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006673 return 0;
6674
Willy Tarreau8797c062007-05-07 00:55:35 +02006675 len = txn->rsp.sl.st.v_l;
6676 ptr = txn->rsp.sol;
6677
6678 while ((len-- > 0) && (*ptr++ != '/'));
6679 if (len <= 0)
6680 return 0;
6681
6682 test->ptr = ptr;
6683 test->len = len;
6684
6685 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6686 return 1;
6687}
6688
6689/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006690static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006691acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6692 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006693{
6694 struct http_txn *txn = l7;
6695 char *ptr;
6696 int len;
6697
Willy Tarreaub6866442008-07-14 23:54:42 +02006698 if (!txn)
6699 return 0;
6700
Willy Tarreau655dce92009-11-08 13:10:58 +01006701 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006702 return 0;
6703
Willy Tarreau8797c062007-05-07 00:55:35 +02006704 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006705 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006706
6707 test->i = __strl2ui(ptr, len);
6708 test->flags = ACL_TEST_F_VOL_1ST;
6709 return 1;
6710}
6711
6712/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006713static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006714acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6715 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006716{
6717 struct http_txn *txn = l7;
6718
Willy Tarreaub6866442008-07-14 23:54:42 +02006719 if (!txn)
6720 return 0;
6721
Willy Tarreau655dce92009-11-08 13:10:58 +01006722 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006723 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006724
Willy Tarreauc11416f2007-06-17 16:58:38 +02006725 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6726 /* ensure the indexes are not affected */
6727 return 0;
6728
Willy Tarreau8797c062007-05-07 00:55:35 +02006729 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006730 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006731
Willy Tarreauf3d25982007-05-08 22:45:09 +02006732 /* we do not need to set READ_ONLY because the data is in a buffer */
6733 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006734 return 1;
6735}
6736
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006737static int
6738acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6739 struct acl_expr *expr, struct acl_test *test)
6740{
6741 struct http_txn *txn = l7;
6742
Willy Tarreaub6866442008-07-14 23:54:42 +02006743 if (!txn)
6744 return 0;
6745
Willy Tarreau655dce92009-11-08 13:10:58 +01006746 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006747 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006748
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006749 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6750 /* ensure the indexes are not affected */
6751 return 0;
6752
6753 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006754 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 +01006755 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6756 test->i = AF_INET;
6757
6758 /*
6759 * If we are parsing url in frontend space, we prepare backend stage
6760 * to not parse again the same url ! optimization lazyness...
6761 */
6762 if (px->options & PR_O_HTTP_PROXY)
6763 l4->flags |= SN_ADDR_SET;
6764
6765 test->flags = ACL_TEST_F_READ_ONLY;
6766 return 1;
6767}
6768
6769static int
6770acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6771 struct acl_expr *expr, struct acl_test *test)
6772{
6773 struct http_txn *txn = l7;
6774
Willy Tarreaub6866442008-07-14 23:54:42 +02006775 if (!txn)
6776 return 0;
6777
Willy Tarreau655dce92009-11-08 13:10:58 +01006778 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006779 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006780
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006781 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6782 /* ensure the indexes are not affected */
6783 return 0;
6784
6785 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006786 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 +01006787 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6788
6789 if (px->options & PR_O_HTTP_PROXY)
6790 l4->flags |= SN_ADDR_SET;
6791
6792 test->flags = ACL_TEST_F_READ_ONLY;
6793 return 1;
6794}
6795
Willy Tarreauc11416f2007-06-17 16:58:38 +02006796/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6797 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6798 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006799static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006800acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006801 struct acl_expr *expr, struct acl_test *test)
6802{
6803 struct http_txn *txn = l7;
6804 struct hdr_idx *idx = &txn->hdr_idx;
6805 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006806
Willy Tarreaub6866442008-07-14 23:54:42 +02006807 if (!txn)
6808 return 0;
6809
Willy Tarreau33a7e692007-06-10 19:45:56 +02006810 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6811 /* search for header from the beginning */
6812 ctx->idx = 0;
6813
Willy Tarreau33a7e692007-06-10 19:45:56 +02006814 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6815 test->flags |= ACL_TEST_F_FETCH_MORE;
6816 test->flags |= ACL_TEST_F_VOL_HDR;
6817 test->len = ctx->vlen;
6818 test->ptr = (char *)ctx->line + ctx->val;
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 Tarreau33a7e692007-06-10 19:45:56 +02006827static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006828acl_fetch_chdr(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(px, l4, txn, txn->req.sol, expr, test);
6844}
6845
6846static int
6847acl_fetch_shdr(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(px, l4, txn, txn->rsp.sol, expr, test);
6859}
6860
6861/* 6. Check on HTTP header count. The number of occurrences is returned.
6862 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6863 */
6864static int
6865acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006866 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;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006871 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006872
Willy Tarreaub6866442008-07-14 23:54:42 +02006873 if (!txn)
6874 return 0;
6875
Willy Tarreau33a7e692007-06-10 19:45:56 +02006876 ctx.idx = 0;
6877 cnt = 0;
6878 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6879 cnt++;
6880
6881 test->i = cnt;
6882 test->flags = ACL_TEST_F_VOL_HDR;
6883 return 1;
6884}
6885
Willy Tarreauc11416f2007-06-17 16:58:38 +02006886static int
6887acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6888 struct acl_expr *expr, struct acl_test *test)
6889{
6890 struct http_txn *txn = l7;
6891
Willy Tarreaub6866442008-07-14 23:54:42 +02006892 if (!txn)
6893 return 0;
6894
Willy Tarreau655dce92009-11-08 13:10:58 +01006895 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006896 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006897
Willy Tarreauc11416f2007-06-17 16:58:38 +02006898 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6899 /* ensure the indexes are not affected */
6900 return 0;
6901
6902 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6903}
6904
6905static int
6906acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6907 struct acl_expr *expr, struct acl_test *test)
6908{
6909 struct http_txn *txn = l7;
6910
Willy Tarreaub6866442008-07-14 23:54:42 +02006911 if (!txn)
6912 return 0;
6913
Willy Tarreau655dce92009-11-08 13:10:58 +01006914 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006915 return 0;
6916
6917 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6918}
6919
Willy Tarreau33a7e692007-06-10 19:45:56 +02006920/* 7. Check on HTTP header's integer value. The integer value is returned.
6921 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006922 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006923 */
6924static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006925acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006926 struct acl_expr *expr, struct acl_test *test)
6927{
6928 struct http_txn *txn = l7;
6929 struct hdr_idx *idx = &txn->hdr_idx;
6930 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006931
Willy Tarreaub6866442008-07-14 23:54:42 +02006932 if (!txn)
6933 return 0;
6934
Willy Tarreau33a7e692007-06-10 19:45:56 +02006935 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6936 /* search for header from the beginning */
6937 ctx->idx = 0;
6938
Willy Tarreau33a7e692007-06-10 19:45:56 +02006939 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6940 test->flags |= ACL_TEST_F_FETCH_MORE;
6941 test->flags |= ACL_TEST_F_VOL_HDR;
6942 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6943 return 1;
6944 }
6945
6946 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6947 test->flags |= ACL_TEST_F_VOL_HDR;
6948 return 0;
6949}
6950
Willy Tarreauc11416f2007-06-17 16:58:38 +02006951static int
6952acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6953 struct acl_expr *expr, struct acl_test *test)
6954{
6955 struct http_txn *txn = l7;
6956
Willy Tarreaub6866442008-07-14 23:54:42 +02006957 if (!txn)
6958 return 0;
6959
Willy Tarreau655dce92009-11-08 13:10:58 +01006960 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006961 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006962
Willy Tarreauc11416f2007-06-17 16:58:38 +02006963 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6964 /* ensure the indexes are not affected */
6965 return 0;
6966
6967 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6968}
6969
6970static int
6971acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6972 struct acl_expr *expr, struct acl_test *test)
6973{
6974 struct http_txn *txn = l7;
6975
Willy Tarreaub6866442008-07-14 23:54:42 +02006976 if (!txn)
6977 return 0;
6978
Willy Tarreau655dce92009-11-08 13:10:58 +01006979 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006980 return 0;
6981
6982 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6983}
6984
Willy Tarreau106f9792009-09-19 07:54:16 +02006985/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6986 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6987 */
6988static int
6989acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6990 struct acl_expr *expr, struct acl_test *test)
6991{
6992 struct http_txn *txn = l7;
6993 struct hdr_idx *idx = &txn->hdr_idx;
6994 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6995
6996 if (!txn)
6997 return 0;
6998
6999 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7000 /* search for header from the beginning */
7001 ctx->idx = 0;
7002
7003 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7004 test->flags |= ACL_TEST_F_FETCH_MORE;
7005 test->flags |= ACL_TEST_F_VOL_HDR;
7006 /* Same optimization as url_ip */
7007 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7008 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7009 test->ptr = (void *)&l4->srv_addr.sin_addr;
7010 test->i = AF_INET;
7011 return 1;
7012 }
7013
7014 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7015 test->flags |= ACL_TEST_F_VOL_HDR;
7016 return 0;
7017}
7018
7019static int
7020acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7021 struct acl_expr *expr, struct acl_test *test)
7022{
7023 struct http_txn *txn = l7;
7024
7025 if (!txn)
7026 return 0;
7027
Willy Tarreau655dce92009-11-08 13:10:58 +01007028 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007029 return 0;
7030
7031 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7032 /* ensure the indexes are not affected */
7033 return 0;
7034
7035 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7036}
7037
7038static int
7039acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7040 struct acl_expr *expr, struct acl_test *test)
7041{
7042 struct http_txn *txn = l7;
7043
7044 if (!txn)
7045 return 0;
7046
Willy Tarreau655dce92009-11-08 13:10:58 +01007047 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007048 return 0;
7049
7050 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7051}
7052
Willy Tarreau737b0c12007-06-10 21:28:46 +02007053/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7054 * the first '/' after the possible hostname, and ends before the possible '?'.
7055 */
7056static int
7057acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7058 struct acl_expr *expr, struct acl_test *test)
7059{
7060 struct http_txn *txn = l7;
7061 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007062
Willy Tarreaub6866442008-07-14 23:54:42 +02007063 if (!txn)
7064 return 0;
7065
Willy Tarreau655dce92009-11-08 13:10:58 +01007066 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007067 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007068
Willy Tarreauc11416f2007-06-17 16:58:38 +02007069 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7070 /* ensure the indexes are not affected */
7071 return 0;
7072
Willy Tarreau962c3f42010-01-10 00:15:35 +01007073 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007074 ptr = http_get_path(txn);
7075 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007076 return 0;
7077
7078 /* OK, we got the '/' ! */
7079 test->ptr = ptr;
7080
7081 while (ptr < end && *ptr != '?')
7082 ptr++;
7083
7084 test->len = ptr - test->ptr;
7085
7086 /* we do not need to set READ_ONLY because the data is in a buffer */
7087 test->flags = ACL_TEST_F_VOL_1ST;
7088 return 1;
7089}
7090
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007091static int
7092acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7093 struct acl_expr *expr, struct acl_test *test)
7094{
7095 struct buffer *req = s->req;
7096 struct http_txn *txn = &s->txn;
7097 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007098
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007099 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7100 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7101 */
7102
7103 if (!s || !req)
7104 return 0;
7105
Willy Tarreau655dce92009-11-08 13:10:58 +01007106 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007107 /* Already decoded as OK */
7108 test->flags |= ACL_TEST_F_SET_RES_PASS;
7109 return 1;
7110 }
7111
7112 /* Try to decode HTTP request */
7113 if (likely(req->lr < req->r))
7114 http_msg_analyzer(req, msg, &txn->hdr_idx);
7115
Willy Tarreau655dce92009-11-08 13:10:58 +01007116 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007117 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7118 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7119 return 1;
7120 }
7121 /* wait for final state */
7122 test->flags |= ACL_TEST_F_MAY_CHANGE;
7123 return 0;
7124 }
7125
7126 /* OK we got a valid HTTP request. We have some minor preparation to
7127 * perform so that further checks can rely on HTTP tests.
7128 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007129 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007130 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7131 s->flags |= SN_REDIRECTABLE;
7132
7133 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7134 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7135 return 1;
7136 }
7137
7138 test->flags |= ACL_TEST_F_SET_RES_PASS;
7139 return 1;
7140}
7141
Willy Tarreau8797c062007-05-07 00:55:35 +02007142
7143/************************************************************************/
7144/* All supported keywords must be declared here. */
7145/************************************************************************/
7146
7147/* Note: must not be declared <const> as its list will be overwritten */
7148static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007149 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7150
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007151 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7152 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7153 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7154 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007155
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007156 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7157 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7158 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7159 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7160 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7161 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7162 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7163 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7164 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007165
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007166 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7167 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7168 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7169 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7170 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7171 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7172 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7173 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7174 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7175 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007176 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007177
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007178 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7179 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7180 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7181 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7182 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7183 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7184 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7185 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7186 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007187 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007188
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007189 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7190 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7191 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7192 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7193 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7194 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7195 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007196
Willy Tarreauf3d25982007-05-08 22:45:09 +02007197 { NULL, NULL, NULL, NULL },
7198
7199#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007200 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7201 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7202 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7203 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7204 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7205 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7206 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7207
Willy Tarreau8797c062007-05-07 00:55:35 +02007208 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7209 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7210 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7211 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7212 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7213 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7214 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7215 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7216
7217 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7218 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7219 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7220 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7221 { NULL, NULL, NULL, NULL },
7222#endif
7223}};
7224
7225
7226__attribute__((constructor))
7227static void __http_protocol_init(void)
7228{
7229 acl_register_keywords(&acl_kws);
7230}
7231
7232
Willy Tarreau58f10d72006-12-04 02:26:12 +01007233/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007234 * Local variables:
7235 * c-indent-level: 8
7236 * c-basic-offset: 8
7237 * End:
7238 */