blob: 5b1ff9f9d3dae735fd607af5234835f0d3b9b296 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaua9679ac2010-01-03 17:32:57 +010082/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020083const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
103const char *HTTP_401_fmt =
104 "HTTP/1.0 401 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200107 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
114 [HTTP_ERR_400] = 400,
115 [HTTP_ERR_403] = 403,
116 [HTTP_ERR_408] = 408,
117 [HTTP_ERR_500] = 500,
118 [HTTP_ERR_502] = 502,
119 [HTTP_ERR_503] = 503,
120 [HTTP_ERR_504] = 504,
121};
122
Willy Tarreau80587432006-12-24 17:47:20 +0100123static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100125 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 "Cache-Control: no-cache\r\n"
127 "Connection: close\r\n"
128 "Content-Type: text/html\r\n"
129 "\r\n"
130 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
131
132 [HTTP_ERR_403] =
133 "HTTP/1.0 403 Forbidden\r\n"
134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
139
140 [HTTP_ERR_408] =
141 "HTTP/1.0 408 Request Time-out\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
147
148 [HTTP_ERR_500] =
149 "HTTP/1.0 500 Server Error\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
155
156 [HTTP_ERR_502] =
157 "HTTP/1.0 502 Bad Gateway\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
163
164 [HTTP_ERR_503] =
165 "HTTP/1.0 503 Service Unavailable\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
171
172 [HTTP_ERR_504] =
173 "HTTP/1.0 504 Gateway Time-out\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
179
180};
181
Willy Tarreau80587432006-12-24 17:47:20 +0100182/* We must put the messages here since GCC cannot initialize consts depending
183 * on strlen().
184 */
185struct chunk http_err_chunks[HTTP_ERR_SIZE];
186
Willy Tarreau42250582007-04-01 01:30:43 +0200187#define FD_SETS_ARE_BITFIELDS
188#ifdef FD_SETS_ARE_BITFIELDS
189/*
190 * This map is used with all the FD_* macros to check whether a particular bit
191 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
192 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
193 * byte should be encoded. Be careful to always pass bytes from 0 to 255
194 * exclusively to the macros.
195 */
196fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
198
199#else
200#error "Check if your OS uses bitfields for fd_sets"
201#endif
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203void init_proto_http()
204{
Willy Tarreau42250582007-04-01 01:30:43 +0200205 int i;
206 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100207 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200208
Willy Tarreau80587432006-12-24 17:47:20 +0100209 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
210 if (!http_err_msgs[msg]) {
211 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
212 abort();
213 }
214
215 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
216 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
217 }
Willy Tarreau42250582007-04-01 01:30:43 +0200218
219 /* initialize the log header encoding map : '{|}"#' should be encoded with
220 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
221 * URL encoding only requires '"', '#' to be encoded as well as non-
222 * printable characters above.
223 */
224 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
225 memset(url_encode_map, 0, sizeof(url_encode_map));
226 for (i = 0; i < 32; i++) {
227 FD_SET(i, hdr_encode_map);
228 FD_SET(i, url_encode_map);
229 }
230 for (i = 127; i < 256; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234
235 tmp = "\"#{|}";
236 while (*tmp) {
237 FD_SET(*tmp, hdr_encode_map);
238 tmp++;
239 }
240
241 tmp = "\"#";
242 while (*tmp) {
243 FD_SET(*tmp, url_encode_map);
244 tmp++;
245 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200246
247 /* memory allocations */
248 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200249 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100250}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
Willy Tarreau53b6c742006-12-17 13:37:46 +0100252/*
253 * We have 26 list of methods (1 per first letter), each of which can have
254 * up to 3 entries (2 valid, 1 null).
255 */
256struct http_method_desc {
257 http_meth_t meth;
258 int len;
259 const char text[8];
260};
261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100262const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263 ['C' - 'A'] = {
264 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
265 },
266 ['D' - 'A'] = {
267 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
268 },
269 ['G' - 'A'] = {
270 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
271 },
272 ['H' - 'A'] = {
273 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
274 },
275 ['P' - 'A'] = {
276 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
277 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
278 },
279 ['T' - 'A'] = {
280 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
281 },
282 /* rest is empty like this :
283 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
284 */
285};
286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200288 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289 * RFC2616 for those chars.
290 */
291
292const char http_is_spht[256] = {
293 [' '] = 1, ['\t'] = 1,
294};
295
296const char http_is_crlf[256] = {
297 ['\r'] = 1, ['\n'] = 1,
298};
299
300const char http_is_lws[256] = {
301 [' '] = 1, ['\t'] = 1,
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_sep[256] = {
306 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
307 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
308 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
309 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
310 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
311};
312
313const char http_is_ctl[256] = {
314 [0 ... 31] = 1,
315 [127] = 1,
316};
317
318/*
319 * A token is any ASCII char that is neither a separator nor a CTL char.
320 * Do not overwrite values in assignment since gcc-2.95 will not handle
321 * them correctly. Instead, define every non-CTL char's status.
322 */
323const char http_is_token[256] = {
324 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
325 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
326 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
327 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
328 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
329 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
330 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
331 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
332 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
333 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
334 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
335 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
336 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
337 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
338 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
339 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
340 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
341 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
342 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
343 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
344 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
345 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
346 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
347 ['|'] = 1, ['}'] = 0, ['~'] = 1,
348};
349
350
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100351/*
352 * An http ver_token is any ASCII which can be found in an HTTP version,
353 * which includes 'H', 'T', 'P', '/', '.' and any digit.
354 */
355const char http_is_ver_token[256] = {
356 ['.'] = 1, ['/'] = 1,
357 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
358 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
359 ['H'] = 1, ['P'] = 1, ['T'] = 1,
360};
361
362
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100363/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100364 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
365 */
366#if defined(DEBUG_FSM)
367static void http_silent_debug(int line, struct session *s)
368{
369 int size = 0;
370 size += snprintf(trash + size, sizeof(trash) - size,
371 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
372 line,
373 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
374 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
375 write(-1, trash, size);
376 size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
379 line,
380 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
381 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
382
383 write(-1, trash, size);
384}
385#else
386#define http_silent_debug(l,s) do { } while (0)
387#endif
388
389/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100390 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
391 * CRLF. Text length is measured first, so it cannot be NULL.
392 * The header is also automatically added to the index <hdr_idx>, and the end
393 * of headers is automatically adjusted. The number of bytes added is returned
394 * on success, otherwise <0 is returned indicating an error.
395 */
396int http_header_add_tail(struct buffer *b, struct http_msg *msg,
397 struct hdr_idx *hdr_idx, const char *text)
398{
399 int bytes, len;
400
401 len = strlen(text);
402 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
403 if (!bytes)
404 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100405 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100406 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
407}
408
409/*
410 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
411 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
412 * the buffer is only opened and the space reserved, but nothing is copied.
413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
417int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
418 struct hdr_idx *hdr_idx, const char *text, int len)
419{
420 int bytes;
421
422 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428
429/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100430 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
431 * If so, returns the position of the first non-space character relative to
432 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
433 * to return a pointer to the place after the first space. Returns 0 if the
434 * header name does not match. Checks are case-insensitive.
435 */
436int http_header_match2(const char *hdr, const char *end,
437 const char *name, int len)
438{
439 const char *val;
440
441 if (hdr + len >= end)
442 return 0;
443 if (hdr[len] != ':')
444 return 0;
445 if (strncasecmp(hdr, name, len) != 0)
446 return 0;
447 val = hdr + len + 1;
448 while (val < end && HTTP_IS_SPHT(*val))
449 val++;
450 if ((val >= end) && (len + 2 <= end - hdr))
451 return len + 2; /* we may replace starting from second space */
452 return val - hdr;
453}
454
Willy Tarreau33a7e692007-06-10 19:45:56 +0200455/* Find the end of the header value contained between <s> and <e>.
456 * See RFC2616, par 2.2 for more information. Note that it requires
457 * a valid header to return a valid result.
458 */
459const char *find_hdr_value_end(const char *s, const char *e)
460{
461 int quoted, qdpair;
462
463 quoted = qdpair = 0;
464 for (; s < e; s++) {
465 if (qdpair) qdpair = 0;
466 else if (quoted && *s == '\\') qdpair = 1;
467 else if (quoted && *s == '"') quoted = 0;
468 else if (*s == '"') quoted = 1;
469 else if (*s == ',') return s;
470 }
471 return s;
472}
473
474/* Find the first or next occurrence of header <name> in message buffer <sol>
475 * using headers index <idx>, and return it in the <ctx> structure. This
476 * structure holds everything necessary to use the header and find next
477 * occurrence. If its <idx> member is 0, the header is searched from the
478 * beginning. Otherwise, the next occurrence is returned. The function returns
479 * 1 when it finds a value, and 0 when there is no more.
480 */
481int http_find_header2(const char *name, int len,
482 const char *sol, struct hdr_idx *idx,
483 struct hdr_ctx *ctx)
484{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200485 const char *eol, *sov;
486 int cur_idx;
487
488 if (ctx->idx) {
489 /* We have previously returned a value, let's search
490 * another one on the same line.
491 */
492 cur_idx = ctx->idx;
493 sol = ctx->line;
494 sov = sol + ctx->val + ctx->vlen;
495 eol = sol + idx->v[cur_idx].len;
496
497 if (sov >= eol)
498 /* no more values in this header */
499 goto next_hdr;
500
501 /* values remaining for this header, skip the comma */
502 sov++;
503 while (sov < eol && http_is_lws[(unsigned char)*sov])
504 sov++;
505
506 goto return_hdr;
507 }
508
509 /* first request for this header */
510 sol += hdr_idx_first_pos(idx);
511 cur_idx = hdr_idx_first_idx(idx);
512
513 while (cur_idx) {
514 eol = sol + idx->v[cur_idx].len;
515
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200516 if (len == 0) {
517 /* No argument was passed, we want any header.
518 * To achieve this, we simply build a fake request. */
519 while (sol + len < eol && sol[len] != ':')
520 len++;
521 name = sol;
522 }
523
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 if ((len < eol - sol) &&
525 (sol[len] == ':') &&
526 (strncasecmp(sol, name, len) == 0)) {
527
528 sov = sol + len + 1;
529 while (sov < eol && http_is_lws[(unsigned char)*sov])
530 sov++;
531 return_hdr:
532 ctx->line = sol;
533 ctx->idx = cur_idx;
534 ctx->val = sov - sol;
535
536 eol = find_hdr_value_end(sov, eol);
537 ctx->vlen = eol - sov;
538 return 1;
539 }
540 next_hdr:
541 sol = eol + idx->v[cur_idx].cr + 1;
542 cur_idx = idx->v[cur_idx].next;
543 }
544 return 0;
545}
546
547int http_find_header(const char *name,
548 const char *sol, struct hdr_idx *idx,
549 struct hdr_ctx *ctx)
550{
551 return http_find_header2(name, strlen(name), sol, idx, ctx);
552}
553
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100554/* This function handles a server error at the stream interface level. The
555 * stream interface is assumed to be already in a closed state. An optional
556 * message is copied into the input buffer, and an HTTP status code stored.
557 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100558 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100560static void http_server_error(struct session *t, struct stream_interface *si,
561 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100563 buffer_erase(si->ob);
564 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200565 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100566 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100567 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100568 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100569 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 }
571 if (!(t->flags & SN_ERR_MASK))
572 t->flags |= err;
573 if (!(t->flags & SN_FINST_MASK))
574 t->flags |= finst;
575}
576
Willy Tarreau80587432006-12-24 17:47:20 +0100577/* This function returns the appropriate error location for the given session
578 * and message.
579 */
580
581struct chunk *error_message(struct session *s, int msgnum)
582{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200583 if (s->be->errmsg[msgnum].str)
584 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100585 else if (s->fe->errmsg[msgnum].str)
586 return &s->fe->errmsg[msgnum];
587 else
588 return &http_err_chunks[msgnum];
589}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591/*
592 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
593 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
594 */
595static http_meth_t find_http_meth(const char *str, const int len)
596{
597 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100598 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100599
600 m = ((unsigned)*str - 'A');
601
602 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100603 for (h = http_methods[m]; h->len > 0; h++) {
604 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100605 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100606 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100607 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100608 };
609 return HTTP_METH_OTHER;
610 }
611 return HTTP_METH_NONE;
612
613}
614
Willy Tarreau21d2af32008-02-14 20:25:24 +0100615/* Parse the URI from the given transaction (which is assumed to be in request
616 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
617 * It is returned otherwise.
618 */
619static char *
620http_get_path(struct http_txn *txn)
621{
622 char *ptr, *end;
623
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100624 ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100625 end = ptr + txn->req.sl.rq.u_l;
626
627 if (ptr >= end)
628 return NULL;
629
630 /* RFC2616, par. 5.1.2 :
631 * Request-URI = "*" | absuri | abspath | authority
632 */
633
634 if (*ptr == '*')
635 return NULL;
636
637 if (isalpha((unsigned char)*ptr)) {
638 /* this is a scheme as described by RFC3986, par. 3.1 */
639 ptr++;
640 while (ptr < end &&
641 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
642 ptr++;
643 /* skip '://' */
644 if (ptr == end || *ptr++ != ':')
645 return NULL;
646 if (ptr == end || *ptr++ != '/')
647 return NULL;
648 if (ptr == end || *ptr++ != '/')
649 return NULL;
650 }
651 /* skip [user[:passwd]@]host[:[port]] */
652
653 while (ptr < end && *ptr != '/')
654 ptr++;
655
656 if (ptr == end)
657 return NULL;
658
659 /* OK, we got the '/' ! */
660 return ptr;
661}
662
Willy Tarreauefb453c2008-10-26 20:49:47 +0100663/* Returns a 302 for a redirectable request. This may only be called just after
664 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
665 * left unchanged and will follow normal proxy processing.
666 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100668{
669 struct http_txn *txn;
670 struct chunk rdr;
671 char *path;
672 int len;
673
674 /* 1: create the response header */
675 rdr.len = strlen(HTTP_302);
676 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100677 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100678 memcpy(rdr.str, HTTP_302, rdr.len);
679
680 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200681 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100682 return;
683
684 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
685 rdr.len += s->srv->rdr_len;
686
687 /* 3: add the request URI */
688 txn = &s->txn;
689 path = http_get_path(txn);
690 if (!path)
691 return;
692
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100693 len = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200694 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695 return;
696
697 memcpy(rdr.str + rdr.len, path, len);
698 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100699 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
700 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701
702 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100703 si->shutr(si);
704 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100705 si->err_type = SI_ET_NONE;
706 si->err_loc = NULL;
707 si->state = SI_ST_CLO;
708
709 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100710 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711
712 /* FIXME: we should increase a counter of redirects per server and per backend. */
713 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100714 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100715}
716
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100717/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100718 * that the server side is closed. Note that err_type is actually a
719 * bitmask, where almost only aborts may be cumulated with other
720 * values. We consider that aborted operations are more important
721 * than timeouts or errors due to the fact that nobody else in the
722 * logs might explain incomplete retries. All others should avoid
723 * being cumulated. It should normally not be possible to have multiple
724 * aborts at once, but just in case, the first one in sequence is reported.
725 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100726void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100727{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100728 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100729
730 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100731 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
732 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100733 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100734 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
735 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100736 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100737 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
738 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100739 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100740 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
741 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100742 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100743 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
744 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100746 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
747 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100748 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100749 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
750 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751}
752
Willy Tarreau42250582007-04-01 01:30:43 +0200753extern const char sess_term_cond[8];
754extern const char sess_fin_state[8];
755extern const char *monthname[12];
756const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
757const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
758 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
759 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200760struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200761struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100762
Emeric Brun3a058f32009-06-30 18:26:00 +0200763void http_sess_clflog(struct session *s)
764{
765 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
766 struct proxy *fe = s->fe;
767 struct proxy *be = s->be;
768 struct proxy *prx_log;
769 struct http_txn *txn = &s->txn;
770 int tolog, level, err;
771 char *uri, *h;
772 char *svid;
773 struct tm tm;
774 static char tmpline[MAX_SYSLOG_LEN];
775 int hdr;
776 size_t w;
777 int t_request;
778
779 prx_log = fe;
780 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
781 (s->conn_retries != be->conn_retries) ||
782 txn->status >= 500;
783
784 if (s->cli_addr.ss_family == AF_INET)
785 inet_ntop(AF_INET,
786 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
787 pn, sizeof(pn));
788 else
789 inet_ntop(AF_INET6,
790 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
791 pn, sizeof(pn));
792
793 get_gmtime(s->logs.accept_date.tv_sec, &tm);
794
795 /* FIXME: let's limit ourselves to frontend logging for now. */
796 tolog = fe->to_log;
797
798 h = tmpline;
799
800 w = snprintf(h, sizeof(tmpline),
801 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
802 pn,
803 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
804 tm.tm_hour, tm.tm_min, tm.tm_sec);
805 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
806 goto trunc;
807 h += w;
808
809 if (h >= tmpline + sizeof(tmpline) - 4)
810 goto trunc;
811
812 *(h++) = ' ';
813 *(h++) = '\"';
814 uri = txn->uri ? txn->uri : "<BADREQ>";
815 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
816 '#', url_encode_map, uri);
817 *(h++) = '\"';
818
819 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
820 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
821 goto trunc;
822 h += w;
823
824 if (h >= tmpline + sizeof(tmpline) - 9)
825 goto trunc;
826 memcpy(h, " \"-\" \"-\"", 8);
827 h += 8;
828
829 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
830 " %d %03d",
831 (s->cli_addr.ss_family == AF_INET) ?
832 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
833 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
834 (int)s->logs.accept_date.tv_usec/1000);
835 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
836 goto trunc;
837 h += w;
838
839 w = strlen(fe->id);
840 if (h >= tmpline + sizeof(tmpline) - 4 - w)
841 goto trunc;
842 *(h++) = ' ';
843 *(h++) = '\"';
844 memcpy(h, fe->id, w);
845 h += w;
846 *(h++) = '\"';
847
848 w = strlen(be->id);
849 if (h >= tmpline + sizeof(tmpline) - 4 - w)
850 goto trunc;
851 *(h++) = ' ';
852 *(h++) = '\"';
853 memcpy(h, be->id, w);
854 h += w;
855 *(h++) = '\"';
856
857 svid = (tolog & LW_SVID) ?
858 (s->data_source != DATA_SRC_STATS) ?
859 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
860
861 w = strlen(svid);
862 if (h >= tmpline + sizeof(tmpline) - 4 - w)
863 goto trunc;
864 *(h++) = ' ';
865 *(h++) = '\"';
866 memcpy(h, svid, w);
867 h += w;
868 *(h++) = '\"';
869
870 t_request = -1;
871 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
872 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
873 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
874 " %d %ld %ld %ld %ld",
875 t_request,
876 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
877 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
878 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
879 s->logs.t_close);
880 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
881 goto trunc;
882 h += w;
883
884 if (h >= tmpline + sizeof(tmpline) - 8)
885 goto trunc;
886 *(h++) = ' ';
887 *(h++) = '\"';
888 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
889 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
890 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
891 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
892 *(h++) = '\"';
893
894 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
895 " %d %d %d %d %d %ld %ld",
896 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
897 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
898 s->logs.srv_queue_size, s->logs.prx_queue_size);
899
900 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
901 goto trunc;
902 h += w;
903
904 if (txn->cli_cookie) {
905 w = strlen(txn->cli_cookie);
906 if (h >= tmpline + sizeof(tmpline) - 4 - w)
907 goto trunc;
908 *(h++) = ' ';
909 *(h++) = '\"';
910 memcpy(h, txn->cli_cookie, w);
911 h += w;
912 *(h++) = '\"';
913 } else {
914 if (h >= tmpline + sizeof(tmpline) - 5)
915 goto trunc;
916 memcpy(h, " \"-\"", 4);
917 h += 4;
918 }
919
920 if (txn->srv_cookie) {
921 w = strlen(txn->srv_cookie);
922 if (h >= tmpline + sizeof(tmpline) - 4 - w)
923 goto trunc;
924 *(h++) = ' ';
925 *(h++) = '\"';
926 memcpy(h, txn->srv_cookie, w);
927 h += w;
928 *(h++) = '\"';
929 } else {
930 if (h >= tmpline + sizeof(tmpline) - 5)
931 goto trunc;
932 memcpy(h, " \"-\"", 4);
933 h += 4;
934 }
935
936 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
937 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
938 if (h >= sizeof (tmpline) + tmpline - 4)
939 goto trunc;
940 *(h++) = ' ';
941 *(h++) = '\"';
942 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
943 '#', hdr_encode_map, txn->req.cap[hdr]);
944 *(h++) = '\"';
945 }
946 }
947
948 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
949 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
950 if (h >= sizeof (tmpline) + tmpline - 4)
951 goto trunc;
952 *(h++) = ' ';
953 *(h++) = '\"';
954 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
955 '#', hdr_encode_map, txn->rsp.cap[hdr]);
956 *(h++) = '\"';
957 }
958 }
959
960trunc:
961 *h = '\0';
962
963 level = LOG_INFO;
964 if (err && (fe->options2 & PR_O2_LOGERRORS))
965 level = LOG_ERR;
966
967 send_log(prx_log, level, "%s\n", tmpline);
968
969 s->logs.logwait = 0;
970}
971
Willy Tarreau42250582007-04-01 01:30:43 +0200972/*
973 * send a log for the session when we have enough info about it.
974 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100976void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200977{
978 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
979 struct proxy *fe = s->fe;
980 struct proxy *be = s->be;
981 struct proxy *prx_log;
982 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200983 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200984 char *uri, *h;
985 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200986 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200987 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200988 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200989 int hdr;
990
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200991 /* if we don't want to log normal traffic, return now */
992 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
993 (s->conn_retries != be->conn_retries) ||
994 txn->status >= 500;
995 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
996 return;
997
Willy Tarreau42250582007-04-01 01:30:43 +0200998 if (fe->logfac1 < 0 && fe->logfac2 < 0)
999 return;
1000 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001001
Emeric Brun3a058f32009-06-30 18:26:00 +02001002 if (prx_log->options2 & PR_O2_CLFLOG)
1003 return http_sess_clflog(s);
1004
Willy Tarreau42250582007-04-01 01:30:43 +02001005 if (s->cli_addr.ss_family == AF_INET)
1006 inet_ntop(AF_INET,
1007 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1008 pn, sizeof(pn));
1009 else
1010 inet_ntop(AF_INET6,
1011 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1012 pn, sizeof(pn));
1013
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001014 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001015
1016 /* FIXME: let's limit ourselves to frontend logging for now. */
1017 tolog = fe->to_log;
1018
1019 h = tmpline;
1020 if (fe->to_log & LW_REQHDR &&
1021 txn->req.cap &&
1022 (h < tmpline + sizeof(tmpline) - 10)) {
1023 *(h++) = ' ';
1024 *(h++) = '{';
1025 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1026 if (hdr)
1027 *(h++) = '|';
1028 if (txn->req.cap[hdr] != NULL)
1029 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1030 '#', hdr_encode_map, txn->req.cap[hdr]);
1031 }
1032 *(h++) = '}';
1033 }
1034
1035 if (fe->to_log & LW_RSPHDR &&
1036 txn->rsp.cap &&
1037 (h < tmpline + sizeof(tmpline) - 7)) {
1038 *(h++) = ' ';
1039 *(h++) = '{';
1040 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1041 if (hdr)
1042 *(h++) = '|';
1043 if (txn->rsp.cap[hdr] != NULL)
1044 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1045 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1046 }
1047 *(h++) = '}';
1048 }
1049
1050 if (h < tmpline + sizeof(tmpline) - 4) {
1051 *(h++) = ' ';
1052 *(h++) = '"';
1053 uri = txn->uri ? txn->uri : "<BADREQ>";
1054 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1055 '#', url_encode_map, uri);
1056 *(h++) = '"';
1057 }
1058 *h = '\0';
1059
1060 svid = (tolog & LW_SVID) ?
1061 (s->data_source != DATA_SRC_STATS) ?
1062 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1063
Willy Tarreau70089872008-06-13 21:12:51 +02001064 t_request = -1;
1065 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1066 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1067
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001068 level = LOG_INFO;
1069 if (err && (fe->options2 & PR_O2_LOGERRORS))
1070 level = LOG_ERR;
1071
1072 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001073 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001074 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1075 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001076 pn,
1077 (s->cli_addr.ss_family == AF_INET) ?
1078 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1079 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001080 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001081 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001082 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001083 t_request,
1084 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001085 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1086 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1087 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1088 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001089 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001090 txn->cli_cookie ? txn->cli_cookie : "-",
1091 txn->srv_cookie ? txn->srv_cookie : "-",
1092 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1093 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1094 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1095 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1096 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001097 (s->flags & SN_REDISP)?"+":"",
1098 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001099 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1100
1101 s->logs.logwait = 0;
1102}
1103
Willy Tarreau117f59e2007-03-04 18:17:17 +01001104
1105/*
1106 * Capture headers from message starting at <som> according to header list
1107 * <cap_hdr>, and fill the <idx> structure appropriately.
1108 */
1109void capture_headers(char *som, struct hdr_idx *idx,
1110 char **cap, struct cap_hdr *cap_hdr)
1111{
1112 char *eol, *sol, *col, *sov;
1113 int cur_idx;
1114 struct cap_hdr *h;
1115 int len;
1116
1117 sol = som + hdr_idx_first_pos(idx);
1118 cur_idx = hdr_idx_first_idx(idx);
1119
1120 while (cur_idx) {
1121 eol = sol + idx->v[cur_idx].len;
1122
1123 col = sol;
1124 while (col < eol && *col != ':')
1125 col++;
1126
1127 sov = col + 1;
1128 while (sov < eol && http_is_lws[(unsigned char)*sov])
1129 sov++;
1130
1131 for (h = cap_hdr; h; h = h->next) {
1132 if ((h->namelen == col - sol) &&
1133 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1134 if (cap[h->index] == NULL)
1135 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001136 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001137
1138 if (cap[h->index] == NULL) {
1139 Alert("HTTP capture : out of memory.\n");
1140 continue;
1141 }
1142
1143 len = eol - sov;
1144 if (len > h->len)
1145 len = h->len;
1146
1147 memcpy(cap[h->index], sov, len);
1148 cap[h->index][len]=0;
1149 }
1150 }
1151 sol = eol + idx->v[cur_idx].cr + 1;
1152 cur_idx = idx->v[cur_idx].next;
1153 }
1154}
1155
1156
Willy Tarreau42250582007-04-01 01:30:43 +02001157/* either we find an LF at <ptr> or we jump to <bad>.
1158 */
1159#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1160
1161/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1162 * otherwise to <http_msg_ood> with <state> set to <st>.
1163 */
1164#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1165 ptr++; \
1166 if (likely(ptr < end)) \
1167 goto good; \
1168 else { \
1169 state = (st); \
1170 goto http_msg_ood; \
1171 } \
1172 } while (0)
1173
1174
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001176 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1178 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1179 * will give undefined results.
1180 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1181 * and that msg->sol points to the beginning of the response.
1182 * If a complete line is found (which implies that at least one CR or LF is
1183 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1184 * returned indicating an incomplete line (which does not mean that parts have
1185 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1186 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1187 * upon next call.
1188 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001189 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1191 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001192 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001194const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1195 unsigned int state, const char *ptr, const char *end,
1196 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001197{
Willy Tarreau8973c702007-01-21 23:58:29 +01001198 switch (state) {
1199 http_msg_rpver:
1200 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001201 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1203
1204 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001205 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1207 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001208 state = HTTP_MSG_ERROR;
1209 break;
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211 http_msg_rpver_sp:
1212 case HTTP_MSG_RPVER_SP:
1213 if (likely(!HTTP_IS_LWS(*ptr))) {
1214 msg->sl.st.c = ptr - msg_buf;
1215 goto http_msg_rpcode;
1216 }
1217 if (likely(HTTP_IS_SPHT(*ptr)))
1218 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1219 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001220 state = HTTP_MSG_ERROR;
1221 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001222
1223 http_msg_rpcode:
1224 case HTTP_MSG_RPCODE:
1225 if (likely(!HTTP_IS_LWS(*ptr)))
1226 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1227
1228 if (likely(HTTP_IS_SPHT(*ptr))) {
1229 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1230 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1231 }
1232
1233 /* so it's a CR/LF, so there is no reason phrase */
1234 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1235 http_msg_rsp_reason:
1236 /* FIXME: should we support HTTP responses without any reason phrase ? */
1237 msg->sl.st.r = ptr - msg_buf;
1238 msg->sl.st.r_l = 0;
1239 goto http_msg_rpline_eol;
1240
1241 http_msg_rpcode_sp:
1242 case HTTP_MSG_RPCODE_SP:
1243 if (likely(!HTTP_IS_LWS(*ptr))) {
1244 msg->sl.st.r = ptr - msg_buf;
1245 goto http_msg_rpreason;
1246 }
1247 if (likely(HTTP_IS_SPHT(*ptr)))
1248 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1249 /* so it's a CR/LF, so there is no reason phrase */
1250 goto http_msg_rsp_reason;
1251
1252 http_msg_rpreason:
1253 case HTTP_MSG_RPREASON:
1254 if (likely(!HTTP_IS_CRLF(*ptr)))
1255 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1256 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1257 http_msg_rpline_eol:
1258 /* We have seen the end of line. Note that we do not
1259 * necessarily have the \n yet, but at least we know that we
1260 * have EITHER \r OR \n, otherwise the response would not be
1261 * complete. We can then record the response length and return
1262 * to the caller which will be able to register it.
1263 */
1264 msg->sl.st.l = ptr - msg->sol;
1265 return ptr;
1266
1267#ifdef DEBUG_FULL
1268 default:
1269 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1270 exit(1);
1271#endif
1272 }
1273
1274 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001275 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 if (ret_state)
1277 *ret_state = state;
1278 if (ret_ptr)
1279 *ret_ptr = (char *)ptr;
1280 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001281}
1282
1283
1284/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285 * This function parses a request line between <ptr> and <end>, starting with
1286 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1287 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1288 * will give undefined results.
1289 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1290 * and that msg->sol points to the beginning of the request.
1291 * If a complete line is found (which implies that at least one CR or LF is
1292 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1293 * returned indicating an incomplete line (which does not mean that parts have
1294 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1295 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1296 * upon next call.
1297 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001298 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1300 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001301 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001302 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001303const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1304 unsigned int state, const char *ptr, const char *end,
1305 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 switch (state) {
1308 http_msg_rqmeth:
1309 case HTTP_MSG_RQMETH:
1310 if (likely(HTTP_IS_TOKEN(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001314 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 if (likely(HTTP_IS_CRLF(*ptr))) {
1319 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001320 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 http_msg_req09_uri:
1322 msg->sl.rq.u = ptr - msg_buf;
1323 http_msg_req09_uri_e:
1324 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1325 http_msg_req09_ver:
1326 msg->sl.rq.v = ptr - msg_buf;
1327 msg->sl.rq.v_l = 0;
1328 goto http_msg_rqline_eol;
1329 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001330 state = HTTP_MSG_ERROR;
1331 break;
1332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 http_msg_rqmeth_sp:
1334 case HTTP_MSG_RQMETH_SP:
1335 if (likely(!HTTP_IS_LWS(*ptr))) {
1336 msg->sl.rq.u = ptr - msg_buf;
1337 goto http_msg_rquri;
1338 }
1339 if (likely(HTTP_IS_SPHT(*ptr)))
1340 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1341 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1342 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 http_msg_rquri:
1345 case HTTP_MSG_RQURI:
1346 if (likely(!HTTP_IS_LWS(*ptr)))
1347 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001348
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (likely(HTTP_IS_SPHT(*ptr))) {
1350 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1351 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1352 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1355 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 http_msg_rquri_sp:
1358 case HTTP_MSG_RQURI_SP:
1359 if (likely(!HTTP_IS_LWS(*ptr))) {
1360 msg->sl.rq.v = ptr - msg_buf;
1361 goto http_msg_rqver;
1362 }
1363 if (likely(HTTP_IS_SPHT(*ptr)))
1364 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1365 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1366 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 http_msg_rqver:
1369 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001370 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001372
1373 if (likely(HTTP_IS_CRLF(*ptr))) {
1374 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1375 http_msg_rqline_eol:
1376 /* We have seen the end of line. Note that we do not
1377 * necessarily have the \n yet, but at least we know that we
1378 * have EITHER \r OR \n, otherwise the request would not be
1379 * complete. We can then record the request length and return
1380 * to the caller which will be able to register it.
1381 */
1382 msg->sl.rq.l = ptr - msg->sol;
1383 return ptr;
1384 }
1385
1386 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001387 state = HTTP_MSG_ERROR;
1388 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390#ifdef DEBUG_FULL
1391 default:
1392 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1393 exit(1);
1394#endif
1395 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001398 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 if (ret_state)
1400 *ret_state = state;
1401 if (ret_ptr)
1402 *ret_ptr = (char *)ptr;
1403 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001405
1406
Willy Tarreau8973c702007-01-21 23:58:29 +01001407/*
1408 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001409 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 * when data are missing and recalled at the exact same location with no
1411 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001412 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001413 * fields. Note that msg->som and msg->sol will be initialized after completing
1414 * the first state, so that none of the msg pointers has to be initialized
1415 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1418{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001419 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001422 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 ptr = buf->lr;
1424 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 if (unlikely(ptr >= end))
1427 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001430 /*
1431 * First, states that are specific to the response only.
1432 * We check them first so that request and headers are
1433 * closer to each other (accessed more often).
1434 */
1435 http_msg_rpbefore:
1436 case HTTP_MSG_RPBEFORE:
1437 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001438 /* we have a start of message, but we have to check
1439 * first if we need to remove some CRLF. We can only
1440 * do this when send_max=0.
1441 */
1442 char *beg = buf->w + buf->send_max;
1443 if (beg >= buf->data + buf->size)
1444 beg -= buf->size;
1445 if (unlikely(ptr != beg)) {
1446 if (buf->send_max)
1447 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001448 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001449 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001451 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001452 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 hdr_idx_init(idx);
1454 state = HTTP_MSG_RPVER;
1455 goto http_msg_rpver;
1456 }
1457
1458 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1459 goto http_msg_invalid;
1460
1461 if (unlikely(*ptr == '\n'))
1462 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1463 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1464 /* stop here */
1465
1466 http_msg_rpbefore_cr:
1467 case HTTP_MSG_RPBEFORE_CR:
1468 EXPECT_LF_HERE(ptr, http_msg_invalid);
1469 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1470 /* stop here */
1471
1472 http_msg_rpver:
1473 case HTTP_MSG_RPVER:
1474 case HTTP_MSG_RPVER_SP:
1475 case HTTP_MSG_RPCODE:
1476 case HTTP_MSG_RPCODE_SP:
1477 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001478 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001479 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001480 if (unlikely(!ptr))
1481 return;
1482
1483 /* we have a full response and we know that we have either a CR
1484 * or an LF at <ptr>.
1485 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001486 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1488
1489 msg->sol = ptr;
1490 if (likely(*ptr == '\r'))
1491 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1492 goto http_msg_rpline_end;
1493
1494 http_msg_rpline_end:
1495 case HTTP_MSG_RPLINE_END:
1496 /* msg->sol must point to the first of CR or LF. */
1497 EXPECT_LF_HERE(ptr, http_msg_invalid);
1498 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1499 /* stop here */
1500
1501 /*
1502 * Second, states that are specific to the request only
1503 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 http_msg_rqbefore:
1505 case HTTP_MSG_RQBEFORE:
1506 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001507 /* we have a start of message, but we have to check
1508 * first if we need to remove some CRLF. We can only
1509 * do this when send_max=0.
1510 */
1511 char *beg = buf->w + buf->send_max;
1512 if (beg >= buf->data + buf->size)
1513 beg -= buf->size;
1514 if (likely(ptr != beg)) {
1515 if (buf->send_max)
1516 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001517 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001518 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001520 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001521 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001522 /* we will need this when keep-alive will be supported
1523 hdr_idx_init(idx);
1524 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 state = HTTP_MSG_RQMETH;
1526 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1530 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (unlikely(*ptr == '\n'))
1533 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 http_msg_rqbefore_cr:
1538 case HTTP_MSG_RQBEFORE_CR:
1539 EXPECT_LF_HERE(ptr, http_msg_invalid);
1540 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001541 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 http_msg_rqmeth:
1544 case HTTP_MSG_RQMETH:
1545 case HTTP_MSG_RQMETH_SP:
1546 case HTTP_MSG_RQURI:
1547 case HTTP_MSG_RQURI_SP:
1548 case HTTP_MSG_RQVER:
1549 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001550 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 if (unlikely(!ptr))
1552 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 /* we have a full request and we know that we have either a CR
1555 * or an LF at <ptr>.
1556 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001557 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 msg->sol = ptr;
1561 if (likely(*ptr == '\r'))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 http_msg_rqline_end:
1566 case HTTP_MSG_RQLINE_END:
1567 /* check for HTTP/0.9 request : no version information available.
1568 * msg->sol must point to the first of CR or LF.
1569 */
1570 if (unlikely(msg->sl.rq.v_l == 0))
1571 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001572
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 EXPECT_LF_HERE(ptr, http_msg_invalid);
1574 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001575 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001576
Willy Tarreau8973c702007-01-21 23:58:29 +01001577 /*
1578 * Common states below
1579 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 http_msg_hdr_first:
1581 case HTTP_MSG_HDR_FIRST:
1582 msg->sol = ptr;
1583 if (likely(!HTTP_IS_CRLF(*ptr))) {
1584 goto http_msg_hdr_name;
1585 }
1586
1587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1589 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 http_msg_hdr_name:
1592 case HTTP_MSG_HDR_NAME:
1593 /* assumes msg->sol points to the first char */
1594 if (likely(HTTP_IS_TOKEN(*ptr)))
1595 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (likely(*ptr == ':')) {
1598 msg->col = ptr - buf->data;
1599 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1600 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001601
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001602 if (likely(msg->err_pos < -1) || *ptr == '\n')
1603 goto http_msg_invalid;
1604
1605 if (msg->err_pos == -1) /* capture error pointer */
1606 msg->err_pos = ptr - buf->data; /* >= 0 now */
1607
1608 /* and we still accept this non-token character */
1609 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 http_msg_hdr_l1_sp:
1612 case HTTP_MSG_HDR_L1_SP:
1613 /* assumes msg->sol points to the first char and msg->col to the colon */
1614 if (likely(HTTP_IS_SPHT(*ptr)))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 /* header value can be basically anything except CR/LF */
1618 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 if (likely(!HTTP_IS_CRLF(*ptr))) {
1621 goto http_msg_hdr_val;
1622 }
1623
1624 if (likely(*ptr == '\r'))
1625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1626 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 http_msg_hdr_l1_lf:
1629 case HTTP_MSG_HDR_L1_LF:
1630 EXPECT_LF_HERE(ptr, http_msg_invalid);
1631 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 http_msg_hdr_l1_lws:
1634 case HTTP_MSG_HDR_L1_LWS:
1635 if (likely(HTTP_IS_SPHT(*ptr))) {
1636 /* replace HT,CR,LF with spaces */
1637 for (; buf->data+msg->sov < ptr; msg->sov++)
1638 buf->data[msg->sov] = ' ';
1639 goto http_msg_hdr_l1_sp;
1640 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001641 /* we had a header consisting only in spaces ! */
1642 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 goto http_msg_complete_header;
1644
1645 http_msg_hdr_val:
1646 case HTTP_MSG_HDR_VAL:
1647 /* assumes msg->sol points to the first char, msg->col to the
1648 * colon, and msg->sov points to the first character of the
1649 * value.
1650 */
1651 if (likely(!HTTP_IS_CRLF(*ptr)))
1652 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 msg->eol = ptr;
1655 /* Note: we could also copy eol into ->eoh so that we have the
1656 * real header end in case it ends with lots of LWS, but is this
1657 * really needed ?
1658 */
1659 if (likely(*ptr == '\r'))
1660 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1661 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001662
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 http_msg_hdr_l2_lf:
1664 case HTTP_MSG_HDR_L2_LF:
1665 EXPECT_LF_HERE(ptr, http_msg_invalid);
1666 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 http_msg_hdr_l2_lws:
1669 case HTTP_MSG_HDR_L2_LWS:
1670 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1671 /* LWS: replace HT,CR,LF with spaces */
1672 for (; msg->eol < ptr; msg->eol++)
1673 *msg->eol = ' ';
1674 goto http_msg_hdr_val;
1675 }
1676 http_msg_complete_header:
1677 /*
1678 * It was a new header, so the last one is finished.
1679 * Assumes msg->sol points to the first char, msg->col to the
1680 * colon, msg->sov points to the first character of the value
1681 * and msg->eol to the first CR or LF so we know how the line
1682 * ends. We insert last header into the index.
1683 */
1684 /*
1685 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1686 write(2, msg->sol, msg->eol-msg->sol);
1687 fprintf(stderr,"\n");
1688 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1691 idx, idx->tail) < 0))
1692 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 msg->sol = ptr;
1695 if (likely(!HTTP_IS_CRLF(*ptr))) {
1696 goto http_msg_hdr_name;
1697 }
1698
1699 if (likely(*ptr == '\r'))
1700 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1701 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 http_msg_last_lf:
1704 case HTTP_MSG_LAST_LF:
1705 /* Assumes msg->sol points to the first of either CR or LF */
1706 EXPECT_LF_HERE(ptr, http_msg_invalid);
1707 ptr++;
1708 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001709 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001711 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 return;
1713#ifdef DEBUG_FULL
1714 default:
1715 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1716 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001717#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 }
1719 http_msg_ood:
1720 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001721 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 buf->lr = ptr;
1723 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 http_msg_invalid:
1726 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001727 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001728 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 return;
1730}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001731
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001732/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1733 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1734 * nothing is done and 1 is returned.
1735 */
1736static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1737{
1738 int delta;
1739 char *cur_end;
1740
1741 if (msg->sl.rq.v_l != 0)
1742 return 1;
1743
1744 msg->sol = req->data + msg->som;
1745 cur_end = msg->sol + msg->sl.rq.l;
1746 delta = 0;
1747
1748 if (msg->sl.rq.u_l == 0) {
1749 /* if no URI was set, add "/" */
1750 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1751 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001752 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001753 }
1754 /* add HTTP version */
1755 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001756 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001757 cur_end += delta;
1758 cur_end = (char *)http_parse_reqline(msg, req->data,
1759 HTTP_MSG_RQMETH,
1760 msg->sol, cur_end + 1,
1761 NULL, NULL);
1762 if (unlikely(!cur_end))
1763 return 0;
1764
1765 /* we have a full HTTP/1.0 request now and we know that
1766 * we have either a CR or an LF at <ptr>.
1767 */
1768 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1769 return 1;
1770}
1771
Willy Tarreau5b154472009-12-21 20:11:07 +01001772/* Parse the Connection: headaer of an HTTP request, and set the transaction
1773 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1774 * is also set so that we don't parse a second time. If some dangerous values
1775 * are encountered, we leave the status to indicate that the request might be
1776 * interpreted as keep-alive, but we also set the connection flags to indicate
1777 * that we WANT it to be a close, so that the header will be fixed. This
1778 * function should only be called when we know we're interested in checking
1779 * the request (not a CONNECT, and FE or BE mangles the header).
1780 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001781void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001782{
1783 struct http_msg *msg = &txn->req;
1784 struct hdr_ctx ctx;
1785 int conn_cl, conn_ka;
1786
1787 if (txn->flags & TX_CON_HDR_PARS)
1788 return;
1789
1790 conn_cl = 0;
1791 conn_ka = 0;
1792 ctx.idx = 0;
1793
1794 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1795 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1796 conn_cl = 1;
1797 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1798 conn_ka = 1;
1799 }
1800
1801 /* Determine if the client wishes keep-alive or close.
1802 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1803 * are persistent unless "Connection: close" is explicitly specified.
1804 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1805 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1806 * they explicitly specify "Connection: Keep-Alive", regardless of any
1807 * optional "Keep-Alive" header.
1808 * Note that if we find a request with both "Connection: close" and
1809 * "Connection: Keep-Alive", we indicate we want a close but don't have
1810 * it, so that it can be enforced later.
1811 */
1812
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001813 if (conn_cl && conn_ka) {
1814 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1815 }
1816 else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
Willy Tarreau5b154472009-12-21 20:11:07 +01001817 if (conn_cl) {
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001818 txn->flags |= TX_REQ_CONN_CLO;
1819 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1820 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01001821 }
1822 } else { /* HTTP/1.0 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001823 if (!conn_ka) {
1824 txn->flags |= TX_REQ_CONN_CLO;
1825 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1826 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1827 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001828 }
1829 txn->flags |= TX_CON_HDR_PARS;
1830}
1831
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1833 * first byte of body, and increments msg->sov by the number of bytes parsed,
1834 * so that we know we can forward between ->som and ->sov. Note that due to
1835 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1836 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001837 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001838 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001839 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001841{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 char *ptr = buf->lr;
1843 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001844 unsigned int chunk = 0;
1845
1846 /* The chunk size is in the following form, though we are only
1847 * interested in the size and CRLF :
1848 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1849 */
1850 while (1) {
1851 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 if (c < 0) /* not a hex digit anymore */
1856 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857 if (++ptr >= end)
1858 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001859 if (chunk & 0xF000000) /* overflow will occur */
1860 return -1;
1861 chunk = (chunk << 4) + c;
1862 }
1863
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 /* empty size not allowed */
1865 if (ptr == buf->lr)
1866 return -1;
1867
1868 while (http_is_spht[(unsigned char)*ptr]) {
1869 if (++ptr >= end)
1870 ptr = buf->data;
1871 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001872 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001873 }
1874
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 /* Up to there, we know that at least one byte is present at *ptr. Check
1876 * for the end of chunk size.
1877 */
1878 while (1) {
1879 if (likely(HTTP_IS_CRLF(*ptr))) {
1880 /* we now have a CR or an LF at ptr */
1881 if (likely(*ptr == '\r')) {
1882 if (++ptr >= end)
1883 ptr = buf->data;
1884 if (ptr == buf->r)
1885 return 0;
1886 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001887
Willy Tarreaud98cf932009-12-27 22:54:55 +01001888 if (*ptr != '\n')
1889 return -1;
1890 if (++ptr >= end)
1891 ptr = buf->data;
1892 /* done */
1893 break;
1894 }
1895 else if (*ptr == ';') {
1896 /* chunk extension, ends at next CRLF */
1897 if (++ptr >= end)
1898 ptr = buf->data;
1899 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001900 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901
1902 while (!HTTP_IS_CRLF(*ptr)) {
1903 if (++ptr >= end)
1904 ptr = buf->data;
1905 if (ptr == buf->r)
1906 return 0;
1907 }
1908 /* we have a CRLF now, loop above */
1909 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001910 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 }
1914
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 /* OK we found our CRLF and now <ptr> points to the next byte,
1916 * which may or may not be present. We save that into ->lr and
1917 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 msg->sov += ptr - buf->lr;
1920 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001923 return 1;
1924}
1925
Willy Tarreaud98cf932009-12-27 22:54:55 +01001926/* This function skips trailers in the buffer <buf> associated with HTTP
1927 * message <msg>. The first visited position is buf->lr. If the end of
1928 * the trailers is found, it is automatically scheduled to be forwarded,
1929 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1930 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001931 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1932 * zero. If a parse error is encountered, the function returns < 0 and does not
1933 * change anything except maybe buf->lr and msg->sov. Note that the message
1934 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1935 * which implies that all non-trailers data have already been scheduled for
1936 * forwarding, and that the difference between msg->som and msg->sov exactly
1937 * matches the length of trailers already parsed and not forwarded. It is also
1938 * important to note that this function is designed to be able to parse wrapped
1939 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 */
1941int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1942{
1943 /* we have buf->lr which points to next line. Look for CRLF. */
1944 while (1) {
1945 char *p1 = NULL, *p2 = NULL;
1946 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001947 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948
1949 /* scan current line and stop at LF or CRLF */
1950 while (1) {
1951 if (ptr == buf->r)
1952 return 0;
1953
1954 if (*ptr == '\n') {
1955 if (!p1)
1956 p1 = ptr;
1957 p2 = ptr;
1958 break;
1959 }
1960
1961 if (*ptr == '\r') {
1962 if (p1)
1963 return -1;
1964 p1 = ptr;
1965 }
1966
1967 ptr++;
1968 if (ptr >= buf->data + buf->size)
1969 ptr = buf->data;
1970 }
1971
1972 /* after LF; point to beginning of next line */
1973 p2++;
1974 if (p2 >= buf->data + buf->size)
1975 p2 = buf->data;
1976
Willy Tarreau638cd022010-01-03 07:42:04 +01001977 bytes = p2 - buf->lr;
1978 if (bytes < 0)
1979 bytes += buf->size;
1980
1981 /* schedule this line for forwarding */
1982 msg->sov += bytes;
1983 if (msg->sov >= buf->size)
1984 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985
Willy Tarreau638cd022010-01-03 07:42:04 +01001986 if (p1 == buf->lr) {
1987 /* LF/CRLF at beginning of line => end of trailers at p2.
1988 * Everything was scheduled for forwarding, there's nothing
1989 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001990 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001991 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 msg->msg_state = HTTP_MSG_DONE;
1993 return 1;
1994 }
1995 /* OK, next line then */
1996 buf->lr = p2;
1997 }
1998}
1999
2000/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2001 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2002 * ->som, buf->lr in order to include this part into the next forwarding phase.
2003 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2004 * not enough data are available, the function does not change anything and
2005 * returns zero. If a parse error is encountered, the function returns < 0 and
2006 * does not change anything. Note: this function is designed to parse wrapped
2007 * CRLF at the end of the buffer.
2008 */
2009int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2010{
2011 char *ptr;
2012 int bytes;
2013
2014 /* NB: we'll check data availabilty at the end. It's not a
2015 * problem because whatever we match first will be checked
2016 * against the correct length.
2017 */
2018 bytes = 1;
2019 ptr = buf->lr;
2020 if (*ptr == '\r') {
2021 bytes++;
2022 ptr++;
2023 if (ptr >= buf->data + buf->size)
2024 ptr = buf->data;
2025 }
2026
2027 if (buf->l < bytes)
2028 return 0;
2029
2030 if (*ptr != '\n')
2031 return -1;
2032
2033 ptr++;
2034 if (ptr >= buf->data + buf->size)
2035 ptr = buf->data;
2036 buf->lr = ptr;
2037 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2038 msg->sov = ptr - buf->data;
2039 msg->som = msg->sov - bytes;
2040 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2041 return 1;
2042}
Willy Tarreau5b154472009-12-21 20:11:07 +01002043
Willy Tarreau83e3af02009-12-28 17:39:57 +01002044void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2045{
2046 char *end = buf->data + buf->size;
2047 int off = buf->data + buf->size - buf->w;
2048
2049 /* two possible cases :
2050 * - the buffer is in one contiguous block, we move it in-place
2051 * - the buffer is in two blocks, we move it via the trash
2052 */
2053 if (buf->l) {
2054 int block1 = buf->l;
2055 int block2 = 0;
2056 if (buf->r <= buf->w) {
2057 /* non-contiguous block */
2058 block1 = buf->data + buf->size - buf->w;
2059 block2 = buf->r - buf->data;
2060 }
2061 if (block2)
2062 memcpy(trash, buf->data, block2);
2063 memmove(buf->data, buf->w, block1);
2064 if (block2)
2065 memcpy(buf->data + block1, trash, block2);
2066 }
2067
2068 /* adjust all known pointers */
2069 buf->w = buf->data;
2070 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2071 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2072 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2073 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2074
2075 /* adjust relative pointers */
2076 msg->som = 0;
2077 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2078 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2079 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2080
2081 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2082 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2083
2084 if (msg->err_pos >= 0) {
2085 msg->err_pos += off;
2086 if (msg->err_pos >= buf->size)
2087 msg->err_pos -= buf->size;
2088 }
2089
2090 buf->flags &= ~BF_FULL;
2091 if (buf->l >= buffer_max_len(buf))
2092 buf->flags |= BF_FULL;
2093}
2094
Willy Tarreaud787e662009-07-07 10:14:51 +02002095/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2096 * processing can continue on next analysers, or zero if it either needs more
2097 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2098 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2099 * when it has nothing left to do, and may remove any analyser when it wants to
2100 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002101 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002102int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002103{
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 /*
2105 * We will parse the partial (or complete) lines.
2106 * We will check the request syntax, and also join multi-line
2107 * headers. An index of all the lines will be elaborated while
2108 * parsing.
2109 *
2110 * For the parsing, we use a 28 states FSM.
2111 *
2112 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002113 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002114 * req->data + msg->eoh = end of processed headers / start of current one
2115 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 * req->lr = first non-visited byte
2117 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002118 *
2119 * At end of parsing, we may perform a capture of the error (if any), and
2120 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2121 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2122 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002124
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002126 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 struct http_txn *txn = &s->txn;
2128 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002129 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002130
Willy Tarreau6bf17362009-02-24 10:48:35 +01002131 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2132 now_ms, __FUNCTION__,
2133 s,
2134 req,
2135 req->rex, req->wex,
2136 req->flags,
2137 req->l,
2138 req->analysers);
2139
Willy Tarreau52a0c602009-08-16 22:45:38 +02002140 /* we're speaking HTTP here, so let's speak HTTP to the client */
2141 s->srv_error = http_return_srv_error;
2142
Willy Tarreau83e3af02009-12-28 17:39:57 +01002143 /* There's a protected area at the end of the buffer for rewriting
2144 * purposes. We don't want to start to parse the request if the
2145 * protected area is affected, because we may have to move processed
2146 * data later, which is much more complicated.
2147 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002148 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002149 if ((txn->flags & TX_NOT_FIRST) &&
2150 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002151 req->r < req->lr ||
2152 req->r > req->data + req->size - global.tune.maxrewrite)) {
2153 if (req->send_max) {
2154 /* some data has still not left the buffer, wake us once that's done */
2155 buffer_dont_connect(req);
2156 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2157 return 0;
2158 }
2159 if (req->l <= req->size - global.tune.maxrewrite)
2160 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002161 }
2162
Willy Tarreau065e8332010-01-08 00:30:20 +01002163 /* Note that we have the same problem with the response ; we
2164 * may want to send a redirect, error or anything which requires
2165 * some spare space. So we'll ensure that we have at least
2166 * maxrewrite bytes available in the response buffer before
2167 * processing that one. This will only affect pipelined
2168 * keep-alive requests.
2169 */
2170 if ((txn->flags & TX_NOT_FIRST) &&
2171 unlikely((s->rep->flags & BF_FULL) ||
2172 s->rep->r < s->rep->lr ||
2173 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2174 if (s->rep->send_max) {
2175 /* don't let a connection request be initiated */
2176 buffer_dont_connect(req);
2177 return 0;
2178 }
2179 }
2180
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002181 if (likely(req->lr < req->r))
2182 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002183 }
2184
Willy Tarreau59234e92008-11-30 23:51:27 +01002185 /* 1: we might have to print this header in debug mode */
2186 if (unlikely((global.mode & MODE_DEBUG) &&
2187 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002188 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002190
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 sol = req->data + msg->som;
2192 eol = sol + msg->sl.rq.l;
2193 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002194
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 sol += hdr_idx_first_pos(&txn->hdr_idx);
2196 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 while (cur_idx) {
2199 eol = sol + txn->hdr_idx.v[cur_idx].len;
2200 debug_hdr("clihdr", s, sol, eol);
2201 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2202 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002203 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 }
2205
Willy Tarreau58f10d72006-12-04 02:26:12 +01002206
Willy Tarreau59234e92008-11-30 23:51:27 +01002207 /*
2208 * Now we quickly check if we have found a full valid request.
2209 * If not so, we check the FD and buffer states before leaving.
2210 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002211 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002212 * requests are checked first.
2213 *
2214 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002215
Willy Tarreau655dce92009-11-08 13:10:58 +01002216 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002217 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002218 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002219 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2221 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002222
Willy Tarreau59234e92008-11-30 23:51:27 +01002223 /* 1: Since we are in header mode, if there's no space
2224 * left for headers, we won't be able to free more
2225 * later, so the session will never terminate. We
2226 * must terminate it now.
2227 */
2228 if (unlikely(req->flags & BF_FULL)) {
2229 /* FIXME: check if URI is set and return Status
2230 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002231 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 goto return_bad_req;
2233 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002234
Willy Tarreau59234e92008-11-30 23:51:27 +01002235 /* 2: have we encountered a read error ? */
2236 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002237 if (txn->flags & TX_NOT_FIRST)
2238 goto failed_keep_alive;
2239
Willy Tarreau59234e92008-11-30 23:51:27 +01002240 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002241 if (msg->err_pos >= 0)
2242 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002243 msg->msg_state = HTTP_MSG_ERROR;
2244 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002245
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002246 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002247 if (s->listener->counters)
2248 s->listener->counters->failed_req++;
2249
Willy Tarreau59234e92008-11-30 23:51:27 +01002250 if (!(s->flags & SN_ERR_MASK))
2251 s->flags |= SN_ERR_CLICL;
2252 if (!(s->flags & SN_FINST_MASK))
2253 s->flags |= SN_FINST_R;
2254 return 0;
2255 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002256
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 /* 3: has the read timeout expired ? */
2258 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002259 if (txn->flags & TX_NOT_FIRST)
2260 goto failed_keep_alive;
2261
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002263 if (msg->err_pos >= 0)
2264 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 txn->status = 408;
2266 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2267 msg->msg_state = HTTP_MSG_ERROR;
2268 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002269
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002270 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002271 if (s->listener->counters)
2272 s->listener->counters->failed_req++;
2273
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 if (!(s->flags & SN_ERR_MASK))
2275 s->flags |= SN_ERR_CLITO;
2276 if (!(s->flags & SN_FINST_MASK))
2277 s->flags |= SN_FINST_R;
2278 return 0;
2279 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002280
Willy Tarreau59234e92008-11-30 23:51:27 +01002281 /* 4: have we encountered a close ? */
2282 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002283 if (txn->flags & TX_NOT_FIRST)
2284 goto failed_keep_alive;
2285
Willy Tarreau4076a152009-04-02 15:18:36 +02002286 if (msg->err_pos >= 0)
2287 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 txn->status = 400;
2289 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2290 msg->msg_state = HTTP_MSG_ERROR;
2291 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002292
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002293 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002294 if (s->listener->counters)
2295 s->listener->counters->failed_req++;
2296
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 if (!(s->flags & SN_ERR_MASK))
2298 s->flags |= SN_ERR_CLICL;
2299 if (!(s->flags & SN_FINST_MASK))
2300 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002301 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002302 }
2303
Willy Tarreau520d95e2009-09-19 21:04:57 +02002304 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002305 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2306
Willy Tarreau59234e92008-11-30 23:51:27 +01002307 /* just set the request timeout once at the beginning of the request */
2308 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002309 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 /* we're not ready yet */
2312 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002313
2314 failed_keep_alive:
2315 /* Here we process low-level errors for keep-alive requests. In
2316 * short, if the request is not the first one and it experiences
2317 * a timeout, read error or shutdown, we just silently close so
2318 * that the client can try again.
2319 */
2320 txn->status = 0;
2321 msg->msg_state = HTTP_MSG_RQBEFORE;
2322 req->analysers = 0;
2323 s->logs.logwait = 0;
2324 stream_int_cond_close(req->prod, NULL);
2325 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002327
Willy Tarreaud787e662009-07-07 10:14:51 +02002328 /* OK now we have a complete HTTP request with indexed headers. Let's
2329 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002330 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2331 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2332 * points to the CRLF of the request line. req->lr points to the first
2333 * byte after the last LF. msg->col and msg->sov point to the first
2334 * byte of data. msg->eol cannot be trusted because it may have been
2335 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002336 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002337
Willy Tarreaud787e662009-07-07 10:14:51 +02002338 /* Maybe we found in invalid header name while we were configured not
2339 * to block on that, so we have to capture it now.
2340 */
2341 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002342 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /* ensure we keep this pointer to the beginning of the message */
2345 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 /*
2348 * 1: identify the method
2349 */
2350 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2351
2352 /* we can make use of server redirect on GET and HEAD */
2353 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2354 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 /*
2357 * 2: check if the URI matches the monitor_uri.
2358 * We have to do this for every request which gets in, because
2359 * the monitor-uri is defined by the frontend.
2360 */
2361 if (unlikely((s->fe->monitor_uri_len != 0) &&
2362 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2363 !memcmp(&req->data[msg->sl.rq.u],
2364 s->fe->monitor_uri,
2365 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002366 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002368 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002370
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002372
Willy Tarreau59234e92008-11-30 23:51:27 +01002373 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002374 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2375 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002376
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 ret = acl_pass(ret);
2378 if (cond->pol == ACL_COND_UNLESS)
2379 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (ret) {
2382 /* we fail this request, let's return 503 service unavail */
2383 txn->status = 503;
2384 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2385 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002386 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002387 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002388
Willy Tarreau59234e92008-11-30 23:51:27 +01002389 /* nothing to fail, let's reply normaly */
2390 txn->status = 200;
2391 stream_int_retnclose(req->prod, &http_200_chunk);
2392 goto return_prx_cond;
2393 }
2394
2395 /*
2396 * 3: Maybe we have to copy the original REQURI for the logs ?
2397 * Note: we cannot log anymore if the request has been
2398 * classified as invalid.
2399 */
2400 if (unlikely(s->logs.logwait & LW_REQ)) {
2401 /* we have a complete HTTP request that we must log */
2402 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2403 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002404
Willy Tarreau59234e92008-11-30 23:51:27 +01002405 if (urilen >= REQURI_LEN)
2406 urilen = REQURI_LEN - 1;
2407 memcpy(txn->uri, &req->data[msg->som], urilen);
2408 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002409
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 if (!(s->logs.logwait &= ~LW_REQ))
2411 s->do_log(s);
2412 } else {
2413 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002414 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002415 }
Willy Tarreau06619262006-12-17 08:37:22 +01002416
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002418 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2419 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002420
Willy Tarreau5b154472009-12-21 20:11:07 +01002421 /* ... and check if the request is HTTP/1.1 or above */
2422 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002423 ((req->data[msg->sl.rq.v + 5] > '1') ||
2424 ((req->data[msg->sl.rq.v + 5] == '1') &&
2425 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002426 txn->flags |= TX_REQ_VER_11;
2427
2428 /* "connection" has not been parsed yet */
2429 txn->flags &= ~TX_CON_HDR_PARS;
2430
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002431 /* transfer length unknown*/
2432 txn->flags &= ~TX_REQ_XFER_LEN;
2433
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 /* 5: we may need to capture headers */
2435 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2436 capture_headers(req->data + msg->som, &txn->hdr_idx,
2437 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002438
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002439 /* 6: determine the transfer-length.
2440 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2441 * the presence of a message-body in a REQUEST and its transfer length
2442 * must be determined that way (in order of precedence) :
2443 * 1. The presence of a message-body in a request is signaled by the
2444 * inclusion of a Content-Length or Transfer-Encoding header field
2445 * in the request's header fields. When a request message contains
2446 * both a message-body of non-zero length and a method that does
2447 * not define any semantics for that request message-body, then an
2448 * origin server SHOULD either ignore the message-body or respond
2449 * with an appropriate error message (e.g., 413). A proxy or
2450 * gateway, when presented the same request, SHOULD either forward
2451 * the request inbound with the message- body or ignore the
2452 * message-body when determining a response.
2453 *
2454 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2455 * and the "chunked" transfer-coding (Section 6.2) is used, the
2456 * transfer-length is defined by the use of this transfer-coding.
2457 * If a Transfer-Encoding header field is present and the "chunked"
2458 * transfer-coding is not present, the transfer-length is defined
2459 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002460 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002461 * 3. If a Content-Length header field is present, its decimal value in
2462 * OCTETs represents both the entity-length and the transfer-length.
2463 * If a message is received with both a Transfer-Encoding header
2464 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002465 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002466 * 4. By the server closing the connection. (Closing the connection
2467 * cannot be used to indicate the end of a request body, since that
2468 * would leave no possibility for the server to send back a response.)
2469 *
2470 * Whenever a transfer-coding is applied to a message-body, the set of
2471 * transfer-codings MUST include "chunked", unless the message indicates
2472 * it is terminated by closing the connection. When the "chunked"
2473 * transfer-coding is used, it MUST be the last transfer-coding applied
2474 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002475 */
2476
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 /* CONNECT sets a tunnel and ignores everything else */
2478 if (txn->meth == HTTP_METH_CONNECT)
2479 goto skip_xfer_len;
2480
2481 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002482 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002483 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002484 while ((txn->flags & TX_REQ_VER_11) &&
2485 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002486 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2487 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2488 else if (txn->flags & TX_REQ_TE_CHNK) {
2489 /* bad transfer-encoding (chunked followed by something else) */
2490 use_close_only = 1;
2491 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2492 break;
2493 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002494 }
2495
Willy Tarreau32b47f42009-10-18 20:55:02 +02002496 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002497 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2499 signed long long cl;
2500
2501 if (!ctx.vlen)
2502 goto return_bad_req;
2503
2504 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2505 goto return_bad_req; /* parse failure */
2506
2507 if (cl < 0)
2508 goto return_bad_req;
2509
2510 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2511 goto return_bad_req; /* already specified, was different */
2512
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002513 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002514 msg->hdr_content_len = cl;
2515 }
2516
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002517 /* bodyless requests have a known length */
2518 if (!use_close_only)
2519 txn->flags |= TX_REQ_XFER_LEN;
2520
2521 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002522 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002523 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002524 req->analyse_exp = TICK_ETERNITY;
2525 return 1;
2526
2527 return_bad_req:
2528 /* We centralize bad requests processing here */
2529 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2530 /* we detected a parsing error. We want to archive this request
2531 * in the dedicated proxy area for later troubleshooting.
2532 */
2533 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2534 }
2535
2536 txn->req.msg_state = HTTP_MSG_ERROR;
2537 txn->status = 400;
2538 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002539
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002540 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002541 if (s->listener->counters)
2542 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002543
2544 return_prx_cond:
2545 if (!(s->flags & SN_ERR_MASK))
2546 s->flags |= SN_ERR_PRXCOND;
2547 if (!(s->flags & SN_FINST_MASK))
2548 s->flags |= SN_FINST_R;
2549
2550 req->analysers = 0;
2551 req->analyse_exp = TICK_ETERNITY;
2552 return 0;
2553}
2554
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002555/* This stream analyser runs all HTTP request processing which is common to
2556 * frontends and backends, which means blocking ACLs, filters, connection-close,
2557 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002559 * either needs more data or wants to immediately abort the request (eg: deny,
2560 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002561 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002562int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002563{
Willy Tarreaud787e662009-07-07 10:14:51 +02002564 struct http_txn *txn = &s->txn;
2565 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002566 struct acl_cond *cond;
2567 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002568 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002569 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002570
Willy Tarreau655dce92009-11-08 13:10:58 +01002571 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002572 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002573 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002574 return 0;
2575 }
2576
Willy Tarreau3a816292009-07-07 10:55:49 +02002577 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002578 req->analyse_exp = TICK_ETERNITY;
2579
2580 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2581 now_ms, __FUNCTION__,
2582 s,
2583 req,
2584 req->rex, req->wex,
2585 req->flags,
2586 req->l,
2587 req->analysers);
2588
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002589 /* first check whether we have some ACLs set to block this request */
2590 list_for_each_entry(cond, &px->block_cond, list) {
2591 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002592
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002593 ret = acl_pass(ret);
2594 if (cond->pol == ACL_COND_UNLESS)
2595 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002596
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002597 if (ret) {
2598 txn->status = 403;
2599 /* let's log the request time */
2600 s->logs.tv_request = now;
2601 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2602 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002603 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002604 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002605
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002606 /* try headers filters */
2607 if (px->req_exp != NULL) {
2608 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2609 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002610
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 /* has the request been denied ? */
2612 if (txn->flags & TX_CLDENY) {
2613 /* no need to go further */
2614 txn->status = 403;
2615 /* let's log the request time */
2616 s->logs.tv_request = now;
2617 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2618 goto return_prx_cond;
2619 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002620
2621 /* When a connection is tarpitted, we use the tarpit timeout,
2622 * which may be the same as the connect timeout if unspecified.
2623 * If unset, then set it to zero because we really want it to
2624 * eventually expire. We build the tarpit as an analyser.
2625 */
2626 if (txn->flags & TX_CLTARPIT) {
2627 buffer_erase(s->req);
2628 /* wipe the request out so that we can drop the connection early
2629 * if the client closes first.
2630 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002631 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002632 req->analysers = 0; /* remove switching rules etc... */
2633 req->analysers |= AN_REQ_HTTP_TARPIT;
2634 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2635 if (!req->analyse_exp)
2636 req->analyse_exp = tick_add(now_ms, 0);
2637 return 1;
2638 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002639 }
Willy Tarreau06619262006-12-17 08:37:22 +01002640
Willy Tarreau5b154472009-12-21 20:11:07 +01002641 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2642 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002643 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2644 * in which headers are mangled. However, if another mode is set, it will
2645 * affect it (eg: server-close/keep-alive + httpclose = close).
Willy Tarreau42736642009-10-18 21:04:35 +02002646 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002647
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002648 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002649 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002650 int tmp = TX_CON_WANT_TUN;
2651 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2652 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002653 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2654 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002655 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002656 tmp = TX_CON_WANT_CLO;
2657
Willy Tarreau5b154472009-12-21 20:11:07 +01002658 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2659 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002660
Willy Tarreau8db1c172010-01-05 23:12:12 +01002661 if (!(txn->flags & TX_CON_HDR_PARS))
2662 http_req_parse_connection_header(txn);
2663
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002664 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2665 if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)
2666 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2667 if (!(txn->flags & TX_REQ_XFER_LEN))
2668 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2669 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002670 }
2671
2672 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2673 * once we know the backend, because the tunnel mode can be implied by the
2674 * lack of any close/keepalive options in both the FE and the BE. Since
2675 * this information can evolve with time, we proceed by trying to make the
2676 * header status match the desired status. For this, we'll have to adjust
2677 * the "Connection" header. The test for persistent connections has already
2678 * been performed, so we only enter here if there is a risk the connection
2679 * is considered as persistent and we want it to be closed on the server
2680 * side. It would be nice if we could enter this place only when a
2681 * Connection header exists. Note that a CONNECT method will not enter
2682 * here.
2683 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002684 if (!(txn->flags & TX_REQ_CONN_CLO) &&
2685 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
2686 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002687 char *cur_ptr, *cur_end, *cur_next;
2688 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002689 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002690 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002691
Willy Tarreau5b154472009-12-21 20:11:07 +01002692 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002693 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002694
Willy Tarreau5b154472009-12-21 20:11:07 +01002695 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002696 cur_hdr = &txn->hdr_idx.v[cur_idx];
2697 cur_ptr = cur_next;
2698 cur_end = cur_ptr + cur_hdr->len;
2699 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002700
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002701 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002702 if (!val)
2703 continue;
2704
2705 /* 3 possibilities :
2706 * - we have already set "Connection: close" or we're in
2707 * HTTP/1.0, so we remove this line.
2708 * - we have not yet set "Connection: close", but this line
2709 * indicates close. We leave it untouched and set the flag.
2710 * - we have not yet set "Connection: close", and this line
2711 * indicates non-close. We replace it and set the flag.
2712 */
2713 if (must_delete) {
2714 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2715 http_msg_move_end(&txn->req, delta);
2716 cur_next += delta;
2717 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2718 txn->hdr_idx.used--;
2719 cur_hdr->len = 0;
2720 txn->flags |= TX_REQ_CONN_CLO;
2721 } else {
2722 if (cur_end - cur_ptr - val != 5 ||
2723 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2724 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2725 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002726 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002727 cur_hdr->len += delta;
2728 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002729 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002730 txn->flags |= TX_REQ_CONN_CLO;
2731 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002732 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002733 } /* for loop */
2734 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002735
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002736 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002737 list_for_each_entry(wl, &px->req_add, list) {
2738 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002739 goto return_bad_req;
2740 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002741
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002742 /* check if stats URI was requested, and if an auth is needed */
2743 if (px->uri_auth != NULL &&
2744 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2745 /* we have to check the URI and auth for this request.
2746 * FIXME!!! that one is rather dangerous, we want to
2747 * make it follow standard rules (eg: clear req->analysers).
2748 */
2749 if (stats_check_uri_auth(s, px)) {
2750 req->analysers = 0;
2751 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002752 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002753 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002754
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755 /* check whether we have some ACLs set to redirect this request */
2756 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002757 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002758
Willy Tarreauf285f542010-01-03 20:03:03 +01002759 if (rule->cond) {
2760 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2761 ret = acl_pass(ret);
2762 if (rule->cond->pol == ACL_COND_UNLESS)
2763 ret = !ret;
2764 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002765
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002766 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002767 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002768 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002769
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770 /* build redirect message */
2771 switch(rule->code) {
2772 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 msg_fmt = HTTP_303;
2774 break;
2775 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002776 msg_fmt = HTTP_301;
2777 break;
2778 case 302:
2779 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002780 msg_fmt = HTTP_302;
2781 break;
2782 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002783
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002784 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002785 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002786
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002787 switch(rule->type) {
2788 case REDIRECT_TYPE_PREFIX: {
2789 const char *path;
2790 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002791
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002792 path = http_get_path(txn);
2793 /* build message using path */
2794 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002795 pathlen = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002796 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2797 int qs = 0;
2798 while (qs < pathlen) {
2799 if (path[qs] == '?') {
2800 pathlen = qs;
2801 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002802 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002804 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002805 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002806 } else {
2807 path = "/";
2808 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002809 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002810
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002811 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002812 goto return_bad_req;
2813
2814 /* add prefix. Note that if prefix == "/", we don't want to
2815 * add anything, otherwise it makes it hard for the user to
2816 * configure a self-redirection.
2817 */
2818 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002819 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2820 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002821 }
2822
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002823 /* add path */
2824 memcpy(rdr.str + rdr.len, path, pathlen);
2825 rdr.len += pathlen;
2826 break;
2827 }
2828 case REDIRECT_TYPE_LOCATION:
2829 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002830 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002831 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 /* add location */
2834 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2835 rdr.len += rule->rdr_len;
2836 break;
2837 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002838
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002839 if (rule->cookie_len) {
2840 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2841 rdr.len += 14;
2842 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2843 rdr.len += rule->cookie_len;
2844 memcpy(rdr.str + rdr.len, "\r\n", 2);
2845 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002846 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002847
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002848 /* add end of headers and the keep-alive/close status.
2849 * We may choose to set keep-alive if the Location begins
2850 * with a slash, because the client will come back to the
2851 * same server.
2852 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002853 txn->status = rule->code;
2854 /* let's log the request time */
2855 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002856
2857 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2858 (txn->flags & TX_REQ_XFER_LEN) &&
2859 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2860 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2861 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2862 /* keep-alive possible */
2863 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2864 rdr.len += 28;
2865 buffer_write(req->prod->ob, rdr.str, rdr.len);
2866 /* "eat" the request */
2867 buffer_ignore(req, msg->sov - msg->som);
2868 msg->som = msg->sov;
2869 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01002870 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
2871 txn->req.msg_state = HTTP_MSG_CLOSED;
2872 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002873 break;
2874 } else {
2875 /* keep-alive not possible */
2876 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2877 rdr.len += 23;
Willy Tarreauea65e682010-01-08 00:26:50 +01002878 buffer_write(req->prod->ob, rdr.str, rdr.len);
2879 stream_int_cond_close(req->prod, NULL);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002880 goto return_prx_cond;
2881 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002882 }
2883 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002884
Willy Tarreau2be39392010-01-03 17:24:51 +01002885 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2886 * If this happens, then the data will not come immediately, so we must
2887 * send all what we have without waiting. Note that due to the small gain
2888 * in waiting for the body of the request, it's easier to simply put the
2889 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2890 * itself once used.
2891 */
2892 req->flags |= BF_SEND_DONTWAIT;
2893
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002894 /* that's OK for us now, let's move on to next analysers */
2895 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002896
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002897 return_bad_req:
2898 /* We centralize bad requests processing here */
2899 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2900 /* we detected a parsing error. We want to archive this request
2901 * in the dedicated proxy area for later troubleshooting.
2902 */
2903 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2904 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002905
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002906 txn->req.msg_state = HTTP_MSG_ERROR;
2907 txn->status = 400;
2908 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002909
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002910 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002911 if (s->listener->counters)
2912 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002914 return_prx_cond:
2915 if (!(s->flags & SN_ERR_MASK))
2916 s->flags |= SN_ERR_PRXCOND;
2917 if (!(s->flags & SN_FINST_MASK))
2918 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002919
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002920 req->analysers = 0;
2921 req->analyse_exp = TICK_ETERNITY;
2922 return 0;
2923}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002924
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002925/* This function performs all the processing enabled for the current request.
2926 * It returns 1 if the processing can continue on next analysers, or zero if it
2927 * needs more data, encounters an error, or wants to immediately abort the
2928 * request. It relies on buffers flags, and updates s->req->analysers.
2929 */
2930int http_process_request(struct session *s, struct buffer *req, int an_bit)
2931{
2932 struct http_txn *txn = &s->txn;
2933 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002934
Willy Tarreau655dce92009-11-08 13:10:58 +01002935 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002936 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002937 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002938 return 0;
2939 }
2940
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002941 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2942 now_ms, __FUNCTION__,
2943 s,
2944 req,
2945 req->rex, req->wex,
2946 req->flags,
2947 req->l,
2948 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002949
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 /*
2951 * Right now, we know that we have processed the entire headers
2952 * and that unwanted requests have been filtered out. We can do
2953 * whatever we want with the remaining request. Also, now we
2954 * may have separate values for ->fe, ->be.
2955 */
Willy Tarreau06619262006-12-17 08:37:22 +01002956
Willy Tarreau59234e92008-11-30 23:51:27 +01002957 /*
2958 * If HTTP PROXY is set we simply get remote server address
2959 * parsing incoming request.
2960 */
2961 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2962 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2963 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002964
Willy Tarreau59234e92008-11-30 23:51:27 +01002965 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002966 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 * Note that doing so might move headers in the request, but
2968 * the fields will stay coherent and the URI will not move.
2969 * This should only be performed in the backend.
2970 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002971 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002972 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2973 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002974
Willy Tarreau59234e92008-11-30 23:51:27 +01002975 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002976 * 8: the appsession cookie was looked up very early in 1.2,
2977 * so let's do the same now.
2978 */
2979
2980 /* It needs to look into the URI */
2981 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002982 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002983 }
2984
2985 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002986 * 9: add X-Forwarded-For if either the frontend or the backend
2987 * asks for it.
2988 */
2989 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2990 if (s->cli_addr.ss_family == AF_INET) {
2991 /* Add an X-Forwarded-For header unless the source IP is
2992 * in the 'except' network range.
2993 */
2994 if ((!s->fe->except_mask.s_addr ||
2995 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2996 != s->fe->except_net.s_addr) &&
2997 (!s->be->except_mask.s_addr ||
2998 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2999 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003000 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003001 unsigned char *pn;
3002 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003003
3004 /* Note: we rely on the backend to get the header name to be used for
3005 * x-forwarded-for, because the header is really meant for the backends.
3006 * However, if the backend did not specify any option, we have to rely
3007 * on the frontend's header name.
3008 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003009 if (s->be->fwdfor_hdr_len) {
3010 len = s->be->fwdfor_hdr_len;
3011 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003012 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003013 len = s->fe->fwdfor_hdr_len;
3014 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003015 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003016 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003017
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003018 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003019 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003020 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003021 }
3022 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003023 else if (s->cli_addr.ss_family == AF_INET6) {
3024 /* FIXME: for the sake of completeness, we should also support
3025 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003026 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 int len;
3028 char pn[INET6_ADDRSTRLEN];
3029 inet_ntop(AF_INET6,
3030 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3031 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003032
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 /* Note: we rely on the backend to get the header name to be used for
3034 * x-forwarded-for, because the header is really meant for the backends.
3035 * However, if the backend did not specify any option, we have to rely
3036 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003037 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003038 if (s->be->fwdfor_hdr_len) {
3039 len = s->be->fwdfor_hdr_len;
3040 memcpy(trash, s->be->fwdfor_hdr_name, len);
3041 } else {
3042 len = s->fe->fwdfor_hdr_len;
3043 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003044 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003045 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003046
Willy Tarreau59234e92008-11-30 23:51:27 +01003047 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003048 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003049 goto return_bad_req;
3050 }
3051 }
3052
3053 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003054 * 10: add X-Original-To if either the frontend or the backend
3055 * asks for it.
3056 */
3057 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3058
3059 /* FIXME: don't know if IPv6 can handle that case too. */
3060 if (s->cli_addr.ss_family == AF_INET) {
3061 /* Add an X-Original-To header unless the destination IP is
3062 * in the 'except' network range.
3063 */
3064 if (!(s->flags & SN_FRT_ADDR_SET))
3065 get_frt_addr(s);
3066
3067 if ((!s->fe->except_mask_to.s_addr ||
3068 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3069 != s->fe->except_to.s_addr) &&
3070 (!s->be->except_mask_to.s_addr ||
3071 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3072 != s->be->except_to.s_addr)) {
3073 int len;
3074 unsigned char *pn;
3075 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3076
3077 /* Note: we rely on the backend to get the header name to be used for
3078 * x-original-to, because the header is really meant for the backends.
3079 * However, if the backend did not specify any option, we have to rely
3080 * on the frontend's header name.
3081 */
3082 if (s->be->orgto_hdr_len) {
3083 len = s->be->orgto_hdr_len;
3084 memcpy(trash, s->be->orgto_hdr_name, len);
3085 } else {
3086 len = s->fe->orgto_hdr_len;
3087 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003088 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003089 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3090
3091 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003092 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003093 goto return_bad_req;
3094 }
3095 }
3096 }
3097
Willy Tarreau78599912009-10-17 20:12:21 +02003098 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003099 if (!(txn->flags & TX_REQ_CONN_CLO) &&
3100 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3101 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau78599912009-10-17 20:12:21 +02003102 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003103 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003104 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003105 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003107
3108 /* If we have no server assigned yet and we're balancing on url_param
3109 * with a POST request, we may be interested in checking the body for
3110 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003111 */
3112 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3113 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003114 s->be->url_param_post_limit != 0 &&
3115 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003116 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003117 buffer_dont_connect(req);
3118 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003119 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003120
Willy Tarreaud98cf932009-12-27 22:54:55 +01003121 if (txn->flags & TX_REQ_XFER_LEN)
3122 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003123
Willy Tarreau59234e92008-11-30 23:51:27 +01003124 /*************************************************************
3125 * OK, that's finished for the headers. We have done what we *
3126 * could. Let's switch to the DATA state. *
3127 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003128 req->analyse_exp = TICK_ETERNITY;
3129 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003130
Willy Tarreau59234e92008-11-30 23:51:27 +01003131 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003132 /* OK let's go on with the BODY now */
3133 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003134
Willy Tarreau59234e92008-11-30 23:51:27 +01003135 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003136 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003137 /* we detected a parsing error. We want to archive this request
3138 * in the dedicated proxy area for later troubleshooting.
3139 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003140 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003141 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003142
Willy Tarreau59234e92008-11-30 23:51:27 +01003143 txn->req.msg_state = HTTP_MSG_ERROR;
3144 txn->status = 400;
3145 req->analysers = 0;
3146 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003147
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003148 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003149 if (s->listener->counters)
3150 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003151
Willy Tarreau59234e92008-11-30 23:51:27 +01003152 if (!(s->flags & SN_ERR_MASK))
3153 s->flags |= SN_ERR_PRXCOND;
3154 if (!(s->flags & SN_FINST_MASK))
3155 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003156 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003157}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003158
Willy Tarreau60b85b02008-11-30 23:28:40 +01003159/* This function is an analyser which processes the HTTP tarpit. It always
3160 * returns zero, at the beginning because it prevents any other processing
3161 * from occurring, and at the end because it terminates the request.
3162 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003163int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003164{
3165 struct http_txn *txn = &s->txn;
3166
3167 /* This connection is being tarpitted. The CLIENT side has
3168 * already set the connect expiration date to the right
3169 * timeout. We just have to check that the client is still
3170 * there and that the timeout has not expired.
3171 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003172 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003173 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3174 !tick_is_expired(req->analyse_exp, now_ms))
3175 return 0;
3176
3177 /* We will set the queue timer to the time spent, just for
3178 * logging purposes. We fake a 500 server error, so that the
3179 * attacker will not suspect his connection has been tarpitted.
3180 * It will not cause trouble to the logs because we can exclude
3181 * the tarpitted connections by filtering on the 'PT' status flags.
3182 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003183 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3184
3185 txn->status = 500;
3186 if (req->flags != BF_READ_ERROR)
3187 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3188
3189 req->analysers = 0;
3190 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003191
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003192 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003193 if (s->listener->counters)
3194 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003195
Willy Tarreau60b85b02008-11-30 23:28:40 +01003196 if (!(s->flags & SN_ERR_MASK))
3197 s->flags |= SN_ERR_PRXCOND;
3198 if (!(s->flags & SN_FINST_MASK))
3199 s->flags |= SN_FINST_T;
3200 return 0;
3201}
3202
Willy Tarreaud34af782008-11-30 23:36:37 +01003203/* This function is an analyser which processes the HTTP request body. It looks
3204 * for parameters to be used for the load balancing algorithm (url_param). It
3205 * must only be called after the standard HTTP request processing has occurred,
3206 * because it expects the request to be parsed. It returns zero if it needs to
3207 * read more data, or 1 once it has completed its analysis.
3208 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003209int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003210{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003211 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003212 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003213 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003214
3215 /* We have to parse the HTTP request body to find any required data.
3216 * "balance url_param check_post" should have been the only way to get
3217 * into this. We were brought here after HTTP header analysis, so all
3218 * related structures are ready.
3219 */
3220
Willy Tarreau522d6c02009-12-06 18:49:18 +01003221 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3222 goto missing_data;
3223
3224 if (msg->msg_state < HTTP_MSG_100_SENT) {
3225 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3226 * send an HTTP/1.1 100 Continue intermediate response.
3227 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003228 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003229 struct hdr_ctx ctx;
3230 ctx.idx = 0;
3231 /* Expect is allowed in 1.1, look for it */
3232 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3233 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3234 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3235 }
3236 }
3237 msg->msg_state = HTTP_MSG_100_SENT;
3238 }
3239
3240 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003241 /* we have msg->col and msg->sov which both point to the first
3242 * byte of message body. msg->som still points to the beginning
3243 * of the message. We must save the body in req->lr because it
3244 * survives buffer re-alignments.
3245 */
3246 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003247 if (txn->flags & TX_REQ_TE_CHNK)
3248 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3249 else
3250 msg->msg_state = HTTP_MSG_DATA;
3251 }
3252
3253 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003254 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003255 * set ->sov and ->lr to point to the body and switch to DATA or
3256 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003257 */
3258 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003259
Willy Tarreau115acb92009-12-26 13:56:06 +01003260 if (!ret)
3261 goto missing_data;
3262 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003263 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003264 }
3265
Willy Tarreaud98cf932009-12-27 22:54:55 +01003266 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003267 * We have the first non-header byte in msg->col, which is either the
3268 * beginning of the chunk size or of the data. The first data byte is in
3269 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3270 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003271 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003272
3273 if (msg->hdr_content_len < limit)
3274 limit = msg->hdr_content_len;
3275
Willy Tarreau7c96f672009-12-27 22:47:25 +01003276 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003277 goto http_end;
3278
3279 missing_data:
3280 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003281 if (req->flags & BF_FULL)
3282 goto return_bad_req;
3283
Willy Tarreau522d6c02009-12-06 18:49:18 +01003284 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3285 txn->status = 408;
3286 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3287 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003288 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003289
3290 /* we get here if we need to wait for more data */
3291 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003292 /* Not enough data. We'll re-use the http-request
3293 * timeout here. Ideally, we should set the timeout
3294 * relative to the accept() date. We just set the
3295 * request timeout once at the beginning of the
3296 * request.
3297 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003298 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003299 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003300 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003301 return 0;
3302 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003303
3304 http_end:
3305 /* The situation will not evolve, so let's give up on the analysis. */
3306 s->logs.tv_request = now; /* update the request timer to reflect full request */
3307 req->analysers &= ~an_bit;
3308 req->analyse_exp = TICK_ETERNITY;
3309 return 1;
3310
3311 return_bad_req: /* let's centralize all bad requests */
3312 txn->req.msg_state = HTTP_MSG_ERROR;
3313 txn->status = 400;
3314 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3315
3316 return_err_msg:
3317 req->analysers = 0;
3318 s->fe->counters.failed_req++;
3319 if (s->listener->counters)
3320 s->listener->counters->failed_req++;
3321
3322 if (!(s->flags & SN_ERR_MASK))
3323 s->flags |= SN_ERR_PRXCOND;
3324 if (!(s->flags & SN_FINST_MASK))
3325 s->flags |= SN_FINST_R;
3326 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003327}
3328
Willy Tarreau610ecce2010-01-04 21:15:02 +01003329/* Terminate current transaction and prepare a new one. This is very tricky
3330 * right now but it works.
3331 */
3332void http_end_txn_clean_session(struct session *s)
3333{
3334 /* FIXME: We need a more portable way of releasing a backend's and a
3335 * server's connections. We need a safer way to reinitialize buffer
3336 * flags. We also need a more accurate method for computing per-request
3337 * data.
3338 */
3339 http_silent_debug(__LINE__, s);
3340
3341 s->req->cons->flags |= SI_FL_NOLINGER;
3342 s->req->cons->shutr(s->req->cons);
3343 s->req->cons->shutw(s->req->cons);
3344
3345 http_silent_debug(__LINE__, s);
3346
3347 if (s->flags & SN_BE_ASSIGNED)
3348 s->be->beconn--;
3349
3350 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3351 session_process_counters(s);
3352
3353 if (s->txn.status) {
3354 int n;
3355
3356 n = s->txn.status / 100;
3357 if (n < 1 || n > 5)
3358 n = 0;
3359
3360 if (s->fe->mode == PR_MODE_HTTP)
3361 s->fe->counters.p.http.rsp[n]++;
3362
3363 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3364 (s->be->mode == PR_MODE_HTTP))
3365 s->be->counters.p.http.rsp[n]++;
3366 }
3367
3368 /* don't count other requests' data */
3369 s->logs.bytes_in -= s->req->l - s->req->send_max;
3370 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3371
3372 /* let's do a final log if we need it */
3373 if (s->logs.logwait &&
3374 !(s->flags & SN_MONITOR) &&
3375 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3376 s->do_log(s);
3377 }
3378
3379 s->logs.accept_date = date; /* user-visible date for logging */
3380 s->logs.tv_accept = now; /* corrected date for internal use */
3381 tv_zero(&s->logs.tv_request);
3382 s->logs.t_queue = -1;
3383 s->logs.t_connect = -1;
3384 s->logs.t_data = -1;
3385 s->logs.t_close = 0;
3386 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3387 s->logs.srv_queue_size = 0; /* we will get this number soon */
3388
3389 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3390 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3391
3392 if (s->pend_pos)
3393 pendconn_free(s->pend_pos);
3394
3395 if (s->srv) {
3396 if (s->flags & SN_CURR_SESS) {
3397 s->flags &= ~SN_CURR_SESS;
3398 s->srv->cur_sess--;
3399 }
3400 if (may_dequeue_tasks(s->srv, s->be))
3401 process_srv_queue(s->srv);
3402 }
3403
3404 if (unlikely(s->srv_conn))
3405 sess_change_server(s, NULL);
3406 s->srv = NULL;
3407
3408 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3409 s->req->cons->fd = -1; /* just to help with debugging */
3410 s->req->cons->err_type = SI_ET_NONE;
3411 s->req->cons->err_loc = NULL;
3412 s->req->cons->exp = TICK_ETERNITY;
3413 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003414 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3415 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003416 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3417 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3418 s->txn.meth = 0;
3419 http_reset_txn(s);
3420 s->txn.flags |= TX_NOT_FIRST;
3421 if (s->be->options2 & PR_O2_INDEPSTR)
3422 s->req->cons->flags |= SI_FL_INDEP_STR;
3423
3424 /* if the request buffer is not empty, it means we're
3425 * about to process another request, so send pending
3426 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003427 * Just don't do this if the buffer is close to be full,
3428 * because the request will wait for it to flush a little
3429 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003430 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003431 if (s->req->l > s->req->send_max) {
3432 if (s->rep->send_max &&
3433 !(s->rep->flags & BF_FULL) &&
3434 s->rep->lr <= s->rep->r &&
3435 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3436 s->rep->flags |= BF_EXPECT_MORE;
3437 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003438
3439 /* we're removing the analysers, we MUST re-enable events detection */
3440 buffer_auto_read(s->req);
3441 buffer_auto_close(s->req);
3442 buffer_auto_read(s->rep);
3443 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003444
3445 /* make ->lr point to the first non-forwarded byte */
3446 s->req->lr = s->req->w + s->req->send_max;
3447 if (s->req->lr >= s->req->data + s->req->size)
3448 s->req->lr -= s->req->size;
3449 s->rep->lr = s->rep->w + s->rep->send_max;
3450 if (s->rep->lr >= s->rep->data + s->rep->size)
3451 s->rep->lr -= s->req->size;
3452
3453 s->req->analysers |= s->fe->fe_req_ana;
3454 s->rep->analysers = 0;
3455
3456 http_silent_debug(__LINE__, s);
3457}
3458
3459
3460/* This function updates the request state machine according to the response
3461 * state machine and buffer flags. It returns 1 if it changes anything (flag
3462 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3463 * it is only used to find when a request/response couple is complete. Both
3464 * this function and its equivalent should loop until both return zero. It
3465 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3466 */
3467int http_sync_req_state(struct session *s)
3468{
3469 struct buffer *buf = s->req;
3470 struct http_txn *txn = &s->txn;
3471 unsigned int old_flags = buf->flags;
3472 unsigned int old_state = txn->req.msg_state;
3473
3474 http_silent_debug(__LINE__, s);
3475 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3476 return 0;
3477
3478 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003479 /* No need to read anymore, the request was completely parsed.
3480 * We can shut the read side unless we want to abort_on_close.
3481 */
3482 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3483 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003484
3485 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3486 goto wait_other_side;
3487
3488 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3489 /* The server has not finished to respond, so we
3490 * don't want to move in order not to upset it.
3491 */
3492 goto wait_other_side;
3493 }
3494
3495 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3496 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003497 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003498 txn->req.msg_state = HTTP_MSG_TUNNEL;
3499 goto wait_other_side;
3500 }
3501
3502 /* When we get here, it means that both the request and the
3503 * response have finished receiving. Depending on the connection
3504 * mode, we'll have to wait for the last bytes to leave in either
3505 * direction, and sometimes for a close to be effective.
3506 */
3507
3508 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3509 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3510 /* Server-close mode : queue a connection close to the server */
3511 buffer_shutw_now(buf);
3512 buf->cons->flags |= SI_FL_NOLINGER;
3513 }
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003514 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003515 /* Option forceclose is set, let's enforce it now
3516 * that we're not expecting any new data to come.
3517 */
3518 buffer_shutr_now(buf);
3519 buffer_shutw_now(buf);
3520 buf->cons->flags |= SI_FL_NOLINGER;
3521 }
3522 /* other modes include httpclose (no action) and keepalive (not implemented) */
3523 }
3524
3525 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3526 /* if we've just closed an output, let's switch */
3527 if (!(buf->flags & BF_OUT_EMPTY)) {
3528 txn->req.msg_state = HTTP_MSG_CLOSING;
3529 goto http_msg_closing;
3530 }
3531 else {
3532 txn->req.msg_state = HTTP_MSG_CLOSED;
3533 goto http_msg_closed;
3534 }
3535 }
3536 else {
3537 /* other modes are used as a tunnel right now */
Willy Tarreau90deb182010-01-07 00:20:41 +01003538 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003539 txn->req.msg_state = HTTP_MSG_TUNNEL;
3540 goto wait_other_side;
3541 }
3542 }
3543
3544 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3545 http_msg_closing:
3546 /* nothing else to forward, just waiting for the output buffer
3547 * to be empty and for the shutw_now to take effect.
3548 */
3549 if (buf->flags & BF_OUT_EMPTY) {
3550 txn->req.msg_state = HTTP_MSG_CLOSED;
3551 goto http_msg_closed;
3552 }
3553 else if (buf->flags & BF_SHUTW) {
3554 txn->req.msg_state = HTTP_MSG_ERROR;
3555 goto wait_other_side;
3556 }
3557 }
3558
3559 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3560 http_msg_closed:
3561 goto wait_other_side;
3562 }
3563
3564 wait_other_side:
3565 http_silent_debug(__LINE__, s);
3566 return txn->req.msg_state != old_state || buf->flags != old_flags;
3567}
3568
3569
3570/* This function updates the response state machine according to the request
3571 * state machine and buffer flags. It returns 1 if it changes anything (flag
3572 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3573 * it is only used to find when a request/response couple is complete. Both
3574 * this function and its equivalent should loop until both return zero. It
3575 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3576 */
3577int http_sync_res_state(struct session *s)
3578{
3579 struct buffer *buf = s->rep;
3580 struct http_txn *txn = &s->txn;
3581 unsigned int old_flags = buf->flags;
3582 unsigned int old_state = txn->rsp.msg_state;
3583
3584 http_silent_debug(__LINE__, s);
3585 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3586 return 0;
3587
3588 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3589 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003590 * still monitor the server connection for a possible close
3591 * while the request is being uploaded, so we don't disable
3592 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003593 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003594 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003595
3596 if (txn->req.msg_state == HTTP_MSG_ERROR)
3597 goto wait_other_side;
3598
3599 if (txn->req.msg_state < HTTP_MSG_DONE) {
3600 /* The client seems to still be sending data, probably
3601 * because we got an error response during an upload.
3602 * We have the choice of either breaking the connection
3603 * or letting it pass through. Let's do the later.
3604 */
3605 goto wait_other_side;
3606 }
3607
3608 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3609 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003610 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003611 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3612 goto wait_other_side;
3613 }
3614
3615 /* When we get here, it means that both the request and the
3616 * response have finished receiving. Depending on the connection
3617 * mode, we'll have to wait for the last bytes to leave in either
3618 * direction, and sometimes for a close to be effective.
3619 */
3620
3621 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3622 /* Server-close mode : shut read and wait for the request
3623 * side to close its output buffer. The caller will detect
3624 * when we're in DONE and the other is in CLOSED and will
3625 * catch that for the final cleanup.
3626 */
3627 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3628 buffer_shutr_now(buf);
3629 goto wait_other_side;
3630 }
3631 else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) &&
3632 ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) {
3633 /* Option forceclose is set, let's enforce it now
3634 * that we're not expecting any new data to come.
3635 * The caller knows the session is complete once
3636 * both states are CLOSED.
3637 */
3638 buffer_shutr_now(buf);
3639 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003640 }
3641 else {
3642 /* other modes include httpclose (no action) and keepalive
3643 * (not implemented). These modes are used as a tunnel right
3644 * now.
3645 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003646 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003647 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3648 goto wait_other_side;
3649 }
3650
3651 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3652 /* if we've just closed an output, let's switch */
3653 if (!(buf->flags & BF_OUT_EMPTY)) {
3654 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3655 goto http_msg_closing;
3656 }
3657 else {
3658 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3659 goto http_msg_closed;
3660 }
3661 }
3662 goto wait_other_side;
3663 }
3664
3665 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3666 http_msg_closing:
3667 /* nothing else to forward, just waiting for the output buffer
3668 * to be empty and for the shutw_now to take effect.
3669 */
3670 if (buf->flags & BF_OUT_EMPTY) {
3671 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3672 goto http_msg_closed;
3673 }
3674 else if (buf->flags & BF_SHUTW) {
3675 txn->rsp.msg_state = HTTP_MSG_ERROR;
3676 goto wait_other_side;
3677 }
3678 }
3679
3680 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3681 http_msg_closed:
3682 /* drop any pending data */
3683 buffer_ignore(buf, buf->l - buf->send_max);
3684 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003685 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003686 goto wait_other_side;
3687 }
3688
3689 wait_other_side:
3690 http_silent_debug(__LINE__, s);
3691 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3692}
3693
3694
3695/* Resync the request and response state machines. Return 1 if either state
3696 * changes.
3697 */
3698int http_resync_states(struct session *s)
3699{
3700 struct http_txn *txn = &s->txn;
3701 int old_req_state = txn->req.msg_state;
3702 int old_res_state = txn->rsp.msg_state;
3703
3704 http_silent_debug(__LINE__, s);
3705 http_sync_req_state(s);
3706 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003707 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003708 if (!http_sync_res_state(s))
3709 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003710 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003711 if (!http_sync_req_state(s))
3712 break;
3713 }
3714 http_silent_debug(__LINE__, s);
3715 /* OK, both state machines agree on a compatible state.
3716 * There are a few cases we're interested in :
3717 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
3718 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3719 * directions, so let's simply disable both analysers.
3720 * - HTTP_MSG_CLOSED on the response only means we must abort the
3721 * request.
3722 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
3723 * with server-close mode means we've completed one request and we
3724 * must re-initialize the server connection.
3725 */
3726
3727 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3728 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
3729 (txn->req.msg_state == HTTP_MSG_CLOSED &&
3730 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
3731 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003732 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003733 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003734 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003735 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01003736 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01003738 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3739 txn->rsp.msg_state == HTTP_MSG_ERROR ||
3740 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003741 s->rep->analysers = 0;
3742 buffer_auto_close(s->rep);
3743 buffer_auto_read(s->rep);
3744 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003745 buffer_abort(s->req);
3746 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01003747 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003749 }
3750 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3751 txn->rsp.msg_state == HTTP_MSG_DONE &&
3752 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
3753 /* server-close: terminate this server connection and
3754 * reinitialize a fresh-new transaction.
3755 */
3756 http_end_txn_clean_session(s);
3757 }
3758
3759 http_silent_debug(__LINE__, s);
3760 return txn->req.msg_state != old_req_state ||
3761 txn->rsp.msg_state != old_res_state;
3762}
3763
Willy Tarreaud98cf932009-12-27 22:54:55 +01003764/* This function is an analyser which forwards request body (including chunk
3765 * sizes if any). It is called as soon as we must forward, even if we forward
3766 * zero byte. The only situation where it must not be called is when we're in
3767 * tunnel mode and we want to forward till the close. It's used both to forward
3768 * remaining data and to resync after end of body. It expects the msg_state to
3769 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3770 * read more data, or 1 once we can go on with next request or end the session.
3771 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3772 * bytes of pending data + the headers if not already done (between som and sov).
3773 * It eventually adjusts som to match sov after the data in between have been sent.
3774 */
3775int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3776{
3777 struct http_txn *txn = &s->txn;
3778 struct http_msg *msg = &s->txn.req;
3779
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003780 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3781 return 0;
3782
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003783 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3784 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3785 /* Output closed while we were sending data. We must abort. */
3786 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01003787 buffer_auto_read(req);
3788 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003789 req->analysers &= ~an_bit;
3790 return 1;
3791 }
3792
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003793 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003794
3795 /* Note that we don't have to send 100-continue back because we don't
3796 * need the data to complete our job, and it's up to the server to
3797 * decide whether to return 100, 417 or anything else in return of
3798 * an "Expect: 100-continue" header.
3799 */
3800
3801 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3802 /* we have msg->col and msg->sov which both point to the first
3803 * byte of message body. msg->som still points to the beginning
3804 * of the message. We must save the body in req->lr because it
3805 * survives buffer re-alignments.
3806 */
3807 req->lr = req->data + msg->sov;
3808 if (txn->flags & TX_REQ_TE_CHNK)
3809 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3810 else {
3811 msg->msg_state = HTTP_MSG_DATA;
3812 }
3813 }
3814
Willy Tarreaud98cf932009-12-27 22:54:55 +01003815 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003816 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01003817 /* we may have some data pending */
3818 if (msg->hdr_content_len || msg->som != msg->sov) {
3819 int bytes = msg->sov - msg->som;
3820 if (bytes < 0) /* sov may have wrapped at the end */
3821 bytes += req->size;
3822 buffer_forward(req, bytes + msg->hdr_content_len);
3823 msg->hdr_content_len = 0; /* don't forward that again */
3824 msg->som = msg->sov;
3825 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003826
Willy Tarreaucaabe412010-01-03 23:08:28 +01003827 if (msg->msg_state == HTTP_MSG_DATA) {
3828 /* must still forward */
3829 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003830 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003831
3832 /* nothing left to forward */
3833 if (txn->flags & TX_REQ_TE_CHNK)
3834 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003835 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01003836 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003837 }
3838 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003839 /* read the chunk size and assign it to ->hdr_content_len, then
3840 * set ->sov and ->lr to point to the body and switch to DATA or
3841 * TRAILERS state.
3842 */
3843 int ret = http_parse_chunk_size(req, msg);
3844
3845 if (!ret)
3846 goto missing_data;
3847 else if (ret < 0)
3848 goto return_bad_req;
3849 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003850 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003851 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3852 /* we want the CRLF after the data */
3853 int ret;
3854
Willy Tarreaud3347ee2010-01-04 02:02:25 +01003855 req->lr = req->w + req->send_max;
3856 if (req->lr >= req->data + req->size)
3857 req->lr -= req->size;
3858
Willy Tarreaud98cf932009-12-27 22:54:55 +01003859 ret = http_skip_chunk_crlf(req, msg);
3860
3861 if (ret == 0)
3862 goto missing_data;
3863 else if (ret < 0)
3864 goto return_bad_req;
3865 /* we're in MSG_CHUNK_SIZE now */
3866 }
3867 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3868 int ret = http_forward_trailers(req, msg);
3869
3870 if (ret == 0)
3871 goto missing_data;
3872 else if (ret < 0)
3873 goto return_bad_req;
3874 /* we're in HTTP_MSG_DONE now */
3875 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003876 else {
3877 /* other states, DONE...TUNNEL */
3878 if (http_resync_states(s)) {
3879 /* some state changes occurred, maybe the analyser
3880 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01003881 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
3883 goto return_bad_req;
3884 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003885 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003887 }
3888 }
3889
Willy Tarreaud98cf932009-12-27 22:54:55 +01003890 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003891 /* stop waiting for data if the input is closed before the end */
3892 if (req->flags & BF_SHUTR)
3893 goto return_bad_req;
3894
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003895 /* waiting for the last bits to leave the buffer */
3896 if (req->flags & BF_SHUTW)
3897 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898
3899 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003900 return 0;
3901
Willy Tarreaud98cf932009-12-27 22:54:55 +01003902 return_bad_req: /* let's centralize all bad requests */
3903 txn->req.msg_state = HTTP_MSG_ERROR;
3904 txn->status = 400;
3905 /* Note: we don't send any error if some data were already sent */
3906 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3907
Willy Tarreau90deb182010-01-07 00:20:41 +01003908 buffer_auto_read(req);
3909 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003910 req->analysers = 0;
3911 s->fe->counters.failed_req++;
3912 if (s->listener->counters)
3913 s->listener->counters->failed_req++;
3914
3915 if (!(s->flags & SN_ERR_MASK))
3916 s->flags |= SN_ERR_PRXCOND;
3917 if (!(s->flags & SN_FINST_MASK))
3918 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003919 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01003920 return 0;
3921}
3922
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003923/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3924 * processing can continue on next analysers, or zero if it either needs more
3925 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3926 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3927 * when it has nothing left to do, and may remove any analyser when it wants to
3928 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003929 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003930int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003931{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003932 struct http_txn *txn = &s->txn;
3933 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003934 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003935 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003936 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003937 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003938
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003939 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 +02003940 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003941 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003942 rep,
3943 rep->rex, rep->wex,
3944 rep->flags,
3945 rep->l,
3946 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003947
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003948 /*
3949 * Now parse the partial (or complete) lines.
3950 * We will check the response syntax, and also join multi-line
3951 * headers. An index of all the lines will be elaborated while
3952 * parsing.
3953 *
3954 * For the parsing, we use a 28 states FSM.
3955 *
3956 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003957 * rep->data + msg->som = beginning of response
3958 * rep->data + msg->eoh = end of processed headers / start of current one
3959 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003960 * rep->lr = first non-visited byte
3961 * rep->r = end of data
3962 */
3963
Willy Tarreau83e3af02009-12-28 17:39:57 +01003964 /* There's a protected area at the end of the buffer for rewriting
3965 * purposes. We don't want to start to parse the request if the
3966 * protected area is affected, because we may have to move processed
3967 * data later, which is much more complicated.
3968 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003969 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3970 if (unlikely((rep->flags & BF_FULL) ||
3971 rep->r < rep->lr ||
3972 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3973 if (rep->send_max) {
3974 /* some data has still not left the buffer, wake us once that's done */
3975 buffer_dont_close(rep);
3976 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3977 return 0;
3978 }
3979 if (rep->l <= rep->size - global.tune.maxrewrite)
3980 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003981 }
3982
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003983 if (likely(rep->lr < rep->r))
3984 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003985 }
3986
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003987 /* 1: we might have to print this header in debug mode */
3988 if (unlikely((global.mode & MODE_DEBUG) &&
3989 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003990 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003991 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003992
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003993 sol = rep->data + msg->som;
3994 eol = sol + msg->sl.rq.l;
3995 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003996
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003997 sol += hdr_idx_first_pos(&txn->hdr_idx);
3998 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003999
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004000 while (cur_idx) {
4001 eol = sol + txn->hdr_idx.v[cur_idx].len;
4002 debug_hdr("srvhdr", s, sol, eol);
4003 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4004 cur_idx = txn->hdr_idx.v[cur_idx].next;
4005 }
4006 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004007
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004008 /*
4009 * Now we quickly check if we have found a full valid response.
4010 * If not so, we check the FD and buffer states before leaving.
4011 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004012 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004013 * responses are checked first.
4014 *
4015 * Depending on whether the client is still there or not, we
4016 * may send an error response back or not. Note that normally
4017 * we should only check for HTTP status there, and check I/O
4018 * errors somewhere else.
4019 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004020
Willy Tarreau655dce92009-11-08 13:10:58 +01004021 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004022 /* Invalid response */
4023 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4024 /* we detected a parsing error. We want to archive this response
4025 * in the dedicated proxy area for later troubleshooting.
4026 */
4027 hdr_response_bad:
4028 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4029 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4030
4031 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004032 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004033 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004034 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4035 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004036
Willy Tarreau90deb182010-01-07 00:20:41 +01004037 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004038 rep->analysers = 0;
4039 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004040 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004041 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4042
4043 if (!(s->flags & SN_ERR_MASK))
4044 s->flags |= SN_ERR_PRXCOND;
4045 if (!(s->flags & SN_FINST_MASK))
4046 s->flags |= SN_FINST_H;
4047
4048 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004049 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004050
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004051 /* too large response does not fit in buffer. */
4052 else if (rep->flags & BF_FULL) {
4053 goto hdr_response_bad;
4054 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004055
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004056 /* read error */
4057 else if (rep->flags & BF_READ_ERROR) {
4058 if (msg->err_pos >= 0)
4059 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004060
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004061 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004062 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004063 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004064 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4065 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004066
Willy Tarreau90deb182010-01-07 00:20:41 +01004067 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004068 rep->analysers = 0;
4069 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004070 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004071 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004072
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004073 if (!(s->flags & SN_ERR_MASK))
4074 s->flags |= SN_ERR_SRVCL;
4075 if (!(s->flags & SN_FINST_MASK))
4076 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004077 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004078 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004080 /* read timeout : return a 504 to the client. */
4081 else if (rep->flags & BF_READ_TIMEOUT) {
4082 if (msg->err_pos >= 0)
4083 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004084
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004085 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004086 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004087 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004088 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4089 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004090
Willy Tarreau90deb182010-01-07 00:20:41 +01004091 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004092 rep->analysers = 0;
4093 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004094 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004095 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004096
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004097 if (!(s->flags & SN_ERR_MASK))
4098 s->flags |= SN_ERR_SRVTO;
4099 if (!(s->flags & SN_FINST_MASK))
4100 s->flags |= SN_FINST_H;
4101 return 0;
4102 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004104 /* close from server */
4105 else if (rep->flags & BF_SHUTR) {
4106 if (msg->err_pos >= 0)
4107 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004108
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004109 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004110 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004111 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004112 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4113 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004114
Willy Tarreau90deb182010-01-07 00:20:41 +01004115 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004116 rep->analysers = 0;
4117 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004118 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004119 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004120
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004121 if (!(s->flags & SN_ERR_MASK))
4122 s->flags |= SN_ERR_SRVCL;
4123 if (!(s->flags & SN_FINST_MASK))
4124 s->flags |= SN_FINST_H;
4125 return 0;
4126 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004127
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004128 /* write error to client (we don't send any message then) */
4129 else if (rep->flags & BF_WRITE_ERROR) {
4130 if (msg->err_pos >= 0)
4131 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004132
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004133 s->be->counters.failed_resp++;
4134 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004135 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004136
4137 if (!(s->flags & SN_ERR_MASK))
4138 s->flags |= SN_ERR_CLICL;
4139 if (!(s->flags & SN_FINST_MASK))
4140 s->flags |= SN_FINST_H;
4141
4142 /* process_session() will take care of the error */
4143 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004144 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004145
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004146 buffer_dont_close(rep);
4147 return 0;
4148 }
4149
4150 /* More interesting part now : we know that we have a complete
4151 * response which at least looks like HTTP. We have an indicator
4152 * of each header's length, so we can parse them quickly.
4153 */
4154
4155 if (unlikely(msg->err_pos >= 0))
4156 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4157
4158 /* ensure we keep this pointer to the beginning of the message */
4159 msg->sol = rep->data + msg->som;
4160
4161 /*
4162 * 1: get the status code
4163 */
4164 n = rep->data[msg->sl.st.c] - '0';
4165 if (n < 1 || n > 5)
4166 n = 0;
4167 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004168
Willy Tarreau5b154472009-12-21 20:11:07 +01004169 /* check if the response is HTTP/1.1 or above */
4170 if ((msg->sl.st.v_l == 8) &&
4171 ((rep->data[msg->som + 5] > '1') ||
4172 ((rep->data[msg->som + 5] == '1') &&
4173 (rep->data[msg->som + 7] >= '1'))))
4174 txn->flags |= TX_RES_VER_11;
4175
4176 /* "connection" has not been parsed yet */
4177 txn->flags &= ~TX_CON_HDR_PARS;
4178
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004179 /* transfer length unknown*/
4180 txn->flags &= ~TX_RES_XFER_LEN;
4181
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004182 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
4183
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004184 if (txn->status >= 100 && txn->status < 500)
4185 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4186 else
4187 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004189 /*
4190 * 2: check for cacheability.
4191 */
4192
4193 switch (txn->status) {
4194 case 200:
4195 case 203:
4196 case 206:
4197 case 300:
4198 case 301:
4199 case 410:
4200 /* RFC2616 @13.4:
4201 * "A response received with a status code of
4202 * 200, 203, 206, 300, 301 or 410 MAY be stored
4203 * by a cache (...) unless a cache-control
4204 * directive prohibits caching."
4205 *
4206 * RFC2616 @9.5: POST method :
4207 * "Responses to this method are not cacheable,
4208 * unless the response includes appropriate
4209 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004210 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004211 if (likely(txn->meth != HTTP_METH_POST) &&
4212 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4213 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4214 break;
4215 default:
4216 break;
4217 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004218
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004219 /*
4220 * 3: we may need to capture headers
4221 */
4222 s->logs.logwait &= ~LW_RESP;
4223 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
4224 capture_headers(rep->data + msg->som, &txn->hdr_idx,
4225 txn->rsp.cap, s->fe->rsp_cap);
4226
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004227 /* 4: determine the transfer-length.
4228 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4229 * the presence of a message-body in a RESPONSE and its transfer length
4230 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004231 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004232 * All responses to the HEAD request method MUST NOT include a
4233 * message-body, even though the presence of entity-header fields
4234 * might lead one to believe they do. All 1xx (informational), 204
4235 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4236 * message-body. All other responses do include a message-body,
4237 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004238 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004239 * 1. Any response which "MUST NOT" include a message-body (such as the
4240 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4241 * always terminated by the first empty line after the header fields,
4242 * regardless of the entity-header fields present in the message.
4243 *
4244 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4245 * the "chunked" transfer-coding (Section 6.2) is used, the
4246 * transfer-length is defined by the use of this transfer-coding.
4247 * If a Transfer-Encoding header field is present and the "chunked"
4248 * transfer-coding is not present, the transfer-length is defined by
4249 * the sender closing the connection.
4250 *
4251 * 3. If a Content-Length header field is present, its decimal value in
4252 * OCTETs represents both the entity-length and the transfer-length.
4253 * If a message is received with both a Transfer-Encoding header
4254 * field and a Content-Length header field, the latter MUST be ignored.
4255 *
4256 * 4. If the message uses the media type "multipart/byteranges", and
4257 * the transfer-length is not otherwise specified, then this self-
4258 * delimiting media type defines the transfer-length. This media
4259 * type MUST NOT be used unless the sender knows that the recipient
4260 * can parse it; the presence in a request of a Range header with
4261 * multiple byte-range specifiers from a 1.1 client implies that the
4262 * client can parse multipart/byteranges responses.
4263 *
4264 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004265 */
4266
4267 /* Skip parsing if no content length is possible. The response flags
4268 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004269 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004270 * FIXME: should we parse anyway and return an error on chunked encoding ?
4271 */
4272 if (txn->meth == HTTP_METH_HEAD ||
4273 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004274 txn->status == 204 || txn->status == 304) {
4275 txn->flags |= TX_RES_XFER_LEN;
4276 goto skip_content_length;
4277 }
4278
4279 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004280 goto skip_content_length;
4281
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004282 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004283 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004284 while ((txn->flags & TX_RES_VER_11) &&
4285 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004286 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4287 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4288 else if (txn->flags & TX_RES_TE_CHNK) {
4289 /* bad transfer-encoding (chunked followed by something else) */
4290 use_close_only = 1;
4291 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4292 break;
4293 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004294 }
4295
4296 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4297 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004298 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004299 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4300 signed long long cl;
4301
4302 if (!ctx.vlen)
4303 goto hdr_response_bad;
4304
4305 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4306 goto hdr_response_bad; /* parse failure */
4307
4308 if (cl < 0)
4309 goto hdr_response_bad;
4310
4311 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4312 goto hdr_response_bad; /* already specified, was different */
4313
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004314 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004315 msg->hdr_content_len = cl;
4316 }
4317
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004318 /* FIXME: we should also implement the multipart/byterange method.
4319 * For now on, we resort to close mode in this case (unknown length).
4320 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004321skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004322
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004323 /* end of job, return OK */
4324 rep->analysers &= ~an_bit;
4325 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004326 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004327 return 1;
4328}
4329
4330/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004331 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4332 * and updates t->rep->analysers. It might make sense to explode it into several
4333 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004334 */
4335int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4336{
4337 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004338 struct http_msg *msg = &txn->rsp;
4339 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004340 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004341 int conn_ka = 0, conn_cl = 0;
4342 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004343 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004344
4345 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4346 now_ms, __FUNCTION__,
4347 t,
4348 rep,
4349 rep->rex, rep->wex,
4350 rep->flags,
4351 rep->l,
4352 rep->analysers);
4353
Willy Tarreau655dce92009-11-08 13:10:58 +01004354 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004355 return 0;
4356
4357 rep->analysers &= ~an_bit;
4358 rep->analyse_exp = TICK_ETERNITY;
4359
Willy Tarreau5b154472009-12-21 20:11:07 +01004360 /* Now we have to check if we need to modify the Connection header.
4361 * This is more difficult on the response than it is on the request,
4362 * because we can have two different HTTP versions and we don't know
4363 * how the client will interprete a response. For instance, let's say
4364 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4365 * HTTP/1.1 response without any header. Maybe it will bound itself to
4366 * HTTP/1.0 because it only knows about it, and will consider the lack
4367 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4368 * the lack of header as a keep-alive. Thus we will use two flags
4369 * indicating how a request MAY be understood by the client. In case
4370 * of multiple possibilities, we'll fix the header to be explicit. If
4371 * ambiguous cases such as both close and keepalive are seen, then we
4372 * will fall back to explicit close. Note that we won't take risks with
4373 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4374 */
4375
4376 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004377 (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) &&
4378 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4379 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004380 int may_keep = 0, may_close = 0; /* how it may be understood */
4381 struct hdr_ctx ctx;
4382
4383 ctx.idx = 0;
4384 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4385 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4386 conn_cl = 1;
4387 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4388 conn_ka = 1;
4389 }
4390
4391 if (conn_cl) {
4392 /* close header present */
4393 may_close = 1;
4394 if (conn_ka) /* we have both close and keep-alive */
4395 may_keep = 1;
4396 }
4397 else if (conn_ka) {
4398 /* keep-alive alone */
4399 may_keep = 1;
4400 }
4401 else {
4402 /* no close nor keep-alive header */
4403 if (txn->flags & TX_RES_VER_11)
4404 may_keep = 1;
4405 else
4406 may_close = 1;
4407
4408 if (txn->flags & TX_REQ_VER_11)
4409 may_keep = 1;
4410 else
4411 may_close = 1;
4412 }
4413
4414 /* let's update the transaction status to reflect any close.
4415 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004416 * handled. We also explicitly state that we will close in
4417 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004418 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004419 if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01004420 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4421 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004422 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4423
4424 /* Now we must adjust the response header :
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004425 * - set "close" if may_keep and (WANT_CLO | httpclose)
Willy Tarreau5b154472009-12-21 20:11:07 +01004426 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4427 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
Willy Tarreau5b154472009-12-21 20:11:07 +01004428 */
Willy Tarreau0dfdf192010-01-05 11:33:11 +01004429 if (may_keep &&
4430 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO ||
4431 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreau5b154472009-12-21 20:11:07 +01004432 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004433 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4434 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4435 must_del_close = 1;
4436 if (!(txn->flags & TX_REQ_VER_11))
4437 must_keep = 1;
4438 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004439
4440 txn->flags |= TX_CON_HDR_PARS;
4441 }
4442
4443 /* We might have to check for "Connection:" if the server
4444 * returns a connection status that is not compatible with
4445 * the client's or with the config.
4446 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004447 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004448 char *cur_ptr, *cur_end, *cur_next;
4449 int cur_idx, old_idx, delta, val;
4450 int must_delete;
4451 struct hdr_idx_elem *cur_hdr;
4452
4453 /* we just have to remove the headers if both sides are 1.0 */
4454 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004455
4456 /* same if we want to re-enable keep-alive on 1.1 */
4457 must_delete |= must_del_close;
4458
Willy Tarreau5b154472009-12-21 20:11:07 +01004459 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4460
4461 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4462 cur_hdr = &txn->hdr_idx.v[cur_idx];
4463 cur_ptr = cur_next;
4464 cur_end = cur_ptr + cur_hdr->len;
4465 cur_next = cur_end + cur_hdr->cr + 1;
4466
4467 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4468 if (!val)
4469 continue;
4470
4471 /* 3 possibilities :
4472 * - we have already set "Connection: close" or we're in
4473 * HTTP/1.0, so we remove this line.
4474 * - we have not yet set "Connection: close", but this line
4475 * indicates close. We leave it untouched and set the flag.
4476 * - we have not yet set "Connection: close", and this line
4477 * indicates non-close. We replace it and set the flag.
4478 */
4479 if (must_delete) {
4480 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4481 http_msg_move_end(&txn->rsp, delta);
4482 cur_next += delta;
4483 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4484 txn->hdr_idx.used--;
4485 cur_hdr->len = 0;
4486 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004487 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004488 } else {
4489 if (cur_end - cur_ptr - val != 5 ||
4490 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4491 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4492 "close", 5);
4493 cur_next += delta;
4494 cur_hdr->len += delta;
4495 http_msg_move_end(&txn->rsp, delta);
4496 }
4497 must_delete = 1;
4498 must_close = 0;
4499 }
4500 } /* for loop */
4501 } /* if must close keep-alive */
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);
4574 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4575 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 Tarreau5b154472009-12-21 20:11:07 +01004665 * 8: add "Connection: close" if needed and not yet set. This is
4666 * only needed for 1.1 responses since we know there is no other
4667 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004668 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004669 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004670 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004671 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004672 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004673 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004674 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004675 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4676 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4677 "Connection: keep-alive", 22) < 0))
4678 goto return_bad_resp;
4679 must_keep = 0;
4680 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004681
Willy Tarreaud98cf932009-12-27 22:54:55 +01004682 if (txn->flags & TX_RES_XFER_LEN)
4683 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004684
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004685 /*************************************************************
4686 * OK, that's finished for the headers. We have done what we *
4687 * could. Let's switch to the DATA state. *
4688 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004689
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004690 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004691
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004692 /* if the user wants to log as soon as possible, without counting
4693 * bytes from the server, then this is the right moment. We have
4694 * to temporarily assign bytes_out to log what we currently have.
4695 */
4696 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4697 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4698 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004699 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004700 t->logs.bytes_out = 0;
4701 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004702
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004703 /* Note: we must not try to cheat by jumping directly to DATA,
4704 * otherwise we would not let the client side wake up.
4705 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004706
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004707 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004708 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004709 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004710}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004711
Willy Tarreaud98cf932009-12-27 22:54:55 +01004712/* This function is an analyser which forwards response body (including chunk
4713 * sizes if any). It is called as soon as we must forward, even if we forward
4714 * zero byte. The only situation where it must not be called is when we're in
4715 * tunnel mode and we want to forward till the close. It's used both to forward
4716 * remaining data and to resync after end of body. It expects the msg_state to
4717 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4718 * read more data, or 1 once we can go on with next request or end the session.
4719 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4720 * bytes of pending data + the headers if not already done (between som and sov).
4721 * It eventually adjusts som to match sov after the data in between have been sent.
4722 */
4723int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4724{
4725 struct http_txn *txn = &s->txn;
4726 struct http_msg *msg = &s->txn.rsp;
4727
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004728 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4729 return 0;
4730
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004731 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004732 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004733 !s->req->analysers) {
4734 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4735 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004736 buffer_auto_read(res);
4737 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004738 res->analysers &= ~an_bit;
4739 return 1;
4740 }
4741
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004742 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004743
Willy Tarreaud98cf932009-12-27 22:54:55 +01004744 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4745 /* we have msg->col and msg->sov which both point to the first
4746 * byte of message body. msg->som still points to the beginning
4747 * of the message. We must save the body in req->lr because it
4748 * survives buffer re-alignments.
4749 */
4750 res->lr = res->data + msg->sov;
4751 if (txn->flags & TX_RES_TE_CHNK)
4752 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4753 else {
4754 msg->msg_state = HTTP_MSG_DATA;
4755 }
4756 }
4757
Willy Tarreaud98cf932009-12-27 22:54:55 +01004758 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004759 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004760 /* we may have some data pending */
4761 if (msg->hdr_content_len || msg->som != msg->sov) {
4762 int bytes = msg->sov - msg->som;
4763 if (bytes < 0) /* sov may have wrapped at the end */
4764 bytes += res->size;
4765 buffer_forward(res, bytes + msg->hdr_content_len);
4766 msg->hdr_content_len = 0; /* don't forward that again */
4767 msg->som = msg->sov;
4768 }
4769
Willy Tarreaucaabe412010-01-03 23:08:28 +01004770 if (msg->msg_state == HTTP_MSG_DATA) {
4771 /* must still forward */
4772 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004773 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004774
4775 /* nothing left to forward */
4776 if (txn->flags & TX_RES_TE_CHNK)
4777 msg->msg_state = HTTP_MSG_DATA_CRLF;
4778 else
4779 msg->msg_state = HTTP_MSG_DONE;
4780 }
4781 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004782 /* read the chunk size and assign it to ->hdr_content_len, then
4783 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4784 */
4785 int ret = http_parse_chunk_size(res, msg);
4786
4787 if (!ret)
4788 goto missing_data;
4789 else if (ret < 0)
4790 goto return_bad_res;
4791 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004792 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004793 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4794 /* we want the CRLF after the data */
4795 int ret;
4796
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004797 res->lr = res->w + res->send_max;
4798 if (res->lr >= res->data + res->size)
4799 res->lr -= res->size;
4800
Willy Tarreaud98cf932009-12-27 22:54:55 +01004801 ret = http_skip_chunk_crlf(res, msg);
4802
4803 if (!ret)
4804 goto missing_data;
4805 else if (ret < 0)
4806 goto return_bad_res;
4807 /* we're in MSG_CHUNK_SIZE now */
4808 }
4809 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4810 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004811
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812 if (ret == 0)
4813 goto missing_data;
4814 else if (ret < 0)
4815 goto return_bad_res;
4816 /* we're in HTTP_MSG_DONE now */
4817 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004818 else {
4819 /* other states, DONE...TUNNEL */
4820 if (http_resync_states(s)) {
4821 http_silent_debug(__LINE__, s);
4822 /* some state changes occurred, maybe the analyser
4823 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01004824 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4826 goto return_bad_res;
4827 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01004828 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004829 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004830 }
4831 }
4832
Willy Tarreaud98cf932009-12-27 22:54:55 +01004833 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004834 /* stop waiting for data if the input is closed before the end */
4835 if (res->flags & BF_SHUTR)
4836 goto return_bad_res;
4837
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838 if (!s->req->analysers)
4839 goto return_bad_res;
4840
Willy Tarreaud98cf932009-12-27 22:54:55 +01004841 /* forward the chunk size as well as any pending data */
4842 if (msg->hdr_content_len || msg->som != msg->sov) {
4843 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4844 msg->hdr_content_len = 0; /* don't forward that again */
4845 msg->som = msg->sov;
4846 }
4847
Willy Tarreaud98cf932009-12-27 22:54:55 +01004848 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004849 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004850 return 0;
4851
4852 return_bad_res: /* let's centralize all bad resuests */
4853 txn->rsp.msg_state = HTTP_MSG_ERROR;
4854 txn->status = 502;
4855 stream_int_cond_close(res->cons, NULL);
4856
Willy Tarreau90deb182010-01-07 00:20:41 +01004857 buffer_auto_close(res);
4858 buffer_auto_read(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004859 res->analysers = 0;
4860 s->be->counters.failed_resp++;
4861 if (s->srv) {
4862 s->srv->counters.failed_resp++;
4863 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4864 }
4865
4866 if (!(s->flags & SN_ERR_MASK))
4867 s->flags |= SN_ERR_PRXCOND;
4868 if (!(s->flags & SN_FINST_MASK))
4869 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004870 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004871 return 0;
4872}
4873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004874/* Iterate the same filter through all request headers.
4875 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004876 * Since it can manage the switch to another backend, it updates the per-proxy
4877 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004878 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004879int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004880{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004881 char term;
4882 char *cur_ptr, *cur_end, *cur_next;
4883 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004884 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004885 struct hdr_idx_elem *cur_hdr;
4886 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004888 last_hdr = 0;
4889
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004890 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004891 old_idx = 0;
4892
4893 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004894 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004895 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004896 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004897 (exp->action == ACT_ALLOW ||
4898 exp->action == ACT_DENY ||
4899 exp->action == ACT_TARPIT))
4900 return 0;
4901
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004902 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004903 if (!cur_idx)
4904 break;
4905
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004906 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004907 cur_ptr = cur_next;
4908 cur_end = cur_ptr + cur_hdr->len;
4909 cur_next = cur_end + cur_hdr->cr + 1;
4910
4911 /* Now we have one header between cur_ptr and cur_end,
4912 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004913 */
4914
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004915 /* The annoying part is that pattern matching needs
4916 * that we modify the contents to null-terminate all
4917 * strings before testing them.
4918 */
4919
4920 term = *cur_end;
4921 *cur_end = '\0';
4922
4923 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4924 switch (exp->action) {
4925 case ACT_SETBE:
4926 /* It is not possible to jump a second time.
4927 * FIXME: should we return an HTTP/500 here so that
4928 * the admin knows there's a problem ?
4929 */
4930 if (t->be != t->fe)
4931 break;
4932
4933 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004934 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004935 last_hdr = 1;
4936 break;
4937
4938 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004939 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004940 last_hdr = 1;
4941 break;
4942
4943 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004944 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004945 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004946
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004947 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004948 if (t->listener->counters)
4949 t->listener->counters->denied_resp++;
4950
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004951 break;
4952
4953 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004954 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004955 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004956
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004957 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004958 if (t->listener->counters)
4959 t->listener->counters->denied_resp++;
4960
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004961 break;
4962
4963 case ACT_REPLACE:
4964 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4965 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4966 /* FIXME: if the user adds a newline in the replacement, the
4967 * index will not be recalculated for now, and the new line
4968 * will not be counted as a new header.
4969 */
4970
4971 cur_end += delta;
4972 cur_next += delta;
4973 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004974 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004975 break;
4976
4977 case ACT_REMOVE:
4978 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4979 cur_next += delta;
4980
Willy Tarreaufa355d42009-11-29 18:12:29 +01004981 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004982 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4983 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004984 cur_hdr->len = 0;
4985 cur_end = NULL; /* null-term has been rewritten */
4986 break;
4987
4988 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004989 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004990 if (cur_end)
4991 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004992
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004993 /* keep the link from this header to next one in case of later
4994 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004995 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004996 old_idx = cur_idx;
4997 }
4998 return 0;
4999}
5000
5001
5002/* Apply the filter to the request line.
5003 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5004 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005005 * Since it can manage the switch to another backend, it updates the per-proxy
5006 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005007 */
5008int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5009{
5010 char term;
5011 char *cur_ptr, *cur_end;
5012 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005013 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005014 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005015
Willy Tarreau58f10d72006-12-04 02:26:12 +01005016
Willy Tarreau3d300592007-03-18 18:34:41 +01005017 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005018 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005019 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005020 (exp->action == ACT_ALLOW ||
5021 exp->action == ACT_DENY ||
5022 exp->action == ACT_TARPIT))
5023 return 0;
5024 else if (exp->action == ACT_REMOVE)
5025 return 0;
5026
5027 done = 0;
5028
Willy Tarreau9cdde232007-05-02 20:58:19 +02005029 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005030 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005031
5032 /* Now we have the request line between cur_ptr and cur_end */
5033
5034 /* The annoying part is that pattern matching needs
5035 * that we modify the contents to null-terminate all
5036 * strings before testing them.
5037 */
5038
5039 term = *cur_end;
5040 *cur_end = '\0';
5041
5042 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5043 switch (exp->action) {
5044 case ACT_SETBE:
5045 /* It is not possible to jump a second time.
5046 * FIXME: should we return an HTTP/500 here so that
5047 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005048 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005049 if (t->be != t->fe)
5050 break;
5051
5052 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005053 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005054 done = 1;
5055 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005056
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005057 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005058 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005059 done = 1;
5060 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005062 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005063 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005064
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005065 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005066 if (t->listener->counters)
5067 t->listener->counters->denied_resp++;
5068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005069 done = 1;
5070 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005072 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005073 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005074
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005075 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005076 if (t->listener->counters)
5077 t->listener->counters->denied_resp++;
5078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005079 done = 1;
5080 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005082 case ACT_REPLACE:
5083 *cur_end = term; /* restore the string terminator */
5084 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5085 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5086 /* FIXME: if the user adds a newline in the replacement, the
5087 * index will not be recalculated for now, and the new line
5088 * will not be counted as a new header.
5089 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005090
Willy Tarreaufa355d42009-11-29 18:12:29 +01005091 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005092 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01005093
Willy Tarreau9cdde232007-05-02 20:58:19 +02005094 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005095 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005096 HTTP_MSG_RQMETH,
5097 cur_ptr, cur_end + 1,
5098 NULL, NULL);
5099 if (unlikely(!cur_end))
5100 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005102 /* we have a full request and we know that we have either a CR
5103 * or an LF at <ptr>.
5104 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005105 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5106 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005107 /* there is no point trying this regex on headers */
5108 return 1;
5109 }
5110 }
5111 *cur_end = term; /* restore the string terminator */
5112 return done;
5113}
Willy Tarreau97de6242006-12-27 17:18:38 +01005114
Willy Tarreau58f10d72006-12-04 02:26:12 +01005115
Willy Tarreau58f10d72006-12-04 02:26:12 +01005116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005117/*
5118 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
5119 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005120 * unparsable request. Since it can manage the switch to another backend, it
5121 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005122 */
5123int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
5124{
Willy Tarreau3d300592007-03-18 18:34:41 +01005125 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005126 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005127 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005128 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005129
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005130 /*
5131 * The interleaving of transformations and verdicts
5132 * makes it difficult to decide to continue or stop
5133 * the evaluation.
5134 */
5135
Willy Tarreau3d300592007-03-18 18:34:41 +01005136 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005137 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5138 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
5139 exp = exp->next;
5140 continue;
5141 }
5142
5143 /* Apply the filter to the request line. */
5144 ret = apply_filter_to_req_line(t, req, exp);
5145 if (unlikely(ret < 0))
5146 return -1;
5147
5148 if (likely(ret == 0)) {
5149 /* The filter did not match the request, it can be
5150 * iterated through all headers.
5151 */
5152 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005153 }
5154 exp = exp->next;
5155 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005156 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005157}
5158
5159
Willy Tarreaua15645d2007-03-18 16:22:39 +01005160
Willy Tarreau58f10d72006-12-04 02:26:12 +01005161/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005162 * Try to retrieve the server associated to the appsession.
5163 * If the server is found, it's assigned to the session.
5164 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005165void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005166 struct http_txn *txn = &t->txn;
5167 appsess *asession = NULL;
5168 char *sessid_temp = NULL;
5169
Cyril Bontéb21570a2009-11-29 20:04:48 +01005170 if (len > t->be->appsession_len) {
5171 len = t->be->appsession_len;
5172 }
5173
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005174 if (t->be->options2 & PR_O2_AS_REQL) {
5175 /* request-learn option is enabled : store the sessid in the session for future use */
5176 if (t->sessid != NULL) {
5177 /* free previously allocated memory as we don't need the session id found in the URL anymore */
5178 pool_free2(apools.sessid, t->sessid);
5179 }
5180
5181 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5182 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5183 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5184 return;
5185 }
5186
Cyril Bontéb21570a2009-11-29 20:04:48 +01005187 memcpy(t->sessid, buf, len);
5188 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005189 }
5190
5191 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5192 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5193 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5194 return;
5195 }
5196
Cyril Bontéb21570a2009-11-29 20:04:48 +01005197 memcpy(sessid_temp, buf, len);
5198 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005199
5200 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5201 /* free previously allocated memory */
5202 pool_free2(apools.sessid, sessid_temp);
5203
5204 if (asession != NULL) {
5205 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5206 if (!(t->be->options2 & PR_O2_AS_REQL))
5207 asession->request_count++;
5208
5209 if (asession->serverid != NULL) {
5210 struct server *srv = t->be->srv;
5211 while (srv) {
5212 if (strcmp(srv->id, asession->serverid) == 0) {
5213 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
5214 /* we found the server and it's usable */
5215 txn->flags &= ~TX_CK_MASK;
5216 txn->flags |= TX_CK_VALID;
5217 t->flags |= SN_DIRECT | SN_ASSIGNED;
5218 t->srv = srv;
5219 break;
5220 } else {
5221 txn->flags &= ~TX_CK_MASK;
5222 txn->flags |= TX_CK_DOWN;
5223 }
5224 }
5225 srv = srv->next;
5226 }
5227 }
5228 }
5229}
5230
5231/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005232 * Manage client-side cookie. It can impact performance by about 2% so it is
5233 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005234 */
5235void manage_client_side_cookies(struct session *t, struct buffer *req)
5236{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005237 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005238 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005239 char *del_colon, *del_cookie, *colon;
5240 int app_cookies;
5241
Willy Tarreau58f10d72006-12-04 02:26:12 +01005242 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005243 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005244
Willy Tarreau2a324282006-12-05 00:05:46 +01005245 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005246 * we start with the start line.
5247 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005248 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005249 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005250
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005251 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005252 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005253 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005254
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005255 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005256 cur_ptr = cur_next;
5257 cur_end = cur_ptr + cur_hdr->len;
5258 cur_next = cur_end + cur_hdr->cr + 1;
5259
5260 /* We have one full header between cur_ptr and cur_end, and the
5261 * next header starts at cur_next. We're only interested in
5262 * "Cookie:" headers.
5263 */
5264
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005265 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5266 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005267 old_idx = cur_idx;
5268 continue;
5269 }
5270
5271 /* Now look for cookies. Conforming to RFC2109, we have to support
5272 * attributes whose name begin with a '$', and associate them with
5273 * the right cookie, if we want to delete this cookie.
5274 * So there are 3 cases for each cookie read :
5275 * 1) it's a special attribute, beginning with a '$' : ignore it.
5276 * 2) it's a server id cookie that we *MAY* want to delete : save
5277 * some pointers on it (last semi-colon, beginning of cookie...)
5278 * 3) it's an application cookie : we *MAY* have to delete a previous
5279 * "special" cookie.
5280 * At the end of loop, if a "special" cookie remains, we may have to
5281 * remove it. If no application cookie persists in the header, we
5282 * *MUST* delete it
5283 */
5284
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005285 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005286
Willy Tarreau58f10d72006-12-04 02:26:12 +01005287 /* del_cookie == NULL => nothing to be deleted */
5288 del_colon = del_cookie = NULL;
5289 app_cookies = 0;
5290
5291 while (p1 < cur_end) {
5292 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005293 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005294 while (p1 < cur_end) {
5295 if (*p1 == ';' || *p1 == ',')
5296 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005297 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005298 break;
5299 p1++;
5300 }
5301
5302 if (p1 == cur_end)
5303 break;
5304
5305 /* p1 is at the beginning of the cookie name */
5306 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005307 while (p2 < cur_end && *p2 != '=') {
5308 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5309 /* oops, the cookie name was truncated, resync */
5310 p1 = p2;
5311 goto resync_name;
5312 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005313 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005315
5316 if (p2 == cur_end)
5317 break;
5318
5319 p3 = p2 + 1; /* skips the '=' sign */
5320 if (p3 == cur_end)
5321 break;
5322
Willy Tarreau305ae852010-01-03 19:45:54 +01005323 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5324 p5 = p4 = p3;
5325 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5326 if (!isspace((unsigned char)*p5))
5327 p4 = p5 + 1;
5328 p5++;
5329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005330
5331 /* here, we have the cookie name between p1 and p2,
5332 * and its value between p3 and p4.
5333 * we can process it :
5334 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005335 * Cookie: NAME=VALUE ;
5336 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005337 * | || || +--> p4
5338 * | || |+-------> p3
5339 * | || +--------> p2
5340 * | |+------------> p1
5341 * | +-------------> colon
5342 * +--------------------> cur_ptr
5343 */
5344
5345 if (*p1 == '$') {
5346 /* skip this one */
5347 }
5348 else {
5349 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005350 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005351 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005352 (p4 - p1 >= t->fe->capture_namelen) &&
5353 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005354 int log_len = p4 - p1;
5355
Willy Tarreau086b3b42007-05-13 21:45:51 +02005356 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005357 Alert("HTTP logging : out of memory.\n");
5358 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005359 if (log_len > t->fe->capture_len)
5360 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005361 memcpy(txn->cli_cookie, p1, log_len);
5362 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005363 }
5364 }
5365
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005366 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5367 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005368 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005369 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005370 char *delim;
5371
5372 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5373 * have the server ID betweek p3 and delim, and the original cookie between
5374 * delim+1 and p4. Otherwise, delim==p4 :
5375 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005376 * Cookie: NAME=SRV~VALUE ;
5377 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005378 * | || || | +--> p4
5379 * | || || +--------> delim
5380 * | || |+-----------> p3
5381 * | || +------------> p2
5382 * | |+----------------> p1
5383 * | +-----------------> colon
5384 * +------------------------> cur_ptr
5385 */
5386
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005387 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005388 for (delim = p3; delim < p4; delim++)
5389 if (*delim == COOKIE_DELIM)
5390 break;
5391 }
5392 else
5393 delim = p4;
5394
5395
5396 /* Here, we'll look for the first running server which supports the cookie.
5397 * This allows to share a same cookie between several servers, for example
5398 * to dedicate backup servers to specific servers only.
5399 * However, to prevent clients from sticking to cookie-less backup server
5400 * when they have incidentely learned an empty cookie, we simply ignore
5401 * empty cookies and mark them as invalid.
5402 */
5403 if (delim == p3)
5404 srv = NULL;
5405
5406 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005407 if (srv->cookie && (srv->cklen == delim - p3) &&
5408 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005409 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005410 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005411 txn->flags &= ~TX_CK_MASK;
5412 txn->flags |= TX_CK_VALID;
5413 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005414 t->srv = srv;
5415 break;
5416 } else {
5417 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005418 txn->flags &= ~TX_CK_MASK;
5419 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005420 }
5421 }
5422 srv = srv->next;
5423 }
5424
Willy Tarreau3d300592007-03-18 18:34:41 +01005425 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005426 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005427 txn->flags &= ~TX_CK_MASK;
5428 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005429 }
5430
5431 /* depending on the cookie mode, we may have to either :
5432 * - delete the complete cookie if we're in insert+indirect mode, so that
5433 * the server never sees it ;
5434 * - remove the server id from the cookie value, and tag the cookie as an
5435 * application cookie so that it does not get accidentely removed later,
5436 * if we're in cookie prefix mode
5437 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005438 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005439 int delta; /* negative */
5440
5441 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5442 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005443 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005444 cur_end += delta;
5445 cur_next += delta;
5446 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005447 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005448
5449 del_cookie = del_colon = NULL;
5450 app_cookies++; /* protect the header from deletion */
5451 }
5452 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005453 (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 +01005454 del_cookie = p1;
5455 del_colon = colon;
5456 }
5457 } else {
5458 /* now we know that we must keep this cookie since it's
5459 * not ours. But if we wanted to delete our cookie
5460 * earlier, we cannot remove the complete header, but we
5461 * can remove the previous block itself.
5462 */
5463 app_cookies++;
5464
5465 if (del_cookie != NULL) {
5466 int delta; /* negative */
5467
5468 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5469 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005470 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005471 cur_end += delta;
5472 cur_next += delta;
5473 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005474 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005475 del_cookie = del_colon = NULL;
5476 }
5477 }
5478
Cyril Bontéb21570a2009-11-29 20:04:48 +01005479 if (t->be->appsession_name != NULL) {
5480 int cmp_len, value_len;
5481 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005482
Cyril Bontéb21570a2009-11-29 20:04:48 +01005483 if (t->be->options2 & PR_O2_AS_PFX) {
5484 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5485 value_begin = p1 + t->be->appsession_name_len;
5486 value_len = p4 - p1 - t->be->appsession_name_len;
5487 } else {
5488 cmp_len = p2 - p1;
5489 value_begin = p3;
5490 value_len = p4 - p3;
5491 }
5492
5493 /* let's see if the cookie is our appcookie */
5494 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5495 /* Cool... it's the right one */
5496 manage_client_side_appsession(t, value_begin, value_len);
5497 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005498#if defined(DEBUG_HASH)
5499 Alert("manage_client_side_cookies\n");
5500 appsession_hash_dump(&(t->be->htbl_proxy));
5501#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005502 }/* end if ((t->proxy->appsession_name != NULL) ... */
5503 }
5504
5505 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005506 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005507 } /* while (p1 < cur_end) */
5508
5509 /* There's no more cookie on this line.
5510 * We may have marked the last one(s) for deletion.
5511 * We must do this now in two ways :
5512 * - if there is no app cookie, we simply delete the header ;
5513 * - if there are app cookies, we must delete the end of the
5514 * string properly, including the colon/semi-colon before
5515 * the cookie name.
5516 */
5517 if (del_cookie != NULL) {
5518 int delta;
5519 if (app_cookies) {
5520 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5521 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005522 cur_hdr->len += delta;
5523 } else {
5524 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005525
5526 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005527 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5528 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005529 cur_hdr->len = 0;
5530 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005531 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005532 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005533 }
5534
5535 /* keep the link from this header to next one */
5536 old_idx = cur_idx;
5537 } /* end of cookie processing on this header */
5538}
5539
5540
Willy Tarreaua15645d2007-03-18 16:22:39 +01005541/* Iterate the same filter through all response headers contained in <rtr>.
5542 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5543 */
5544int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5545{
5546 char term;
5547 char *cur_ptr, *cur_end, *cur_next;
5548 int cur_idx, old_idx, last_hdr;
5549 struct http_txn *txn = &t->txn;
5550 struct hdr_idx_elem *cur_hdr;
5551 int len, delta;
5552
5553 last_hdr = 0;
5554
5555 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5556 old_idx = 0;
5557
5558 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005559 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005560 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005561 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005562 (exp->action == ACT_ALLOW ||
5563 exp->action == ACT_DENY))
5564 return 0;
5565
5566 cur_idx = txn->hdr_idx.v[old_idx].next;
5567 if (!cur_idx)
5568 break;
5569
5570 cur_hdr = &txn->hdr_idx.v[cur_idx];
5571 cur_ptr = cur_next;
5572 cur_end = cur_ptr + cur_hdr->len;
5573 cur_next = cur_end + cur_hdr->cr + 1;
5574
5575 /* Now we have one header between cur_ptr and cur_end,
5576 * and the next header starts at cur_next.
5577 */
5578
5579 /* The annoying part is that pattern matching needs
5580 * that we modify the contents to null-terminate all
5581 * strings before testing them.
5582 */
5583
5584 term = *cur_end;
5585 *cur_end = '\0';
5586
5587 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5588 switch (exp->action) {
5589 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005590 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005591 last_hdr = 1;
5592 break;
5593
5594 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005595 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005596 last_hdr = 1;
5597 break;
5598
5599 case ACT_REPLACE:
5600 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5601 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5602 /* FIXME: if the user adds a newline in the replacement, the
5603 * index will not be recalculated for now, and the new line
5604 * will not be counted as a new header.
5605 */
5606
5607 cur_end += delta;
5608 cur_next += delta;
5609 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005610 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005611 break;
5612
5613 case ACT_REMOVE:
5614 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5615 cur_next += delta;
5616
Willy Tarreaufa355d42009-11-29 18:12:29 +01005617 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005618 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5619 txn->hdr_idx.used--;
5620 cur_hdr->len = 0;
5621 cur_end = NULL; /* null-term has been rewritten */
5622 break;
5623
5624 }
5625 }
5626 if (cur_end)
5627 *cur_end = term; /* restore the string terminator */
5628
5629 /* keep the link from this header to next one in case of later
5630 * removal of next header.
5631 */
5632 old_idx = cur_idx;
5633 }
5634 return 0;
5635}
5636
5637
5638/* Apply the filter to the status line in the response buffer <rtr>.
5639 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5640 * or -1 if a replacement resulted in an invalid status line.
5641 */
5642int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5643{
5644 char term;
5645 char *cur_ptr, *cur_end;
5646 int done;
5647 struct http_txn *txn = &t->txn;
5648 int len, delta;
5649
5650
Willy Tarreau3d300592007-03-18 18:34:41 +01005651 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005652 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005653 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005654 (exp->action == ACT_ALLOW ||
5655 exp->action == ACT_DENY))
5656 return 0;
5657 else if (exp->action == ACT_REMOVE)
5658 return 0;
5659
5660 done = 0;
5661
Willy Tarreau9cdde232007-05-02 20:58:19 +02005662 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005663 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5664
5665 /* Now we have the status line between cur_ptr and cur_end */
5666
5667 /* The annoying part is that pattern matching needs
5668 * that we modify the contents to null-terminate all
5669 * strings before testing them.
5670 */
5671
5672 term = *cur_end;
5673 *cur_end = '\0';
5674
5675 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5676 switch (exp->action) {
5677 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005678 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005679 done = 1;
5680 break;
5681
5682 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005683 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005684 done = 1;
5685 break;
5686
5687 case ACT_REPLACE:
5688 *cur_end = term; /* restore the string terminator */
5689 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5690 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5691 /* FIXME: if the user adds a newline in the replacement, the
5692 * index will not be recalculated for now, and the new line
5693 * will not be counted as a new header.
5694 */
5695
Willy Tarreaufa355d42009-11-29 18:12:29 +01005696 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005697 cur_end += delta;
5698
Willy Tarreau9cdde232007-05-02 20:58:19 +02005699 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005700 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005701 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005702 cur_ptr, cur_end + 1,
5703 NULL, NULL);
5704 if (unlikely(!cur_end))
5705 return -1;
5706
5707 /* we have a full respnse and we know that we have either a CR
5708 * or an LF at <ptr>.
5709 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005710 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005711 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5712 /* there is no point trying this regex on headers */
5713 return 1;
5714 }
5715 }
5716 *cur_end = term; /* restore the string terminator */
5717 return done;
5718}
5719
5720
5721
5722/*
5723 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5724 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5725 * unparsable response.
5726 */
5727int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5728{
Willy Tarreau3d300592007-03-18 18:34:41 +01005729 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005730 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005731 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005732 int ret;
5733
5734 /*
5735 * The interleaving of transformations and verdicts
5736 * makes it difficult to decide to continue or stop
5737 * the evaluation.
5738 */
5739
Willy Tarreau3d300592007-03-18 18:34:41 +01005740 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005741 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5742 exp->action == ACT_PASS)) {
5743 exp = exp->next;
5744 continue;
5745 }
5746
5747 /* Apply the filter to the status line. */
5748 ret = apply_filter_to_sts_line(t, rtr, exp);
5749 if (unlikely(ret < 0))
5750 return -1;
5751
5752 if (likely(ret == 0)) {
5753 /* The filter did not match the response, it can be
5754 * iterated through all headers.
5755 */
5756 apply_filter_to_resp_headers(t, rtr, exp);
5757 }
5758 exp = exp->next;
5759 }
5760 return 0;
5761}
5762
5763
5764
5765/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005766 * Manage server-side cookies. It can impact performance by about 2% so it is
5767 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005768 */
5769void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5770{
5771 struct http_txn *txn = &t->txn;
5772 char *p1, *p2, *p3, *p4;
5773
Willy Tarreaua15645d2007-03-18 16:22:39 +01005774 char *cur_ptr, *cur_end, *cur_next;
5775 int cur_idx, old_idx, delta;
5776
Willy Tarreaua15645d2007-03-18 16:22:39 +01005777 /* Iterate through the headers.
5778 * we start with the start line.
5779 */
5780 old_idx = 0;
5781 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5782
5783 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5784 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005785 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005786
5787 cur_hdr = &txn->hdr_idx.v[cur_idx];
5788 cur_ptr = cur_next;
5789 cur_end = cur_ptr + cur_hdr->len;
5790 cur_next = cur_end + cur_hdr->cr + 1;
5791
5792 /* We have one full header between cur_ptr and cur_end, and the
5793 * next header starts at cur_next. We're only interested in
5794 * "Cookie:" headers.
5795 */
5796
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005797 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5798 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005799 old_idx = cur_idx;
5800 continue;
5801 }
5802
5803 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005804 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005805
5806
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005807 /* maybe we only wanted to see if there was a set-cookie. Note that
5808 * the cookie capture is declared in the fronend.
5809 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005810 if (t->be->cookie_name == NULL &&
5811 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005812 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005813 return;
5814
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005815 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005816
5817 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005818 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5819 break;
5820
5821 /* p1 is at the beginning of the cookie name */
5822 p2 = p1;
5823
5824 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5825 p2++;
5826
5827 if (p2 == cur_end || *p2 == ';') /* next cookie */
5828 break;
5829
5830 p3 = p2 + 1; /* skip the '=' sign */
5831 if (p3 == cur_end)
5832 break;
5833
5834 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005835 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005836 p4++;
5837
5838 /* here, we have the cookie name between p1 and p2,
5839 * and its value between p3 and p4.
5840 * we can process it.
5841 */
5842
5843 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005844 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005845 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005846 (p4 - p1 >= t->fe->capture_namelen) &&
5847 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005848 int log_len = p4 - p1;
5849
Willy Tarreau086b3b42007-05-13 21:45:51 +02005850 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005851 Alert("HTTP logging : out of memory.\n");
5852 }
5853
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005854 if (log_len > t->fe->capture_len)
5855 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005856 memcpy(txn->srv_cookie, p1, log_len);
5857 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005858 }
5859
5860 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005861 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5862 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005863 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005864 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005865
5866 /* If the cookie is in insert mode on a known server, we'll delete
5867 * this occurrence because we'll insert another one later.
5868 * We'll delete it too if the "indirect" option is set and we're in
5869 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005870 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5871 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005872 /* this header must be deleted */
5873 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5874 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5875 txn->hdr_idx.used--;
5876 cur_hdr->len = 0;
5877 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005878 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879
Willy Tarreau3d300592007-03-18 18:34:41 +01005880 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005881 }
5882 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005883 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005884 /* replace bytes p3->p4 with the cookie name associated
5885 * with this server since we know it.
5886 */
5887 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5888 cur_hdr->len += delta;
5889 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005890 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891
Willy Tarreau3d300592007-03-18 18:34:41 +01005892 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005893 }
5894 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005895 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005896 /* insert the cookie name associated with this server
5897 * before existing cookie, and insert a delimitor between them..
5898 */
5899 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5900 cur_hdr->len += delta;
5901 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005902 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005903
5904 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005905 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906 }
5907 }
5908 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005909 else if (t->be->appsession_name != NULL) {
5910 int cmp_len, value_len;
5911 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005912
Cyril Bontéb21570a2009-11-29 20:04:48 +01005913 if (t->be->options2 & PR_O2_AS_PFX) {
5914 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5915 value_begin = p1 + t->be->appsession_name_len;
5916 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5917 } else {
5918 cmp_len = p2 - p1;
5919 value_begin = p3;
5920 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005921 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005922
5923 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5924 /* Cool... it's the right one */
5925 if (t->sessid != NULL) {
5926 /* free previously allocated memory as we don't need it anymore */
5927 pool_free2(apools.sessid, t->sessid);
5928 }
5929 /* Store the sessid in the session for future use */
5930 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5931 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5932 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5933 return;
5934 }
5935 memcpy(t->sessid, value_begin, value_len);
5936 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005937 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005938 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5940 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005941 /* keep the link from this header to next one */
5942 old_idx = cur_idx;
5943 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005944
5945 if (t->sessid != NULL) {
5946 appsess *asession = NULL;
5947 /* only do insert, if lookup fails */
5948 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5949 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01005950 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005951 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5952 Alert("Not enough Memory process_srv():asession:calloc().\n");
5953 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5954 return;
5955 }
5956 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5957 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5958 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5959 return;
5960 }
5961 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5962 asession->sessid[t->be->appsession_len] = 0;
5963
Willy Tarreau1fac7532010-01-09 19:23:06 +01005964 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005965 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5966 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5967 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5968 return;
5969 }
5970 asession->serverid[0] = '\0';
5971 memcpy(asession->serverid, t->srv->id, server_id_len);
5972
5973 asession->request_count = 0;
5974 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5975 }
5976
5977 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5978 asession->request_count++;
5979 }
5980
5981#if defined(DEBUG_HASH)
5982 Alert("manage_server_side_cookies\n");
5983 appsession_hash_dump(&(t->be->htbl_proxy));
5984#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005985}
5986
5987
5988
5989/*
5990 * Check if response is cacheable or not. Updates t->flags.
5991 */
5992void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5993{
5994 struct http_txn *txn = &t->txn;
5995 char *p1, *p2;
5996
5997 char *cur_ptr, *cur_end, *cur_next;
5998 int cur_idx;
5999
Willy Tarreau5df51872007-11-25 16:20:08 +01006000 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006001 return;
6002
6003 /* Iterate through the headers.
6004 * we start with the start line.
6005 */
6006 cur_idx = 0;
6007 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
6008
6009 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6010 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006011 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006012
6013 cur_hdr = &txn->hdr_idx.v[cur_idx];
6014 cur_ptr = cur_next;
6015 cur_end = cur_ptr + cur_hdr->len;
6016 cur_next = cur_end + cur_hdr->cr + 1;
6017
6018 /* We have one full header between cur_ptr and cur_end, and the
6019 * next header starts at cur_next. We're only interested in
6020 * "Cookie:" headers.
6021 */
6022
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006023 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6024 if (val) {
6025 if ((cur_end - (cur_ptr + val) >= 8) &&
6026 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6027 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6028 return;
6029 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006030 }
6031
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006032 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6033 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006034 continue;
6035
6036 /* OK, right now we know we have a cache-control header at cur_ptr */
6037
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006038 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006039
6040 if (p1 >= cur_end) /* no more info */
6041 continue;
6042
6043 /* p1 is at the beginning of the value */
6044 p2 = p1;
6045
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006046 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006047 p2++;
6048
6049 /* we have a complete value between p1 and p2 */
6050 if (p2 < cur_end && *p2 == '=') {
6051 /* we have something of the form no-cache="set-cookie" */
6052 if ((cur_end - p1 >= 21) &&
6053 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6054 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006055 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056 continue;
6057 }
6058
6059 /* OK, so we know that either p2 points to the end of string or to a comma */
6060 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6061 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6062 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6063 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006064 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006065 return;
6066 }
6067
6068 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006069 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006070 continue;
6071 }
6072 }
6073}
6074
6075
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076/*
6077 * Try to retrieve a known appsession in the URI, then the associated server.
6078 * If the server is found, it's assigned to the session.
6079 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006080void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006081{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006082 char *end_params, *first_param, *cur_param, *next_param;
6083 char separator;
6084 int value_len;
6085
6086 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006088 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006089 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006091 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092
Cyril Bontéb21570a2009-11-29 20:04:48 +01006093 first_param = NULL;
6094 switch (mode) {
6095 case PR_O2_AS_M_PP:
6096 first_param = memchr(begin, ';', len);
6097 break;
6098 case PR_O2_AS_M_QS:
6099 first_param = memchr(begin, '?', len);
6100 break;
6101 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006102
Cyril Bontéb21570a2009-11-29 20:04:48 +01006103 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006104 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006105 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006106
Cyril Bontéb21570a2009-11-29 20:04:48 +01006107 switch (mode) {
6108 case PR_O2_AS_M_PP:
6109 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6110 end_params = (char *) begin + len;
6111 }
6112 separator = ';';
6113 break;
6114 case PR_O2_AS_M_QS:
6115 end_params = (char *) begin + len;
6116 separator = '&';
6117 break;
6118 default:
6119 /* unknown mode, shouldn't happen */
6120 return;
6121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Cyril Bontéb21570a2009-11-29 20:04:48 +01006123 cur_param = next_param = end_params;
6124 while (cur_param > first_param) {
6125 cur_param--;
6126 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6127 /* let's see if this is the appsession parameter */
6128 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6129 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6130 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6131 /* Cool... it's the right one */
6132 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6133 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6134 if (value_len > 0) {
6135 manage_client_side_appsession(t, cur_param, value_len);
6136 }
6137 break;
6138 }
6139 next_param = cur_param;
6140 }
6141 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006142#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006143 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006144 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006145#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006146}
6147
6148
Willy Tarreaub2513902006-12-17 14:52:38 +01006149/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006150 * In a GET or HEAD request, check if the requested URI matches the stats uri
6151 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006152 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006153 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006154 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006155 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01006156 *
6157 * Returns 1 if the session's state changes, otherwise 0.
6158 */
6159int stats_check_uri_auth(struct session *t, struct proxy *backend)
6160{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006161 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006162 struct uri_auth *uri_auth = backend->uri_auth;
6163 struct user_auth *user;
6164 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006165 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006166
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006167 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006169 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006170 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006171 return 0;
6172
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006173 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006174
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006175 /* the URI is in h */
6176 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006177 return 0;
6178
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006179 h += uri_auth->uri_len;
6180 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
6181 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006182 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006183 break;
6184 }
6185 h++;
6186 }
6187
6188 if (uri_auth->refresh) {
6189 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6190 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
6191 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006192 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006193 break;
6194 }
6195 h++;
6196 }
6197 }
6198
Willy Tarreau55bb8452007-10-17 18:44:57 +02006199 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
6200 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
6201 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006202 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006203 break;
6204 }
6205 h++;
6206 }
6207
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006208 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6209
Willy Tarreaub2513902006-12-17 14:52:38 +01006210 /* we are in front of a interceptable URI. Let's check
6211 * if there's an authentication and if it's valid.
6212 */
6213 user = uri_auth->users;
6214 if (!user) {
6215 /* no user auth required, it's OK */
6216 authenticated = 1;
6217 } else {
6218 authenticated = 0;
6219
6220 /* a user list is defined, we have to check.
6221 * skip 21 chars for "Authorization: Basic ".
6222 */
6223
6224 /* FIXME: this should move to an earlier place */
6225 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006226 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
6227 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6228 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01006229 if (len > 14 &&
6230 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02006231 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01006232 break;
6233 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006234 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01006235 }
6236
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006237 if (txn->auth_hdr.len < 21 ||
6238 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01006239 user = NULL;
6240
6241 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006242 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
6243 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01006244 user->user_pwd, user->user_len)) {
6245 authenticated = 1;
6246 break;
6247 }
6248 user = user->next;
6249 }
6250 }
6251
6252 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01006253 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01006254
6255 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006256 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
6257 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006258 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01006259 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02006260 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006261 if (!(t->flags & SN_ERR_MASK))
6262 t->flags |= SN_ERR_PRXCOND;
6263 if (!(t->flags & SN_FINST_MASK))
6264 t->flags |= SN_FINST_R;
6265 return 1;
6266 }
6267
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006268 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01006269 * data.
6270 */
Willy Tarreau70089872008-06-13 21:12:51 +02006271 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01006272 t->data_source = DATA_SRC_STATS;
6273 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02006274 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02006275 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
6276 t->rep->prod->private = t;
6277 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01006278 return 1;
6279}
6280
Willy Tarreau4076a152009-04-02 15:18:36 +02006281/*
6282 * Capture a bad request or response and archive it in the proxy's structure.
6283 */
6284void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6285 struct buffer *buf, struct http_msg *msg,
6286 struct proxy *other_end)
6287{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006288 es->len = buf->r - (buf->data + msg->som);
6289 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006290 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006291 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006292 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006293 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006294 es->when = date; // user-visible date
6295 es->sid = s->uniq_id;
6296 es->srv = s->srv;
6297 es->oe = other_end;
6298 es->src = s->cli_addr;
6299}
Willy Tarreaub2513902006-12-17 14:52:38 +01006300
Willy Tarreaubaaee002006-06-26 02:48:02 +02006301/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302 * Print a debug line with a header
6303 */
6304void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6305{
6306 int len, max;
6307 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006308 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309 max = end - start;
6310 UBOUND(max, sizeof(trash) - len - 1);
6311 len += strlcpy2(trash + len, start, max + 1);
6312 trash[len++] = '\n';
6313 write(1, trash, len);
6314}
6315
Willy Tarreau0937bc42009-12-22 15:03:09 +01006316/*
6317 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6318 * the required fields are properly allocated and that we only need to (re)init
6319 * them. This should be used before processing any new request.
6320 */
6321void http_init_txn(struct session *s)
6322{
6323 struct http_txn *txn = &s->txn;
6324 struct proxy *fe = s->fe;
6325
6326 txn->flags = 0;
6327 txn->status = -1;
6328
6329 txn->req.sol = txn->req.eol = NULL;
6330 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6331 txn->rsp.sol = txn->rsp.eol = NULL;
6332 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6333 txn->req.hdr_content_len = 0LL;
6334 txn->rsp.hdr_content_len = 0LL;
6335 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6336 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6337 chunk_reset(&txn->auth_hdr);
6338
6339 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6340 if (fe->options2 & PR_O2_REQBUG_OK)
6341 txn->req.err_pos = -1; /* let buggy requests pass */
6342
Willy Tarreau46023632010-01-07 22:51:47 +01006343 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006344 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6345
Willy Tarreau46023632010-01-07 22:51:47 +01006346 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006347 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6348
6349 if (txn->hdr_idx.v)
6350 hdr_idx_init(&txn->hdr_idx);
6351}
6352
6353/* to be used at the end of a transaction */
6354void http_end_txn(struct session *s)
6355{
6356 struct http_txn *txn = &s->txn;
6357
6358 /* these ones will have been dynamically allocated */
6359 pool_free2(pool2_requri, txn->uri);
6360 pool_free2(pool2_capture, txn->cli_cookie);
6361 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau762a2362010-01-09 13:57:26 +01006362 pool_free2(apools.sessid, s->sessid);
6363 s->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006364 txn->uri = NULL;
6365 txn->srv_cookie = NULL;
6366 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006367
6368 if (txn->req.cap) {
6369 struct cap_hdr *h;
6370 for (h = s->fe->req_cap; h; h = h->next)
6371 pool_free2(h->pool, txn->req.cap[h->index]);
6372 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6373 }
6374
6375 if (txn->rsp.cap) {
6376 struct cap_hdr *h;
6377 for (h = s->fe->rsp_cap; h; h = h->next)
6378 pool_free2(h->pool, txn->rsp.cap[h->index]);
6379 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6380 }
6381
Willy Tarreau0937bc42009-12-22 15:03:09 +01006382}
6383
6384/* to be used at the end of a transaction to prepare a new one */
6385void http_reset_txn(struct session *s)
6386{
6387 http_end_txn(s);
6388 http_init_txn(s);
6389
6390 s->be = s->fe;
6391 s->req->analysers = s->listener->analysers;
6392 s->logs.logwait = s->fe->to_log;
6393 s->srv = s->prev_srv = s->srv_conn = NULL;
6394 s->pend_pos = NULL;
6395 s->conn_retries = s->be->conn_retries;
6396
6397 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6398
6399 s->req->rto = s->fe->timeout.client;
6400 s->req->wto = s->be->timeout.server;
6401 s->req->cto = s->be->timeout.connect;
6402
6403 s->rep->rto = s->be->timeout.server;
6404 s->rep->wto = s->fe->timeout.client;
6405 s->rep->cto = TICK_ETERNITY;
6406
6407 s->req->rex = TICK_ETERNITY;
6408 s->req->wex = TICK_ETERNITY;
6409 s->req->analyse_exp = TICK_ETERNITY;
6410 s->rep->rex = TICK_ETERNITY;
6411 s->rep->wex = TICK_ETERNITY;
6412 s->rep->analyse_exp = TICK_ETERNITY;
6413}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006414
Willy Tarreau8797c062007-05-07 00:55:35 +02006415/************************************************************************/
6416/* The code below is dedicated to ACL parsing and matching */
6417/************************************************************************/
6418
6419
6420
6421
6422/* 1. Check on METHOD
6423 * We use the pre-parsed method if it is known, and store its number as an
6424 * integer. If it is unknown, we use the pointer and the length.
6425 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006426static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006427{
6428 int len, meth;
6429
Willy Tarreauae8b7962007-06-09 23:10:04 +02006430 len = strlen(*text);
6431 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006432
6433 pattern->val.i = meth;
6434 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006435 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006436 if (!pattern->ptr.str)
6437 return 0;
6438 pattern->len = len;
6439 }
6440 return 1;
6441}
6442
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006443static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006444acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6445 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006446{
6447 int meth;
6448 struct http_txn *txn = l7;
6449
Willy Tarreaub6866442008-07-14 23:54:42 +02006450 if (!txn)
6451 return 0;
6452
Willy Tarreau655dce92009-11-08 13:10:58 +01006453 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006454 return 0;
6455
Willy Tarreau8797c062007-05-07 00:55:35 +02006456 meth = txn->meth;
6457 test->i = meth;
6458 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006459 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6460 /* ensure the indexes are not affected */
6461 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006462 test->len = txn->req.sl.rq.m_l;
6463 test->ptr = txn->req.sol;
6464 }
6465 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6466 return 1;
6467}
6468
6469static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6470{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006471 int icase;
6472
Willy Tarreau8797c062007-05-07 00:55:35 +02006473 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006474 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006475
6476 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006477 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006478
6479 /* Other method, we must compare the strings */
6480 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006481 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006482
6483 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6484 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6485 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006486 return ACL_PAT_FAIL;
6487 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006488}
6489
6490/* 2. Check on Request/Status Version
6491 * We simply compare strings here.
6492 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006493static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006494{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006495 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006496 if (!pattern->ptr.str)
6497 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006498 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006499 return 1;
6500}
6501
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006502static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006503acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6504 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006505{
6506 struct http_txn *txn = l7;
6507 char *ptr;
6508 int len;
6509
Willy Tarreaub6866442008-07-14 23:54:42 +02006510 if (!txn)
6511 return 0;
6512
Willy Tarreau655dce92009-11-08 13:10:58 +01006513 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006514 return 0;
6515
Willy Tarreau8797c062007-05-07 00:55:35 +02006516 len = txn->req.sl.rq.v_l;
6517 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6518
6519 while ((len-- > 0) && (*ptr++ != '/'));
6520 if (len <= 0)
6521 return 0;
6522
6523 test->ptr = ptr;
6524 test->len = len;
6525
6526 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6527 return 1;
6528}
6529
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006530static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006531acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6532 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006533{
6534 struct http_txn *txn = l7;
6535 char *ptr;
6536 int len;
6537
Willy Tarreaub6866442008-07-14 23:54:42 +02006538 if (!txn)
6539 return 0;
6540
Willy Tarreau655dce92009-11-08 13:10:58 +01006541 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006542 return 0;
6543
Willy Tarreau8797c062007-05-07 00:55:35 +02006544 len = txn->rsp.sl.st.v_l;
6545 ptr = txn->rsp.sol;
6546
6547 while ((len-- > 0) && (*ptr++ != '/'));
6548 if (len <= 0)
6549 return 0;
6550
6551 test->ptr = ptr;
6552 test->len = len;
6553
6554 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6555 return 1;
6556}
6557
6558/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006559static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006560acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6561 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006562{
6563 struct http_txn *txn = l7;
6564 char *ptr;
6565 int len;
6566
Willy Tarreaub6866442008-07-14 23:54:42 +02006567 if (!txn)
6568 return 0;
6569
Willy Tarreau655dce92009-11-08 13:10:58 +01006570 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006571 return 0;
6572
Willy Tarreau8797c062007-05-07 00:55:35 +02006573 len = txn->rsp.sl.st.c_l;
6574 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6575
6576 test->i = __strl2ui(ptr, len);
6577 test->flags = ACL_TEST_F_VOL_1ST;
6578 return 1;
6579}
6580
6581/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006582static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006583acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6584 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006585{
6586 struct http_txn *txn = l7;
6587
Willy Tarreaub6866442008-07-14 23:54:42 +02006588 if (!txn)
6589 return 0;
6590
Willy Tarreau655dce92009-11-08 13:10:58 +01006591 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006592 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006593
Willy Tarreauc11416f2007-06-17 16:58:38 +02006594 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6595 /* ensure the indexes are not affected */
6596 return 0;
6597
Willy Tarreau8797c062007-05-07 00:55:35 +02006598 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006599 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006600
Willy Tarreauf3d25982007-05-08 22:45:09 +02006601 /* we do not need to set READ_ONLY because the data is in a buffer */
6602 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006603 return 1;
6604}
6605
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006606static int
6607acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6608 struct acl_expr *expr, struct acl_test *test)
6609{
6610 struct http_txn *txn = l7;
6611
Willy Tarreaub6866442008-07-14 23:54:42 +02006612 if (!txn)
6613 return 0;
6614
Willy Tarreau655dce92009-11-08 13:10:58 +01006615 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006616 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006617
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006618 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6619 /* ensure the indexes are not affected */
6620 return 0;
6621
6622 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006623 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006624 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6625 test->i = AF_INET;
6626
6627 /*
6628 * If we are parsing url in frontend space, we prepare backend stage
6629 * to not parse again the same url ! optimization lazyness...
6630 */
6631 if (px->options & PR_O_HTTP_PROXY)
6632 l4->flags |= SN_ADDR_SET;
6633
6634 test->flags = ACL_TEST_F_READ_ONLY;
6635 return 1;
6636}
6637
6638static int
6639acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6640 struct acl_expr *expr, struct acl_test *test)
6641{
6642 struct http_txn *txn = l7;
6643
Willy Tarreaub6866442008-07-14 23:54:42 +02006644 if (!txn)
6645 return 0;
6646
Willy Tarreau655dce92009-11-08 13:10:58 +01006647 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006648 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006649
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006650 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6651 /* ensure the indexes are not affected */
6652 return 0;
6653
6654 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006655 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006656 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6657
6658 if (px->options & PR_O_HTTP_PROXY)
6659 l4->flags |= SN_ADDR_SET;
6660
6661 test->flags = ACL_TEST_F_READ_ONLY;
6662 return 1;
6663}
6664
Willy Tarreauc11416f2007-06-17 16:58:38 +02006665/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6666 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6667 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006668static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006669acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006670 struct acl_expr *expr, struct acl_test *test)
6671{
6672 struct http_txn *txn = l7;
6673 struct hdr_idx *idx = &txn->hdr_idx;
6674 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006675
Willy Tarreaub6866442008-07-14 23:54:42 +02006676 if (!txn)
6677 return 0;
6678
Willy Tarreau33a7e692007-06-10 19:45:56 +02006679 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6680 /* search for header from the beginning */
6681 ctx->idx = 0;
6682
Willy Tarreau33a7e692007-06-10 19:45:56 +02006683 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6684 test->flags |= ACL_TEST_F_FETCH_MORE;
6685 test->flags |= ACL_TEST_F_VOL_HDR;
6686 test->len = ctx->vlen;
6687 test->ptr = (char *)ctx->line + ctx->val;
6688 return 1;
6689 }
6690
6691 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6692 test->flags |= ACL_TEST_F_VOL_HDR;
6693 return 0;
6694}
6695
Willy Tarreau33a7e692007-06-10 19:45:56 +02006696static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006697acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6698 struct acl_expr *expr, struct acl_test *test)
6699{
6700 struct http_txn *txn = l7;
6701
Willy Tarreaub6866442008-07-14 23:54:42 +02006702 if (!txn)
6703 return 0;
6704
Willy Tarreau655dce92009-11-08 13:10:58 +01006705 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006706 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006707
Willy Tarreauc11416f2007-06-17 16:58:38 +02006708 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6709 /* ensure the indexes are not affected */
6710 return 0;
6711
6712 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6713}
6714
6715static int
6716acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6717 struct acl_expr *expr, struct acl_test *test)
6718{
6719 struct http_txn *txn = l7;
6720
Willy Tarreaub6866442008-07-14 23:54:42 +02006721 if (!txn)
6722 return 0;
6723
Willy Tarreau655dce92009-11-08 13:10:58 +01006724 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006725 return 0;
6726
6727 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6728}
6729
6730/* 6. Check on HTTP header count. The number of occurrences is returned.
6731 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6732 */
6733static int
6734acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006735 struct acl_expr *expr, struct acl_test *test)
6736{
6737 struct http_txn *txn = l7;
6738 struct hdr_idx *idx = &txn->hdr_idx;
6739 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006740 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006741
Willy Tarreaub6866442008-07-14 23:54:42 +02006742 if (!txn)
6743 return 0;
6744
Willy Tarreau33a7e692007-06-10 19:45:56 +02006745 ctx.idx = 0;
6746 cnt = 0;
6747 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6748 cnt++;
6749
6750 test->i = cnt;
6751 test->flags = ACL_TEST_F_VOL_HDR;
6752 return 1;
6753}
6754
Willy Tarreauc11416f2007-06-17 16:58:38 +02006755static int
6756acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6757 struct acl_expr *expr, struct acl_test *test)
6758{
6759 struct http_txn *txn = l7;
6760
Willy Tarreaub6866442008-07-14 23:54:42 +02006761 if (!txn)
6762 return 0;
6763
Willy Tarreau655dce92009-11-08 13:10:58 +01006764 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006765 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006766
Willy Tarreauc11416f2007-06-17 16:58:38 +02006767 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6768 /* ensure the indexes are not affected */
6769 return 0;
6770
6771 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6772}
6773
6774static int
6775acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6776 struct acl_expr *expr, struct acl_test *test)
6777{
6778 struct http_txn *txn = l7;
6779
Willy Tarreaub6866442008-07-14 23:54:42 +02006780 if (!txn)
6781 return 0;
6782
Willy Tarreau655dce92009-11-08 13:10:58 +01006783 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006784 return 0;
6785
6786 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6787}
6788
Willy Tarreau33a7e692007-06-10 19:45:56 +02006789/* 7. Check on HTTP header's integer value. The integer value is returned.
6790 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006791 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006792 */
6793static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006794acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006795 struct acl_expr *expr, struct acl_test *test)
6796{
6797 struct http_txn *txn = l7;
6798 struct hdr_idx *idx = &txn->hdr_idx;
6799 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006800
Willy Tarreaub6866442008-07-14 23:54:42 +02006801 if (!txn)
6802 return 0;
6803
Willy Tarreau33a7e692007-06-10 19:45:56 +02006804 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6805 /* search for header from the beginning */
6806 ctx->idx = 0;
6807
Willy Tarreau33a7e692007-06-10 19:45:56 +02006808 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6809 test->flags |= ACL_TEST_F_FETCH_MORE;
6810 test->flags |= ACL_TEST_F_VOL_HDR;
6811 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6812 return 1;
6813 }
6814
6815 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6816 test->flags |= ACL_TEST_F_VOL_HDR;
6817 return 0;
6818}
6819
Willy Tarreauc11416f2007-06-17 16:58:38 +02006820static int
6821acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6822 struct acl_expr *expr, struct acl_test *test)
6823{
6824 struct http_txn *txn = l7;
6825
Willy Tarreaub6866442008-07-14 23:54:42 +02006826 if (!txn)
6827 return 0;
6828
Willy Tarreau655dce92009-11-08 13:10:58 +01006829 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006830 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006831
Willy Tarreauc11416f2007-06-17 16:58:38 +02006832 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6833 /* ensure the indexes are not affected */
6834 return 0;
6835
6836 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6837}
6838
6839static int
6840acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6841 struct acl_expr *expr, struct acl_test *test)
6842{
6843 struct http_txn *txn = l7;
6844
Willy Tarreaub6866442008-07-14 23:54:42 +02006845 if (!txn)
6846 return 0;
6847
Willy Tarreau655dce92009-11-08 13:10:58 +01006848 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006849 return 0;
6850
6851 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6852}
6853
Willy Tarreau106f9792009-09-19 07:54:16 +02006854/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6855 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6856 */
6857static int
6858acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6859 struct acl_expr *expr, struct acl_test *test)
6860{
6861 struct http_txn *txn = l7;
6862 struct hdr_idx *idx = &txn->hdr_idx;
6863 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6864
6865 if (!txn)
6866 return 0;
6867
6868 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6869 /* search for header from the beginning */
6870 ctx->idx = 0;
6871
6872 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6873 test->flags |= ACL_TEST_F_FETCH_MORE;
6874 test->flags |= ACL_TEST_F_VOL_HDR;
6875 /* Same optimization as url_ip */
6876 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6877 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6878 test->ptr = (void *)&l4->srv_addr.sin_addr;
6879 test->i = AF_INET;
6880 return 1;
6881 }
6882
6883 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6884 test->flags |= ACL_TEST_F_VOL_HDR;
6885 return 0;
6886}
6887
6888static int
6889acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6890 struct acl_expr *expr, struct acl_test *test)
6891{
6892 struct http_txn *txn = l7;
6893
6894 if (!txn)
6895 return 0;
6896
Willy Tarreau655dce92009-11-08 13:10:58 +01006897 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006898 return 0;
6899
6900 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6901 /* ensure the indexes are not affected */
6902 return 0;
6903
6904 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6905}
6906
6907static int
6908acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6909 struct acl_expr *expr, struct acl_test *test)
6910{
6911 struct http_txn *txn = l7;
6912
6913 if (!txn)
6914 return 0;
6915
Willy Tarreau655dce92009-11-08 13:10:58 +01006916 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006917 return 0;
6918
6919 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6920}
6921
Willy Tarreau737b0c12007-06-10 21:28:46 +02006922/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6923 * the first '/' after the possible hostname, and ends before the possible '?'.
6924 */
6925static int
6926acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6927 struct acl_expr *expr, struct acl_test *test)
6928{
6929 struct http_txn *txn = l7;
6930 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006931
Willy Tarreaub6866442008-07-14 23:54:42 +02006932 if (!txn)
6933 return 0;
6934
Willy Tarreau655dce92009-11-08 13:10:58 +01006935 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006936 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006937
Willy Tarreauc11416f2007-06-17 16:58:38 +02006938 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6939 /* ensure the indexes are not affected */
6940 return 0;
6941
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006942 end = txn->req.sol - txn->req.som + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006943 ptr = http_get_path(txn);
6944 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006945 return 0;
6946
6947 /* OK, we got the '/' ! */
6948 test->ptr = ptr;
6949
6950 while (ptr < end && *ptr != '?')
6951 ptr++;
6952
6953 test->len = ptr - test->ptr;
6954
6955 /* we do not need to set READ_ONLY because the data is in a buffer */
6956 test->flags = ACL_TEST_F_VOL_1ST;
6957 return 1;
6958}
6959
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006960static int
6961acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6962 struct acl_expr *expr, struct acl_test *test)
6963{
6964 struct buffer *req = s->req;
6965 struct http_txn *txn = &s->txn;
6966 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006967
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006968 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6969 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6970 */
6971
6972 if (!s || !req)
6973 return 0;
6974
Willy Tarreau655dce92009-11-08 13:10:58 +01006975 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006976 /* Already decoded as OK */
6977 test->flags |= ACL_TEST_F_SET_RES_PASS;
6978 return 1;
6979 }
6980
6981 /* Try to decode HTTP request */
6982 if (likely(req->lr < req->r))
6983 http_msg_analyzer(req, msg, &txn->hdr_idx);
6984
Willy Tarreau655dce92009-11-08 13:10:58 +01006985 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006986 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6987 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6988 return 1;
6989 }
6990 /* wait for final state */
6991 test->flags |= ACL_TEST_F_MAY_CHANGE;
6992 return 0;
6993 }
6994
6995 /* OK we got a valid HTTP request. We have some minor preparation to
6996 * perform so that further checks can rely on HTTP tests.
6997 */
6998 msg->sol = req->data + msg->som;
6999 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
7000 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7001 s->flags |= SN_REDIRECTABLE;
7002
7003 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7004 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7005 return 1;
7006 }
7007
7008 test->flags |= ACL_TEST_F_SET_RES_PASS;
7009 return 1;
7010}
7011
Willy Tarreau8797c062007-05-07 00:55:35 +02007012
7013/************************************************************************/
7014/* All supported keywords must be declared here. */
7015/************************************************************************/
7016
7017/* Note: must not be declared <const> as its list will be overwritten */
7018static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007019 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7020
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007021 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7022 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7023 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7024 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007025
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007026 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7027 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7028 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7029 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7030 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7031 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7032 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7033 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7034 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007035
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007036 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7037 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7038 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7039 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7040 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7041 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7042 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7043 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7044 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7045 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007046 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007047
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007048 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7049 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7050 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7051 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7052 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7053 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7054 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7055 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7056 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007057 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007058
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007059 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7060 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7061 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7062 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7063 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7064 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7065 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007066
Willy Tarreauf3d25982007-05-08 22:45:09 +02007067 { NULL, NULL, NULL, NULL },
7068
7069#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007070 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7071 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7072 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7073 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7074 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7075 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7076 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7077
Willy Tarreau8797c062007-05-07 00:55:35 +02007078 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7079 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7080 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7081 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7082 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7083 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7084 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7085 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
7086
7087 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
7088 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
7089 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
7090 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
7091 { NULL, NULL, NULL, NULL },
7092#endif
7093}};
7094
7095
7096__attribute__((constructor))
7097static void __http_protocol_init(void)
7098{
7099 acl_register_keywords(&acl_kws);
7100}
7101
7102
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007104 * Local variables:
7105 * c-indent-level: 8
7106 * c-basic-offset: 8
7107 * End:
7108 */