blob: 83e051848da1d21f70fa01eb31e57074f030809c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaua9679ac2010-01-03 17:32:57 +010082/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020083const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
103const char *HTTP_401_fmt =
104 "HTTP/1.0 401 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200107 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
114 [HTTP_ERR_400] = 400,
115 [HTTP_ERR_403] = 403,
116 [HTTP_ERR_408] = 408,
117 [HTTP_ERR_500] = 500,
118 [HTTP_ERR_502] = 502,
119 [HTTP_ERR_503] = 503,
120 [HTTP_ERR_504] = 504,
121};
122
Willy Tarreau80587432006-12-24 17:47:20 +0100123static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100125 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 "Cache-Control: no-cache\r\n"
127 "Connection: close\r\n"
128 "Content-Type: text/html\r\n"
129 "\r\n"
130 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
131
132 [HTTP_ERR_403] =
133 "HTTP/1.0 403 Forbidden\r\n"
134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
139
140 [HTTP_ERR_408] =
141 "HTTP/1.0 408 Request Time-out\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
147
148 [HTTP_ERR_500] =
149 "HTTP/1.0 500 Server Error\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
155
156 [HTTP_ERR_502] =
157 "HTTP/1.0 502 Bad Gateway\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
163
164 [HTTP_ERR_503] =
165 "HTTP/1.0 503 Service Unavailable\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
171
172 [HTTP_ERR_504] =
173 "HTTP/1.0 504 Gateway Time-out\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
179
180};
181
Willy Tarreau80587432006-12-24 17:47:20 +0100182/* We must put the messages here since GCC cannot initialize consts depending
183 * on strlen().
184 */
185struct chunk http_err_chunks[HTTP_ERR_SIZE];
186
Willy Tarreau42250582007-04-01 01:30:43 +0200187#define FD_SETS_ARE_BITFIELDS
188#ifdef FD_SETS_ARE_BITFIELDS
189/*
190 * This map is used with all the FD_* macros to check whether a particular bit
191 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
192 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
193 * byte should be encoded. Be careful to always pass bytes from 0 to 255
194 * exclusively to the macros.
195 */
196fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
198
199#else
200#error "Check if your OS uses bitfields for fd_sets"
201#endif
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203void init_proto_http()
204{
Willy Tarreau42250582007-04-01 01:30:43 +0200205 int i;
206 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100207 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200208
Willy Tarreau80587432006-12-24 17:47:20 +0100209 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
210 if (!http_err_msgs[msg]) {
211 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
212 abort();
213 }
214
215 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
216 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
217 }
Willy Tarreau42250582007-04-01 01:30:43 +0200218
219 /* initialize the log header encoding map : '{|}"#' should be encoded with
220 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
221 * URL encoding only requires '"', '#' to be encoded as well as non-
222 * printable characters above.
223 */
224 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
225 memset(url_encode_map, 0, sizeof(url_encode_map));
226 for (i = 0; i < 32; i++) {
227 FD_SET(i, hdr_encode_map);
228 FD_SET(i, url_encode_map);
229 }
230 for (i = 127; i < 256; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234
235 tmp = "\"#{|}";
236 while (*tmp) {
237 FD_SET(*tmp, hdr_encode_map);
238 tmp++;
239 }
240
241 tmp = "\"#";
242 while (*tmp) {
243 FD_SET(*tmp, url_encode_map);
244 tmp++;
245 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200246
247 /* memory allocations */
248 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200249 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100250}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
Willy Tarreau53b6c742006-12-17 13:37:46 +0100252/*
253 * We have 26 list of methods (1 per first letter), each of which can have
254 * up to 3 entries (2 valid, 1 null).
255 */
256struct http_method_desc {
257 http_meth_t meth;
258 int len;
259 const char text[8];
260};
261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100262const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263 ['C' - 'A'] = {
264 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
265 },
266 ['D' - 'A'] = {
267 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
268 },
269 ['G' - 'A'] = {
270 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
271 },
272 ['H' - 'A'] = {
273 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
274 },
275 ['P' - 'A'] = {
276 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
277 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
278 },
279 ['T' - 'A'] = {
280 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
281 },
282 /* rest is empty like this :
283 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
284 */
285};
286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200288 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289 * RFC2616 for those chars.
290 */
291
292const char http_is_spht[256] = {
293 [' '] = 1, ['\t'] = 1,
294};
295
296const char http_is_crlf[256] = {
297 ['\r'] = 1, ['\n'] = 1,
298};
299
300const char http_is_lws[256] = {
301 [' '] = 1, ['\t'] = 1,
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_sep[256] = {
306 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
307 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
308 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
309 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
310 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
311};
312
313const char http_is_ctl[256] = {
314 [0 ... 31] = 1,
315 [127] = 1,
316};
317
318/*
319 * A token is any ASCII char that is neither a separator nor a CTL char.
320 * Do not overwrite values in assignment since gcc-2.95 will not handle
321 * them correctly. Instead, define every non-CTL char's status.
322 */
323const char http_is_token[256] = {
324 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
325 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
326 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
327 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
328 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
329 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
330 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
331 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
332 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
333 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
334 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
335 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
336 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
337 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
338 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
339 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
340 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
341 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
342 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
343 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
344 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
345 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
346 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
347 ['|'] = 1, ['}'] = 0, ['~'] = 1,
348};
349
350
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100351/*
352 * An http ver_token is any ASCII which can be found in an HTTP version,
353 * which includes 'H', 'T', 'P', '/', '.' and any digit.
354 */
355const char http_is_ver_token[256] = {
356 ['.'] = 1, ['/'] = 1,
357 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
358 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
359 ['H'] = 1, ['P'] = 1, ['T'] = 1,
360};
361
362
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100363/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100364 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
365 */
366#if defined(DEBUG_FSM)
367static void http_silent_debug(int line, struct session *s)
368{
369 int size = 0;
370 size += snprintf(trash + size, sizeof(trash) - size,
371 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
372 line,
373 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
374 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
375 write(-1, trash, size);
376 size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
379 line,
380 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
381 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
382
383 write(-1, trash, size);
384}
385#else
386#define http_silent_debug(l,s) do { } while (0)
387#endif
388
389/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100390 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
391 * CRLF. Text length is measured first, so it cannot be NULL.
392 * The header is also automatically added to the index <hdr_idx>, and the end
393 * of headers is automatically adjusted. The number of bytes added is returned
394 * on success, otherwise <0 is returned indicating an error.
395 */
396int http_header_add_tail(struct buffer *b, struct http_msg *msg,
397 struct hdr_idx *hdr_idx, const char *text)
398{
399 int bytes, len;
400
401 len = strlen(text);
402 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
403 if (!bytes)
404 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100405 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100406 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
407}
408
409/*
410 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
411 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
412 * the buffer is only opened and the space reserved, but nothing is copied.
413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
417int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
418 struct hdr_idx *hdr_idx, const char *text, int len)
419{
420 int bytes;
421
422 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428
429/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100430 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
431 * If so, returns the position of the first non-space character relative to
432 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
433 * to return a pointer to the place after the first space. Returns 0 if the
434 * header name does not match. Checks are case-insensitive.
435 */
436int http_header_match2(const char *hdr, const char *end,
437 const char *name, int len)
438{
439 const char *val;
440
441 if (hdr + len >= end)
442 return 0;
443 if (hdr[len] != ':')
444 return 0;
445 if (strncasecmp(hdr, name, len) != 0)
446 return 0;
447 val = hdr + len + 1;
448 while (val < end && HTTP_IS_SPHT(*val))
449 val++;
450 if ((val >= end) && (len + 2 <= end - hdr))
451 return len + 2; /* we may replace starting from second space */
452 return val - hdr;
453}
454
Willy Tarreau68085d82010-01-18 14:54:04 +0100455/* Find the end of the header value contained between <s> and <e>. See RFC2616,
456 * par 2.2 for more information. Note that it requires a valid header to return
457 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200458 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100459char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200460{
461 int quoted, qdpair;
462
463 quoted = qdpair = 0;
464 for (; s < e; s++) {
465 if (qdpair) qdpair = 0;
466 else if (quoted && *s == '\\') qdpair = 1;
467 else if (quoted && *s == '"') quoted = 0;
468 else if (*s == '"') quoted = 1;
469 else if (*s == ',') return s;
470 }
471 return s;
472}
473
474/* Find the first or next occurrence of header <name> in message buffer <sol>
475 * using headers index <idx>, and return it in the <ctx> structure. This
476 * structure holds everything necessary to use the header and find next
477 * occurrence. If its <idx> member is 0, the header is searched from the
478 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100479 * 1 when it finds a value, and 0 when there is no more. It is designed to work
480 * with headers defined as comma-separated lists. As a special case, if ctx->val
481 * is NULL when searching for a new values of a header, the current header is
482 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200483 */
484int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100485 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200486 struct hdr_ctx *ctx)
487{
Willy Tarreau68085d82010-01-18 14:54:04 +0100488 char *eol, *sov;
489 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200490
Willy Tarreau68085d82010-01-18 14:54:04 +0100491 cur_idx = ctx->idx;
492 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200493 /* We have previously returned a value, let's search
494 * another one on the same line.
495 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200496 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100497 ctx->del = ctx->val + ctx->vlen;
498 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200499 eol = sol + idx->v[cur_idx].len;
500
501 if (sov >= eol)
502 /* no more values in this header */
503 goto next_hdr;
504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 /* values remaining for this header, skip the comma but save it
506 * for later use (eg: for header deletion).
507 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 sov++;
509 while (sov < eol && http_is_lws[(unsigned char)*sov])
510 sov++;
511
512 goto return_hdr;
513 }
514
515 /* first request for this header */
516 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100517 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 while (cur_idx) {
520 eol = sol + idx->v[cur_idx].len;
521
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200522 if (len == 0) {
523 /* No argument was passed, we want any header.
524 * To achieve this, we simply build a fake request. */
525 while (sol + len < eol && sol[len] != ':')
526 len++;
527 name = sol;
528 }
529
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 if ((len < eol - sol) &&
531 (sol[len] == ':') &&
532 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100533 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200534 sov = sol + len + 1;
535 while (sov < eol && http_is_lws[(unsigned char)*sov])
536 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100537
Willy Tarreau33a7e692007-06-10 19:45:56 +0200538 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100539 ctx->prev = old_idx;
540 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 ctx->idx = cur_idx;
542 ctx->val = sov - sol;
543
544 eol = find_hdr_value_end(sov, eol);
545 ctx->vlen = eol - sov;
546 return 1;
547 }
548 next_hdr:
549 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100550 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200551 cur_idx = idx->v[cur_idx].next;
552 }
553 return 0;
554}
555
556int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 struct hdr_ctx *ctx)
559{
560 return http_find_header2(name, strlen(name), sol, idx, ctx);
561}
562
Willy Tarreau68085d82010-01-18 14:54:04 +0100563/* Remove one value of a header. This only works on a <ctx> returned by one of
564 * the http_find_header functions. The value is removed, as well as surrounding
565 * commas if any. If the removed value was alone, the whole header is removed.
566 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
567 * message <msg>. The new index is returned. If it is zero, it means there is
568 * no more header, so any processing may stop. The ctx is always left in a form
569 * that can be handled by http_find_header2() to find next occurrence.
570 */
571int http_remove_header2(struct http_msg *msg, struct buffer *buf,
572 struct hdr_idx *idx, struct hdr_ctx *ctx)
573{
574 int cur_idx = ctx->idx;
575 char *sol = ctx->line;
576 struct hdr_idx_elem *hdr;
577 int delta, skip_comma;
578
579 if (!cur_idx)
580 return 0;
581
582 hdr = &idx->v[cur_idx];
583 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
584 /* This was the only value of the header, we must now remove it entirely. */
585 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
586 http_msg_move_end(msg, delta);
587 idx->used--;
588 hdr->len = 0; /* unused entry */
589 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
590 ctx->idx = ctx->prev; /* walk back to the end of previous header */
591 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
592 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
593 ctx->vlen = 0;
594 return ctx->idx;
595 }
596
597 /* This was not the only value of this header. We have to remove between
598 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
599 * of the list, we remove the last separator.
600 */
601
602 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
603 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
604 sol + ctx->val + ctx->vlen + skip_comma,
605 NULL, 0);
606 hdr->len += delta;
607 http_msg_move_end(msg, delta);
608 ctx->val = ctx->del;
609 ctx->vlen = 0;
610 return ctx->idx;
611}
612
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100613/* This function handles a server error at the stream interface level. The
614 * stream interface is assumed to be already in a closed state. An optional
615 * message is copied into the input buffer, and an HTTP status code stored.
616 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100617 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100619static void http_server_error(struct session *t, struct stream_interface *si,
620 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100622 buffer_auto_read(si->ob);
623 buffer_abort(si->ob);
624 buffer_auto_close(si->ob);
625 buffer_erase(si->ob);
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100626 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200627 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100628 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100629 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100630 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100631 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 }
633 if (!(t->flags & SN_ERR_MASK))
634 t->flags |= err;
635 if (!(t->flags & SN_FINST_MASK))
636 t->flags |= finst;
637}
638
Willy Tarreau80587432006-12-24 17:47:20 +0100639/* This function returns the appropriate error location for the given session
640 * and message.
641 */
642
643struct chunk *error_message(struct session *s, int msgnum)
644{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200645 if (s->be->errmsg[msgnum].str)
646 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100647 else if (s->fe->errmsg[msgnum].str)
648 return &s->fe->errmsg[msgnum];
649 else
650 return &http_err_chunks[msgnum];
651}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652
Willy Tarreau53b6c742006-12-17 13:37:46 +0100653/*
654 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
655 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
656 */
657static http_meth_t find_http_meth(const char *str, const int len)
658{
659 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100660 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100661
662 m = ((unsigned)*str - 'A');
663
664 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100665 for (h = http_methods[m]; h->len > 0; h++) {
666 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100667 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100668 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100669 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100670 };
671 return HTTP_METH_OTHER;
672 }
673 return HTTP_METH_NONE;
674
675}
676
Willy Tarreau21d2af32008-02-14 20:25:24 +0100677/* Parse the URI from the given transaction (which is assumed to be in request
678 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
679 * It is returned otherwise.
680 */
681static char *
682http_get_path(struct http_txn *txn)
683{
684 char *ptr, *end;
685
Willy Tarreau962c3f42010-01-10 00:15:35 +0100686 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100687 end = ptr + txn->req.sl.rq.u_l;
688
689 if (ptr >= end)
690 return NULL;
691
692 /* RFC2616, par. 5.1.2 :
693 * Request-URI = "*" | absuri | abspath | authority
694 */
695
696 if (*ptr == '*')
697 return NULL;
698
699 if (isalpha((unsigned char)*ptr)) {
700 /* this is a scheme as described by RFC3986, par. 3.1 */
701 ptr++;
702 while (ptr < end &&
703 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
704 ptr++;
705 /* skip '://' */
706 if (ptr == end || *ptr++ != ':')
707 return NULL;
708 if (ptr == end || *ptr++ != '/')
709 return NULL;
710 if (ptr == end || *ptr++ != '/')
711 return NULL;
712 }
713 /* skip [user[:passwd]@]host[:[port]] */
714
715 while (ptr < end && *ptr != '/')
716 ptr++;
717
718 if (ptr == end)
719 return NULL;
720
721 /* OK, we got the '/' ! */
722 return ptr;
723}
724
Willy Tarreauefb453c2008-10-26 20:49:47 +0100725/* Returns a 302 for a redirectable request. This may only be called just after
726 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
727 * left unchanged and will follow normal proxy processing.
728 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100730{
731 struct http_txn *txn;
732 struct chunk rdr;
733 char *path;
734 int len;
735
736 /* 1: create the response header */
737 rdr.len = strlen(HTTP_302);
738 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100739 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740 memcpy(rdr.str, HTTP_302, rdr.len);
741
742 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200743 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100744 return;
745
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100746 /* special prefix "/" means don't change URL */
747 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
748 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
749 rdr.len += s->srv->rdr_len;
750 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751
752 /* 3: add the request URI */
753 txn = &s->txn;
754 path = http_get_path(txn);
755 if (!path)
756 return;
757
Willy Tarreau962c3f42010-01-10 00:15:35 +0100758 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200759 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760 return;
761
762 memcpy(rdr.str + rdr.len, path, len);
763 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100764 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
765 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100766
767 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100768 si->shutr(si);
769 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 si->err_type = SI_ET_NONE;
771 si->err_loc = NULL;
772 si->state = SI_ST_CLO;
773
774 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100775 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776
777 /* FIXME: we should increase a counter of redirects per server and per backend. */
778 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100779 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780}
781
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100782/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100783 * that the server side is closed. Note that err_type is actually a
784 * bitmask, where almost only aborts may be cumulated with other
785 * values. We consider that aborted operations are more important
786 * than timeouts or errors due to the fact that nobody else in the
787 * logs might explain incomplete retries. All others should avoid
788 * being cumulated. It should normally not be possible to have multiple
789 * aborts at once, but just in case, the first one in sequence is reported.
790 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100791void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100793 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100794
795 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100796 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
797 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100798 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100799 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
800 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100801 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100802 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
803 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100805 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
806 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100808 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
809 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100810 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100811 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
812 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100814 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
815 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816}
817
Willy Tarreau42250582007-04-01 01:30:43 +0200818extern const char sess_term_cond[8];
819extern const char sess_fin_state[8];
820extern const char *monthname[12];
821const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
822const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
823 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
824 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200825struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200826struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100827
Emeric Brun3a058f32009-06-30 18:26:00 +0200828void http_sess_clflog(struct session *s)
829{
830 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
831 struct proxy *fe = s->fe;
832 struct proxy *be = s->be;
833 struct proxy *prx_log;
834 struct http_txn *txn = &s->txn;
835 int tolog, level, err;
836 char *uri, *h;
837 char *svid;
838 struct tm tm;
839 static char tmpline[MAX_SYSLOG_LEN];
840 int hdr;
841 size_t w;
842 int t_request;
843
844 prx_log = fe;
845 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
846 (s->conn_retries != be->conn_retries) ||
847 txn->status >= 500;
848
849 if (s->cli_addr.ss_family == AF_INET)
850 inet_ntop(AF_INET,
851 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
852 pn, sizeof(pn));
853 else
854 inet_ntop(AF_INET6,
855 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
856 pn, sizeof(pn));
857
858 get_gmtime(s->logs.accept_date.tv_sec, &tm);
859
860 /* FIXME: let's limit ourselves to frontend logging for now. */
861 tolog = fe->to_log;
862
863 h = tmpline;
864
865 w = snprintf(h, sizeof(tmpline),
866 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
867 pn,
868 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
869 tm.tm_hour, tm.tm_min, tm.tm_sec);
870 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
871 goto trunc;
872 h += w;
873
874 if (h >= tmpline + sizeof(tmpline) - 4)
875 goto trunc;
876
877 *(h++) = ' ';
878 *(h++) = '\"';
879 uri = txn->uri ? txn->uri : "<BADREQ>";
880 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
881 '#', url_encode_map, uri);
882 *(h++) = '\"';
883
884 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
885 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
886 goto trunc;
887 h += w;
888
889 if (h >= tmpline + sizeof(tmpline) - 9)
890 goto trunc;
891 memcpy(h, " \"-\" \"-\"", 8);
892 h += 8;
893
894 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
895 " %d %03d",
896 (s->cli_addr.ss_family == AF_INET) ?
897 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
898 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
899 (int)s->logs.accept_date.tv_usec/1000);
900 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
901 goto trunc;
902 h += w;
903
904 w = strlen(fe->id);
905 if (h >= tmpline + sizeof(tmpline) - 4 - w)
906 goto trunc;
907 *(h++) = ' ';
908 *(h++) = '\"';
909 memcpy(h, fe->id, w);
910 h += w;
911 *(h++) = '\"';
912
913 w = strlen(be->id);
914 if (h >= tmpline + sizeof(tmpline) - 4 - w)
915 goto trunc;
916 *(h++) = ' ';
917 *(h++) = '\"';
918 memcpy(h, be->id, w);
919 h += w;
920 *(h++) = '\"';
921
922 svid = (tolog & LW_SVID) ?
923 (s->data_source != DATA_SRC_STATS) ?
924 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
925
926 w = strlen(svid);
927 if (h >= tmpline + sizeof(tmpline) - 4 - w)
928 goto trunc;
929 *(h++) = ' ';
930 *(h++) = '\"';
931 memcpy(h, svid, w);
932 h += w;
933 *(h++) = '\"';
934
935 t_request = -1;
936 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
937 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
938 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
939 " %d %ld %ld %ld %ld",
940 t_request,
941 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
942 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
943 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
944 s->logs.t_close);
945 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
946 goto trunc;
947 h += w;
948
949 if (h >= tmpline + sizeof(tmpline) - 8)
950 goto trunc;
951 *(h++) = ' ';
952 *(h++) = '\"';
953 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
954 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
955 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
956 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
957 *(h++) = '\"';
958
959 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
960 " %d %d %d %d %d %ld %ld",
961 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
962 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
963 s->logs.srv_queue_size, s->logs.prx_queue_size);
964
965 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
966 goto trunc;
967 h += w;
968
969 if (txn->cli_cookie) {
970 w = strlen(txn->cli_cookie);
971 if (h >= tmpline + sizeof(tmpline) - 4 - w)
972 goto trunc;
973 *(h++) = ' ';
974 *(h++) = '\"';
975 memcpy(h, txn->cli_cookie, w);
976 h += w;
977 *(h++) = '\"';
978 } else {
979 if (h >= tmpline + sizeof(tmpline) - 5)
980 goto trunc;
981 memcpy(h, " \"-\"", 4);
982 h += 4;
983 }
984
985 if (txn->srv_cookie) {
986 w = strlen(txn->srv_cookie);
987 if (h >= tmpline + sizeof(tmpline) - 4 - w)
988 goto trunc;
989 *(h++) = ' ';
990 *(h++) = '\"';
991 memcpy(h, txn->srv_cookie, w);
992 h += w;
993 *(h++) = '\"';
994 } else {
995 if (h >= tmpline + sizeof(tmpline) - 5)
996 goto trunc;
997 memcpy(h, " \"-\"", 4);
998 h += 4;
999 }
1000
1001 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1002 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1003 if (h >= sizeof (tmpline) + tmpline - 4)
1004 goto trunc;
1005 *(h++) = ' ';
1006 *(h++) = '\"';
1007 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1008 '#', hdr_encode_map, txn->req.cap[hdr]);
1009 *(h++) = '\"';
1010 }
1011 }
1012
1013 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1014 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1015 if (h >= sizeof (tmpline) + tmpline - 4)
1016 goto trunc;
1017 *(h++) = ' ';
1018 *(h++) = '\"';
1019 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1020 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1021 *(h++) = '\"';
1022 }
1023 }
1024
1025trunc:
1026 *h = '\0';
1027
1028 level = LOG_INFO;
1029 if (err && (fe->options2 & PR_O2_LOGERRORS))
1030 level = LOG_ERR;
1031
1032 send_log(prx_log, level, "%s\n", tmpline);
1033
1034 s->logs.logwait = 0;
1035}
1036
Willy Tarreau42250582007-04-01 01:30:43 +02001037/*
1038 * send a log for the session when we have enough info about it.
1039 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001041void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001042{
1043 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1044 struct proxy *fe = s->fe;
1045 struct proxy *be = s->be;
1046 struct proxy *prx_log;
1047 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001048 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001049 char *uri, *h;
1050 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001051 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001052 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001053 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001054 int hdr;
1055
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001056 /* if we don't want to log normal traffic, return now */
1057 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1058 (s->conn_retries != be->conn_retries) ||
1059 txn->status >= 500;
1060 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1061 return;
1062
Willy Tarreau42250582007-04-01 01:30:43 +02001063 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1064 return;
1065 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066
Emeric Brun3a058f32009-06-30 18:26:00 +02001067 if (prx_log->options2 & PR_O2_CLFLOG)
1068 return http_sess_clflog(s);
1069
Willy Tarreau42250582007-04-01 01:30:43 +02001070 if (s->cli_addr.ss_family == AF_INET)
1071 inet_ntop(AF_INET,
1072 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1073 pn, sizeof(pn));
1074 else
1075 inet_ntop(AF_INET6,
1076 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1077 pn, sizeof(pn));
1078
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001079 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001080
1081 /* FIXME: let's limit ourselves to frontend logging for now. */
1082 tolog = fe->to_log;
1083
1084 h = tmpline;
1085 if (fe->to_log & LW_REQHDR &&
1086 txn->req.cap &&
1087 (h < tmpline + sizeof(tmpline) - 10)) {
1088 *(h++) = ' ';
1089 *(h++) = '{';
1090 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1091 if (hdr)
1092 *(h++) = '|';
1093 if (txn->req.cap[hdr] != NULL)
1094 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1095 '#', hdr_encode_map, txn->req.cap[hdr]);
1096 }
1097 *(h++) = '}';
1098 }
1099
1100 if (fe->to_log & LW_RSPHDR &&
1101 txn->rsp.cap &&
1102 (h < tmpline + sizeof(tmpline) - 7)) {
1103 *(h++) = ' ';
1104 *(h++) = '{';
1105 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1106 if (hdr)
1107 *(h++) = '|';
1108 if (txn->rsp.cap[hdr] != NULL)
1109 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1110 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1111 }
1112 *(h++) = '}';
1113 }
1114
1115 if (h < tmpline + sizeof(tmpline) - 4) {
1116 *(h++) = ' ';
1117 *(h++) = '"';
1118 uri = txn->uri ? txn->uri : "<BADREQ>";
1119 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1120 '#', url_encode_map, uri);
1121 *(h++) = '"';
1122 }
1123 *h = '\0';
1124
1125 svid = (tolog & LW_SVID) ?
1126 (s->data_source != DATA_SRC_STATS) ?
1127 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1128
Willy Tarreau70089872008-06-13 21:12:51 +02001129 t_request = -1;
1130 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1131 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1132
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001133 level = LOG_INFO;
1134 if (err && (fe->options2 & PR_O2_LOGERRORS))
1135 level = LOG_ERR;
1136
1137 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001138 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001139 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1140 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001141 pn,
1142 (s->cli_addr.ss_family == AF_INET) ?
1143 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1144 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001145 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001146 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001147 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001148 t_request,
1149 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001150 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1151 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1152 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1153 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001154 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001155 txn->cli_cookie ? txn->cli_cookie : "-",
1156 txn->srv_cookie ? txn->srv_cookie : "-",
1157 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1158 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1159 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1160 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1161 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001162 (s->flags & SN_REDISP)?"+":"",
1163 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001164 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1165
1166 s->logs.logwait = 0;
1167}
1168
Willy Tarreau117f59e2007-03-04 18:17:17 +01001169
1170/*
1171 * Capture headers from message starting at <som> according to header list
1172 * <cap_hdr>, and fill the <idx> structure appropriately.
1173 */
1174void capture_headers(char *som, struct hdr_idx *idx,
1175 char **cap, struct cap_hdr *cap_hdr)
1176{
1177 char *eol, *sol, *col, *sov;
1178 int cur_idx;
1179 struct cap_hdr *h;
1180 int len;
1181
1182 sol = som + hdr_idx_first_pos(idx);
1183 cur_idx = hdr_idx_first_idx(idx);
1184
1185 while (cur_idx) {
1186 eol = sol + idx->v[cur_idx].len;
1187
1188 col = sol;
1189 while (col < eol && *col != ':')
1190 col++;
1191
1192 sov = col + 1;
1193 while (sov < eol && http_is_lws[(unsigned char)*sov])
1194 sov++;
1195
1196 for (h = cap_hdr; h; h = h->next) {
1197 if ((h->namelen == col - sol) &&
1198 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1199 if (cap[h->index] == NULL)
1200 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001201 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202
1203 if (cap[h->index] == NULL) {
1204 Alert("HTTP capture : out of memory.\n");
1205 continue;
1206 }
1207
1208 len = eol - sov;
1209 if (len > h->len)
1210 len = h->len;
1211
1212 memcpy(cap[h->index], sov, len);
1213 cap[h->index][len]=0;
1214 }
1215 }
1216 sol = eol + idx->v[cur_idx].cr + 1;
1217 cur_idx = idx->v[cur_idx].next;
1218 }
1219}
1220
1221
Willy Tarreau42250582007-04-01 01:30:43 +02001222/* either we find an LF at <ptr> or we jump to <bad>.
1223 */
1224#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1225
1226/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1227 * otherwise to <http_msg_ood> with <state> set to <st>.
1228 */
1229#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1230 ptr++; \
1231 if (likely(ptr < end)) \
1232 goto good; \
1233 else { \
1234 state = (st); \
1235 goto http_msg_ood; \
1236 } \
1237 } while (0)
1238
1239
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001241 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001242 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1243 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1244 * will give undefined results.
1245 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1246 * and that msg->sol points to the beginning of the response.
1247 * If a complete line is found (which implies that at least one CR or LF is
1248 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1249 * returned indicating an incomplete line (which does not mean that parts have
1250 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1251 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1252 * upon next call.
1253 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001254 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001255 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1256 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001257 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001259const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1260 unsigned int state, const char *ptr, const char *end,
1261 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001262{
Willy Tarreau8973c702007-01-21 23:58:29 +01001263 switch (state) {
1264 http_msg_rpver:
1265 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001266 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001267 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1268
1269 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001270 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1272 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001273 state = HTTP_MSG_ERROR;
1274 break;
1275
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 http_msg_rpver_sp:
1277 case HTTP_MSG_RPVER_SP:
1278 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001279 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001280 goto http_msg_rpcode;
1281 }
1282 if (likely(HTTP_IS_SPHT(*ptr)))
1283 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1284 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001285 state = HTTP_MSG_ERROR;
1286 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001287
1288 http_msg_rpcode:
1289 case HTTP_MSG_RPCODE:
1290 if (likely(!HTTP_IS_LWS(*ptr)))
1291 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1292
1293 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001294 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1296 }
1297
1298 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001299 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 http_msg_rsp_reason:
1301 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001302 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 msg->sl.st.r_l = 0;
1304 goto http_msg_rpline_eol;
1305
1306 http_msg_rpcode_sp:
1307 case HTTP_MSG_RPCODE_SP:
1308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001309 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 goto http_msg_rpreason;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1314 /* so it's a CR/LF, so there is no reason phrase */
1315 goto http_msg_rsp_reason;
1316
1317 http_msg_rpreason:
1318 case HTTP_MSG_RPREASON:
1319 if (likely(!HTTP_IS_CRLF(*ptr)))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001321 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 http_msg_rpline_eol:
1323 /* We have seen the end of line. Note that we do not
1324 * necessarily have the \n yet, but at least we know that we
1325 * have EITHER \r OR \n, otherwise the response would not be
1326 * complete. We can then record the response length and return
1327 * to the caller which will be able to register it.
1328 */
1329 msg->sl.st.l = ptr - msg->sol;
1330 return ptr;
1331
1332#ifdef DEBUG_FULL
1333 default:
1334 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1335 exit(1);
1336#endif
1337 }
1338
1339 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001340 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 if (ret_state)
1342 *ret_state = state;
1343 if (ret_ptr)
1344 *ret_ptr = (char *)ptr;
1345 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001346}
1347
1348
1349/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 * This function parses a request line between <ptr> and <end>, starting with
1351 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1352 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1353 * will give undefined results.
1354 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1355 * and that msg->sol points to the beginning of the request.
1356 * If a complete line is found (which implies that at least one CR or LF is
1357 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1358 * returned indicating an incomplete line (which does not mean that parts have
1359 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1360 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1361 * upon next call.
1362 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001363 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1365 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001366 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001367 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001368const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1369 unsigned int state, const char *ptr, const char *end,
1370 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 switch (state) {
1373 http_msg_rqmeth:
1374 case HTTP_MSG_RQMETH:
1375 if (likely(HTTP_IS_TOKEN(*ptr)))
1376 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001379 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1381 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001382
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 if (likely(HTTP_IS_CRLF(*ptr))) {
1384 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001385 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001387 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001389 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001391 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 msg->sl.rq.v_l = 0;
1393 goto http_msg_rqline_eol;
1394 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001395 state = HTTP_MSG_ERROR;
1396 break;
1397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 http_msg_rqmeth_sp:
1399 case HTTP_MSG_RQMETH_SP:
1400 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001401 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 goto http_msg_rquri;
1403 }
1404 if (likely(HTTP_IS_SPHT(*ptr)))
1405 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1406 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1407 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 http_msg_rquri:
1410 case HTTP_MSG_RQURI:
1411 if (likely(!HTTP_IS_LWS(*ptr)))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001415 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1417 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1420 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_rquri_sp:
1423 case HTTP_MSG_RQURI_SP:
1424 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001425 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 goto http_msg_rqver;
1427 }
1428 if (likely(HTTP_IS_SPHT(*ptr)))
1429 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1430 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1431 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 http_msg_rqver:
1434 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001435 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001437
1438 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001439 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001440 http_msg_rqline_eol:
1441 /* We have seen the end of line. Note that we do not
1442 * necessarily have the \n yet, but at least we know that we
1443 * have EITHER \r OR \n, otherwise the request would not be
1444 * complete. We can then record the request length and return
1445 * to the caller which will be able to register it.
1446 */
1447 msg->sl.rq.l = ptr - msg->sol;
1448 return ptr;
1449 }
1450
1451 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001452 state = HTTP_MSG_ERROR;
1453 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455#ifdef DEBUG_FULL
1456 default:
1457 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1458 exit(1);
1459#endif
1460 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001463 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 if (ret_state)
1465 *ret_state = state;
1466 if (ret_ptr)
1467 *ret_ptr = (char *)ptr;
1468 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001470
1471
Willy Tarreau8973c702007-01-21 23:58:29 +01001472/*
1473 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001474 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001475 * when data are missing and recalled at the exact same location with no
1476 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001477 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001478 * fields. Note that msg->som and msg->sol will be initialized after completing
1479 * the first state, so that none of the msg pointers has to be initialized
1480 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1483{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001484 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001487 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 ptr = buf->lr;
1489 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 if (unlikely(ptr >= end))
1492 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001495 /*
1496 * First, states that are specific to the response only.
1497 * We check them first so that request and headers are
1498 * closer to each other (accessed more often).
1499 */
1500 http_msg_rpbefore:
1501 case HTTP_MSG_RPBEFORE:
1502 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001503 /* we have a start of message, but we have to check
1504 * first if we need to remove some CRLF. We can only
1505 * do this when send_max=0.
1506 */
1507 char *beg = buf->w + buf->send_max;
1508 if (beg >= buf->data + buf->size)
1509 beg -= buf->size;
1510 if (unlikely(ptr != beg)) {
1511 if (buf->send_max)
1512 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001513 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001514 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001515 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001516 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001517 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 hdr_idx_init(idx);
1519 state = HTTP_MSG_RPVER;
1520 goto http_msg_rpver;
1521 }
1522
1523 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1524 goto http_msg_invalid;
1525
1526 if (unlikely(*ptr == '\n'))
1527 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1529 /* stop here */
1530
1531 http_msg_rpbefore_cr:
1532 case HTTP_MSG_RPBEFORE_CR:
1533 EXPECT_LF_HERE(ptr, http_msg_invalid);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1535 /* stop here */
1536
1537 http_msg_rpver:
1538 case HTTP_MSG_RPVER:
1539 case HTTP_MSG_RPVER_SP:
1540 case HTTP_MSG_RPCODE:
1541 case HTTP_MSG_RPCODE_SP:
1542 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001543 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001544 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001545 if (unlikely(!ptr))
1546 return;
1547
1548 /* we have a full response and we know that we have either a CR
1549 * or an LF at <ptr>.
1550 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001551 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1553
1554 msg->sol = ptr;
1555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1557 goto http_msg_rpline_end;
1558
1559 http_msg_rpline_end:
1560 case HTTP_MSG_RPLINE_END:
1561 /* msg->sol must point to the first of CR or LF. */
1562 EXPECT_LF_HERE(ptr, http_msg_invalid);
1563 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1564 /* stop here */
1565
1566 /*
1567 * Second, states that are specific to the request only
1568 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 http_msg_rqbefore:
1570 case HTTP_MSG_RQBEFORE:
1571 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001572 /* we have a start of message, but we have to check
1573 * first if we need to remove some CRLF. We can only
1574 * do this when send_max=0.
1575 */
1576 char *beg = buf->w + buf->send_max;
1577 if (beg >= buf->data + buf->size)
1578 beg -= buf->size;
1579 if (likely(ptr != beg)) {
1580 if (buf->send_max)
1581 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001582 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001583 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001585 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001586 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001587 /* we will need this when keep-alive will be supported
1588 hdr_idx_init(idx);
1589 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001590 state = HTTP_MSG_RQMETH;
1591 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1595 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (unlikely(*ptr == '\n'))
1598 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1599 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 http_msg_rqbefore_cr:
1603 case HTTP_MSG_RQBEFORE_CR:
1604 EXPECT_LF_HERE(ptr, http_msg_invalid);
1605 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001606 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 http_msg_rqmeth:
1609 case HTTP_MSG_RQMETH:
1610 case HTTP_MSG_RQMETH_SP:
1611 case HTTP_MSG_RQURI:
1612 case HTTP_MSG_RQURI_SP:
1613 case HTTP_MSG_RQVER:
1614 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001615 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 if (unlikely(!ptr))
1617 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 /* we have a full request and we know that we have either a CR
1620 * or an LF at <ptr>.
1621 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001622 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 msg->sol = ptr;
1626 if (likely(*ptr == '\r'))
1627 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 http_msg_rqline_end:
1631 case HTTP_MSG_RQLINE_END:
1632 /* check for HTTP/0.9 request : no version information available.
1633 * msg->sol must point to the first of CR or LF.
1634 */
1635 if (unlikely(msg->sl.rq.v_l == 0))
1636 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 EXPECT_LF_HERE(ptr, http_msg_invalid);
1639 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001640 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001641
Willy Tarreau8973c702007-01-21 23:58:29 +01001642 /*
1643 * Common states below
1644 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 http_msg_hdr_first:
1646 case HTTP_MSG_HDR_FIRST:
1647 msg->sol = ptr;
1648 if (likely(!HTTP_IS_CRLF(*ptr))) {
1649 goto http_msg_hdr_name;
1650 }
1651
1652 if (likely(*ptr == '\r'))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1654 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 http_msg_hdr_name:
1657 case HTTP_MSG_HDR_NAME:
1658 /* assumes msg->sol points to the first char */
1659 if (likely(HTTP_IS_TOKEN(*ptr)))
1660 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 if (likely(*ptr == ':')) {
1663 msg->col = ptr - buf->data;
1664 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001666
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001667 if (likely(msg->err_pos < -1) || *ptr == '\n')
1668 goto http_msg_invalid;
1669
1670 if (msg->err_pos == -1) /* capture error pointer */
1671 msg->err_pos = ptr - buf->data; /* >= 0 now */
1672
1673 /* and we still accept this non-token character */
1674 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 http_msg_hdr_l1_sp:
1677 case HTTP_MSG_HDR_L1_SP:
1678 /* assumes msg->sol points to the first char and msg->col to the colon */
1679 if (likely(HTTP_IS_SPHT(*ptr)))
1680 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 /* header value can be basically anything except CR/LF */
1683 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 if (likely(!HTTP_IS_CRLF(*ptr))) {
1686 goto http_msg_hdr_val;
1687 }
1688
1689 if (likely(*ptr == '\r'))
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1691 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 http_msg_hdr_l1_lf:
1694 case HTTP_MSG_HDR_L1_LF:
1695 EXPECT_LF_HERE(ptr, http_msg_invalid);
1696 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 http_msg_hdr_l1_lws:
1699 case HTTP_MSG_HDR_L1_LWS:
1700 if (likely(HTTP_IS_SPHT(*ptr))) {
1701 /* replace HT,CR,LF with spaces */
1702 for (; buf->data+msg->sov < ptr; msg->sov++)
1703 buf->data[msg->sov] = ' ';
1704 goto http_msg_hdr_l1_sp;
1705 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001706 /* we had a header consisting only in spaces ! */
1707 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 goto http_msg_complete_header;
1709
1710 http_msg_hdr_val:
1711 case HTTP_MSG_HDR_VAL:
1712 /* assumes msg->sol points to the first char, msg->col to the
1713 * colon, and msg->sov points to the first character of the
1714 * value.
1715 */
1716 if (likely(!HTTP_IS_CRLF(*ptr)))
1717 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 msg->eol = ptr;
1720 /* Note: we could also copy eol into ->eoh so that we have the
1721 * real header end in case it ends with lots of LWS, but is this
1722 * really needed ?
1723 */
1724 if (likely(*ptr == '\r'))
1725 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1726 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 http_msg_hdr_l2_lf:
1729 case HTTP_MSG_HDR_L2_LF:
1730 EXPECT_LF_HERE(ptr, http_msg_invalid);
1731 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 http_msg_hdr_l2_lws:
1734 case HTTP_MSG_HDR_L2_LWS:
1735 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1736 /* LWS: replace HT,CR,LF with spaces */
1737 for (; msg->eol < ptr; msg->eol++)
1738 *msg->eol = ' ';
1739 goto http_msg_hdr_val;
1740 }
1741 http_msg_complete_header:
1742 /*
1743 * It was a new header, so the last one is finished.
1744 * Assumes msg->sol points to the first char, msg->col to the
1745 * colon, msg->sov points to the first character of the value
1746 * and msg->eol to the first CR or LF so we know how the line
1747 * ends. We insert last header into the index.
1748 */
1749 /*
1750 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1751 write(2, msg->sol, msg->eol-msg->sol);
1752 fprintf(stderr,"\n");
1753 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001754
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1756 idx, idx->tail) < 0))
1757 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 msg->sol = ptr;
1760 if (likely(!HTTP_IS_CRLF(*ptr))) {
1761 goto http_msg_hdr_name;
1762 }
1763
1764 if (likely(*ptr == '\r'))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1766 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 http_msg_last_lf:
1769 case HTTP_MSG_LAST_LF:
1770 /* Assumes msg->sol points to the first of either CR or LF */
1771 EXPECT_LF_HERE(ptr, http_msg_invalid);
1772 ptr++;
1773 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001774 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001776 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001777 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 return;
1779#ifdef DEBUG_FULL
1780 default:
1781 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1782 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001783#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 }
1785 http_msg_ood:
1786 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001787 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 buf->lr = ptr;
1789 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 http_msg_invalid:
1792 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001793 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001794 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 return;
1796}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001797
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001798/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1799 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1800 * nothing is done and 1 is returned.
1801 */
1802static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1803{
1804 int delta;
1805 char *cur_end;
1806
1807 if (msg->sl.rq.v_l != 0)
1808 return 1;
1809
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001810 cur_end = msg->sol + msg->sl.rq.l;
1811 delta = 0;
1812
1813 if (msg->sl.rq.u_l == 0) {
1814 /* if no URI was set, add "/" */
1815 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1816 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001817 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001818 }
1819 /* add HTTP version */
1820 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001821 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001822 cur_end += delta;
1823 cur_end = (char *)http_parse_reqline(msg, req->data,
1824 HTTP_MSG_RQMETH,
1825 msg->sol, cur_end + 1,
1826 NULL, NULL);
1827 if (unlikely(!cur_end))
1828 return 0;
1829
1830 /* we have a full HTTP/1.0 request now and we know that
1831 * we have either a CR or an LF at <ptr>.
1832 */
1833 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1834 return 1;
1835}
1836
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001837/* Parse the Connection: header of an HTTP request, looking for both "close"
1838 * and "keep-alive" values. If a buffer is provided and we already know that
1839 * some headers may safely be removed, we remove them now. The <to_del> flags
1840 * are used for that :
1841 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1842 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1843 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1844 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1845 * harmless combinations may be removed. Do not call that after changes have
1846 * been processed. If unused, the buffer can be NULL, and no data will be
1847 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001848 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001850{
Willy Tarreau5b154472009-12-21 20:11:07 +01001851 struct hdr_ctx ctx;
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001854 return;
1855
Willy Tarreau5b154472009-12-21 20:11:07 +01001856 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001857 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01001858 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1860 txn->flags |= TX_HDR_CONN_KAL;
1861 if ((to_del & 2) && buf)
1862 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1863 else
1864 txn->flags |= TX_CON_KAL_SET;
1865 }
1866 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1867 txn->flags |= TX_HDR_CONN_CLO;
1868 if ((to_del & 1) && buf)
1869 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1870 else
1871 txn->flags |= TX_CON_CLO_SET;
1872 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001873 }
1874
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001875 txn->flags |= TX_HDR_CONN_PRS;
1876 return;
1877}
Willy Tarreau5b154472009-12-21 20:11:07 +01001878
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001879/* Apply desired changes on the Connection: header. Values may be removed and/or
1880 * added depending on the <wanted> flags, which are exclusively composed of
1881 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1882 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1883 */
1884void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1885{
1886 struct hdr_ctx ctx;
1887
1888 ctx.idx = 0;
1889
1890 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
1891 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1892 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1893 if (wanted & TX_CON_KAL_SET)
1894 txn->flags |= TX_CON_KAL_SET;
1895 else
1896 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001897 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1899 if (wanted & TX_CON_CLO_SET)
1900 txn->flags |= TX_CON_CLO_SET;
1901 else
1902 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001903 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001904 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905
1906 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1907 return;
1908
1909 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1910 txn->flags |= TX_CON_CLO_SET;
1911 http_header_add_tail2(buf, msg, &txn->hdr_idx, "Connection: close", 17);
1912 }
1913
1914 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1915 txn->flags |= TX_CON_KAL_SET;
1916 http_header_add_tail2(buf, msg, &txn->hdr_idx, "Connection: keep-alive", 22);
1917 }
1918 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001919}
1920
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1922 * first byte of body, and increments msg->sov by the number of bytes parsed,
1923 * so that we know we can forward between ->som and ->sov. Note that due to
1924 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1925 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001928 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001930{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 char *ptr = buf->lr;
1932 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001933 unsigned int chunk = 0;
1934
1935 /* The chunk size is in the following form, though we are only
1936 * interested in the size and CRLF :
1937 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1938 */
1939 while (1) {
1940 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001942 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001944 if (c < 0) /* not a hex digit anymore */
1945 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 if (++ptr >= end)
1947 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001948 if (chunk & 0xF000000) /* overflow will occur */
1949 return -1;
1950 chunk = (chunk << 4) + c;
1951 }
1952
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 /* empty size not allowed */
1954 if (ptr == buf->lr)
1955 return -1;
1956
1957 while (http_is_spht[(unsigned char)*ptr]) {
1958 if (++ptr >= end)
1959 ptr = buf->data;
1960 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001961 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001962 }
1963
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 /* Up to there, we know that at least one byte is present at *ptr. Check
1965 * for the end of chunk size.
1966 */
1967 while (1) {
1968 if (likely(HTTP_IS_CRLF(*ptr))) {
1969 /* we now have a CR or an LF at ptr */
1970 if (likely(*ptr == '\r')) {
1971 if (++ptr >= end)
1972 ptr = buf->data;
1973 if (ptr == buf->r)
1974 return 0;
1975 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001976
Willy Tarreaud98cf932009-12-27 22:54:55 +01001977 if (*ptr != '\n')
1978 return -1;
1979 if (++ptr >= end)
1980 ptr = buf->data;
1981 /* done */
1982 break;
1983 }
1984 else if (*ptr == ';') {
1985 /* chunk extension, ends at next CRLF */
1986 if (++ptr >= end)
1987 ptr = buf->data;
1988 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001989 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990
1991 while (!HTTP_IS_CRLF(*ptr)) {
1992 if (++ptr >= end)
1993 ptr = buf->data;
1994 if (ptr == buf->r)
1995 return 0;
1996 }
1997 /* we have a CRLF now, loop above */
1998 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002000 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002001 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002002 }
2003
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 /* OK we found our CRLF and now <ptr> points to the next byte,
2005 * which may or may not be present. We save that into ->lr and
2006 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002007 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 msg->sov += ptr - buf->lr;
2009 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002010 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
2013}
2014
Willy Tarreaud98cf932009-12-27 22:54:55 +01002015/* This function skips trailers in the buffer <buf> associated with HTTP
2016 * message <msg>. The first visited position is buf->lr. If the end of
2017 * the trailers is found, it is automatically scheduled to be forwarded,
2018 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2019 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002020 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2021 * zero. If a parse error is encountered, the function returns < 0 and does not
2022 * change anything except maybe buf->lr and msg->sov. Note that the message
2023 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2024 * which implies that all non-trailers data have already been scheduled for
2025 * forwarding, and that the difference between msg->som and msg->sov exactly
2026 * matches the length of trailers already parsed and not forwarded. It is also
2027 * important to note that this function is designed to be able to parse wrapped
2028 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029 */
2030int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2031{
2032 /* we have buf->lr which points to next line. Look for CRLF. */
2033 while (1) {
2034 char *p1 = NULL, *p2 = NULL;
2035 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002036 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037
2038 /* scan current line and stop at LF or CRLF */
2039 while (1) {
2040 if (ptr == buf->r)
2041 return 0;
2042
2043 if (*ptr == '\n') {
2044 if (!p1)
2045 p1 = ptr;
2046 p2 = ptr;
2047 break;
2048 }
2049
2050 if (*ptr == '\r') {
2051 if (p1)
2052 return -1;
2053 p1 = ptr;
2054 }
2055
2056 ptr++;
2057 if (ptr >= buf->data + buf->size)
2058 ptr = buf->data;
2059 }
2060
2061 /* after LF; point to beginning of next line */
2062 p2++;
2063 if (p2 >= buf->data + buf->size)
2064 p2 = buf->data;
2065
Willy Tarreau638cd022010-01-03 07:42:04 +01002066 bytes = p2 - buf->lr;
2067 if (bytes < 0)
2068 bytes += buf->size;
2069
2070 /* schedule this line for forwarding */
2071 msg->sov += bytes;
2072 if (msg->sov >= buf->size)
2073 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002074
Willy Tarreau638cd022010-01-03 07:42:04 +01002075 if (p1 == buf->lr) {
2076 /* LF/CRLF at beginning of line => end of trailers at p2.
2077 * Everything was scheduled for forwarding, there's nothing
2078 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002079 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002080 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 msg->msg_state = HTTP_MSG_DONE;
2082 return 1;
2083 }
2084 /* OK, next line then */
2085 buf->lr = p2;
2086 }
2087}
2088
2089/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2090 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2091 * ->som, buf->lr in order to include this part into the next forwarding phase.
2092 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2093 * not enough data are available, the function does not change anything and
2094 * returns zero. If a parse error is encountered, the function returns < 0 and
2095 * does not change anything. Note: this function is designed to parse wrapped
2096 * CRLF at the end of the buffer.
2097 */
2098int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2099{
2100 char *ptr;
2101 int bytes;
2102
2103 /* NB: we'll check data availabilty at the end. It's not a
2104 * problem because whatever we match first will be checked
2105 * against the correct length.
2106 */
2107 bytes = 1;
2108 ptr = buf->lr;
2109 if (*ptr == '\r') {
2110 bytes++;
2111 ptr++;
2112 if (ptr >= buf->data + buf->size)
2113 ptr = buf->data;
2114 }
2115
2116 if (buf->l < bytes)
2117 return 0;
2118
2119 if (*ptr != '\n')
2120 return -1;
2121
2122 ptr++;
2123 if (ptr >= buf->data + buf->size)
2124 ptr = buf->data;
2125 buf->lr = ptr;
2126 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2127 msg->sov = ptr - buf->data;
2128 msg->som = msg->sov - bytes;
2129 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2130 return 1;
2131}
Willy Tarreau5b154472009-12-21 20:11:07 +01002132
Willy Tarreau83e3af02009-12-28 17:39:57 +01002133void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2134{
2135 char *end = buf->data + buf->size;
2136 int off = buf->data + buf->size - buf->w;
2137
2138 /* two possible cases :
2139 * - the buffer is in one contiguous block, we move it in-place
2140 * - the buffer is in two blocks, we move it via the trash
2141 */
2142 if (buf->l) {
2143 int block1 = buf->l;
2144 int block2 = 0;
2145 if (buf->r <= buf->w) {
2146 /* non-contiguous block */
2147 block1 = buf->data + buf->size - buf->w;
2148 block2 = buf->r - buf->data;
2149 }
2150 if (block2)
2151 memcpy(trash, buf->data, block2);
2152 memmove(buf->data, buf->w, block1);
2153 if (block2)
2154 memcpy(buf->data + block1, trash, block2);
2155 }
2156
2157 /* adjust all known pointers */
2158 buf->w = buf->data;
2159 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2160 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2161 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2162 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2163
2164 /* adjust relative pointers */
2165 msg->som = 0;
2166 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2167 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2168 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2169
Willy Tarreau83e3af02009-12-28 17:39:57 +01002170 if (msg->err_pos >= 0) {
2171 msg->err_pos += off;
2172 if (msg->err_pos >= buf->size)
2173 msg->err_pos -= buf->size;
2174 }
2175
2176 buf->flags &= ~BF_FULL;
2177 if (buf->l >= buffer_max_len(buf))
2178 buf->flags |= BF_FULL;
2179}
2180
Willy Tarreaud787e662009-07-07 10:14:51 +02002181/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2182 * processing can continue on next analysers, or zero if it either needs more
2183 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2184 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2185 * when it has nothing left to do, and may remove any analyser when it wants to
2186 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002187 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002188int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002189{
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 /*
2191 * We will parse the partial (or complete) lines.
2192 * We will check the request syntax, and also join multi-line
2193 * headers. An index of all the lines will be elaborated while
2194 * parsing.
2195 *
2196 * For the parsing, we use a 28 states FSM.
2197 *
2198 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002199 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002200 * req->data + msg->eoh = end of processed headers / start of current one
2201 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002202 * req->lr = first non-visited byte
2203 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002204 *
2205 * At end of parsing, we may perform a capture of the error (if any), and
2206 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2207 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2208 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002209 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002210
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002212 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002213 struct http_txn *txn = &s->txn;
2214 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002215 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002216
Willy Tarreau6bf17362009-02-24 10:48:35 +01002217 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2218 now_ms, __FUNCTION__,
2219 s,
2220 req,
2221 req->rex, req->wex,
2222 req->flags,
2223 req->l,
2224 req->analysers);
2225
Willy Tarreau52a0c602009-08-16 22:45:38 +02002226 /* we're speaking HTTP here, so let's speak HTTP to the client */
2227 s->srv_error = http_return_srv_error;
2228
Willy Tarreau83e3af02009-12-28 17:39:57 +01002229 /* There's a protected area at the end of the buffer for rewriting
2230 * purposes. We don't want to start to parse the request if the
2231 * protected area is affected, because we may have to move processed
2232 * data later, which is much more complicated.
2233 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002234 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002235 if ((txn->flags & TX_NOT_FIRST) &&
2236 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002237 req->r < req->lr ||
2238 req->r > req->data + req->size - global.tune.maxrewrite)) {
2239 if (req->send_max) {
2240 /* some data has still not left the buffer, wake us once that's done */
2241 buffer_dont_connect(req);
2242 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2243 return 0;
2244 }
2245 if (req->l <= req->size - global.tune.maxrewrite)
2246 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002247 }
2248
Willy Tarreau065e8332010-01-08 00:30:20 +01002249 /* Note that we have the same problem with the response ; we
2250 * may want to send a redirect, error or anything which requires
2251 * some spare space. So we'll ensure that we have at least
2252 * maxrewrite bytes available in the response buffer before
2253 * processing that one. This will only affect pipelined
2254 * keep-alive requests.
2255 */
2256 if ((txn->flags & TX_NOT_FIRST) &&
2257 unlikely((s->rep->flags & BF_FULL) ||
2258 s->rep->r < s->rep->lr ||
2259 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2260 if (s->rep->send_max) {
2261 /* don't let a connection request be initiated */
2262 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002263 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002264 return 0;
2265 }
2266 }
2267
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002268 if (likely(req->lr < req->r))
2269 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002270 }
2271
Willy Tarreau59234e92008-11-30 23:51:27 +01002272 /* 1: we might have to print this header in debug mode */
2273 if (unlikely((global.mode & MODE_DEBUG) &&
2274 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002275 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002276 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002277
Willy Tarreau962c3f42010-01-10 00:15:35 +01002278 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002279 eol = sol + msg->sl.rq.l;
2280 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002281
Willy Tarreau59234e92008-11-30 23:51:27 +01002282 sol += hdr_idx_first_pos(&txn->hdr_idx);
2283 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002284
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 while (cur_idx) {
2286 eol = sol + txn->hdr_idx.v[cur_idx].len;
2287 debug_hdr("clihdr", s, sol, eol);
2288 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2289 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002290 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 }
2292
Willy Tarreau58f10d72006-12-04 02:26:12 +01002293
Willy Tarreau59234e92008-11-30 23:51:27 +01002294 /*
2295 * Now we quickly check if we have found a full valid request.
2296 * If not so, we check the FD and buffer states before leaving.
2297 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002298 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002299 * requests are checked first. When waiting for a second request
2300 * on a keep-alive session, if we encounter and error, close, t/o,
2301 * we note the error in the session flags but don't set any state.
2302 * Since the error will be noted there, it will not be counted by
2303 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002305
Willy Tarreau655dce92009-11-08 13:10:58 +01002306 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002307 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002309 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002310 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2311 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002312
Willy Tarreau59234e92008-11-30 23:51:27 +01002313 /* 1: Since we are in header mode, if there's no space
2314 * left for headers, we won't be able to free more
2315 * later, so the session will never terminate. We
2316 * must terminate it now.
2317 */
2318 if (unlikely(req->flags & BF_FULL)) {
2319 /* FIXME: check if URI is set and return Status
2320 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002321 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002322 goto return_bad_req;
2323 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002324
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 /* 2: have we encountered a read error ? */
2326 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002327 if (!(s->flags & SN_ERR_MASK))
2328 s->flags |= SN_ERR_CLICL;
2329
Willy Tarreaufcffa692010-01-10 14:21:19 +01002330 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002331 goto failed_keep_alive;
2332
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002334 if (msg->err_pos >= 0)
2335 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 msg->msg_state = HTTP_MSG_ERROR;
2337 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002338
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002339 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002340 if (s->listener->counters)
2341 s->listener->counters->failed_req++;
2342
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 if (!(s->flags & SN_FINST_MASK))
2344 s->flags |= SN_FINST_R;
2345 return 0;
2346 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 /* 3: has the read timeout expired ? */
2349 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002350 if (!(s->flags & SN_ERR_MASK))
2351 s->flags |= SN_ERR_CLITO;
2352
Willy Tarreaufcffa692010-01-10 14:21:19 +01002353 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002354 goto failed_keep_alive;
2355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002357 if (msg->err_pos >= 0)
2358 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 txn->status = 408;
2360 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2361 msg->msg_state = HTTP_MSG_ERROR;
2362 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002363
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002364 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002365 if (s->listener->counters)
2366 s->listener->counters->failed_req++;
2367
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 if (!(s->flags & SN_FINST_MASK))
2369 s->flags |= SN_FINST_R;
2370 return 0;
2371 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002372
Willy Tarreau59234e92008-11-30 23:51:27 +01002373 /* 4: have we encountered a close ? */
2374 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002375 if (!(s->flags & SN_ERR_MASK))
2376 s->flags |= SN_ERR_CLICL;
2377
Willy Tarreaufcffa692010-01-10 14:21:19 +01002378 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002379 goto failed_keep_alive;
2380
Willy Tarreau4076a152009-04-02 15:18:36 +02002381 if (msg->err_pos >= 0)
2382 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002383 txn->status = 400;
2384 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2385 msg->msg_state = HTTP_MSG_ERROR;
2386 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002387
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002388 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002389 if (s->listener->counters)
2390 s->listener->counters->failed_req++;
2391
Willy Tarreau59234e92008-11-30 23:51:27 +01002392 if (!(s->flags & SN_FINST_MASK))
2393 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002394 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002395 }
2396
Willy Tarreau520d95e2009-09-19 21:04:57 +02002397 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002398 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002399 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002400
Willy Tarreaufcffa692010-01-10 14:21:19 +01002401 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2402 /* If the client starts to talk, let's fall back to
2403 * request timeout processing.
2404 */
2405 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002406 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002407 }
2408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002410 if (!tick_isset(req->analyse_exp)) {
2411 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2412 (txn->flags & TX_WAIT_NEXT_RQ) &&
2413 tick_isset(s->be->timeout.httpka))
2414 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2415 else
2416 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2417 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002418
Willy Tarreau59234e92008-11-30 23:51:27 +01002419 /* we're not ready yet */
2420 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002421
2422 failed_keep_alive:
2423 /* Here we process low-level errors for keep-alive requests. In
2424 * short, if the request is not the first one and it experiences
2425 * a timeout, read error or shutdown, we just silently close so
2426 * that the client can try again.
2427 */
2428 txn->status = 0;
2429 msg->msg_state = HTTP_MSG_RQBEFORE;
2430 req->analysers = 0;
2431 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002432 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002433 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002434 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002436
Willy Tarreaud787e662009-07-07 10:14:51 +02002437 /* OK now we have a complete HTTP request with indexed headers. Let's
2438 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002439 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2440 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2441 * points to the CRLF of the request line. req->lr points to the first
2442 * byte after the last LF. msg->col and msg->sov point to the first
2443 * byte of data. msg->eol cannot be trusted because it may have been
2444 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002445 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002446
Willy Tarreaub16a5742010-01-10 14:46:16 +01002447 if (txn->flags & TX_WAIT_NEXT_RQ) {
2448 /* kill the pending keep-alive timeout */
2449 txn->flags &= ~TX_WAIT_NEXT_RQ;
2450 req->analyse_exp = TICK_ETERNITY;
2451 }
2452
2453
Willy Tarreaud787e662009-07-07 10:14:51 +02002454 /* Maybe we found in invalid header name while we were configured not
2455 * to block on that, so we have to capture it now.
2456 */
2457 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002458 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2459
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 /*
2461 * 1: identify the method
2462 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002463 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002464
2465 /* we can make use of server redirect on GET and HEAD */
2466 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2467 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002468
Willy Tarreau59234e92008-11-30 23:51:27 +01002469 /*
2470 * 2: check if the URI matches the monitor_uri.
2471 * We have to do this for every request which gets in, because
2472 * the monitor-uri is defined by the frontend.
2473 */
2474 if (unlikely((s->fe->monitor_uri_len != 0) &&
2475 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002476 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002477 s->fe->monitor_uri,
2478 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002479 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002481 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002483
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002485
Willy Tarreau59234e92008-11-30 23:51:27 +01002486 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002487 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2488 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002489
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 ret = acl_pass(ret);
2491 if (cond->pol == ACL_COND_UNLESS)
2492 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 if (ret) {
2495 /* we fail this request, let's return 503 service unavail */
2496 txn->status = 503;
2497 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2498 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002499 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002500 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 /* nothing to fail, let's reply normaly */
2503 txn->status = 200;
2504 stream_int_retnclose(req->prod, &http_200_chunk);
2505 goto return_prx_cond;
2506 }
2507
2508 /*
2509 * 3: Maybe we have to copy the original REQURI for the logs ?
2510 * Note: we cannot log anymore if the request has been
2511 * classified as invalid.
2512 */
2513 if (unlikely(s->logs.logwait & LW_REQ)) {
2514 /* we have a complete HTTP request that we must log */
2515 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2516 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002517
Willy Tarreau59234e92008-11-30 23:51:27 +01002518 if (urilen >= REQURI_LEN)
2519 urilen = REQURI_LEN - 1;
2520 memcpy(txn->uri, &req->data[msg->som], urilen);
2521 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 if (!(s->logs.logwait &= ~LW_REQ))
2524 s->do_log(s);
2525 } else {
2526 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002527 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 }
Willy Tarreau06619262006-12-17 08:37:22 +01002529
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002531 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2532 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002533
Willy Tarreau5b154472009-12-21 20:11:07 +01002534 /* ... and check if the request is HTTP/1.1 or above */
2535 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002536 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2537 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2538 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002539 txn->flags |= TX_REQ_VER_11;
2540
2541 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002542 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002543
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002544 /* transfer length unknown*/
2545 txn->flags &= ~TX_REQ_XFER_LEN;
2546
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 /* 5: we may need to capture headers */
2548 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002549 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002551
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002552 /* 6: determine the transfer-length.
2553 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2554 * the presence of a message-body in a REQUEST and its transfer length
2555 * must be determined that way (in order of precedence) :
2556 * 1. The presence of a message-body in a request is signaled by the
2557 * inclusion of a Content-Length or Transfer-Encoding header field
2558 * in the request's header fields. When a request message contains
2559 * both a message-body of non-zero length and a method that does
2560 * not define any semantics for that request message-body, then an
2561 * origin server SHOULD either ignore the message-body or respond
2562 * with an appropriate error message (e.g., 413). A proxy or
2563 * gateway, when presented the same request, SHOULD either forward
2564 * the request inbound with the message- body or ignore the
2565 * message-body when determining a response.
2566 *
2567 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2568 * and the "chunked" transfer-coding (Section 6.2) is used, the
2569 * transfer-length is defined by the use of this transfer-coding.
2570 * If a Transfer-Encoding header field is present and the "chunked"
2571 * transfer-coding is not present, the transfer-length is defined
2572 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002573 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002574 * 3. If a Content-Length header field is present, its decimal value in
2575 * OCTETs represents both the entity-length and the transfer-length.
2576 * If a message is received with both a Transfer-Encoding header
2577 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002578 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002579 * 4. By the server closing the connection. (Closing the connection
2580 * cannot be used to indicate the end of a request body, since that
2581 * would leave no possibility for the server to send back a response.)
2582 *
2583 * Whenever a transfer-coding is applied to a message-body, the set of
2584 * transfer-codings MUST include "chunked", unless the message indicates
2585 * it is terminated by closing the connection. When the "chunked"
2586 * transfer-coding is used, it MUST be the last transfer-coding applied
2587 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002588 */
2589
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002590 /* CONNECT sets a tunnel and ignores everything else */
2591 if (txn->meth == HTTP_METH_CONNECT)
2592 goto skip_xfer_len;
2593
2594 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002595 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002596 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002597 while ((txn->flags & TX_REQ_VER_11) &&
2598 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002599 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2600 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2601 else if (txn->flags & TX_REQ_TE_CHNK) {
2602 /* bad transfer-encoding (chunked followed by something else) */
2603 use_close_only = 1;
2604 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2605 break;
2606 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002607 }
2608
Willy Tarreau32b47f42009-10-18 20:55:02 +02002609 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002610 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002611 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2612 signed long long cl;
2613
2614 if (!ctx.vlen)
2615 goto return_bad_req;
2616
2617 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2618 goto return_bad_req; /* parse failure */
2619
2620 if (cl < 0)
2621 goto return_bad_req;
2622
2623 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2624 goto return_bad_req; /* already specified, was different */
2625
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002626 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002627 msg->hdr_content_len = cl;
2628 }
2629
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002630 /* bodyless requests have a known length */
2631 if (!use_close_only)
2632 txn->flags |= TX_REQ_XFER_LEN;
2633
2634 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002635 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002636 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002637 req->analyse_exp = TICK_ETERNITY;
2638 return 1;
2639
2640 return_bad_req:
2641 /* We centralize bad requests processing here */
2642 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2643 /* we detected a parsing error. We want to archive this request
2644 * in the dedicated proxy area for later troubleshooting.
2645 */
2646 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2647 }
2648
2649 txn->req.msg_state = HTTP_MSG_ERROR;
2650 txn->status = 400;
2651 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002652
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002653 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002654 if (s->listener->counters)
2655 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002656
2657 return_prx_cond:
2658 if (!(s->flags & SN_ERR_MASK))
2659 s->flags |= SN_ERR_PRXCOND;
2660 if (!(s->flags & SN_FINST_MASK))
2661 s->flags |= SN_FINST_R;
2662
2663 req->analysers = 0;
2664 req->analyse_exp = TICK_ETERNITY;
2665 return 0;
2666}
2667
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002668/* This stream analyser runs all HTTP request processing which is common to
2669 * frontends and backends, which means blocking ACLs, filters, connection-close,
2670 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002671 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002672 * either needs more data or wants to immediately abort the request (eg: deny,
2673 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002674 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002675int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002676{
Willy Tarreaud787e662009-07-07 10:14:51 +02002677 struct http_txn *txn = &s->txn;
2678 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002679 struct acl_cond *cond;
2680 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002681 struct wordlist *wl;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002682 int del_ka, del_cl;
Willy Tarreaud787e662009-07-07 10:14:51 +02002683
Willy Tarreau655dce92009-11-08 13:10:58 +01002684 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002685 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002686 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002687 return 0;
2688 }
2689
Willy Tarreau3a816292009-07-07 10:55:49 +02002690 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002691 req->analyse_exp = TICK_ETERNITY;
2692
2693 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2694 now_ms, __FUNCTION__,
2695 s,
2696 req,
2697 req->rex, req->wex,
2698 req->flags,
2699 req->l,
2700 req->analysers);
2701
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002702 /* first check whether we have some ACLs set to block this request */
2703 list_for_each_entry(cond, &px->block_cond, list) {
2704 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002705
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706 ret = acl_pass(ret);
2707 if (cond->pol == ACL_COND_UNLESS)
2708 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002709
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002710 if (ret) {
2711 txn->status = 403;
2712 /* let's log the request time */
2713 s->logs.tv_request = now;
2714 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2715 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002716 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002717 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002718
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719 /* try headers filters */
2720 if (px->req_exp != NULL) {
2721 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2722 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002723
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 /* has the request been denied ? */
2725 if (txn->flags & TX_CLDENY) {
2726 /* no need to go further */
2727 txn->status = 403;
2728 /* let's log the request time */
2729 s->logs.tv_request = now;
2730 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2731 goto return_prx_cond;
2732 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002733
2734 /* When a connection is tarpitted, we use the tarpit timeout,
2735 * which may be the same as the connect timeout if unspecified.
2736 * If unset, then set it to zero because we really want it to
2737 * eventually expire. We build the tarpit as an analyser.
2738 */
2739 if (txn->flags & TX_CLTARPIT) {
2740 buffer_erase(s->req);
2741 /* wipe the request out so that we can drop the connection early
2742 * if the client closes first.
2743 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002744 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002745 req->analysers = 0; /* remove switching rules etc... */
2746 req->analysers |= AN_REQ_HTTP_TARPIT;
2747 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2748 if (!req->analyse_exp)
2749 req->analyse_exp = tick_add(now_ms, 0);
2750 return 1;
2751 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 }
Willy Tarreau06619262006-12-17 08:37:22 +01002753
Willy Tarreau5b154472009-12-21 20:11:07 +01002754 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2755 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002756 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2757 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002758 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2759 * avoid to redo the same work if FE and BE have the same settings (common).
2760 * The method consists in checking if options changed between the two calls
2761 * (implying that either one is non-null, or one of them is non-null and we
2762 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002763 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002764
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002765 del_cl = del_ka = 0;
2766
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002767 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002768 ((!(txn->flags & TX_HDR_CONN_PRS) &&
2769 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2770 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2771 (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 +01002772 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002773
Willy Tarreau5b154472009-12-21 20:11:07 +01002774 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2775 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002776 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2777 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002778 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002779 tmp = TX_CON_WANT_CLO;
2780
Willy Tarreau5b154472009-12-21 20:11:07 +01002781 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2782 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002783
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002784 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2785 /* parse the Connection header and possibly clean it */
2786 int to_del = 0;
2787 if ((txn->flags & TX_REQ_VER_11) ||
2788 (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)
2789 to_del |= 2; /* remove "keep-alive" */
2790 if (!(txn->flags & TX_REQ_VER_11))
2791 to_del |= 1; /* remove "close" */
2792 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002793 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002794
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002795 /* check if client or config asks for explicit close in KAL/SCL */
2796 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2797 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2798 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2799 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2800 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
2801 !(txn->flags & TX_REQ_XFER_LEN))) /* no length known => close */
2802 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2803 }
Willy Tarreau78599912009-10-17 20:12:21 +02002804
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002805 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002806 list_for_each_entry(wl, &px->req_add, list) {
2807 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002808 goto return_bad_req;
2809 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002810
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 /* check if stats URI was requested, and if an auth is needed */
2812 if (px->uri_auth != NULL &&
2813 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2814 /* we have to check the URI and auth for this request.
2815 * FIXME!!! that one is rather dangerous, we want to
2816 * make it follow standard rules (eg: clear req->analysers).
2817 */
2818 if (stats_check_uri_auth(s, px)) {
2819 req->analysers = 0;
2820 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002821 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002822 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002823
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002824 /* check whether we have some ACLs set to redirect this request */
2825 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002826 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002827
Willy Tarreauf285f542010-01-03 20:03:03 +01002828 if (rule->cond) {
2829 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2830 ret = acl_pass(ret);
2831 if (rule->cond->pol == ACL_COND_UNLESS)
2832 ret = !ret;
2833 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002834
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002835 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002836 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002837 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002838
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002839 /* build redirect message */
2840 switch(rule->code) {
2841 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002842 msg_fmt = HTTP_303;
2843 break;
2844 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002845 msg_fmt = HTTP_301;
2846 break;
2847 case 302:
2848 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002849 msg_fmt = HTTP_302;
2850 break;
2851 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002852
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002853 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002854 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002855
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002856 switch(rule->type) {
2857 case REDIRECT_TYPE_PREFIX: {
2858 const char *path;
2859 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002860
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 path = http_get_path(txn);
2862 /* build message using path */
2863 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002864 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002865 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2866 int qs = 0;
2867 while (qs < pathlen) {
2868 if (path[qs] == '?') {
2869 pathlen = qs;
2870 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002871 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002872 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002873 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002874 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002875 } else {
2876 path = "/";
2877 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002878 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002879
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002880 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002881 goto return_bad_req;
2882
2883 /* add prefix. Note that if prefix == "/", we don't want to
2884 * add anything, otherwise it makes it hard for the user to
2885 * configure a self-redirection.
2886 */
2887 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002888 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2889 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002890 }
2891
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002892 /* add path */
2893 memcpy(rdr.str + rdr.len, path, pathlen);
2894 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01002895
2896 /* append a slash at the end of the location is needed and missing */
2897 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
2898 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2899 if (rdr.len > rdr.size - 5)
2900 goto return_bad_req;
2901 rdr.str[rdr.len] = '/';
2902 rdr.len++;
2903 }
2904
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002905 break;
2906 }
2907 case REDIRECT_TYPE_LOCATION:
2908 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002909 if (rdr.len + rule->rdr_len > rdr.size - 4)
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 /* add location */
2913 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2914 rdr.len += rule->rdr_len;
2915 break;
2916 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002917
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002918 if (rule->cookie_len) {
2919 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2920 rdr.len += 14;
2921 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2922 rdr.len += rule->cookie_len;
2923 memcpy(rdr.str + rdr.len, "\r\n", 2);
2924 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002925 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002926
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002927 /* add end of headers and the keep-alive/close status.
2928 * We may choose to set keep-alive if the Location begins
2929 * with a slash, because the client will come back to the
2930 * same server.
2931 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002932 txn->status = rule->code;
2933 /* let's log the request time */
2934 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002935
2936 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2937 (txn->flags & TX_REQ_XFER_LEN) &&
2938 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2939 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2940 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2941 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01002942 if (!(txn->flags & TX_REQ_VER_11)) {
2943 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
2944 rdr.len += 24;
2945 }
2946 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2947 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002948 buffer_write(req->prod->ob, rdr.str, rdr.len);
2949 /* "eat" the request */
2950 buffer_ignore(req, msg->sov - msg->som);
2951 msg->som = msg->sov;
2952 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002953 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2954 txn->req.msg_state = HTTP_MSG_CLOSED;
2955 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002956 break;
2957 } else {
2958 /* keep-alive not possible */
2959 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2960 rdr.len += 23;
Willy Tarreau148d0992010-01-10 10:21:21 +01002961 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002962 goto return_prx_cond;
2963 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002964 }
2965 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002966
Willy Tarreau2be39392010-01-03 17:24:51 +01002967 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2968 * If this happens, then the data will not come immediately, so we must
2969 * send all what we have without waiting. Note that due to the small gain
2970 * in waiting for the body of the request, it's easier to simply put the
2971 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2972 * itself once used.
2973 */
2974 req->flags |= BF_SEND_DONTWAIT;
2975
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002976 /* that's OK for us now, let's move on to next analysers */
2977 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002978
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002979 return_bad_req:
2980 /* We centralize bad requests processing here */
2981 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2982 /* we detected a parsing error. We want to archive this request
2983 * in the dedicated proxy area for later troubleshooting.
2984 */
2985 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2986 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002987
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002988 txn->req.msg_state = HTTP_MSG_ERROR;
2989 txn->status = 400;
2990 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002991
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002992 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002993 if (s->listener->counters)
2994 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002995
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002996 return_prx_cond:
2997 if (!(s->flags & SN_ERR_MASK))
2998 s->flags |= SN_ERR_PRXCOND;
2999 if (!(s->flags & SN_FINST_MASK))
3000 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003001
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 req->analysers = 0;
3003 req->analyse_exp = TICK_ETERNITY;
3004 return 0;
3005}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003006
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007/* This function performs all the processing enabled for the current request.
3008 * It returns 1 if the processing can continue on next analysers, or zero if it
3009 * needs more data, encounters an error, or wants to immediately abort the
3010 * request. It relies on buffers flags, and updates s->req->analysers.
3011 */
3012int http_process_request(struct session *s, struct buffer *req, int an_bit)
3013{
3014 struct http_txn *txn = &s->txn;
3015 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003016
Willy Tarreau655dce92009-11-08 13:10:58 +01003017 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003018 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003019 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003020 return 0;
3021 }
3022
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3024 now_ms, __FUNCTION__,
3025 s,
3026 req,
3027 req->rex, req->wex,
3028 req->flags,
3029 req->l,
3030 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003031
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 /*
3033 * Right now, we know that we have processed the entire headers
3034 * and that unwanted requests have been filtered out. We can do
3035 * whatever we want with the remaining request. Also, now we
3036 * may have separate values for ->fe, ->be.
3037 */
Willy Tarreau06619262006-12-17 08:37:22 +01003038
Willy Tarreau59234e92008-11-30 23:51:27 +01003039 /*
3040 * If HTTP PROXY is set we simply get remote server address
3041 * parsing incoming request.
3042 */
3043 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003044 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003045 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003046
Willy Tarreau59234e92008-11-30 23:51:27 +01003047 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003048 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003049 * Note that doing so might move headers in the request, but
3050 * the fields will stay coherent and the URI will not move.
3051 * This should only be performed in the backend.
3052 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003053 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003054 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3055 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003056
Willy Tarreau59234e92008-11-30 23:51:27 +01003057 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003058 * 8: the appsession cookie was looked up very early in 1.2,
3059 * so let's do the same now.
3060 */
3061
3062 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003063 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003064 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003065 }
3066
3067 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003068 * 9: add X-Forwarded-For if either the frontend or the backend
3069 * asks for it.
3070 */
3071 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3072 if (s->cli_addr.ss_family == AF_INET) {
3073 /* Add an X-Forwarded-For header unless the source IP is
3074 * in the 'except' network range.
3075 */
3076 if ((!s->fe->except_mask.s_addr ||
3077 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3078 != s->fe->except_net.s_addr) &&
3079 (!s->be->except_mask.s_addr ||
3080 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3081 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003082 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003083 unsigned char *pn;
3084 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003085
3086 /* Note: we rely on the backend to get the header name to be used for
3087 * x-forwarded-for, because the header is really meant for the backends.
3088 * However, if the backend did not specify any option, we have to rely
3089 * on the frontend's header name.
3090 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003091 if (s->be->fwdfor_hdr_len) {
3092 len = s->be->fwdfor_hdr_len;
3093 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003094 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003095 len = s->fe->fwdfor_hdr_len;
3096 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003097 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003098 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003099
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003100 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003101 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003102 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003103 }
3104 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003105 else if (s->cli_addr.ss_family == AF_INET6) {
3106 /* FIXME: for the sake of completeness, we should also support
3107 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003108 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003109 int len;
3110 char pn[INET6_ADDRSTRLEN];
3111 inet_ntop(AF_INET6,
3112 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3113 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003114
Willy Tarreau59234e92008-11-30 23:51:27 +01003115 /* Note: we rely on the backend to get the header name to be used for
3116 * x-forwarded-for, because the header is really meant for the backends.
3117 * However, if the backend did not specify any option, we have to rely
3118 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003119 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003120 if (s->be->fwdfor_hdr_len) {
3121 len = s->be->fwdfor_hdr_len;
3122 memcpy(trash, s->be->fwdfor_hdr_name, len);
3123 } else {
3124 len = s->fe->fwdfor_hdr_len;
3125 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003126 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003127 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003128
Willy Tarreau59234e92008-11-30 23:51:27 +01003129 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003130 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003131 goto return_bad_req;
3132 }
3133 }
3134
3135 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003136 * 10: add X-Original-To if either the frontend or the backend
3137 * asks for it.
3138 */
3139 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3140
3141 /* FIXME: don't know if IPv6 can handle that case too. */
3142 if (s->cli_addr.ss_family == AF_INET) {
3143 /* Add an X-Original-To header unless the destination IP is
3144 * in the 'except' network range.
3145 */
3146 if (!(s->flags & SN_FRT_ADDR_SET))
3147 get_frt_addr(s);
3148
3149 if ((!s->fe->except_mask_to.s_addr ||
3150 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3151 != s->fe->except_to.s_addr) &&
3152 (!s->be->except_mask_to.s_addr ||
3153 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3154 != s->be->except_to.s_addr)) {
3155 int len;
3156 unsigned char *pn;
3157 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3158
3159 /* Note: we rely on the backend to get the header name to be used for
3160 * x-original-to, because the header is really meant for the backends.
3161 * However, if the backend did not specify any option, we have to rely
3162 * on the frontend's header name.
3163 */
3164 if (s->be->orgto_hdr_len) {
3165 len = s->be->orgto_hdr_len;
3166 memcpy(trash, s->be->orgto_hdr_name, len);
3167 } else {
3168 len = s->fe->orgto_hdr_len;
3169 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003170 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003171 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3172
3173 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003174 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003175 goto return_bad_req;
3176 }
3177 }
3178 }
3179
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003180 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3181 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3182 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3183 unsigned int want_flags = 0;
3184
3185 if (txn->flags & TX_REQ_VER_11) {
3186 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3187 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3188 want_flags |= TX_CON_CLO_SET;
3189 } else {
3190 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
3191 want_flags |= TX_CON_KAL_SET;
3192 }
3193
3194 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3195 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003196 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003197
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003198
Willy Tarreau522d6c02009-12-06 18:49:18 +01003199 /* If we have no server assigned yet and we're balancing on url_param
3200 * with a POST request, we may be interested in checking the body for
3201 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003202 */
3203 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3204 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003205 s->be->url_param_post_limit != 0 &&
3206 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003207 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003208 buffer_dont_connect(req);
3209 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003210 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003211
Willy Tarreaud98cf932009-12-27 22:54:55 +01003212 if (txn->flags & TX_REQ_XFER_LEN)
3213 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003214
Willy Tarreau59234e92008-11-30 23:51:27 +01003215 /*************************************************************
3216 * OK, that's finished for the headers. We have done what we *
3217 * could. Let's switch to the DATA state. *
3218 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003219 req->analyse_exp = TICK_ETERNITY;
3220 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003221
Willy Tarreau59234e92008-11-30 23:51:27 +01003222 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003223 /* OK let's go on with the BODY now */
3224 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003225
Willy Tarreau59234e92008-11-30 23:51:27 +01003226 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003227 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003228 /* we detected a parsing error. We want to archive this request
3229 * in the dedicated proxy area for later troubleshooting.
3230 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003231 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003232 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003233
Willy Tarreau59234e92008-11-30 23:51:27 +01003234 txn->req.msg_state = HTTP_MSG_ERROR;
3235 txn->status = 400;
3236 req->analysers = 0;
3237 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003238
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003239 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003240 if (s->listener->counters)
3241 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003242
Willy Tarreau59234e92008-11-30 23:51:27 +01003243 if (!(s->flags & SN_ERR_MASK))
3244 s->flags |= SN_ERR_PRXCOND;
3245 if (!(s->flags & SN_FINST_MASK))
3246 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003247 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003248}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003249
Willy Tarreau60b85b02008-11-30 23:28:40 +01003250/* This function is an analyser which processes the HTTP tarpit. It always
3251 * returns zero, at the beginning because it prevents any other processing
3252 * from occurring, and at the end because it terminates the request.
3253 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003254int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003255{
3256 struct http_txn *txn = &s->txn;
3257
3258 /* This connection is being tarpitted. The CLIENT side has
3259 * already set the connect expiration date to the right
3260 * timeout. We just have to check that the client is still
3261 * there and that the timeout has not expired.
3262 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003263 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003264 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3265 !tick_is_expired(req->analyse_exp, now_ms))
3266 return 0;
3267
3268 /* We will set the queue timer to the time spent, just for
3269 * logging purposes. We fake a 500 server error, so that the
3270 * attacker will not suspect his connection has been tarpitted.
3271 * It will not cause trouble to the logs because we can exclude
3272 * the tarpitted connections by filtering on the 'PT' status flags.
3273 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003274 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3275
3276 txn->status = 500;
3277 if (req->flags != BF_READ_ERROR)
3278 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3279
3280 req->analysers = 0;
3281 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003282
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003283 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003284 if (s->listener->counters)
3285 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003286
Willy Tarreau60b85b02008-11-30 23:28:40 +01003287 if (!(s->flags & SN_ERR_MASK))
3288 s->flags |= SN_ERR_PRXCOND;
3289 if (!(s->flags & SN_FINST_MASK))
3290 s->flags |= SN_FINST_T;
3291 return 0;
3292}
3293
Willy Tarreaud34af782008-11-30 23:36:37 +01003294/* This function is an analyser which processes the HTTP request body. It looks
3295 * for parameters to be used for the load balancing algorithm (url_param). It
3296 * must only be called after the standard HTTP request processing has occurred,
3297 * because it expects the request to be parsed. It returns zero if it needs to
3298 * read more data, or 1 once it has completed its analysis.
3299 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003300int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003301{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003302 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003303 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003304 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003305
3306 /* We have to parse the HTTP request body to find any required data.
3307 * "balance url_param check_post" should have been the only way to get
3308 * into this. We were brought here after HTTP header analysis, so all
3309 * related structures are ready.
3310 */
3311
Willy Tarreau522d6c02009-12-06 18:49:18 +01003312 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3313 goto missing_data;
3314
3315 if (msg->msg_state < HTTP_MSG_100_SENT) {
3316 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3317 * send an HTTP/1.1 100 Continue intermediate response.
3318 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003319 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003320 struct hdr_ctx ctx;
3321 ctx.idx = 0;
3322 /* Expect is allowed in 1.1, look for it */
3323 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3324 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3325 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3326 }
3327 }
3328 msg->msg_state = HTTP_MSG_100_SENT;
3329 }
3330
3331 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003332 /* we have msg->col and msg->sov which both point to the first
3333 * byte of message body. msg->som still points to the beginning
3334 * of the message. We must save the body in req->lr because it
3335 * survives buffer re-alignments.
3336 */
3337 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003338 if (txn->flags & TX_REQ_TE_CHNK)
3339 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3340 else
3341 msg->msg_state = HTTP_MSG_DATA;
3342 }
3343
3344 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003345 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003346 * set ->sov and ->lr to point to the body and switch to DATA or
3347 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003348 */
3349 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003350
Willy Tarreau115acb92009-12-26 13:56:06 +01003351 if (!ret)
3352 goto missing_data;
3353 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003354 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003355 }
3356
Willy Tarreaud98cf932009-12-27 22:54:55 +01003357 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003358 * We have the first non-header byte in msg->col, which is either the
3359 * beginning of the chunk size or of the data. The first data byte is in
3360 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3361 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003362 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003363
3364 if (msg->hdr_content_len < limit)
3365 limit = msg->hdr_content_len;
3366
Willy Tarreau7c96f672009-12-27 22:47:25 +01003367 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003368 goto http_end;
3369
3370 missing_data:
3371 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003372 if (req->flags & BF_FULL)
3373 goto return_bad_req;
3374
Willy Tarreau522d6c02009-12-06 18:49:18 +01003375 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3376 txn->status = 408;
3377 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3378 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003379 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003380
3381 /* we get here if we need to wait for more data */
3382 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003383 /* Not enough data. We'll re-use the http-request
3384 * timeout here. Ideally, we should set the timeout
3385 * relative to the accept() date. We just set the
3386 * request timeout once at the beginning of the
3387 * request.
3388 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003389 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003390 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003391 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003392 return 0;
3393 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003394
3395 http_end:
3396 /* The situation will not evolve, so let's give up on the analysis. */
3397 s->logs.tv_request = now; /* update the request timer to reflect full request */
3398 req->analysers &= ~an_bit;
3399 req->analyse_exp = TICK_ETERNITY;
3400 return 1;
3401
3402 return_bad_req: /* let's centralize all bad requests */
3403 txn->req.msg_state = HTTP_MSG_ERROR;
3404 txn->status = 400;
3405 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3406
3407 return_err_msg:
3408 req->analysers = 0;
3409 s->fe->counters.failed_req++;
3410 if (s->listener->counters)
3411 s->listener->counters->failed_req++;
3412
3413 if (!(s->flags & SN_ERR_MASK))
3414 s->flags |= SN_ERR_PRXCOND;
3415 if (!(s->flags & SN_FINST_MASK))
3416 s->flags |= SN_FINST_R;
3417 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003418}
3419
Willy Tarreau610ecce2010-01-04 21:15:02 +01003420/* Terminate current transaction and prepare a new one. This is very tricky
3421 * right now but it works.
3422 */
3423void http_end_txn_clean_session(struct session *s)
3424{
3425 /* FIXME: We need a more portable way of releasing a backend's and a
3426 * server's connections. We need a safer way to reinitialize buffer
3427 * flags. We also need a more accurate method for computing per-request
3428 * data.
3429 */
3430 http_silent_debug(__LINE__, s);
3431
3432 s->req->cons->flags |= SI_FL_NOLINGER;
3433 s->req->cons->shutr(s->req->cons);
3434 s->req->cons->shutw(s->req->cons);
3435
3436 http_silent_debug(__LINE__, s);
3437
3438 if (s->flags & SN_BE_ASSIGNED)
3439 s->be->beconn--;
3440
3441 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3442 session_process_counters(s);
3443
3444 if (s->txn.status) {
3445 int n;
3446
3447 n = s->txn.status / 100;
3448 if (n < 1 || n > 5)
3449 n = 0;
3450
3451 if (s->fe->mode == PR_MODE_HTTP)
3452 s->fe->counters.p.http.rsp[n]++;
3453
3454 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3455 (s->be->mode == PR_MODE_HTTP))
3456 s->be->counters.p.http.rsp[n]++;
3457 }
3458
3459 /* don't count other requests' data */
3460 s->logs.bytes_in -= s->req->l - s->req->send_max;
3461 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3462
3463 /* let's do a final log if we need it */
3464 if (s->logs.logwait &&
3465 !(s->flags & SN_MONITOR) &&
3466 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3467 s->do_log(s);
3468 }
3469
3470 s->logs.accept_date = date; /* user-visible date for logging */
3471 s->logs.tv_accept = now; /* corrected date for internal use */
3472 tv_zero(&s->logs.tv_request);
3473 s->logs.t_queue = -1;
3474 s->logs.t_connect = -1;
3475 s->logs.t_data = -1;
3476 s->logs.t_close = 0;
3477 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3478 s->logs.srv_queue_size = 0; /* we will get this number soon */
3479
3480 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3481 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3482
3483 if (s->pend_pos)
3484 pendconn_free(s->pend_pos);
3485
3486 if (s->srv) {
3487 if (s->flags & SN_CURR_SESS) {
3488 s->flags &= ~SN_CURR_SESS;
3489 s->srv->cur_sess--;
3490 }
3491 if (may_dequeue_tasks(s->srv, s->be))
3492 process_srv_queue(s->srv);
3493 }
3494
3495 if (unlikely(s->srv_conn))
3496 sess_change_server(s, NULL);
3497 s->srv = NULL;
3498
3499 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3500 s->req->cons->fd = -1; /* just to help with debugging */
3501 s->req->cons->err_type = SI_ET_NONE;
3502 s->req->cons->err_loc = NULL;
3503 s->req->cons->exp = TICK_ETERNITY;
3504 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003505 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3506 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 +01003507 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003508 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3509 s->txn.meth = 0;
3510 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003511 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003512 if (s->be->options2 & PR_O2_INDEPSTR)
3513 s->req->cons->flags |= SI_FL_INDEP_STR;
3514
3515 /* if the request buffer is not empty, it means we're
3516 * about to process another request, so send pending
3517 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003518 * Just don't do this if the buffer is close to be full,
3519 * because the request will wait for it to flush a little
3520 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003521 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003522 if (s->req->l > s->req->send_max) {
3523 if (s->rep->send_max &&
3524 !(s->rep->flags & BF_FULL) &&
3525 s->rep->lr <= s->rep->r &&
3526 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3527 s->rep->flags |= BF_EXPECT_MORE;
3528 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003529
3530 /* we're removing the analysers, we MUST re-enable events detection */
3531 buffer_auto_read(s->req);
3532 buffer_auto_close(s->req);
3533 buffer_auto_read(s->rep);
3534 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003535
3536 /* make ->lr point to the first non-forwarded byte */
3537 s->req->lr = s->req->w + s->req->send_max;
3538 if (s->req->lr >= s->req->data + s->req->size)
3539 s->req->lr -= s->req->size;
3540 s->rep->lr = s->rep->w + s->rep->send_max;
3541 if (s->rep->lr >= s->rep->data + s->rep->size)
3542 s->rep->lr -= s->req->size;
3543
3544 s->req->analysers |= s->fe->fe_req_ana;
3545 s->rep->analysers = 0;
3546
3547 http_silent_debug(__LINE__, s);
3548}
3549
3550
3551/* This function updates the request state machine according to the response
3552 * state machine and buffer flags. It returns 1 if it changes anything (flag
3553 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3554 * it is only used to find when a request/response couple is complete. Both
3555 * this function and its equivalent should loop until both return zero. It
3556 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3557 */
3558int http_sync_req_state(struct session *s)
3559{
3560 struct buffer *buf = s->req;
3561 struct http_txn *txn = &s->txn;
3562 unsigned int old_flags = buf->flags;
3563 unsigned int old_state = txn->req.msg_state;
3564
3565 http_silent_debug(__LINE__, s);
3566 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3567 return 0;
3568
3569 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003570 /* No need to read anymore, the request was completely parsed.
3571 * We can shut the read side unless we want to abort_on_close.
3572 */
3573 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3574 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003575
3576 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3577 goto wait_other_side;
3578
3579 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3580 /* The server has not finished to respond, so we
3581 * don't want to move in order not to upset it.
3582 */
3583 goto wait_other_side;
3584 }
3585
3586 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3587 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003588 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003589 txn->req.msg_state = HTTP_MSG_TUNNEL;
3590 goto wait_other_side;
3591 }
3592
3593 /* When we get here, it means that both the request and the
3594 * response have finished receiving. Depending on the connection
3595 * mode, we'll have to wait for the last bytes to leave in either
3596 * direction, and sometimes for a close to be effective.
3597 */
3598
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003599 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3600 /* Server-close mode : queue a connection close to the server */
3601 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003602 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003603 }
3604 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3605 /* Option forceclose is set, or either side wants to close,
3606 * let's enforce it now that we're not expecting any new
3607 * data to come. The caller knows the session is complete
3608 * once both states are CLOSED.
3609 */
3610 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003611 buffer_shutr_now(buf);
3612 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003613 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003614 }
3615 else {
3616 /* The last possible modes are keep-alive and tunnel. Since tunnel
3617 * mode does not set the body analyser, we can't reach this place
3618 * in tunnel mode, so we're left with keep-alive only.
3619 * This mode is currently not implemented, we switch to tunnel mode.
3620 */
3621 buffer_auto_read(buf);
3622 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003623 }
3624
3625 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3626 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003627 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3628
Willy Tarreau610ecce2010-01-04 21:15:02 +01003629 if (!(buf->flags & BF_OUT_EMPTY)) {
3630 txn->req.msg_state = HTTP_MSG_CLOSING;
3631 goto http_msg_closing;
3632 }
3633 else {
3634 txn->req.msg_state = HTTP_MSG_CLOSED;
3635 goto http_msg_closed;
3636 }
3637 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003638 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003639 }
3640
3641 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3642 http_msg_closing:
3643 /* nothing else to forward, just waiting for the output buffer
3644 * to be empty and for the shutw_now to take effect.
3645 */
3646 if (buf->flags & BF_OUT_EMPTY) {
3647 txn->req.msg_state = HTTP_MSG_CLOSED;
3648 goto http_msg_closed;
3649 }
3650 else if (buf->flags & BF_SHUTW) {
3651 txn->req.msg_state = HTTP_MSG_ERROR;
3652 goto wait_other_side;
3653 }
3654 }
3655
3656 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3657 http_msg_closed:
3658 goto wait_other_side;
3659 }
3660
3661 wait_other_side:
3662 http_silent_debug(__LINE__, s);
3663 return txn->req.msg_state != old_state || buf->flags != old_flags;
3664}
3665
3666
3667/* This function updates the response state machine according to the request
3668 * state machine and buffer flags. It returns 1 if it changes anything (flag
3669 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3670 * it is only used to find when a request/response couple is complete. Both
3671 * this function and its equivalent should loop until both return zero. It
3672 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3673 */
3674int http_sync_res_state(struct session *s)
3675{
3676 struct buffer *buf = s->rep;
3677 struct http_txn *txn = &s->txn;
3678 unsigned int old_flags = buf->flags;
3679 unsigned int old_state = txn->rsp.msg_state;
3680
3681 http_silent_debug(__LINE__, s);
3682 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3683 return 0;
3684
3685 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3686 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003687 * still monitor the server connection for a possible close
3688 * while the request is being uploaded, so we don't disable
3689 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003690 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003691 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003692
3693 if (txn->req.msg_state == HTTP_MSG_ERROR)
3694 goto wait_other_side;
3695
3696 if (txn->req.msg_state < HTTP_MSG_DONE) {
3697 /* The client seems to still be sending data, probably
3698 * because we got an error response during an upload.
3699 * We have the choice of either breaking the connection
3700 * or letting it pass through. Let's do the later.
3701 */
3702 goto wait_other_side;
3703 }
3704
3705 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3706 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003707 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003708 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3709 goto wait_other_side;
3710 }
3711
3712 /* When we get here, it means that both the request and the
3713 * response have finished receiving. Depending on the connection
3714 * mode, we'll have to wait for the last bytes to leave in either
3715 * direction, and sometimes for a close to be effective.
3716 */
3717
3718 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3719 /* Server-close mode : shut read and wait for the request
3720 * side to close its output buffer. The caller will detect
3721 * when we're in DONE and the other is in CLOSED and will
3722 * catch that for the final cleanup.
3723 */
3724 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3725 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003726 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003727 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3728 /* Option forceclose is set, or either side wants to close,
3729 * let's enforce it now that we're not expecting any new
3730 * data to come. The caller knows the session is complete
3731 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003733 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3734 buffer_shutr_now(buf);
3735 buffer_shutw_now(buf);
3736 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737 }
3738 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003739 /* The last possible modes are keep-alive and tunnel. Since tunnel
3740 * mode does not set the body analyser, we can't reach this place
3741 * in tunnel mode, so we're left with keep-alive only.
3742 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003744 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003745 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746 }
3747
3748 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3749 /* if we've just closed an output, let's switch */
3750 if (!(buf->flags & BF_OUT_EMPTY)) {
3751 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3752 goto http_msg_closing;
3753 }
3754 else {
3755 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3756 goto http_msg_closed;
3757 }
3758 }
3759 goto wait_other_side;
3760 }
3761
3762 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3763 http_msg_closing:
3764 /* nothing else to forward, just waiting for the output buffer
3765 * to be empty and for the shutw_now to take effect.
3766 */
3767 if (buf->flags & BF_OUT_EMPTY) {
3768 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3769 goto http_msg_closed;
3770 }
3771 else if (buf->flags & BF_SHUTW) {
3772 txn->rsp.msg_state = HTTP_MSG_ERROR;
3773 goto wait_other_side;
3774 }
3775 }
3776
3777 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3778 http_msg_closed:
3779 /* drop any pending data */
3780 buffer_ignore(buf, buf->l - buf->send_max);
3781 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003782 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003783 goto wait_other_side;
3784 }
3785
3786 wait_other_side:
3787 http_silent_debug(__LINE__, s);
3788 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3789}
3790
3791
3792/* Resync the request and response state machines. Return 1 if either state
3793 * changes.
3794 */
3795int http_resync_states(struct session *s)
3796{
3797 struct http_txn *txn = &s->txn;
3798 int old_req_state = txn->req.msg_state;
3799 int old_res_state = txn->rsp.msg_state;
3800
3801 http_silent_debug(__LINE__, s);
3802 http_sync_req_state(s);
3803 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003804 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 if (!http_sync_res_state(s))
3806 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003807 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003808 if (!http_sync_req_state(s))
3809 break;
3810 }
3811 http_silent_debug(__LINE__, s);
3812 /* OK, both state machines agree on a compatible state.
3813 * There are a few cases we're interested in :
3814 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3815 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3816 * directions, so let's simply disable both analysers.
3817 * - HTTP_MSG_CLOSED on the response only means we must abort the
3818 * request.
3819 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3820 * with server-close mode means we've completed one request and we
3821 * must re-initialize the server connection.
3822 */
3823
3824 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3825 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3826 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3827 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3828 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003829 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003830 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003831 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003832 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003833 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003834 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003835 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3836 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3837 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003838 s->rep->analysers = 0;
3839 buffer_auto_close(s->rep);
3840 buffer_auto_read(s->rep);
3841 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003842 buffer_abort(s->req);
3843 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003844 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003845 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003846 }
3847 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3848 txn->rsp.msg_state == HTTP_MSG_DONE &&
3849 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3850 /* server-close: terminate this server connection and
3851 * reinitialize a fresh-new transaction.
3852 */
3853 http_end_txn_clean_session(s);
3854 }
3855
3856 http_silent_debug(__LINE__, s);
3857 return txn->req.msg_state != old_req_state ||
3858 txn->rsp.msg_state != old_res_state;
3859}
3860
Willy Tarreaud98cf932009-12-27 22:54:55 +01003861/* This function is an analyser which forwards request body (including chunk
3862 * sizes if any). It is called as soon as we must forward, even if we forward
3863 * zero byte. The only situation where it must not be called is when we're in
3864 * tunnel mode and we want to forward till the close. It's used both to forward
3865 * remaining data and to resync after end of body. It expects the msg_state to
3866 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3867 * read more data, or 1 once we can go on with next request or end the session.
3868 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3869 * bytes of pending data + the headers if not already done (between som and sov).
3870 * It eventually adjusts som to match sov after the data in between have been sent.
3871 */
3872int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3873{
3874 struct http_txn *txn = &s->txn;
3875 struct http_msg *msg = &s->txn.req;
3876
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003877 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3878 return 0;
3879
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003880 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3881 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3882 /* Output closed while we were sending data. We must abort. */
3883 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003884 buffer_auto_read(req);
3885 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003886 req->analysers &= ~an_bit;
3887 return 1;
3888 }
3889
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003890 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003891
3892 /* Note that we don't have to send 100-continue back because we don't
3893 * need the data to complete our job, and it's up to the server to
3894 * decide whether to return 100, 417 or anything else in return of
3895 * an "Expect: 100-continue" header.
3896 */
3897
3898 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3899 /* we have msg->col and msg->sov which both point to the first
3900 * byte of message body. msg->som still points to the beginning
3901 * of the message. We must save the body in req->lr because it
3902 * survives buffer re-alignments.
3903 */
3904 req->lr = req->data + msg->sov;
3905 if (txn->flags & TX_REQ_TE_CHNK)
3906 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3907 else {
3908 msg->msg_state = HTTP_MSG_DATA;
3909 }
3910 }
3911
Willy Tarreaud98cf932009-12-27 22:54:55 +01003912 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003913 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003914 /* we may have some data pending */
3915 if (msg->hdr_content_len || msg->som != msg->sov) {
3916 int bytes = msg->sov - msg->som;
3917 if (bytes < 0) /* sov may have wrapped at the end */
3918 bytes += req->size;
3919 buffer_forward(req, bytes + msg->hdr_content_len);
3920 msg->hdr_content_len = 0; /* don't forward that again */
3921 msg->som = msg->sov;
3922 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003923
Willy Tarreaucaabe412010-01-03 23:08:28 +01003924 if (msg->msg_state == HTTP_MSG_DATA) {
3925 /* must still forward */
3926 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003927 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003928
3929 /* nothing left to forward */
3930 if (txn->flags & TX_REQ_TE_CHNK)
3931 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003933 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003934 }
3935 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003936 /* read the chunk size and assign it to ->hdr_content_len, then
3937 * set ->sov and ->lr to point to the body and switch to DATA or
3938 * TRAILERS state.
3939 */
3940 int ret = http_parse_chunk_size(req, msg);
3941
3942 if (!ret)
3943 goto missing_data;
3944 else if (ret < 0)
3945 goto return_bad_req;
3946 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003947 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003948 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3949 /* we want the CRLF after the data */
3950 int ret;
3951
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003952 req->lr = req->w + req->send_max;
3953 if (req->lr >= req->data + req->size)
3954 req->lr -= req->size;
3955
Willy Tarreaud98cf932009-12-27 22:54:55 +01003956 ret = http_skip_chunk_crlf(req, msg);
3957
3958 if (ret == 0)
3959 goto missing_data;
3960 else if (ret < 0)
3961 goto return_bad_req;
3962 /* we're in MSG_CHUNK_SIZE now */
3963 }
3964 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3965 int ret = http_forward_trailers(req, msg);
3966
3967 if (ret == 0)
3968 goto missing_data;
3969 else if (ret < 0)
3970 goto return_bad_req;
3971 /* we're in HTTP_MSG_DONE now */
3972 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973 else {
3974 /* other states, DONE...TUNNEL */
3975 if (http_resync_states(s)) {
3976 /* some state changes occurred, maybe the analyser
3977 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003978 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003979 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3980 goto return_bad_req;
3981 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003982 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003983 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003984 }
3985 }
3986
Willy Tarreaud98cf932009-12-27 22:54:55 +01003987 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003988 /* stop waiting for data if the input is closed before the end */
3989 if (req->flags & BF_SHUTR)
3990 goto return_bad_req;
3991
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003992 /* waiting for the last bits to leave the buffer */
3993 if (req->flags & BF_SHUTW)
3994 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003995
3996 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003997 return 0;
3998
Willy Tarreaud98cf932009-12-27 22:54:55 +01003999 return_bad_req: /* let's centralize all bad requests */
4000 txn->req.msg_state = HTTP_MSG_ERROR;
4001 txn->status = 400;
4002 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004003 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 +01004004 req->analysers = 0;
4005 s->fe->counters.failed_req++;
4006 if (s->listener->counters)
4007 s->listener->counters->failed_req++;
4008
4009 if (!(s->flags & SN_ERR_MASK))
4010 s->flags |= SN_ERR_PRXCOND;
4011 if (!(s->flags & SN_FINST_MASK))
4012 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004014 return 0;
4015}
4016
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004017/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4018 * processing can continue on next analysers, or zero if it either needs more
4019 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4020 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4021 * when it has nothing left to do, and may remove any analyser when it wants to
4022 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004023 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004024int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004025{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004026 struct http_txn *txn = &s->txn;
4027 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004028 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004029 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004030 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004031 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004032
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004033 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 +02004034 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004035 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004036 rep,
4037 rep->rex, rep->wex,
4038 rep->flags,
4039 rep->l,
4040 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004041
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004042 /*
4043 * Now parse the partial (or complete) lines.
4044 * We will check the response syntax, and also join multi-line
4045 * headers. An index of all the lines will be elaborated while
4046 * parsing.
4047 *
4048 * For the parsing, we use a 28 states FSM.
4049 *
4050 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004051 * rep->data + msg->som = beginning of response
4052 * rep->data + msg->eoh = end of processed headers / start of current one
4053 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004054 * rep->lr = first non-visited byte
4055 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004056 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004057 */
4058
Willy Tarreau83e3af02009-12-28 17:39:57 +01004059 /* There's a protected area at the end of the buffer for rewriting
4060 * purposes. We don't want to start to parse the request if the
4061 * protected area is affected, because we may have to move processed
4062 * data later, which is much more complicated.
4063 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004064 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4065 if (unlikely((rep->flags & BF_FULL) ||
4066 rep->r < rep->lr ||
4067 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4068 if (rep->send_max) {
4069 /* some data has still not left the buffer, wake us once that's done */
4070 buffer_dont_close(rep);
4071 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4072 return 0;
4073 }
4074 if (rep->l <= rep->size - global.tune.maxrewrite)
4075 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004076 }
4077
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004078 if (likely(rep->lr < rep->r))
4079 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004080 }
4081
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004082 /* 1: we might have to print this header in debug mode */
4083 if (unlikely((global.mode & MODE_DEBUG) &&
4084 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004085 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004086 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004087
Willy Tarreau962c3f42010-01-10 00:15:35 +01004088 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004089 eol = sol + msg->sl.rq.l;
4090 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004091
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004092 sol += hdr_idx_first_pos(&txn->hdr_idx);
4093 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004094
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004095 while (cur_idx) {
4096 eol = sol + txn->hdr_idx.v[cur_idx].len;
4097 debug_hdr("srvhdr", s, sol, eol);
4098 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4099 cur_idx = txn->hdr_idx.v[cur_idx].next;
4100 }
4101 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004102
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004103 /*
4104 * Now we quickly check if we have found a full valid response.
4105 * If not so, we check the FD and buffer states before leaving.
4106 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004107 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004108 * responses are checked first.
4109 *
4110 * Depending on whether the client is still there or not, we
4111 * may send an error response back or not. Note that normally
4112 * we should only check for HTTP status there, and check I/O
4113 * errors somewhere else.
4114 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004115
Willy Tarreau655dce92009-11-08 13:10:58 +01004116 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004117 /* Invalid response */
4118 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4119 /* we detected a parsing error. We want to archive this response
4120 * in the dedicated proxy area for later troubleshooting.
4121 */
4122 hdr_response_bad:
4123 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4124 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4125
4126 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004127 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004128 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004129 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4130 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004131
Willy Tarreau90deb182010-01-07 00:20:41 +01004132 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004133 rep->analysers = 0;
4134 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004135 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004136 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4137
4138 if (!(s->flags & SN_ERR_MASK))
4139 s->flags |= SN_ERR_PRXCOND;
4140 if (!(s->flags & SN_FINST_MASK))
4141 s->flags |= SN_FINST_H;
4142
4143 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004144 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004145
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004146 /* too large response does not fit in buffer. */
4147 else if (rep->flags & BF_FULL) {
4148 goto hdr_response_bad;
4149 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004150
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004151 /* read error */
4152 else if (rep->flags & BF_READ_ERROR) {
4153 if (msg->err_pos >= 0)
4154 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004155
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004156 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004157 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004158 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004159 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4160 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004161
Willy Tarreau90deb182010-01-07 00:20:41 +01004162 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004163 rep->analysers = 0;
4164 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004165 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004166 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004167
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004168 if (!(s->flags & SN_ERR_MASK))
4169 s->flags |= SN_ERR_SRVCL;
4170 if (!(s->flags & SN_FINST_MASK))
4171 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004172 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004173 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004174
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004175 /* read timeout : return a 504 to the client. */
4176 else if (rep->flags & BF_READ_TIMEOUT) {
4177 if (msg->err_pos >= 0)
4178 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004179
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004180 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004181 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004182 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004183 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4184 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004185
Willy Tarreau90deb182010-01-07 00:20:41 +01004186 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004187 rep->analysers = 0;
4188 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004189 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004190 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004191
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004192 if (!(s->flags & SN_ERR_MASK))
4193 s->flags |= SN_ERR_SRVTO;
4194 if (!(s->flags & SN_FINST_MASK))
4195 s->flags |= SN_FINST_H;
4196 return 0;
4197 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004198
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004199 /* close from server */
4200 else if (rep->flags & BF_SHUTR) {
4201 if (msg->err_pos >= 0)
4202 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004203
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004204 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004205 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004206 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004207 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4208 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004209
Willy Tarreau90deb182010-01-07 00:20:41 +01004210 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004211 rep->analysers = 0;
4212 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004213 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004214 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004215
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004216 if (!(s->flags & SN_ERR_MASK))
4217 s->flags |= SN_ERR_SRVCL;
4218 if (!(s->flags & SN_FINST_MASK))
4219 s->flags |= SN_FINST_H;
4220 return 0;
4221 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004222
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004223 /* write error to client (we don't send any message then) */
4224 else if (rep->flags & BF_WRITE_ERROR) {
4225 if (msg->err_pos >= 0)
4226 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004227
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004228 s->be->counters.failed_resp++;
4229 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004230 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004231
4232 if (!(s->flags & SN_ERR_MASK))
4233 s->flags |= SN_ERR_CLICL;
4234 if (!(s->flags & SN_FINST_MASK))
4235 s->flags |= SN_FINST_H;
4236
4237 /* process_session() will take care of the error */
4238 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004239 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004240
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004241 buffer_dont_close(rep);
4242 return 0;
4243 }
4244
4245 /* More interesting part now : we know that we have a complete
4246 * response which at least looks like HTTP. We have an indicator
4247 * of each header's length, so we can parse them quickly.
4248 */
4249
4250 if (unlikely(msg->err_pos >= 0))
4251 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4252
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004253 /*
4254 * 1: get the status code
4255 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004256 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004257 if (n < 1 || n > 5)
4258 n = 0;
4259 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004260
Willy Tarreau5b154472009-12-21 20:11:07 +01004261 /* check if the response is HTTP/1.1 or above */
4262 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004263 ((msg->sol[5] > '1') ||
4264 ((msg->sol[5] == '1') &&
4265 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004266 txn->flags |= TX_RES_VER_11;
4267
4268 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004269 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 +01004270
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004271 /* transfer length unknown*/
4272 txn->flags &= ~TX_RES_XFER_LEN;
4273
Willy Tarreau962c3f42010-01-10 00:15:35 +01004274 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004275
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004276 if (txn->status >= 100 && txn->status < 500)
4277 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4278 else
4279 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4280
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004281 /*
4282 * 2: check for cacheability.
4283 */
4284
4285 switch (txn->status) {
4286 case 200:
4287 case 203:
4288 case 206:
4289 case 300:
4290 case 301:
4291 case 410:
4292 /* RFC2616 @13.4:
4293 * "A response received with a status code of
4294 * 200, 203, 206, 300, 301 or 410 MAY be stored
4295 * by a cache (...) unless a cache-control
4296 * directive prohibits caching."
4297 *
4298 * RFC2616 @9.5: POST method :
4299 * "Responses to this method are not cacheable,
4300 * unless the response includes appropriate
4301 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004302 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004303 if (likely(txn->meth != HTTP_METH_POST) &&
4304 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4305 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4306 break;
4307 default:
4308 break;
4309 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004310
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004311 /*
4312 * 3: we may need to capture headers
4313 */
4314 s->logs.logwait &= ~LW_RESP;
4315 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004316 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004317 txn->rsp.cap, s->fe->rsp_cap);
4318
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004319 /* 4: determine the transfer-length.
4320 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4321 * the presence of a message-body in a RESPONSE and its transfer length
4322 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004323 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004324 * All responses to the HEAD request method MUST NOT include a
4325 * message-body, even though the presence of entity-header fields
4326 * might lead one to believe they do. All 1xx (informational), 204
4327 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4328 * message-body. All other responses do include a message-body,
4329 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004330 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004331 * 1. Any response which "MUST NOT" include a message-body (such as the
4332 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4333 * always terminated by the first empty line after the header fields,
4334 * regardless of the entity-header fields present in the message.
4335 *
4336 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4337 * the "chunked" transfer-coding (Section 6.2) is used, the
4338 * transfer-length is defined by the use of this transfer-coding.
4339 * If a Transfer-Encoding header field is present and the "chunked"
4340 * transfer-coding is not present, the transfer-length is defined by
4341 * the sender closing the connection.
4342 *
4343 * 3. If a Content-Length header field is present, its decimal value in
4344 * OCTETs represents both the entity-length and the transfer-length.
4345 * If a message is received with both a Transfer-Encoding header
4346 * field and a Content-Length header field, the latter MUST be ignored.
4347 *
4348 * 4. If the message uses the media type "multipart/byteranges", and
4349 * the transfer-length is not otherwise specified, then this self-
4350 * delimiting media type defines the transfer-length. This media
4351 * type MUST NOT be used unless the sender knows that the recipient
4352 * can parse it; the presence in a request of a Range header with
4353 * multiple byte-range specifiers from a 1.1 client implies that the
4354 * client can parse multipart/byteranges responses.
4355 *
4356 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004357 */
4358
4359 /* Skip parsing if no content length is possible. The response flags
4360 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004361 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004362 * FIXME: should we parse anyway and return an error on chunked encoding ?
4363 */
4364 if (txn->meth == HTTP_METH_HEAD ||
4365 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004366 txn->status == 204 || txn->status == 304) {
4367 txn->flags |= TX_RES_XFER_LEN;
4368 goto skip_content_length;
4369 }
4370
4371 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004372 goto skip_content_length;
4373
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004374 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004375 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004376 while ((txn->flags & TX_RES_VER_11) &&
4377 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004378 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4379 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4380 else if (txn->flags & TX_RES_TE_CHNK) {
4381 /* bad transfer-encoding (chunked followed by something else) */
4382 use_close_only = 1;
4383 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4384 break;
4385 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004386 }
4387
4388 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4389 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004390 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004391 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4392 signed long long cl;
4393
4394 if (!ctx.vlen)
4395 goto hdr_response_bad;
4396
4397 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4398 goto hdr_response_bad; /* parse failure */
4399
4400 if (cl < 0)
4401 goto hdr_response_bad;
4402
4403 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4404 goto hdr_response_bad; /* already specified, was different */
4405
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004406 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004407 msg->hdr_content_len = cl;
4408 }
4409
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004410 /* FIXME: we should also implement the multipart/byterange method.
4411 * For now on, we resort to close mode in this case (unknown length).
4412 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004413skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004414
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004415 /* end of job, return OK */
4416 rep->analysers &= ~an_bit;
4417 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004418 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419 return 1;
4420}
4421
4422/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004423 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4424 * and updates t->rep->analysers. It might make sense to explode it into several
4425 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004426 */
4427int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4428{
4429 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004430 struct http_msg *msg = &txn->rsp;
4431 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004432 struct wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004433
4434 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4435 now_ms, __FUNCTION__,
4436 t,
4437 rep,
4438 rep->rex, rep->wex,
4439 rep->flags,
4440 rep->l,
4441 rep->analysers);
4442
Willy Tarreau655dce92009-11-08 13:10:58 +01004443 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004444 return 0;
4445
4446 rep->analysers &= ~an_bit;
4447 rep->analyse_exp = TICK_ETERNITY;
4448
Willy Tarreau5b154472009-12-21 20:11:07 +01004449 /* Now we have to check if we need to modify the Connection header.
4450 * This is more difficult on the response than it is on the request,
4451 * because we can have two different HTTP versions and we don't know
4452 * how the client will interprete a response. For instance, let's say
4453 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4454 * HTTP/1.1 response without any header. Maybe it will bound itself to
4455 * HTTP/1.0 because it only knows about it, and will consider the lack
4456 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4457 * the lack of header as a keep-alive. Thus we will use two flags
4458 * indicating how a request MAY be understood by the client. In case
4459 * of multiple possibilities, we'll fix the header to be explicit. If
4460 * ambiguous cases such as both close and keepalive are seen, then we
4461 * will fall back to explicit close. Note that we won't take risks with
4462 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004463 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004464 */
4465
4466 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004467 (txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004468 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4469 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004470 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004471
Willy Tarreau60466522010-01-18 19:08:45 +01004472 /* on unknown transfer length, we must close */
4473 if (!(txn->flags & TX_RES_XFER_LEN) &&
4474 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4475 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004476
Willy Tarreau60466522010-01-18 19:08:45 +01004477 /* now adjust header transformations depending on current state */
4478 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4479 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4480 to_del |= 2; /* remove "keep-alive" on any response */
4481 if (!(txn->flags & TX_RES_VER_11))
4482 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004483 }
Willy Tarreau60466522010-01-18 19:08:45 +01004484 else { /* SCL / KAL */
4485 to_del |= 1; /* remove "close" on any response */
4486 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4487 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004488 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004489
Willy Tarreau60466522010-01-18 19:08:45 +01004490 /* Parse and remove some headers from the connection header */
4491 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004492
Willy Tarreau60466522010-01-18 19:08:45 +01004493 /* Some keep-alive responses are converted to Server-close if
4494 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004495 */
Willy Tarreau60466522010-01-18 19:08:45 +01004496 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4497 if ((txn->flags & TX_HDR_CONN_CLO) ||
4498 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4499 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004500 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004501 }
4502
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004503 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004504 /*
4505 * 3: we will have to evaluate the filters.
4506 * As opposed to version 1.2, now they will be evaluated in the
4507 * filters order and not in the header order. This means that
4508 * each filter has to be validated among all headers.
4509 *
4510 * Filters are tried with ->be first, then with ->fe if it is
4511 * different from ->be.
4512 */
4513
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004514 cur_proxy = t->be;
4515 while (1) {
4516 struct proxy *rule_set = cur_proxy;
4517
4518 /* try headers filters */
4519 if (rule_set->rsp_exp != NULL) {
4520 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4521 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004522 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004523 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004524 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4525 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004526 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004527 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004528 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004529 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004530 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004531 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004532 if (!(t->flags & SN_ERR_MASK))
4533 t->flags |= SN_ERR_PRXCOND;
4534 if (!(t->flags & SN_FINST_MASK))
4535 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004536 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004537 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004538 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004539
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004540 /* has the response been denied ? */
4541 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004542 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004543 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004544
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004545 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004546 if (t->listener->counters)
4547 t->listener->counters->denied_resp++;
4548
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004549 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004550 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004551
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004552 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004553 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004554 if (txn->status < 200)
4555 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004556 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004557 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004558 }
4559
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004560 /* check whether we're already working on the frontend */
4561 if (cur_proxy == t->fe)
4562 break;
4563 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004564 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004565
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004566 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004567 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4568 * in which case this is not the right response, and we're waiting for the
4569 * next one. Let's allow this response to go to the client and wait for the
4570 * next one.
4571 */
4572 if (txn->status < 200) {
4573 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004574 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004575 msg->msg_state = HTTP_MSG_RPBEFORE;
4576 txn->status = 0;
4577 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4578 return 1;
4579 }
4580
4581 /* we don't have any 1xx status code now */
4582
4583 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584 * 4: check for server cookie.
4585 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004586 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4587 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004588 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004589
Willy Tarreaubaaee002006-06-26 02:48:02 +02004590
Willy Tarreaua15645d2007-03-18 16:22:39 +01004591 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004592 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004593 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004594 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004595 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004596
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004597 /*
4598 * 6: add server cookie in the response if needed
4599 */
4600 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004601 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004602 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004603
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004604 /* the server is known, it's not the one the client requested, we have to
4605 * insert a set-cookie here, except if we want to insert only on POST
4606 * requests and this one isn't. Note that servers which don't have cookies
4607 * (eg: some backup servers) will return a full cookie removal request.
4608 */
4609 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4610 t->be->cookie_name,
4611 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004612
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613 if (t->be->cookie_domain)
4614 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004615
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004616 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004617 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004618 goto return_bad_resp;
4619 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004620
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004621 /* Here, we will tell an eventual cache on the client side that we don't
4622 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4623 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4624 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4625 */
4626 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004627
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004628 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4629
4630 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004631 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004632 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004633 }
4634 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004635
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004636 /*
4637 * 7: check if result will be cacheable with a cookie.
4638 * We'll block the response if security checks have caught
4639 * nasty things such as a cacheable cookie.
4640 */
4641 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4642 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004643 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004644
4645 /* we're in presence of a cacheable response containing
4646 * a set-cookie header. We'll block it as requested by
4647 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004648 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004649 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004650 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004651
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004652 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004653 if (t->listener->counters)
4654 t->listener->counters->denied_resp++;
4655
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004656 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4657 t->be->id, t->srv?t->srv->id:"<dispatch>");
4658 send_log(t->be, LOG_ALERT,
4659 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4660 t->be->id, t->srv?t->srv->id:"<dispatch>");
4661 goto return_srv_prx_502;
4662 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004663
4664 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004665 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004666 */
Willy Tarreau60466522010-01-18 19:08:45 +01004667 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4668 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4669 unsigned int want_flags = 0;
4670
4671 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4672 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4673 /* we want a keep-alive response here. Keep-alive header
4674 * required if either side is not 1.1.
4675 */
4676 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4677 want_flags |= TX_CON_KAL_SET;
4678 }
4679 else {
4680 /* we want a close response here. Close header required if
4681 * the server is 1.1, regardless of the client.
4682 */
4683 if (txn->flags & TX_RES_VER_11)
4684 want_flags |= TX_CON_CLO_SET;
4685 }
4686
4687 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4688 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004689 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004690
Willy Tarreaud98cf932009-12-27 22:54:55 +01004691 if (txn->flags & TX_RES_XFER_LEN)
4692 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004693
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004694 /*************************************************************
4695 * OK, that's finished for the headers. We have done what we *
4696 * could. Let's switch to the DATA state. *
4697 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004698
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004699 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004700
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004701 /* if the user wants to log as soon as possible, without counting
4702 * bytes from the server, then this is the right moment. We have
4703 * to temporarily assign bytes_out to log what we currently have.
4704 */
4705 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4706 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4707 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004708 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004709 t->logs.bytes_out = 0;
4710 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004711
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004712 /* Note: we must not try to cheat by jumping directly to DATA,
4713 * otherwise we would not let the client side wake up.
4714 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004715
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004716 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004717 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004718 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004719}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004720
Willy Tarreaud98cf932009-12-27 22:54:55 +01004721/* This function is an analyser which forwards response body (including chunk
4722 * sizes if any). It is called as soon as we must forward, even if we forward
4723 * zero byte. The only situation where it must not be called is when we're in
4724 * tunnel mode and we want to forward till the close. It's used both to forward
4725 * remaining data and to resync after end of body. It expects the msg_state to
4726 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4727 * read more data, or 1 once we can go on with next request or end the session.
4728 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4729 * bytes of pending data + the headers if not already done (between som and sov).
4730 * It eventually adjusts som to match sov after the data in between have been sent.
4731 */
4732int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4733{
4734 struct http_txn *txn = &s->txn;
4735 struct http_msg *msg = &s->txn.rsp;
4736
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004737 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4738 return 0;
4739
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004740 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004741 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004742 !s->req->analysers) {
4743 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4744 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004745 buffer_auto_read(res);
4746 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004747 res->analysers &= ~an_bit;
4748 return 1;
4749 }
4750
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004751 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004752
Willy Tarreaud98cf932009-12-27 22:54:55 +01004753 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4754 /* we have msg->col and msg->sov which both point to the first
4755 * byte of message body. msg->som still points to the beginning
4756 * of the message. We must save the body in req->lr because it
4757 * survives buffer re-alignments.
4758 */
4759 res->lr = res->data + msg->sov;
4760 if (txn->flags & TX_RES_TE_CHNK)
4761 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4762 else {
4763 msg->msg_state = HTTP_MSG_DATA;
4764 }
4765 }
4766
Willy Tarreaud98cf932009-12-27 22:54:55 +01004767 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004768 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004769 /* we may have some data pending */
4770 if (msg->hdr_content_len || msg->som != msg->sov) {
4771 int bytes = msg->sov - msg->som;
4772 if (bytes < 0) /* sov may have wrapped at the end */
4773 bytes += res->size;
4774 buffer_forward(res, bytes + msg->hdr_content_len);
4775 msg->hdr_content_len = 0; /* don't forward that again */
4776 msg->som = msg->sov;
4777 }
4778
Willy Tarreaucaabe412010-01-03 23:08:28 +01004779 if (msg->msg_state == HTTP_MSG_DATA) {
4780 /* must still forward */
4781 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004782 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004783
4784 /* nothing left to forward */
4785 if (txn->flags & TX_RES_TE_CHNK)
4786 msg->msg_state = HTTP_MSG_DATA_CRLF;
4787 else
4788 msg->msg_state = HTTP_MSG_DONE;
4789 }
4790 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004791 /* read the chunk size and assign it to ->hdr_content_len, then
4792 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4793 */
4794 int ret = http_parse_chunk_size(res, msg);
4795
4796 if (!ret)
4797 goto missing_data;
4798 else if (ret < 0)
4799 goto return_bad_res;
4800 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004801 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004802 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4803 /* we want the CRLF after the data */
4804 int ret;
4805
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004806 res->lr = res->w + res->send_max;
4807 if (res->lr >= res->data + res->size)
4808 res->lr -= res->size;
4809
Willy Tarreaud98cf932009-12-27 22:54:55 +01004810 ret = http_skip_chunk_crlf(res, msg);
4811
4812 if (!ret)
4813 goto missing_data;
4814 else if (ret < 0)
4815 goto return_bad_res;
4816 /* we're in MSG_CHUNK_SIZE now */
4817 }
4818 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4819 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004820
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821 if (ret == 0)
4822 goto missing_data;
4823 else if (ret < 0)
4824 goto return_bad_res;
4825 /* we're in HTTP_MSG_DONE now */
4826 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004827 else {
4828 /* other states, DONE...TUNNEL */
4829 if (http_resync_states(s)) {
4830 http_silent_debug(__LINE__, s);
4831 /* some state changes occurred, maybe the analyser
4832 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004833 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004834 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4835 goto return_bad_res;
4836 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004837 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004839 }
4840 }
4841
Willy Tarreaud98cf932009-12-27 22:54:55 +01004842 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004843 /* stop waiting for data if the input is closed before the end */
4844 if (res->flags & BF_SHUTR)
4845 goto return_bad_res;
4846
Willy Tarreau610ecce2010-01-04 21:15:02 +01004847 if (!s->req->analysers)
4848 goto return_bad_res;
4849
Willy Tarreaud98cf932009-12-27 22:54:55 +01004850 /* forward the chunk size as well as any pending data */
4851 if (msg->hdr_content_len || msg->som != msg->sov) {
4852 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4853 msg->hdr_content_len = 0; /* don't forward that again */
4854 msg->som = msg->sov;
4855 }
4856
Willy Tarreaud98cf932009-12-27 22:54:55 +01004857 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004858 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004859 return 0;
4860
4861 return_bad_res: /* let's centralize all bad resuests */
4862 txn->rsp.msg_state = HTTP_MSG_ERROR;
4863 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01004864 /* don't send any error message as we're in the body */
4865 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866 res->analysers = 0;
4867 s->be->counters.failed_resp++;
4868 if (s->srv) {
4869 s->srv->counters.failed_resp++;
4870 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4871 }
4872
4873 if (!(s->flags & SN_ERR_MASK))
4874 s->flags |= SN_ERR_PRXCOND;
4875 if (!(s->flags & SN_FINST_MASK))
4876 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004877 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004878 return 0;
4879}
4880
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004881/* Iterate the same filter through all request headers.
4882 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004883 * Since it can manage the switch to another backend, it updates the per-proxy
4884 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004885 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004886int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004887{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004888 char term;
4889 char *cur_ptr, *cur_end, *cur_next;
4890 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004891 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004892 struct hdr_idx_elem *cur_hdr;
4893 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004894
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 last_hdr = 0;
4896
Willy Tarreau962c3f42010-01-10 00:15:35 +01004897 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004898 old_idx = 0;
4899
4900 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004901 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004902 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004903 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004904 (exp->action == ACT_ALLOW ||
4905 exp->action == ACT_DENY ||
4906 exp->action == ACT_TARPIT))
4907 return 0;
4908
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004909 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004910 if (!cur_idx)
4911 break;
4912
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004913 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004914 cur_ptr = cur_next;
4915 cur_end = cur_ptr + cur_hdr->len;
4916 cur_next = cur_end + cur_hdr->cr + 1;
4917
4918 /* Now we have one header between cur_ptr and cur_end,
4919 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004920 */
4921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004922 /* The annoying part is that pattern matching needs
4923 * that we modify the contents to null-terminate all
4924 * strings before testing them.
4925 */
4926
4927 term = *cur_end;
4928 *cur_end = '\0';
4929
4930 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4931 switch (exp->action) {
4932 case ACT_SETBE:
4933 /* It is not possible to jump a second time.
4934 * FIXME: should we return an HTTP/500 here so that
4935 * the admin knows there's a problem ?
4936 */
4937 if (t->be != t->fe)
4938 break;
4939
4940 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004941 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004942 last_hdr = 1;
4943 break;
4944
4945 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004946 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004947 last_hdr = 1;
4948 break;
4949
4950 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004951 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004952 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004953
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004954 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004955 if (t->listener->counters)
4956 t->listener->counters->denied_resp++;
4957
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004958 break;
4959
4960 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004961 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004962 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004963
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004964 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004965 if (t->listener->counters)
4966 t->listener->counters->denied_resp++;
4967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004968 break;
4969
4970 case ACT_REPLACE:
4971 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4972 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4973 /* FIXME: if the user adds a newline in the replacement, the
4974 * index will not be recalculated for now, and the new line
4975 * will not be counted as a new header.
4976 */
4977
4978 cur_end += delta;
4979 cur_next += delta;
4980 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004981 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004982 break;
4983
4984 case ACT_REMOVE:
4985 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4986 cur_next += delta;
4987
Willy Tarreaufa355d42009-11-29 18:12:29 +01004988 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004989 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4990 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004991 cur_hdr->len = 0;
4992 cur_end = NULL; /* null-term has been rewritten */
4993 break;
4994
4995 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004996 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004997 if (cur_end)
4998 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004999
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005000 /* keep the link from this header to next one in case of later
5001 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005002 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005003 old_idx = cur_idx;
5004 }
5005 return 0;
5006}
5007
5008
5009/* Apply the filter to the request line.
5010 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5011 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005012 * Since it can manage the switch to another backend, it updates the per-proxy
5013 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 */
5015int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5016{
5017 char term;
5018 char *cur_ptr, *cur_end;
5019 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005020 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005021 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005022
Willy Tarreau58f10d72006-12-04 02:26:12 +01005023
Willy Tarreau3d300592007-03-18 18:34:41 +01005024 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005025 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005026 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005027 (exp->action == ACT_ALLOW ||
5028 exp->action == ACT_DENY ||
5029 exp->action == ACT_TARPIT))
5030 return 0;
5031 else if (exp->action == ACT_REMOVE)
5032 return 0;
5033
5034 done = 0;
5035
Willy Tarreau962c3f42010-01-10 00:15:35 +01005036 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005037 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005038
5039 /* Now we have the request line between cur_ptr and cur_end */
5040
5041 /* The annoying part is that pattern matching needs
5042 * that we modify the contents to null-terminate all
5043 * strings before testing them.
5044 */
5045
5046 term = *cur_end;
5047 *cur_end = '\0';
5048
5049 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5050 switch (exp->action) {
5051 case ACT_SETBE:
5052 /* It is not possible to jump a second time.
5053 * FIXME: should we return an HTTP/500 here so that
5054 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005055 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005056 if (t->be != t->fe)
5057 break;
5058
5059 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005060 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005061 done = 1;
5062 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005064 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005065 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005066 done = 1;
5067 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005069 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005070 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005071
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005072 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005073 if (t->listener->counters)
5074 t->listener->counters->denied_resp++;
5075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005076 done = 1;
5077 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005079 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005080 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005081
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005082 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005083 if (t->listener->counters)
5084 t->listener->counters->denied_resp++;
5085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005086 done = 1;
5087 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005089 case ACT_REPLACE:
5090 *cur_end = term; /* restore the string terminator */
5091 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5092 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5093 /* FIXME: if the user adds a newline in the replacement, the
5094 * index will not be recalculated for now, and the new line
5095 * will not be counted as a new header.
5096 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005097
Willy Tarreaufa355d42009-11-29 18:12:29 +01005098 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005099 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005100 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005101 HTTP_MSG_RQMETH,
5102 cur_ptr, cur_end + 1,
5103 NULL, NULL);
5104 if (unlikely(!cur_end))
5105 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005107 /* we have a full request and we know that we have either a CR
5108 * or an LF at <ptr>.
5109 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005110 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5111 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005112 /* there is no point trying this regex on headers */
5113 return 1;
5114 }
5115 }
5116 *cur_end = term; /* restore the string terminator */
5117 return done;
5118}
Willy Tarreau97de6242006-12-27 17:18:38 +01005119
Willy Tarreau58f10d72006-12-04 02:26:12 +01005120
Willy Tarreau58f10d72006-12-04 02:26:12 +01005121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005122/*
5123 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5124 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005125 * unparsable request. Since it can manage the switch to another backend, it
5126 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005127 */
5128int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5129{
Willy Tarreau3d300592007-03-18 18:34:41 +01005130 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005131 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005132 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005133 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005135 /*
5136 * The interleaving of transformations and verdicts
5137 * makes it difficult to decide to continue or stop
5138 * the evaluation.
5139 */
5140
Willy Tarreau3d300592007-03-18 18:34:41 +01005141 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005142 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5143 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5144 exp = exp->next;
5145 continue;
5146 }
5147
5148 /* Apply the filter to the request line. */
5149 ret = apply_filter_to_req_line(t, req, exp);
5150 if (unlikely(ret < 0))
5151 return -1;
5152
5153 if (likely(ret == 0)) {
5154 /* The filter did not match the request, it can be
5155 * iterated through all headers.
5156 */
5157 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005158 }
5159 exp = exp->next;
5160 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005161 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005162}
5163
5164
Willy Tarreaua15645d2007-03-18 16:22:39 +01005165
Willy Tarreau58f10d72006-12-04 02:26:12 +01005166/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005167 * Try to retrieve the server associated to the appsession.
5168 * If the server is found, it's assigned to the session.
5169 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005170void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005171 struct http_txn *txn = &t->txn;
5172 appsess *asession = NULL;
5173 char *sessid_temp = NULL;
5174
Cyril Bontéb21570a2009-11-29 20:04:48 +01005175 if (len > t->be->appsession_len) {
5176 len = t->be->appsession_len;
5177 }
5178
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005179 if (t->be->options2 & PR_O2_AS_REQL) {
5180 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005181 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005182 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005183 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005184 }
5185
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005186 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005187 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5188 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5189 return;
5190 }
5191
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005192 memcpy(txn->sessid, buf, len);
5193 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005194 }
5195
5196 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5197 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5198 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5199 return;
5200 }
5201
Cyril Bontéb21570a2009-11-29 20:04:48 +01005202 memcpy(sessid_temp, buf, len);
5203 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005204
5205 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5206 /* free previously allocated memory */
5207 pool_free2(apools.sessid, sessid_temp);
5208
5209 if (asession != NULL) {
5210 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5211 if (!(t->be->options2 & PR_O2_AS_REQL))
5212 asession->request_count++;
5213
5214 if (asession->serverid != NULL) {
5215 struct server *srv = t->be->srv;
5216 while (srv) {
5217 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005218 if ((srv->state & SRV_RUNNING) ||
5219 (t->be->options & PR_O_PERSIST) ||
5220 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005221 /* we found the server and it's usable */
5222 txn->flags &= ~TX_CK_MASK;
5223 txn->flags |= TX_CK_VALID;
5224 t->flags |= SN_DIRECT | SN_ASSIGNED;
5225 t->srv = srv;
5226 break;
5227 } else {
5228 txn->flags &= ~TX_CK_MASK;
5229 txn->flags |= TX_CK_DOWN;
5230 }
5231 }
5232 srv = srv->next;
5233 }
5234 }
5235 }
5236}
5237
5238/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005239 * Manage client-side cookie. It can impact performance by about 2% so it is
5240 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005241 */
5242void manage_client_side_cookies(struct session *t, struct buffer *req)
5243{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005244 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005245 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246 char *del_colon, *del_cookie, *colon;
5247 int app_cookies;
5248
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005250 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005251
Willy Tarreau2a324282006-12-05 00:05:46 +01005252 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005253 * we start with the start line.
5254 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005255 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005256 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005257
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005258 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005259 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005260 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005261
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005262 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005263 cur_ptr = cur_next;
5264 cur_end = cur_ptr + cur_hdr->len;
5265 cur_next = cur_end + cur_hdr->cr + 1;
5266
5267 /* We have one full header between cur_ptr and cur_end, and the
5268 * next header starts at cur_next. We're only interested in
5269 * "Cookie:" headers.
5270 */
5271
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005272 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5273 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005274 old_idx = cur_idx;
5275 continue;
5276 }
5277
5278 /* Now look for cookies. Conforming to RFC2109, we have to support
5279 * attributes whose name begin with a '$', and associate them with
5280 * the right cookie, if we want to delete this cookie.
5281 * So there are 3 cases for each cookie read :
5282 * 1) it's a special attribute, beginning with a '$' : ignore it.
5283 * 2) it's a server id cookie that we *MAY* want to delete : save
5284 * some pointers on it (last semi-colon, beginning of cookie...)
5285 * 3) it's an application cookie : we *MAY* have to delete a previous
5286 * "special" cookie.
5287 * At the end of loop, if a "special" cookie remains, we may have to
5288 * remove it. If no application cookie persists in the header, we
5289 * *MUST* delete it
5290 */
5291
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005292 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005293
Willy Tarreau58f10d72006-12-04 02:26:12 +01005294 /* del_cookie == NULL => nothing to be deleted */
5295 del_colon = del_cookie = NULL;
5296 app_cookies = 0;
5297
5298 while (p1 < cur_end) {
5299 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005300 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005301 while (p1 < cur_end) {
5302 if (*p1 == ';' || *p1 == ',')
5303 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005304 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005305 break;
5306 p1++;
5307 }
5308
5309 if (p1 == cur_end)
5310 break;
5311
5312 /* p1 is at the beginning of the cookie name */
5313 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005314 while (p2 < cur_end && *p2 != '=') {
5315 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5316 /* oops, the cookie name was truncated, resync */
5317 p1 = p2;
5318 goto resync_name;
5319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005320 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005321 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005322
5323 if (p2 == cur_end)
5324 break;
5325
5326 p3 = p2 + 1; /* skips the '=' sign */
5327 if (p3 == cur_end)
5328 break;
5329
Willy Tarreau305ae852010-01-03 19:45:54 +01005330 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5331 p5 = p4 = p3;
5332 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5333 if (!isspace((unsigned char)*p5))
5334 p4 = p5 + 1;
5335 p5++;
5336 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005337
5338 /* here, we have the cookie name between p1 and p2,
5339 * and its value between p3 and p4.
5340 * we can process it :
5341 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005342 * Cookie: NAME=VALUE ;
5343 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005344 * | || || +--> p4
5345 * | || |+-------> p3
5346 * | || +--------> p2
5347 * | |+------------> p1
5348 * | +-------------> colon
5349 * +--------------------> cur_ptr
5350 */
5351
5352 if (*p1 == '$') {
5353 /* skip this one */
5354 }
5355 else {
5356 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005357 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005358 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005359 (p4 - p1 >= t->fe->capture_namelen) &&
5360 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005361 int log_len = p4 - p1;
5362
Willy Tarreau086b3b42007-05-13 21:45:51 +02005363 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005364 Alert("HTTP logging : out of memory.\n");
5365 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005366 if (log_len > t->fe->capture_len)
5367 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005368 memcpy(txn->cli_cookie, p1, log_len);
5369 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005370 }
5371 }
5372
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005373 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5374 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005375 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005376 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005377 char *delim;
5378
5379 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5380 * have the server ID betweek p3 and delim, and the original cookie between
5381 * delim+1 and p4. Otherwise, delim==p4 :
5382 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005383 * Cookie: NAME=SRV~VALUE ;
5384 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005385 * | || || | +--> p4
5386 * | || || +--------> delim
5387 * | || |+-----------> p3
5388 * | || +------------> p2
5389 * | |+----------------> p1
5390 * | +-----------------> colon
5391 * +------------------------> cur_ptr
5392 */
5393
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005394 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005395 for (delim = p3; delim < p4; delim++)
5396 if (*delim == COOKIE_DELIM)
5397 break;
5398 }
5399 else
5400 delim = p4;
5401
5402
5403 /* Here, we'll look for the first running server which supports the cookie.
5404 * This allows to share a same cookie between several servers, for example
5405 * to dedicate backup servers to specific servers only.
5406 * However, to prevent clients from sticking to cookie-less backup server
5407 * when they have incidentely learned an empty cookie, we simply ignore
5408 * empty cookies and mark them as invalid.
5409 */
5410 if (delim == p3)
5411 srv = NULL;
5412
5413 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005414 if (srv->cookie && (srv->cklen == delim - p3) &&
5415 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005416 if ((srv->state & SRV_RUNNING) ||
5417 (t->be->options & PR_O_PERSIST) ||
5418 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005419 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005420 txn->flags &= ~TX_CK_MASK;
5421 txn->flags |= TX_CK_VALID;
5422 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005423 t->srv = srv;
5424 break;
5425 } else {
5426 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005427 txn->flags &= ~TX_CK_MASK;
5428 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005429 }
5430 }
5431 srv = srv->next;
5432 }
5433
Willy Tarreau3d300592007-03-18 18:34:41 +01005434 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005435 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005436 txn->flags &= ~TX_CK_MASK;
5437 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005438 }
5439
5440 /* depending on the cookie mode, we may have to either :
5441 * - delete the complete cookie if we're in insert+indirect mode, so that
5442 * the server never sees it ;
5443 * - remove the server id from the cookie value, and tag the cookie as an
5444 * application cookie so that it does not get accidentely removed later,
5445 * if we're in cookie prefix mode
5446 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005447 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005448 int delta; /* negative */
5449
5450 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5451 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005452 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005453 cur_end += delta;
5454 cur_next += delta;
5455 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005456 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005457
5458 del_cookie = del_colon = NULL;
5459 app_cookies++; /* protect the header from deletion */
5460 }
5461 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005462 (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 +01005463 del_cookie = p1;
5464 del_colon = colon;
5465 }
5466 } else {
5467 /* now we know that we must keep this cookie since it's
5468 * not ours. But if we wanted to delete our cookie
5469 * earlier, we cannot remove the complete header, but we
5470 * can remove the previous block itself.
5471 */
5472 app_cookies++;
5473
5474 if (del_cookie != NULL) {
5475 int delta; /* negative */
5476
5477 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5478 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005479 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005480 cur_end += delta;
5481 cur_next += delta;
5482 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005483 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005484 del_cookie = del_colon = NULL;
5485 }
5486 }
5487
Cyril Bontéb21570a2009-11-29 20:04:48 +01005488 if (t->be->appsession_name != NULL) {
5489 int cmp_len, value_len;
5490 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005491
Cyril Bontéb21570a2009-11-29 20:04:48 +01005492 if (t->be->options2 & PR_O2_AS_PFX) {
5493 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5494 value_begin = p1 + t->be->appsession_name_len;
5495 value_len = p4 - p1 - t->be->appsession_name_len;
5496 } else {
5497 cmp_len = p2 - p1;
5498 value_begin = p3;
5499 value_len = p4 - p3;
5500 }
5501
5502 /* let's see if the cookie is our appcookie */
5503 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5504 /* Cool... it's the right one */
5505 manage_client_side_appsession(t, value_begin, value_len);
5506 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005507#if defined(DEBUG_HASH)
5508 Alert("manage_client_side_cookies\n");
5509 appsession_hash_dump(&(t->be->htbl_proxy));
5510#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005511 }/* end if ((t->proxy->appsession_name != NULL) ... */
5512 }
5513
5514 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005515 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005516 } /* while (p1 < cur_end) */
5517
5518 /* There's no more cookie on this line.
5519 * We may have marked the last one(s) for deletion.
5520 * We must do this now in two ways :
5521 * - if there is no app cookie, we simply delete the header ;
5522 * - if there are app cookies, we must delete the end of the
5523 * string properly, including the colon/semi-colon before
5524 * the cookie name.
5525 */
5526 if (del_cookie != NULL) {
5527 int delta;
5528 if (app_cookies) {
5529 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5530 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005531 cur_hdr->len += delta;
5532 } else {
5533 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005534
5535 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005536 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5537 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005538 cur_hdr->len = 0;
5539 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005540 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005541 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005542 }
5543
5544 /* keep the link from this header to next one */
5545 old_idx = cur_idx;
5546 } /* end of cookie processing on this header */
5547}
5548
5549
Willy Tarreaua15645d2007-03-18 16:22:39 +01005550/* Iterate the same filter through all response headers contained in <rtr>.
5551 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5552 */
5553int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5554{
5555 char term;
5556 char *cur_ptr, *cur_end, *cur_next;
5557 int cur_idx, old_idx, last_hdr;
5558 struct http_txn *txn = &t->txn;
5559 struct hdr_idx_elem *cur_hdr;
5560 int len, delta;
5561
5562 last_hdr = 0;
5563
Willy Tarreau962c3f42010-01-10 00:15:35 +01005564 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005565 old_idx = 0;
5566
5567 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005568 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005569 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005570 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005571 (exp->action == ACT_ALLOW ||
5572 exp->action == ACT_DENY))
5573 return 0;
5574
5575 cur_idx = txn->hdr_idx.v[old_idx].next;
5576 if (!cur_idx)
5577 break;
5578
5579 cur_hdr = &txn->hdr_idx.v[cur_idx];
5580 cur_ptr = cur_next;
5581 cur_end = cur_ptr + cur_hdr->len;
5582 cur_next = cur_end + cur_hdr->cr + 1;
5583
5584 /* Now we have one header between cur_ptr and cur_end,
5585 * and the next header starts at cur_next.
5586 */
5587
5588 /* The annoying part is that pattern matching needs
5589 * that we modify the contents to null-terminate all
5590 * strings before testing them.
5591 */
5592
5593 term = *cur_end;
5594 *cur_end = '\0';
5595
5596 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5597 switch (exp->action) {
5598 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005599 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005600 last_hdr = 1;
5601 break;
5602
5603 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005604 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005605 last_hdr = 1;
5606 break;
5607
5608 case ACT_REPLACE:
5609 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5610 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5611 /* FIXME: if the user adds a newline in the replacement, the
5612 * index will not be recalculated for now, and the new line
5613 * will not be counted as a new header.
5614 */
5615
5616 cur_end += delta;
5617 cur_next += delta;
5618 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005619 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005620 break;
5621
5622 case ACT_REMOVE:
5623 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5624 cur_next += delta;
5625
Willy Tarreaufa355d42009-11-29 18:12:29 +01005626 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005627 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5628 txn->hdr_idx.used--;
5629 cur_hdr->len = 0;
5630 cur_end = NULL; /* null-term has been rewritten */
5631 break;
5632
5633 }
5634 }
5635 if (cur_end)
5636 *cur_end = term; /* restore the string terminator */
5637
5638 /* keep the link from this header to next one in case of later
5639 * removal of next header.
5640 */
5641 old_idx = cur_idx;
5642 }
5643 return 0;
5644}
5645
5646
5647/* Apply the filter to the status line in the response buffer <rtr>.
5648 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5649 * or -1 if a replacement resulted in an invalid status line.
5650 */
5651int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5652{
5653 char term;
5654 char *cur_ptr, *cur_end;
5655 int done;
5656 struct http_txn *txn = &t->txn;
5657 int len, delta;
5658
5659
Willy Tarreau3d300592007-03-18 18:34:41 +01005660 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005661 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005662 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005663 (exp->action == ACT_ALLOW ||
5664 exp->action == ACT_DENY))
5665 return 0;
5666 else if (exp->action == ACT_REMOVE)
5667 return 0;
5668
5669 done = 0;
5670
Willy Tarreau962c3f42010-01-10 00:15:35 +01005671 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005672 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5673
5674 /* Now we have the status line between cur_ptr and cur_end */
5675
5676 /* The annoying part is that pattern matching needs
5677 * that we modify the contents to null-terminate all
5678 * strings before testing them.
5679 */
5680
5681 term = *cur_end;
5682 *cur_end = '\0';
5683
5684 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5685 switch (exp->action) {
5686 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005687 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005688 done = 1;
5689 break;
5690
5691 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005692 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005693 done = 1;
5694 break;
5695
5696 case ACT_REPLACE:
5697 *cur_end = term; /* restore the string terminator */
5698 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5699 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5700 /* FIXME: if the user adds a newline in the replacement, the
5701 * index will not be recalculated for now, and the new line
5702 * will not be counted as a new header.
5703 */
5704
Willy Tarreaufa355d42009-11-29 18:12:29 +01005705 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005706 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005707 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005708 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005709 cur_ptr, cur_end + 1,
5710 NULL, NULL);
5711 if (unlikely(!cur_end))
5712 return -1;
5713
5714 /* we have a full respnse and we know that we have either a CR
5715 * or an LF at <ptr>.
5716 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005717 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005718 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5719 /* there is no point trying this regex on headers */
5720 return 1;
5721 }
5722 }
5723 *cur_end = term; /* restore the string terminator */
5724 return done;
5725}
5726
5727
5728
5729/*
5730 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5731 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5732 * unparsable response.
5733 */
5734int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5735{
Willy Tarreau3d300592007-03-18 18:34:41 +01005736 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005737 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005738 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005739 int ret;
5740
5741 /*
5742 * The interleaving of transformations and verdicts
5743 * makes it difficult to decide to continue or stop
5744 * the evaluation.
5745 */
5746
Willy Tarreau3d300592007-03-18 18:34:41 +01005747 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005748 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5749 exp->action == ACT_PASS)) {
5750 exp = exp->next;
5751 continue;
5752 }
5753
5754 /* Apply the filter to the status line. */
5755 ret = apply_filter_to_sts_line(t, rtr, exp);
5756 if (unlikely(ret < 0))
5757 return -1;
5758
5759 if (likely(ret == 0)) {
5760 /* The filter did not match the response, it can be
5761 * iterated through all headers.
5762 */
5763 apply_filter_to_resp_headers(t, rtr, exp);
5764 }
5765 exp = exp->next;
5766 }
5767 return 0;
5768}
5769
5770
5771
5772/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005773 * Manage server-side cookies. It can impact performance by about 2% so it is
5774 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005775 */
5776void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5777{
5778 struct http_txn *txn = &t->txn;
5779 char *p1, *p2, *p3, *p4;
5780
Willy Tarreaua15645d2007-03-18 16:22:39 +01005781 char *cur_ptr, *cur_end, *cur_next;
5782 int cur_idx, old_idx, delta;
5783
Willy Tarreaua15645d2007-03-18 16:22:39 +01005784 /* Iterate through the headers.
5785 * we start with the start line.
5786 */
5787 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005788 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005789
5790 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5791 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005792 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005793
5794 cur_hdr = &txn->hdr_idx.v[cur_idx];
5795 cur_ptr = cur_next;
5796 cur_end = cur_ptr + cur_hdr->len;
5797 cur_next = cur_end + cur_hdr->cr + 1;
5798
5799 /* We have one full header between cur_ptr and cur_end, and the
5800 * next header starts at cur_next. We're only interested in
5801 * "Cookie:" headers.
5802 */
5803
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005804 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5805 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005806 old_idx = cur_idx;
5807 continue;
5808 }
5809
5810 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005811 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005812
5813
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005814 /* maybe we only wanted to see if there was a set-cookie. Note that
5815 * the cookie capture is declared in the fronend.
5816 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005817 if (t->be->cookie_name == NULL &&
5818 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005819 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005820 return;
5821
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005822 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005823
5824 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005825 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5826 break;
5827
5828 /* p1 is at the beginning of the cookie name */
5829 p2 = p1;
5830
5831 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5832 p2++;
5833
5834 if (p2 == cur_end || *p2 == ';') /* next cookie */
5835 break;
5836
5837 p3 = p2 + 1; /* skip the '=' sign */
5838 if (p3 == cur_end)
5839 break;
5840
5841 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005842 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005843 p4++;
5844
5845 /* here, we have the cookie name between p1 and p2,
5846 * and its value between p3 and p4.
5847 * we can process it.
5848 */
5849
5850 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005851 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005852 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005853 (p4 - p1 >= t->fe->capture_namelen) &&
5854 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005855 int log_len = p4 - p1;
5856
Willy Tarreau086b3b42007-05-13 21:45:51 +02005857 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005858 Alert("HTTP logging : out of memory.\n");
5859 }
5860
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005861 if (log_len > t->fe->capture_len)
5862 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005863 memcpy(txn->srv_cookie, p1, log_len);
5864 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005865 }
5866
5867 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005868 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5869 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005870 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005871 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005872
5873 /* If the cookie is in insert mode on a known server, we'll delete
5874 * this occurrence because we'll insert another one later.
5875 * We'll delete it too if the "indirect" option is set and we're in
5876 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005877 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5878 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879 /* this header must be deleted */
5880 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5881 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5882 txn->hdr_idx.used--;
5883 cur_hdr->len = 0;
5884 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005885 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005886
Willy Tarreau3d300592007-03-18 18:34:41 +01005887 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005888 }
5889 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005890 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891 /* replace bytes p3->p4 with the cookie name associated
5892 * with this server since we know it.
5893 */
5894 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5895 cur_hdr->len += delta;
5896 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005897 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005898
Willy Tarreau3d300592007-03-18 18:34:41 +01005899 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005900 }
5901 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005902 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005903 /* insert the cookie name associated with this server
5904 * before existing cookie, and insert a delimitor between them..
5905 */
5906 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5907 cur_hdr->len += delta;
5908 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005909 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005910
5911 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005912 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005913 }
5914 }
5915 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005916 else if (t->be->appsession_name != NULL) {
5917 int cmp_len, value_len;
5918 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005919
Cyril Bontéb21570a2009-11-29 20:04:48 +01005920 if (t->be->options2 & PR_O2_AS_PFX) {
5921 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5922 value_begin = p1 + t->be->appsession_name_len;
5923 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5924 } else {
5925 cmp_len = p2 - p1;
5926 value_begin = p3;
5927 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005928 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005929
5930 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5931 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005932 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005933 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005934 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01005935 }
5936 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005937 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005938 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5939 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5940 return;
5941 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005942 memcpy(txn->sessid, value_begin, value_len);
5943 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005945 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005946 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5947 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948 /* keep the link from this header to next one */
5949 old_idx = cur_idx;
5950 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005951
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005952 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005953 appsess *asession = NULL;
5954 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005955 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005956 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01005957 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005958 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5959 Alert("Not enough Memory process_srv():asession:calloc().\n");
5960 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5961 return;
5962 }
5963 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5964 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5965 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005966 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005967 return;
5968 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005969 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005970 asession->sessid[t->be->appsession_len] = 0;
5971
Willy Tarreau1fac7532010-01-09 19:23:06 +01005972 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005973 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5974 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5975 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01005976 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005977 return;
5978 }
5979 asession->serverid[0] = '\0';
5980 memcpy(asession->serverid, t->srv->id, server_id_len);
5981
5982 asession->request_count = 0;
5983 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5984 }
5985
5986 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5987 asession->request_count++;
5988 }
5989
5990#if defined(DEBUG_HASH)
5991 Alert("manage_server_side_cookies\n");
5992 appsession_hash_dump(&(t->be->htbl_proxy));
5993#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005994}
5995
5996
5997
5998/*
5999 * Check if response is cacheable or not. Updates t->flags.
6000 */
6001void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6002{
6003 struct http_txn *txn = &t->txn;
6004 char *p1, *p2;
6005
6006 char *cur_ptr, *cur_end, *cur_next;
6007 int cur_idx;
6008
Willy Tarreau5df51872007-11-25 16:20:08 +01006009 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006010 return;
6011
6012 /* Iterate through the headers.
6013 * we start with the start line.
6014 */
6015 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006016 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006017
6018 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6019 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006020 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006021
6022 cur_hdr = &txn->hdr_idx.v[cur_idx];
6023 cur_ptr = cur_next;
6024 cur_end = cur_ptr + cur_hdr->len;
6025 cur_next = cur_end + cur_hdr->cr + 1;
6026
6027 /* We have one full header between cur_ptr and cur_end, and the
6028 * next header starts at cur_next. We're only interested in
6029 * "Cookie:" headers.
6030 */
6031
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006032 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6033 if (val) {
6034 if ((cur_end - (cur_ptr + val) >= 8) &&
6035 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6036 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6037 return;
6038 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006039 }
6040
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006041 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6042 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006043 continue;
6044
6045 /* OK, right now we know we have a cache-control header at cur_ptr */
6046
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006047 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006048
6049 if (p1 >= cur_end) /* no more info */
6050 continue;
6051
6052 /* p1 is at the beginning of the value */
6053 p2 = p1;
6054
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006055 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056 p2++;
6057
6058 /* we have a complete value between p1 and p2 */
6059 if (p2 < cur_end && *p2 == '=') {
6060 /* we have something of the form no-cache="set-cookie" */
6061 if ((cur_end - p1 >= 21) &&
6062 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6063 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006064 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006065 continue;
6066 }
6067
6068 /* OK, so we know that either p2 points to the end of string or to a comma */
6069 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6070 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6071 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6072 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006073 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006074 return;
6075 }
6076
6077 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006078 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006079 continue;
6080 }
6081 }
6082}
6083
6084
Willy Tarreau58f10d72006-12-04 02:26:12 +01006085/*
6086 * Try to retrieve a known appsession in the URI, then the associated server.
6087 * If the server is found, it's assigned to the session.
6088 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006091 char *end_params, *first_param, *cur_param, *next_param;
6092 char separator;
6093 int value_len;
6094
6095 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006096
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006097 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006098 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006099 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006100 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006101
Cyril Bontéb21570a2009-11-29 20:04:48 +01006102 first_param = NULL;
6103 switch (mode) {
6104 case PR_O2_AS_M_PP:
6105 first_param = memchr(begin, ';', len);
6106 break;
6107 case PR_O2_AS_M_QS:
6108 first_param = memchr(begin, '?', len);
6109 break;
6110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111
Cyril Bontéb21570a2009-11-29 20:04:48 +01006112 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115
Cyril Bontéb21570a2009-11-29 20:04:48 +01006116 switch (mode) {
6117 case PR_O2_AS_M_PP:
6118 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6119 end_params = (char *) begin + len;
6120 }
6121 separator = ';';
6122 break;
6123 case PR_O2_AS_M_QS:
6124 end_params = (char *) begin + len;
6125 separator = '&';
6126 break;
6127 default:
6128 /* unknown mode, shouldn't happen */
6129 return;
6130 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006131
Cyril Bontéb21570a2009-11-29 20:04:48 +01006132 cur_param = next_param = end_params;
6133 while (cur_param > first_param) {
6134 cur_param--;
6135 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6136 /* let's see if this is the appsession parameter */
6137 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6138 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6139 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6140 /* Cool... it's the right one */
6141 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6142 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6143 if (value_len > 0) {
6144 manage_client_side_appsession(t, cur_param, value_len);
6145 }
6146 break;
6147 }
6148 next_param = cur_param;
6149 }
6150 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006151#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006152 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006153 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006154#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006155}
6156
6157
Willy Tarreaub2513902006-12-17 14:52:38 +01006158/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006159 * In a GET or HEAD request, check if the requested URI matches the stats uri
6160 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006161 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006162 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006163 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006164 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006165 *
6166 * Returns 1 if the session's state changes, otherwise 0.
6167 */
6168int stats_check_uri_auth(struct session *t, struct proxy *backend)
6169{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006170 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006171 struct uri_auth *uri_auth = backend->uri_auth;
6172 struct user_auth *user;
6173 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006174 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006175
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006176 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006178 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006179 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006180 return 0;
6181
Willy Tarreau962c3f42010-01-10 00:15:35 +01006182 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006183
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006184 /* the URI is in h */
6185 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006186 return 0;
6187
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006188 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006189 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006190 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006191 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006192 break;
6193 }
6194 h++;
6195 }
6196
6197 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006198 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6199 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006200 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006201 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006202 break;
6203 }
6204 h++;
6205 }
6206 }
6207
Willy Tarreau962c3f42010-01-10 00:15:35 +01006208 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6209 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006210 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006211 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006212 break;
6213 }
6214 h++;
6215 }
6216
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006217 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6218
Willy Tarreaub2513902006-12-17 14:52:38 +01006219 /* we are in front of a interceptable URI. Let's check
6220 * if there's an authentication and if it's valid.
6221 */
6222 user = uri_auth->users;
6223 if (!user) {
6224 /* no user auth required, it's OK */
6225 authenticated = 1;
6226 } else {
6227 authenticated = 0;
6228
6229 /* a user list is defined, we have to check.
6230 * skip 21 chars for "Authorization: Basic ".
6231 */
6232
6233 /* FIXME: this should move to an earlier place */
6234 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006235 h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006236 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6237 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006238 if (len > 14 &&
6239 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006240 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006241 break;
6242 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006243 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006244 }
6245
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006246 if (txn->auth_hdr.len < 21 ||
6247 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006248 user = NULL;
6249
6250 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006251 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6252 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006253 user->user_pwd, user->user_len)) {
6254 authenticated = 1;
6255 break;
6256 }
6257 user = user->next;
6258 }
6259 }
6260
6261 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006262 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006263
6264 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006265 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6266 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006267 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006268 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006269 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006270 if (!(t->flags & SN_ERR_MASK))
6271 t->flags |= SN_ERR_PRXCOND;
6272 if (!(t->flags & SN_FINST_MASK))
6273 t->flags |= SN_FINST_R;
6274 return 1;
6275 }
6276
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006277 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006278 * data.
6279 */
Willy Tarreau70089872008-06-13 21:12:51 +02006280 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006281 t->data_source = DATA_SRC_STATS;
6282 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006283 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006284 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6285 t->rep->prod->private = t;
6286 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006287 return 1;
6288}
6289
Willy Tarreau4076a152009-04-02 15:18:36 +02006290/*
6291 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006292 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6293 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006294 */
6295void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6296 struct buffer *buf, struct http_msg *msg,
6297 struct proxy *other_end)
6298{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006299 es->len = buf->r - (buf->data + msg->som);
6300 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006301 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006302 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006303 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006304 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006305 es->when = date; // user-visible date
6306 es->sid = s->uniq_id;
6307 es->srv = s->srv;
6308 es->oe = other_end;
6309 es->src = s->cli_addr;
6310}
Willy Tarreaub2513902006-12-17 14:52:38 +01006311
Willy Tarreaubaaee002006-06-26 02:48:02 +02006312/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006313 * Print a debug line with a header
6314 */
6315void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6316{
6317 int len, max;
6318 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006319 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320 max = end - start;
6321 UBOUND(max, sizeof(trash) - len - 1);
6322 len += strlcpy2(trash + len, start, max + 1);
6323 trash[len++] = '\n';
6324 write(1, trash, len);
6325}
6326
Willy Tarreau0937bc42009-12-22 15:03:09 +01006327/*
6328 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6329 * the required fields are properly allocated and that we only need to (re)init
6330 * them. This should be used before processing any new request.
6331 */
6332void http_init_txn(struct session *s)
6333{
6334 struct http_txn *txn = &s->txn;
6335 struct proxy *fe = s->fe;
6336
6337 txn->flags = 0;
6338 txn->status = -1;
6339
6340 txn->req.sol = txn->req.eol = NULL;
6341 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6342 txn->rsp.sol = txn->rsp.eol = NULL;
6343 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6344 txn->req.hdr_content_len = 0LL;
6345 txn->rsp.hdr_content_len = 0LL;
6346 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6347 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6348 chunk_reset(&txn->auth_hdr);
6349
6350 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6351 if (fe->options2 & PR_O2_REQBUG_OK)
6352 txn->req.err_pos = -1; /* let buggy requests pass */
6353
Willy Tarreau46023632010-01-07 22:51:47 +01006354 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006355 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6356
Willy Tarreau46023632010-01-07 22:51:47 +01006357 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006358 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6359
6360 if (txn->hdr_idx.v)
6361 hdr_idx_init(&txn->hdr_idx);
6362}
6363
6364/* to be used at the end of a transaction */
6365void http_end_txn(struct session *s)
6366{
6367 struct http_txn *txn = &s->txn;
6368
6369 /* these ones will have been dynamically allocated */
6370 pool_free2(pool2_requri, txn->uri);
6371 pool_free2(pool2_capture, txn->cli_cookie);
6372 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006373 pool_free2(apools.sessid, txn->sessid);
6374 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006375 txn->uri = NULL;
6376 txn->srv_cookie = NULL;
6377 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006378
6379 if (txn->req.cap) {
6380 struct cap_hdr *h;
6381 for (h = s->fe->req_cap; h; h = h->next)
6382 pool_free2(h->pool, txn->req.cap[h->index]);
6383 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6384 }
6385
6386 if (txn->rsp.cap) {
6387 struct cap_hdr *h;
6388 for (h = s->fe->rsp_cap; h; h = h->next)
6389 pool_free2(h->pool, txn->rsp.cap[h->index]);
6390 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6391 }
6392
Willy Tarreau0937bc42009-12-22 15:03:09 +01006393}
6394
6395/* to be used at the end of a transaction to prepare a new one */
6396void http_reset_txn(struct session *s)
6397{
6398 http_end_txn(s);
6399 http_init_txn(s);
6400
6401 s->be = s->fe;
6402 s->req->analysers = s->listener->analysers;
6403 s->logs.logwait = s->fe->to_log;
6404 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006405 /* re-init store persistence */
6406 s->store_count = 0;
6407
Willy Tarreau0937bc42009-12-22 15:03:09 +01006408 s->pend_pos = NULL;
6409 s->conn_retries = s->be->conn_retries;
6410
6411 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6412
6413 s->req->rto = s->fe->timeout.client;
6414 s->req->wto = s->be->timeout.server;
6415 s->req->cto = s->be->timeout.connect;
6416
6417 s->rep->rto = s->be->timeout.server;
6418 s->rep->wto = s->fe->timeout.client;
6419 s->rep->cto = TICK_ETERNITY;
6420
6421 s->req->rex = TICK_ETERNITY;
6422 s->req->wex = TICK_ETERNITY;
6423 s->req->analyse_exp = TICK_ETERNITY;
6424 s->rep->rex = TICK_ETERNITY;
6425 s->rep->wex = TICK_ETERNITY;
6426 s->rep->analyse_exp = TICK_ETERNITY;
6427}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006428
Willy Tarreau8797c062007-05-07 00:55:35 +02006429/************************************************************************/
6430/* The code below is dedicated to ACL parsing and matching */
6431/************************************************************************/
6432
6433
6434
6435
6436/* 1. Check on METHOD
6437 * We use the pre-parsed method if it is known, and store its number as an
6438 * integer. If it is unknown, we use the pointer and the length.
6439 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006440static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006441{
6442 int len, meth;
6443
Willy Tarreauae8b7962007-06-09 23:10:04 +02006444 len = strlen(*text);
6445 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006446
6447 pattern->val.i = meth;
6448 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006449 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006450 if (!pattern->ptr.str)
6451 return 0;
6452 pattern->len = len;
6453 }
6454 return 1;
6455}
6456
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006457static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006458acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6459 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006460{
6461 int meth;
6462 struct http_txn *txn = l7;
6463
Willy Tarreaub6866442008-07-14 23:54:42 +02006464 if (!txn)
6465 return 0;
6466
Willy Tarreau655dce92009-11-08 13:10:58 +01006467 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006468 return 0;
6469
Willy Tarreau8797c062007-05-07 00:55:35 +02006470 meth = txn->meth;
6471 test->i = meth;
6472 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006473 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6474 /* ensure the indexes are not affected */
6475 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006476 test->len = txn->req.sl.rq.m_l;
6477 test->ptr = txn->req.sol;
6478 }
6479 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6480 return 1;
6481}
6482
6483static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6484{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006485 int icase;
6486
Willy Tarreau8797c062007-05-07 00:55:35 +02006487 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006488 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006489
6490 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006491 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006492
6493 /* Other method, we must compare the strings */
6494 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006495 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006496
6497 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6498 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6499 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006500 return ACL_PAT_FAIL;
6501 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006502}
6503
6504/* 2. Check on Request/Status Version
6505 * We simply compare strings here.
6506 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006507static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006508{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006509 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006510 if (!pattern->ptr.str)
6511 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006512 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006513 return 1;
6514}
6515
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006516static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006517acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6518 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006519{
6520 struct http_txn *txn = l7;
6521 char *ptr;
6522 int len;
6523
Willy Tarreaub6866442008-07-14 23:54:42 +02006524 if (!txn)
6525 return 0;
6526
Willy Tarreau655dce92009-11-08 13:10:58 +01006527 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006528 return 0;
6529
Willy Tarreau8797c062007-05-07 00:55:35 +02006530 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006531 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006532
6533 while ((len-- > 0) && (*ptr++ != '/'));
6534 if (len <= 0)
6535 return 0;
6536
6537 test->ptr = ptr;
6538 test->len = len;
6539
6540 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6541 return 1;
6542}
6543
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006544static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006545acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6546 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006547{
6548 struct http_txn *txn = l7;
6549 char *ptr;
6550 int len;
6551
Willy Tarreaub6866442008-07-14 23:54:42 +02006552 if (!txn)
6553 return 0;
6554
Willy Tarreau655dce92009-11-08 13:10:58 +01006555 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006556 return 0;
6557
Willy Tarreau8797c062007-05-07 00:55:35 +02006558 len = txn->rsp.sl.st.v_l;
6559 ptr = txn->rsp.sol;
6560
6561 while ((len-- > 0) && (*ptr++ != '/'));
6562 if (len <= 0)
6563 return 0;
6564
6565 test->ptr = ptr;
6566 test->len = len;
6567
6568 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6569 return 1;
6570}
6571
6572/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006573static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006574acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6575 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006576{
6577 struct http_txn *txn = l7;
6578 char *ptr;
6579 int len;
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->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006585 return 0;
6586
Willy Tarreau8797c062007-05-07 00:55:35 +02006587 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006588 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006589
6590 test->i = __strl2ui(ptr, len);
6591 test->flags = ACL_TEST_F_VOL_1ST;
6592 return 1;
6593}
6594
6595/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006596static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006597acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6598 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006599{
6600 struct http_txn *txn = l7;
6601
Willy Tarreaub6866442008-07-14 23:54:42 +02006602 if (!txn)
6603 return 0;
6604
Willy Tarreau655dce92009-11-08 13:10:58 +01006605 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006606 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006607
Willy Tarreauc11416f2007-06-17 16:58:38 +02006608 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6609 /* ensure the indexes are not affected */
6610 return 0;
6611
Willy Tarreau8797c062007-05-07 00:55:35 +02006612 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006613 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006614
Willy Tarreauf3d25982007-05-08 22:45:09 +02006615 /* we do not need to set READ_ONLY because the data is in a buffer */
6616 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006617 return 1;
6618}
6619
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006620static int
6621acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6622 struct acl_expr *expr, struct acl_test *test)
6623{
6624 struct http_txn *txn = l7;
6625
Willy Tarreaub6866442008-07-14 23:54:42 +02006626 if (!txn)
6627 return 0;
6628
Willy Tarreau655dce92009-11-08 13:10:58 +01006629 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006630 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006631
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006632 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6633 /* ensure the indexes are not affected */
6634 return 0;
6635
6636 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006637 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 +01006638 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6639 test->i = AF_INET;
6640
6641 /*
6642 * If we are parsing url in frontend space, we prepare backend stage
6643 * to not parse again the same url ! optimization lazyness...
6644 */
6645 if (px->options & PR_O_HTTP_PROXY)
6646 l4->flags |= SN_ADDR_SET;
6647
6648 test->flags = ACL_TEST_F_READ_ONLY;
6649 return 1;
6650}
6651
6652static int
6653acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6654 struct acl_expr *expr, struct acl_test *test)
6655{
6656 struct http_txn *txn = l7;
6657
Willy Tarreaub6866442008-07-14 23:54:42 +02006658 if (!txn)
6659 return 0;
6660
Willy Tarreau655dce92009-11-08 13:10:58 +01006661 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006662 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006663
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006664 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6665 /* ensure the indexes are not affected */
6666 return 0;
6667
6668 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006669 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 +01006670 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6671
6672 if (px->options & PR_O_HTTP_PROXY)
6673 l4->flags |= SN_ADDR_SET;
6674
6675 test->flags = ACL_TEST_F_READ_ONLY;
6676 return 1;
6677}
6678
Willy Tarreauc11416f2007-06-17 16:58:38 +02006679/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6680 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6681 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006682static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006683acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006684 struct acl_expr *expr, struct acl_test *test)
6685{
6686 struct http_txn *txn = l7;
6687 struct hdr_idx *idx = &txn->hdr_idx;
6688 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006689
Willy Tarreaub6866442008-07-14 23:54:42 +02006690 if (!txn)
6691 return 0;
6692
Willy Tarreau33a7e692007-06-10 19:45:56 +02006693 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6694 /* search for header from the beginning */
6695 ctx->idx = 0;
6696
Willy Tarreau33a7e692007-06-10 19:45:56 +02006697 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6698 test->flags |= ACL_TEST_F_FETCH_MORE;
6699 test->flags |= ACL_TEST_F_VOL_HDR;
6700 test->len = ctx->vlen;
6701 test->ptr = (char *)ctx->line + ctx->val;
6702 return 1;
6703 }
6704
6705 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6706 test->flags |= ACL_TEST_F_VOL_HDR;
6707 return 0;
6708}
6709
Willy Tarreau33a7e692007-06-10 19:45:56 +02006710static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006711acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6712 struct acl_expr *expr, struct acl_test *test)
6713{
6714 struct http_txn *txn = l7;
6715
Willy Tarreaub6866442008-07-14 23:54:42 +02006716 if (!txn)
6717 return 0;
6718
Willy Tarreau655dce92009-11-08 13:10:58 +01006719 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006720 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006721
Willy Tarreauc11416f2007-06-17 16:58:38 +02006722 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6723 /* ensure the indexes are not affected */
6724 return 0;
6725
6726 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6727}
6728
6729static int
6730acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6731 struct acl_expr *expr, struct acl_test *test)
6732{
6733 struct http_txn *txn = l7;
6734
Willy Tarreaub6866442008-07-14 23:54:42 +02006735 if (!txn)
6736 return 0;
6737
Willy Tarreau655dce92009-11-08 13:10:58 +01006738 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006739 return 0;
6740
6741 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6742}
6743
6744/* 6. Check on HTTP header count. The number of occurrences is returned.
6745 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6746 */
6747static int
6748acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006749 struct acl_expr *expr, struct acl_test *test)
6750{
6751 struct http_txn *txn = l7;
6752 struct hdr_idx *idx = &txn->hdr_idx;
6753 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006754 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006755
Willy Tarreaub6866442008-07-14 23:54:42 +02006756 if (!txn)
6757 return 0;
6758
Willy Tarreau33a7e692007-06-10 19:45:56 +02006759 ctx.idx = 0;
6760 cnt = 0;
6761 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6762 cnt++;
6763
6764 test->i = cnt;
6765 test->flags = ACL_TEST_F_VOL_HDR;
6766 return 1;
6767}
6768
Willy Tarreauc11416f2007-06-17 16:58:38 +02006769static int
6770acl_fetch_chdr_cnt(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)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006779 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006780
Willy Tarreauc11416f2007-06-17 16:58:38 +02006781 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6782 /* ensure the indexes are not affected */
6783 return 0;
6784
6785 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6786}
6787
6788static int
6789acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6790 struct acl_expr *expr, struct acl_test *test)
6791{
6792 struct http_txn *txn = l7;
6793
Willy Tarreaub6866442008-07-14 23:54:42 +02006794 if (!txn)
6795 return 0;
6796
Willy Tarreau655dce92009-11-08 13:10:58 +01006797 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006798 return 0;
6799
6800 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6801}
6802
Willy Tarreau33a7e692007-06-10 19:45:56 +02006803/* 7. Check on HTTP header's integer value. The integer value is returned.
6804 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006805 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006806 */
6807static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006808acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006809 struct acl_expr *expr, struct acl_test *test)
6810{
6811 struct http_txn *txn = l7;
6812 struct hdr_idx *idx = &txn->hdr_idx;
6813 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006814
Willy Tarreaub6866442008-07-14 23:54:42 +02006815 if (!txn)
6816 return 0;
6817
Willy Tarreau33a7e692007-06-10 19:45:56 +02006818 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6819 /* search for header from the beginning */
6820 ctx->idx = 0;
6821
Willy Tarreau33a7e692007-06-10 19:45:56 +02006822 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6823 test->flags |= ACL_TEST_F_FETCH_MORE;
6824 test->flags |= ACL_TEST_F_VOL_HDR;
6825 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6826 return 1;
6827 }
6828
6829 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6830 test->flags |= ACL_TEST_F_VOL_HDR;
6831 return 0;
6832}
6833
Willy Tarreauc11416f2007-06-17 16:58:38 +02006834static int
6835acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6836 struct acl_expr *expr, struct acl_test *test)
6837{
6838 struct http_txn *txn = l7;
6839
Willy Tarreaub6866442008-07-14 23:54:42 +02006840 if (!txn)
6841 return 0;
6842
Willy Tarreau655dce92009-11-08 13:10:58 +01006843 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006844 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006845
Willy Tarreauc11416f2007-06-17 16:58:38 +02006846 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6847 /* ensure the indexes are not affected */
6848 return 0;
6849
6850 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6851}
6852
6853static int
6854acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6855 struct acl_expr *expr, struct acl_test *test)
6856{
6857 struct http_txn *txn = l7;
6858
Willy Tarreaub6866442008-07-14 23:54:42 +02006859 if (!txn)
6860 return 0;
6861
Willy Tarreau655dce92009-11-08 13:10:58 +01006862 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006863 return 0;
6864
6865 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6866}
6867
Willy Tarreau106f9792009-09-19 07:54:16 +02006868/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6869 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6870 */
6871static int
6872acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6873 struct acl_expr *expr, struct acl_test *test)
6874{
6875 struct http_txn *txn = l7;
6876 struct hdr_idx *idx = &txn->hdr_idx;
6877 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6878
6879 if (!txn)
6880 return 0;
6881
6882 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6883 /* search for header from the beginning */
6884 ctx->idx = 0;
6885
6886 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6887 test->flags |= ACL_TEST_F_FETCH_MORE;
6888 test->flags |= ACL_TEST_F_VOL_HDR;
6889 /* Same optimization as url_ip */
6890 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6891 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6892 test->ptr = (void *)&l4->srv_addr.sin_addr;
6893 test->i = AF_INET;
6894 return 1;
6895 }
6896
6897 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6898 test->flags |= ACL_TEST_F_VOL_HDR;
6899 return 0;
6900}
6901
6902static int
6903acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6904 struct acl_expr *expr, struct acl_test *test)
6905{
6906 struct http_txn *txn = l7;
6907
6908 if (!txn)
6909 return 0;
6910
Willy Tarreau655dce92009-11-08 13:10:58 +01006911 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006912 return 0;
6913
6914 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6915 /* ensure the indexes are not affected */
6916 return 0;
6917
6918 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6919}
6920
6921static int
6922acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6923 struct acl_expr *expr, struct acl_test *test)
6924{
6925 struct http_txn *txn = l7;
6926
6927 if (!txn)
6928 return 0;
6929
Willy Tarreau655dce92009-11-08 13:10:58 +01006930 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006931 return 0;
6932
6933 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6934}
6935
Willy Tarreau737b0c12007-06-10 21:28:46 +02006936/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6937 * the first '/' after the possible hostname, and ends before the possible '?'.
6938 */
6939static int
6940acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6941 struct acl_expr *expr, struct acl_test *test)
6942{
6943 struct http_txn *txn = l7;
6944 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006945
Willy Tarreaub6866442008-07-14 23:54:42 +02006946 if (!txn)
6947 return 0;
6948
Willy Tarreau655dce92009-11-08 13:10:58 +01006949 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006950 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006951
Willy Tarreauc11416f2007-06-17 16:58:38 +02006952 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6953 /* ensure the indexes are not affected */
6954 return 0;
6955
Willy Tarreau962c3f42010-01-10 00:15:35 +01006956 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006957 ptr = http_get_path(txn);
6958 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006959 return 0;
6960
6961 /* OK, we got the '/' ! */
6962 test->ptr = ptr;
6963
6964 while (ptr < end && *ptr != '?')
6965 ptr++;
6966
6967 test->len = ptr - test->ptr;
6968
6969 /* we do not need to set READ_ONLY because the data is in a buffer */
6970 test->flags = ACL_TEST_F_VOL_1ST;
6971 return 1;
6972}
6973
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006974static int
6975acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6976 struct acl_expr *expr, struct acl_test *test)
6977{
6978 struct buffer *req = s->req;
6979 struct http_txn *txn = &s->txn;
6980 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006981
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006982 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6983 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6984 */
6985
6986 if (!s || !req)
6987 return 0;
6988
Willy Tarreau655dce92009-11-08 13:10:58 +01006989 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006990 /* Already decoded as OK */
6991 test->flags |= ACL_TEST_F_SET_RES_PASS;
6992 return 1;
6993 }
6994
6995 /* Try to decode HTTP request */
6996 if (likely(req->lr < req->r))
6997 http_msg_analyzer(req, msg, &txn->hdr_idx);
6998
Willy Tarreau655dce92009-11-08 13:10:58 +01006999 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007000 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7001 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7002 return 1;
7003 }
7004 /* wait for final state */
7005 test->flags |= ACL_TEST_F_MAY_CHANGE;
7006 return 0;
7007 }
7008
7009 /* OK we got a valid HTTP request. We have some minor preparation to
7010 * perform so that further checks can rely on HTTP tests.
7011 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007012 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007013 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7014 s->flags |= SN_REDIRECTABLE;
7015
7016 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7017 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7018 return 1;
7019 }
7020
7021 test->flags |= ACL_TEST_F_SET_RES_PASS;
7022 return 1;
7023}
7024
Willy Tarreau8797c062007-05-07 00:55:35 +02007025
7026/************************************************************************/
7027/* All supported keywords must be declared here. */
7028/************************************************************************/
7029
7030/* Note: must not be declared <const> as its list will be overwritten */
7031static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007032 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7033
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007034 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7035 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7036 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7037 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007038
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007039 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7040 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7041 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7042 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7043 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7044 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7045 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7046 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7047 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007048
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007049 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7050 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7051 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7052 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7053 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7054 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7055 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7056 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7057 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7058 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007059 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007060
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007061 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7062 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7063 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7064 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7065 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7066 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7067 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7068 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7069 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007070 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007071
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007072 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7073 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7074 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7075 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7076 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7077 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7078 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007079
Willy Tarreauf3d25982007-05-08 22:45:09 +02007080 { NULL, NULL, NULL, NULL },
7081
7082#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007083 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7084 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7085 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7086 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7087 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7088 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7089 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7090
Willy Tarreau8797c062007-05-07 00:55:35 +02007091 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7092 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7093 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7094 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7095 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7096 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7097 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7098 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7099
7100 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7101 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7102 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7103 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7104 { NULL, NULL, NULL, NULL },
7105#endif
7106}};
7107
7108
7109__attribute__((constructor))
7110static void __http_protocol_init(void)
7111{
7112 acl_register_keywords(&acl_kws);
7113}
7114
7115
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007117 * Local variables:
7118 * c-indent-level: 8
7119 * c-basic-offset: 8
7120 * End:
7121 */