blob: 8816699ac9d61ebdc2b369b1e2ce720ec71031c9 [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>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020047#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/fd.h>
50#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020052#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010056#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010058#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020059#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/task.h>
61
Willy Tarreau522d6c02009-12-06 18:49:18 +010062const char HTTP_100[] =
63 "HTTP/1.1 100 Continue\r\n\r\n";
64
65const struct chunk http_100_chunk = {
66 .str = (char *)&HTTP_100,
67 .len = sizeof(HTTP_100)-1
68};
69
Willy Tarreau1c47f852006-07-09 08:22:27 +020070/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010071const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020072 "HTTP/1.0 200 OK\r\n"
73 "Cache-Control: no-cache\r\n"
74 "Connection: close\r\n"
75 "Content-Type: text/html\r\n"
76 "\r\n"
77 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
78
Willy Tarreau0f772532006-12-23 20:51:41 +010079const struct chunk http_200_chunk = {
80 .str = (char *)&HTTP_200,
81 .len = sizeof(HTTP_200)-1
82};
83
Willy Tarreaua9679ac2010-01-03 17:32:57 +010084/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020089 "Location: "; /* not terminated since it will be concatenated with the URL */
90
Willy Tarreau0f772532006-12-23 20:51:41 +010091const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
97/* same as 302 except that the browser MUST retry with the GET method */
98const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100101 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
105const char *HTTP_401_fmt =
106 "HTTP/1.0 401 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200109 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau844a7e72010-01-31 21:46:18 +0100114const char *HTTP_407_fmt =
115 "HTTP/1.0 407 Unauthorized\r\n"
116 "Cache-Control: no-cache\r\n"
117 "Connection: close\r\n"
118 "Content-Type: text/html\r\n"
119 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
120 "\r\n"
121 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
122
Willy Tarreau0f772532006-12-23 20:51:41 +0100123
124const int http_err_codes[HTTP_ERR_SIZE] = {
125 [HTTP_ERR_400] = 400,
126 [HTTP_ERR_403] = 403,
127 [HTTP_ERR_408] = 408,
128 [HTTP_ERR_500] = 500,
129 [HTTP_ERR_502] = 502,
130 [HTTP_ERR_503] = 503,
131 [HTTP_ERR_504] = 504,
132};
133
Willy Tarreau80587432006-12-24 17:47:20 +0100134static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100136 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 "Cache-Control: no-cache\r\n"
138 "Connection: close\r\n"
139 "Content-Type: text/html\r\n"
140 "\r\n"
141 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
142
143 [HTTP_ERR_403] =
144 "HTTP/1.0 403 Forbidden\r\n"
145 "Cache-Control: no-cache\r\n"
146 "Connection: close\r\n"
147 "Content-Type: text/html\r\n"
148 "\r\n"
149 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
150
151 [HTTP_ERR_408] =
152 "HTTP/1.0 408 Request Time-out\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
158
159 [HTTP_ERR_500] =
160 "HTTP/1.0 500 Server Error\r\n"
161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
166
167 [HTTP_ERR_502] =
168 "HTTP/1.0 502 Bad Gateway\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
174
175 [HTTP_ERR_503] =
176 "HTTP/1.0 503 Service Unavailable\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
182
183 [HTTP_ERR_504] =
184 "HTTP/1.0 504 Gateway Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
190
191};
192
Willy Tarreau80587432006-12-24 17:47:20 +0100193/* We must put the messages here since GCC cannot initialize consts depending
194 * on strlen().
195 */
196struct chunk http_err_chunks[HTTP_ERR_SIZE];
197
Willy Tarreau42250582007-04-01 01:30:43 +0200198#define FD_SETS_ARE_BITFIELDS
199#ifdef FD_SETS_ARE_BITFIELDS
200/*
201 * This map is used with all the FD_* macros to check whether a particular bit
202 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
203 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
204 * byte should be encoded. Be careful to always pass bytes from 0 to 255
205 * exclusively to the macros.
206 */
207fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
208fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209
210#else
211#error "Check if your OS uses bitfields for fd_sets"
212#endif
213
Willy Tarreau80587432006-12-24 17:47:20 +0100214void init_proto_http()
215{
Willy Tarreau42250582007-04-01 01:30:43 +0200216 int i;
217 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100218 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200219
Willy Tarreau80587432006-12-24 17:47:20 +0100220 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
221 if (!http_err_msgs[msg]) {
222 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
223 abort();
224 }
225
226 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
227 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
228 }
Willy Tarreau42250582007-04-01 01:30:43 +0200229
230 /* initialize the log header encoding map : '{|}"#' should be encoded with
231 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
232 * URL encoding only requires '"', '#' to be encoded as well as non-
233 * printable characters above.
234 */
235 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
236 memset(url_encode_map, 0, sizeof(url_encode_map));
237 for (i = 0; i < 32; i++) {
238 FD_SET(i, hdr_encode_map);
239 FD_SET(i, url_encode_map);
240 }
241 for (i = 127; i < 256; i++) {
242 FD_SET(i, hdr_encode_map);
243 FD_SET(i, url_encode_map);
244 }
245
246 tmp = "\"#{|}";
247 while (*tmp) {
248 FD_SET(*tmp, hdr_encode_map);
249 tmp++;
250 }
251
252 tmp = "\"#";
253 while (*tmp) {
254 FD_SET(*tmp, url_encode_map);
255 tmp++;
256 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200257
258 /* memory allocations */
259 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200260 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100261}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263/*
264 * We have 26 list of methods (1 per first letter), each of which can have
265 * up to 3 entries (2 valid, 1 null).
266 */
267struct http_method_desc {
268 http_meth_t meth;
269 int len;
270 const char text[8];
271};
272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100273const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100274 ['C' - 'A'] = {
275 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
276 },
277 ['D' - 'A'] = {
278 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
279 },
280 ['G' - 'A'] = {
281 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
282 },
283 ['H' - 'A'] = {
284 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
285 },
286 ['P' - 'A'] = {
287 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
288 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
289 },
290 ['T' - 'A'] = {
291 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
292 },
293 /* rest is empty like this :
294 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
295 */
296};
297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100298/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200299 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100300 * RFC2616 for those chars.
301 */
302
303const char http_is_spht[256] = {
304 [' '] = 1, ['\t'] = 1,
305};
306
307const char http_is_crlf[256] = {
308 ['\r'] = 1, ['\n'] = 1,
309};
310
311const char http_is_lws[256] = {
312 [' '] = 1, ['\t'] = 1,
313 ['\r'] = 1, ['\n'] = 1,
314};
315
316const char http_is_sep[256] = {
317 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
318 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
319 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
320 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
321 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
322};
323
324const char http_is_ctl[256] = {
325 [0 ... 31] = 1,
326 [127] = 1,
327};
328
329/*
330 * A token is any ASCII char that is neither a separator nor a CTL char.
331 * Do not overwrite values in assignment since gcc-2.95 will not handle
332 * them correctly. Instead, define every non-CTL char's status.
333 */
334const char http_is_token[256] = {
335 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
336 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
337 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
338 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
339 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
340 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
341 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
342 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
343 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
344 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
345 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
346 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
347 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
348 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
349 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
350 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
351 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
352 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
353 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
354 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
355 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
356 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
357 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
358 ['|'] = 1, ['}'] = 0, ['~'] = 1,
359};
360
361
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100362/*
363 * An http ver_token is any ASCII which can be found in an HTTP version,
364 * which includes 'H', 'T', 'P', '/', '.' and any digit.
365 */
366const char http_is_ver_token[256] = {
367 ['.'] = 1, ['/'] = 1,
368 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
369 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
370 ['H'] = 1, ['P'] = 1, ['T'] = 1,
371};
372
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100375 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
376 */
377#if defined(DEBUG_FSM)
378static void http_silent_debug(int line, struct session *s)
379{
380 int size = 0;
381 size += snprintf(trash + size, sizeof(trash) - size,
382 "[%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",
383 line,
384 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
385 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);
386 write(-1, trash, size);
387 size = 0;
388 size += snprintf(trash + size, sizeof(trash) - size,
389 " %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",
390 line,
391 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
392 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);
393
394 write(-1, trash, size);
395}
396#else
397#define http_silent_debug(l,s) do { } while (0)
398#endif
399
400/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100401 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
402 * CRLF. Text length is measured first, so it cannot be NULL.
403 * The header is also automatically added to the index <hdr_idx>, and the end
404 * of headers is automatically adjusted. The number of bytes added is returned
405 * on success, otherwise <0 is returned indicating an error.
406 */
407int http_header_add_tail(struct buffer *b, struct http_msg *msg,
408 struct hdr_idx *hdr_idx, const char *text)
409{
410 int bytes, len;
411
412 len = strlen(text);
413 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
414 if (!bytes)
415 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100416 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100417 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
418}
419
420/*
421 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
422 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
423 * the buffer is only opened and the space reserved, but nothing is copied.
424 * The header is also automatically added to the index <hdr_idx>, and the end
425 * of headers is automatically adjusted. The number of bytes added is returned
426 * on success, otherwise <0 is returned indicating an error.
427 */
428int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
429 struct hdr_idx *hdr_idx, const char *text, int len)
430{
431 int bytes;
432
433 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
434 if (!bytes)
435 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100436 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100437 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
438}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439
440/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100441 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
442 * If so, returns the position of the first non-space character relative to
443 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
444 * to return a pointer to the place after the first space. Returns 0 if the
445 * header name does not match. Checks are case-insensitive.
446 */
447int http_header_match2(const char *hdr, const char *end,
448 const char *name, int len)
449{
450 const char *val;
451
452 if (hdr + len >= end)
453 return 0;
454 if (hdr[len] != ':')
455 return 0;
456 if (strncasecmp(hdr, name, len) != 0)
457 return 0;
458 val = hdr + len + 1;
459 while (val < end && HTTP_IS_SPHT(*val))
460 val++;
461 if ((val >= end) && (len + 2 <= end - hdr))
462 return len + 2; /* we may replace starting from second space */
463 return val - hdr;
464}
465
Willy Tarreau68085d82010-01-18 14:54:04 +0100466/* Find the end of the header value contained between <s> and <e>. See RFC2616,
467 * par 2.2 for more information. Note that it requires a valid header to return
468 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200469 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100470char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200471{
472 int quoted, qdpair;
473
474 quoted = qdpair = 0;
475 for (; s < e; s++) {
476 if (qdpair) qdpair = 0;
477 else if (quoted && *s == '\\') qdpair = 1;
478 else if (quoted && *s == '"') quoted = 0;
479 else if (*s == '"') quoted = 1;
480 else if (*s == ',') return s;
481 }
482 return s;
483}
484
485/* Find the first or next occurrence of header <name> in message buffer <sol>
486 * using headers index <idx>, and return it in the <ctx> structure. This
487 * structure holds everything necessary to use the header and find next
488 * occurrence. If its <idx> member is 0, the header is searched from the
489 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100490 * 1 when it finds a value, and 0 when there is no more. It is designed to work
491 * with headers defined as comma-separated lists. As a special case, if ctx->val
492 * is NULL when searching for a new values of a header, the current header is
493 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200494 */
495int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100496 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 struct hdr_ctx *ctx)
498{
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *eol, *sov;
500 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200501
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 cur_idx = ctx->idx;
503 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504 /* We have previously returned a value, let's search
505 * another one on the same line.
506 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100508 ctx->del = ctx->val + ctx->vlen;
509 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 eol = sol + idx->v[cur_idx].len;
511
512 if (sov >= eol)
513 /* no more values in this header */
514 goto next_hdr;
515
Willy Tarreau68085d82010-01-18 14:54:04 +0100516 /* values remaining for this header, skip the comma but save it
517 * for later use (eg: for header deletion).
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sov++;
520 while (sov < eol && http_is_lws[(unsigned char)*sov])
521 sov++;
522
523 goto return_hdr;
524 }
525
526 /* first request for this header */
527 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200529 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 while (cur_idx) {
531 eol = sol + idx->v[cur_idx].len;
532
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200533 if (len == 0) {
534 /* No argument was passed, we want any header.
535 * To achieve this, we simply build a fake request. */
536 while (sol + len < eol && sol[len] != ':')
537 len++;
538 name = sol;
539 }
540
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 if ((len < eol - sol) &&
542 (sol[len] == ':') &&
543 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100544 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200545 sov = sol + len + 1;
546 while (sov < eol && http_is_lws[(unsigned char)*sov])
547 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100548
Willy Tarreau33a7e692007-06-10 19:45:56 +0200549 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100550 ctx->prev = old_idx;
551 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->idx = cur_idx;
553 ctx->val = sov - sol;
554
555 eol = find_hdr_value_end(sov, eol);
556 ctx->vlen = eol - sov;
557 return 1;
558 }
559 next_hdr:
560 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200562 cur_idx = idx->v[cur_idx].next;
563 }
564 return 0;
565}
566
567int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100568 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200569 struct hdr_ctx *ctx)
570{
571 return http_find_header2(name, strlen(name), sol, idx, ctx);
572}
573
Willy Tarreau68085d82010-01-18 14:54:04 +0100574/* Remove one value of a header. This only works on a <ctx> returned by one of
575 * the http_find_header functions. The value is removed, as well as surrounding
576 * commas if any. If the removed value was alone, the whole header is removed.
577 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
578 * message <msg>. The new index is returned. If it is zero, it means there is
579 * no more header, so any processing may stop. The ctx is always left in a form
580 * that can be handled by http_find_header2() to find next occurrence.
581 */
582int http_remove_header2(struct http_msg *msg, struct buffer *buf,
583 struct hdr_idx *idx, struct hdr_ctx *ctx)
584{
585 int cur_idx = ctx->idx;
586 char *sol = ctx->line;
587 struct hdr_idx_elem *hdr;
588 int delta, skip_comma;
589
590 if (!cur_idx)
591 return 0;
592
593 hdr = &idx->v[cur_idx];
594 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
595 /* This was the only value of the header, we must now remove it entirely. */
596 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
597 http_msg_move_end(msg, delta);
598 idx->used--;
599 hdr->len = 0; /* unused entry */
600 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
601 ctx->idx = ctx->prev; /* walk back to the end of previous header */
602 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
603 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
604 ctx->vlen = 0;
605 return ctx->idx;
606 }
607
608 /* This was not the only value of this header. We have to remove between
609 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
610 * of the list, we remove the last separator.
611 */
612
613 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
614 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
615 sol + ctx->val + ctx->vlen + skip_comma,
616 NULL, 0);
617 hdr->len += delta;
618 http_msg_move_end(msg, delta);
619 ctx->val = ctx->del;
620 ctx->vlen = 0;
621 return ctx->idx;
622}
623
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100624/* This function handles a server error at the stream interface level. The
625 * stream interface is assumed to be already in a closed state. An optional
626 * message is copied into the input buffer, and an HTTP status code stored.
627 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100628 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100630static void http_server_error(struct session *t, struct stream_interface *si,
631 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100633 buffer_auto_read(si->ob);
634 buffer_abort(si->ob);
635 buffer_auto_close(si->ob);
636 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200637 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100638 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100639 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100640 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 }
643 if (!(t->flags & SN_ERR_MASK))
644 t->flags |= err;
645 if (!(t->flags & SN_FINST_MASK))
646 t->flags |= finst;
647}
648
Willy Tarreau80587432006-12-24 17:47:20 +0100649/* This function returns the appropriate error location for the given session
650 * and message.
651 */
652
653struct chunk *error_message(struct session *s, int msgnum)
654{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200655 if (s->be->errmsg[msgnum].str)
656 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100657 else if (s->fe->errmsg[msgnum].str)
658 return &s->fe->errmsg[msgnum];
659 else
660 return &http_err_chunks[msgnum];
661}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
Willy Tarreau53b6c742006-12-17 13:37:46 +0100663/*
664 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
665 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
666 */
667static http_meth_t find_http_meth(const char *str, const int len)
668{
669 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100670 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100671
672 m = ((unsigned)*str - 'A');
673
674 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 for (h = http_methods[m]; h->len > 0; h++) {
676 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100677 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100679 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 };
681 return HTTP_METH_OTHER;
682 }
683 return HTTP_METH_NONE;
684
685}
686
Willy Tarreau21d2af32008-02-14 20:25:24 +0100687/* Parse the URI from the given transaction (which is assumed to be in request
688 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
689 * It is returned otherwise.
690 */
691static char *
692http_get_path(struct http_txn *txn)
693{
694 char *ptr, *end;
695
Willy Tarreau962c3f42010-01-10 00:15:35 +0100696 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100697 end = ptr + txn->req.sl.rq.u_l;
698
699 if (ptr >= end)
700 return NULL;
701
702 /* RFC2616, par. 5.1.2 :
703 * Request-URI = "*" | absuri | abspath | authority
704 */
705
706 if (*ptr == '*')
707 return NULL;
708
709 if (isalpha((unsigned char)*ptr)) {
710 /* this is a scheme as described by RFC3986, par. 3.1 */
711 ptr++;
712 while (ptr < end &&
713 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
714 ptr++;
715 /* skip '://' */
716 if (ptr == end || *ptr++ != ':')
717 return NULL;
718 if (ptr == end || *ptr++ != '/')
719 return NULL;
720 if (ptr == end || *ptr++ != '/')
721 return NULL;
722 }
723 /* skip [user[:passwd]@]host[:[port]] */
724
725 while (ptr < end && *ptr != '/')
726 ptr++;
727
728 if (ptr == end)
729 return NULL;
730
731 /* OK, we got the '/' ! */
732 return ptr;
733}
734
Willy Tarreauefb453c2008-10-26 20:49:47 +0100735/* Returns a 302 for a redirectable request. This may only be called just after
736 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
737 * left unchanged and will follow normal proxy processing.
738 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740{
741 struct http_txn *txn;
742 struct chunk rdr;
743 char *path;
744 int len;
745
746 /* 1: create the response header */
747 rdr.len = strlen(HTTP_302);
748 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100749 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100750 memcpy(rdr.str, HTTP_302, rdr.len);
751
752 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200753 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754 return;
755
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100756 /* special prefix "/" means don't change URL */
757 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
758 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
759 rdr.len += s->srv->rdr_len;
760 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100761
762 /* 3: add the request URI */
763 txn = &s->txn;
764 path = http_get_path(txn);
765 if (!path)
766 return;
767
Willy Tarreau962c3f42010-01-10 00:15:35 +0100768 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200769 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 return;
771
772 memcpy(rdr.str + rdr.len, path, len);
773 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100774
775 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
776 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
777 rdr.len += 29;
778 } else {
779 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
780 rdr.len += 23;
781 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100782
783 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100784 si->shutr(si);
785 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100786 si->err_type = SI_ET_NONE;
787 si->err_loc = NULL;
788 si->state = SI_ST_CLO;
789
790 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100791 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792
793 /* FIXME: we should increase a counter of redirects per server and per backend. */
794 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100795 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796}
797
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100798/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799 * that the server side is closed. Note that err_type is actually a
800 * bitmask, where almost only aborts may be cumulated with other
801 * values. We consider that aborted operations are more important
802 * than timeouts or errors due to the fact that nobody else in the
803 * logs might explain incomplete retries. All others should avoid
804 * being cumulated. It should normally not be possible to have multiple
805 * aborts at once, but just in case, the first one in sequence is reported.
806 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100807void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100809 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100810
811 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100812 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
813 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100814 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
831 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832}
833
Willy Tarreau42250582007-04-01 01:30:43 +0200834extern const char sess_term_cond[8];
835extern const char sess_fin_state[8];
836extern const char *monthname[12];
837const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
838const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
839 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
840 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200841struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200842struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100843
Emeric Brun3a058f32009-06-30 18:26:00 +0200844void http_sess_clflog(struct session *s)
845{
846 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
847 struct proxy *fe = s->fe;
848 struct proxy *be = s->be;
849 struct proxy *prx_log;
850 struct http_txn *txn = &s->txn;
851 int tolog, level, err;
852 char *uri, *h;
853 char *svid;
854 struct tm tm;
855 static char tmpline[MAX_SYSLOG_LEN];
856 int hdr;
857 size_t w;
858 int t_request;
859
860 prx_log = fe;
861 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
862 (s->conn_retries != be->conn_retries) ||
863 txn->status >= 500;
864
865 if (s->cli_addr.ss_family == AF_INET)
866 inet_ntop(AF_INET,
867 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
868 pn, sizeof(pn));
869 else
870 inet_ntop(AF_INET6,
871 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
872 pn, sizeof(pn));
873
874 get_gmtime(s->logs.accept_date.tv_sec, &tm);
875
876 /* FIXME: let's limit ourselves to frontend logging for now. */
877 tolog = fe->to_log;
878
879 h = tmpline;
880
881 w = snprintf(h, sizeof(tmpline),
882 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
883 pn,
884 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
885 tm.tm_hour, tm.tm_min, tm.tm_sec);
886 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
887 goto trunc;
888 h += w;
889
890 if (h >= tmpline + sizeof(tmpline) - 4)
891 goto trunc;
892
893 *(h++) = ' ';
894 *(h++) = '\"';
895 uri = txn->uri ? txn->uri : "<BADREQ>";
896 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
897 '#', url_encode_map, uri);
898 *(h++) = '\"';
899
900 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
901 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
902 goto trunc;
903 h += w;
904
905 if (h >= tmpline + sizeof(tmpline) - 9)
906 goto trunc;
907 memcpy(h, " \"-\" \"-\"", 8);
908 h += 8;
909
910 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
911 " %d %03d",
912 (s->cli_addr.ss_family == AF_INET) ?
913 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
914 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
915 (int)s->logs.accept_date.tv_usec/1000);
916 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
917 goto trunc;
918 h += w;
919
920 w = strlen(fe->id);
921 if (h >= tmpline + sizeof(tmpline) - 4 - w)
922 goto trunc;
923 *(h++) = ' ';
924 *(h++) = '\"';
925 memcpy(h, fe->id, w);
926 h += w;
927 *(h++) = '\"';
928
929 w = strlen(be->id);
930 if (h >= tmpline + sizeof(tmpline) - 4 - w)
931 goto trunc;
932 *(h++) = ' ';
933 *(h++) = '\"';
934 memcpy(h, be->id, w);
935 h += w;
936 *(h++) = '\"';
937
938 svid = (tolog & LW_SVID) ?
939 (s->data_source != DATA_SRC_STATS) ?
940 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
941
942 w = strlen(svid);
943 if (h >= tmpline + sizeof(tmpline) - 4 - w)
944 goto trunc;
945 *(h++) = ' ';
946 *(h++) = '\"';
947 memcpy(h, svid, w);
948 h += w;
949 *(h++) = '\"';
950
951 t_request = -1;
952 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
953 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
954 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
955 " %d %ld %ld %ld %ld",
956 t_request,
957 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
958 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
959 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
960 s->logs.t_close);
961 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
962 goto trunc;
963 h += w;
964
965 if (h >= tmpline + sizeof(tmpline) - 8)
966 goto trunc;
967 *(h++) = ' ';
968 *(h++) = '\"';
969 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
970 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
971 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
972 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
973 *(h++) = '\"';
974
975 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
976 " %d %d %d %d %d %ld %ld",
977 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
978 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
979 s->logs.srv_queue_size, s->logs.prx_queue_size);
980
981 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
982 goto trunc;
983 h += w;
984
985 if (txn->cli_cookie) {
986 w = strlen(txn->cli_cookie);
987 if (h >= tmpline + sizeof(tmpline) - 4 - w)
988 goto trunc;
989 *(h++) = ' ';
990 *(h++) = '\"';
991 memcpy(h, txn->cli_cookie, w);
992 h += w;
993 *(h++) = '\"';
994 } else {
995 if (h >= tmpline + sizeof(tmpline) - 5)
996 goto trunc;
997 memcpy(h, " \"-\"", 4);
998 h += 4;
999 }
1000
1001 if (txn->srv_cookie) {
1002 w = strlen(txn->srv_cookie);
1003 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1004 goto trunc;
1005 *(h++) = ' ';
1006 *(h++) = '\"';
1007 memcpy(h, txn->srv_cookie, w);
1008 h += w;
1009 *(h++) = '\"';
1010 } else {
1011 if (h >= tmpline + sizeof(tmpline) - 5)
1012 goto trunc;
1013 memcpy(h, " \"-\"", 4);
1014 h += 4;
1015 }
1016
1017 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1018 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1019 if (h >= sizeof (tmpline) + tmpline - 4)
1020 goto trunc;
1021 *(h++) = ' ';
1022 *(h++) = '\"';
1023 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1024 '#', hdr_encode_map, txn->req.cap[hdr]);
1025 *(h++) = '\"';
1026 }
1027 }
1028
1029 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1030 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1031 if (h >= sizeof (tmpline) + tmpline - 4)
1032 goto trunc;
1033 *(h++) = ' ';
1034 *(h++) = '\"';
1035 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1036 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1037 *(h++) = '\"';
1038 }
1039 }
1040
1041trunc:
1042 *h = '\0';
1043
1044 level = LOG_INFO;
1045 if (err && (fe->options2 & PR_O2_LOGERRORS))
1046 level = LOG_ERR;
1047
1048 send_log(prx_log, level, "%s\n", tmpline);
1049
1050 s->logs.logwait = 0;
1051}
1052
Willy Tarreau42250582007-04-01 01:30:43 +02001053/*
1054 * send a log for the session when we have enough info about it.
1055 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001056 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001057void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001058{
1059 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1060 struct proxy *fe = s->fe;
1061 struct proxy *be = s->be;
1062 struct proxy *prx_log;
1063 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001064 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001065 char *uri, *h;
1066 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001067 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001068 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001069 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001070 int hdr;
1071
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001072 /* if we don't want to log normal traffic, return now */
1073 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1074 (s->conn_retries != be->conn_retries) ||
1075 txn->status >= 500;
1076 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1077 return;
1078
Willy Tarreau42250582007-04-01 01:30:43 +02001079 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1080 return;
1081 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082
Emeric Brun3a058f32009-06-30 18:26:00 +02001083 if (prx_log->options2 & PR_O2_CLFLOG)
1084 return http_sess_clflog(s);
1085
Willy Tarreau42250582007-04-01 01:30:43 +02001086 if (s->cli_addr.ss_family == AF_INET)
1087 inet_ntop(AF_INET,
1088 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1089 pn, sizeof(pn));
1090 else
1091 inet_ntop(AF_INET6,
1092 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1093 pn, sizeof(pn));
1094
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001095 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001096
1097 /* FIXME: let's limit ourselves to frontend logging for now. */
1098 tolog = fe->to_log;
1099
1100 h = tmpline;
1101 if (fe->to_log & LW_REQHDR &&
1102 txn->req.cap &&
1103 (h < tmpline + sizeof(tmpline) - 10)) {
1104 *(h++) = ' ';
1105 *(h++) = '{';
1106 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1107 if (hdr)
1108 *(h++) = '|';
1109 if (txn->req.cap[hdr] != NULL)
1110 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1111 '#', hdr_encode_map, txn->req.cap[hdr]);
1112 }
1113 *(h++) = '}';
1114 }
1115
1116 if (fe->to_log & LW_RSPHDR &&
1117 txn->rsp.cap &&
1118 (h < tmpline + sizeof(tmpline) - 7)) {
1119 *(h++) = ' ';
1120 *(h++) = '{';
1121 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1122 if (hdr)
1123 *(h++) = '|';
1124 if (txn->rsp.cap[hdr] != NULL)
1125 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1126 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1127 }
1128 *(h++) = '}';
1129 }
1130
1131 if (h < tmpline + sizeof(tmpline) - 4) {
1132 *(h++) = ' ';
1133 *(h++) = '"';
1134 uri = txn->uri ? txn->uri : "<BADREQ>";
1135 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1136 '#', url_encode_map, uri);
1137 *(h++) = '"';
1138 }
1139 *h = '\0';
1140
1141 svid = (tolog & LW_SVID) ?
1142 (s->data_source != DATA_SRC_STATS) ?
1143 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1144
Willy Tarreau70089872008-06-13 21:12:51 +02001145 t_request = -1;
1146 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1147 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1148
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001149 level = LOG_INFO;
1150 if (err && (fe->options2 & PR_O2_LOGERRORS))
1151 level = LOG_ERR;
1152
1153 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001154 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001155 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1156 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001157 pn,
1158 (s->cli_addr.ss_family == AF_INET) ?
1159 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1160 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001161 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001162 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001163 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001164 t_request,
1165 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001166 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1167 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1168 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1169 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001170 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001171 txn->cli_cookie ? txn->cli_cookie : "-",
1172 txn->srv_cookie ? txn->srv_cookie : "-",
1173 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1174 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1175 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1176 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1177 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001178 (s->flags & SN_REDISP)?"+":"",
1179 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001180 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1181
1182 s->logs.logwait = 0;
1183}
1184
Willy Tarreau117f59e2007-03-04 18:17:17 +01001185
1186/*
1187 * Capture headers from message starting at <som> according to header list
1188 * <cap_hdr>, and fill the <idx> structure appropriately.
1189 */
1190void capture_headers(char *som, struct hdr_idx *idx,
1191 char **cap, struct cap_hdr *cap_hdr)
1192{
1193 char *eol, *sol, *col, *sov;
1194 int cur_idx;
1195 struct cap_hdr *h;
1196 int len;
1197
1198 sol = som + hdr_idx_first_pos(idx);
1199 cur_idx = hdr_idx_first_idx(idx);
1200
1201 while (cur_idx) {
1202 eol = sol + idx->v[cur_idx].len;
1203
1204 col = sol;
1205 while (col < eol && *col != ':')
1206 col++;
1207
1208 sov = col + 1;
1209 while (sov < eol && http_is_lws[(unsigned char)*sov])
1210 sov++;
1211
1212 for (h = cap_hdr; h; h = h->next) {
1213 if ((h->namelen == col - sol) &&
1214 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1215 if (cap[h->index] == NULL)
1216 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001217 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001218
1219 if (cap[h->index] == NULL) {
1220 Alert("HTTP capture : out of memory.\n");
1221 continue;
1222 }
1223
1224 len = eol - sov;
1225 if (len > h->len)
1226 len = h->len;
1227
1228 memcpy(cap[h->index], sov, len);
1229 cap[h->index][len]=0;
1230 }
1231 }
1232 sol = eol + idx->v[cur_idx].cr + 1;
1233 cur_idx = idx->v[cur_idx].next;
1234 }
1235}
1236
1237
Willy Tarreau42250582007-04-01 01:30:43 +02001238/* either we find an LF at <ptr> or we jump to <bad>.
1239 */
1240#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1241
1242/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1243 * otherwise to <http_msg_ood> with <state> set to <st>.
1244 */
1245#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1246 ptr++; \
1247 if (likely(ptr < end)) \
1248 goto good; \
1249 else { \
1250 state = (st); \
1251 goto http_msg_ood; \
1252 } \
1253 } while (0)
1254
1255
Willy Tarreaubaaee002006-06-26 02:48:02 +02001256/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001257 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1259 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1260 * will give undefined results.
1261 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1262 * and that msg->sol points to the beginning of the response.
1263 * If a complete line is found (which implies that at least one CR or LF is
1264 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1265 * returned indicating an incomplete line (which does not mean that parts have
1266 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1267 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1268 * upon next call.
1269 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001270 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1272 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001273 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001275const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1276 unsigned int state, const char *ptr, const char *end,
1277 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001278{
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 switch (state) {
1280 http_msg_rpver:
1281 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001282 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001283 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1284
1285 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1288 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001289 state = HTTP_MSG_ERROR;
1290 break;
1291
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 http_msg_rpver_sp:
1293 case HTTP_MSG_RPVER_SP:
1294 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001295 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 goto http_msg_rpcode;
1297 }
1298 if (likely(HTTP_IS_SPHT(*ptr)))
1299 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1300 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001301 state = HTTP_MSG_ERROR;
1302 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001303
1304 http_msg_rpcode:
1305 case HTTP_MSG_RPCODE:
1306 if (likely(!HTTP_IS_LWS(*ptr)))
1307 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1308
1309 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001310 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1312 }
1313
1314 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001315 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 http_msg_rsp_reason:
1317 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001318 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 msg->sl.st.r_l = 0;
1320 goto http_msg_rpline_eol;
1321
1322 http_msg_rpcode_sp:
1323 case HTTP_MSG_RPCODE_SP:
1324 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001325 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 goto http_msg_rpreason;
1327 }
1328 if (likely(HTTP_IS_SPHT(*ptr)))
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1330 /* so it's a CR/LF, so there is no reason phrase */
1331 goto http_msg_rsp_reason;
1332
1333 http_msg_rpreason:
1334 case HTTP_MSG_RPREASON:
1335 if (likely(!HTTP_IS_CRLF(*ptr)))
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001337 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 http_msg_rpline_eol:
1339 /* We have seen the end of line. Note that we do not
1340 * necessarily have the \n yet, but at least we know that we
1341 * have EITHER \r OR \n, otherwise the response would not be
1342 * complete. We can then record the response length and return
1343 * to the caller which will be able to register it.
1344 */
1345 msg->sl.st.l = ptr - msg->sol;
1346 return ptr;
1347
1348#ifdef DEBUG_FULL
1349 default:
1350 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1351 exit(1);
1352#endif
1353 }
1354
1355 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001356 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 if (ret_state)
1358 *ret_state = state;
1359 if (ret_ptr)
1360 *ret_ptr = (char *)ptr;
1361 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001362}
1363
Willy Tarreau8973c702007-01-21 23:58:29 +01001364/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 * This function parses a request line between <ptr> and <end>, starting with
1366 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1367 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1368 * will give undefined results.
1369 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1370 * and that msg->sol points to the beginning of the request.
1371 * If a complete line is found (which implies that at least one CR or LF is
1372 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1373 * returned indicating an incomplete line (which does not mean that parts have
1374 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1375 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1376 * upon next call.
1377 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001378 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1380 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001381 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001382 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001383const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1384 unsigned int state, const char *ptr, const char *end,
1385 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001386{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 switch (state) {
1388 http_msg_rqmeth:
1389 case HTTP_MSG_RQMETH:
1390 if (likely(HTTP_IS_TOKEN(*ptr)))
1391 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001394 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1396 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 if (likely(HTTP_IS_CRLF(*ptr))) {
1399 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001400 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001402 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001404 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001406 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 msg->sl.rq.v_l = 0;
1408 goto http_msg_rqline_eol;
1409 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001410 state = HTTP_MSG_ERROR;
1411 break;
1412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 http_msg_rqmeth_sp:
1414 case HTTP_MSG_RQMETH_SP:
1415 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001416 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 goto http_msg_rquri;
1418 }
1419 if (likely(HTTP_IS_SPHT(*ptr)))
1420 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1421 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1422 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_rquri:
1425 case HTTP_MSG_RQURI:
1426 if (likely(!HTTP_IS_LWS(*ptr)))
1427 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001430 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1435 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 http_msg_rquri_sp:
1438 case HTTP_MSG_RQURI_SP:
1439 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001440 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 goto http_msg_rqver;
1442 }
1443 if (likely(HTTP_IS_SPHT(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1445 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1446 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 http_msg_rqver:
1449 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001450 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001452
1453 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001454 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001455 http_msg_rqline_eol:
1456 /* We have seen the end of line. Note that we do not
1457 * necessarily have the \n yet, but at least we know that we
1458 * have EITHER \r OR \n, otherwise the request would not be
1459 * complete. We can then record the request length and return
1460 * to the caller which will be able to register it.
1461 */
1462 msg->sl.rq.l = ptr - msg->sol;
1463 return ptr;
1464 }
1465
1466 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001467 state = HTTP_MSG_ERROR;
1468 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470#ifdef DEBUG_FULL
1471 default:
1472 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1473 exit(1);
1474#endif
1475 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001478 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 if (ret_state)
1480 *ret_state = state;
1481 if (ret_ptr)
1482 *ret_ptr = (char *)ptr;
1483 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001485
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001486/*
1487 * Returns the data from Authorization header. Function may be called more
1488 * than once so data is stored in txn->auth_data. When no header is found
1489 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1490 * searching again for something we are unable to find anyway.
1491 */
1492
1493char get_http_auth_buff[BUFSIZE];
1494
1495int
1496get_http_auth(struct session *s)
1497{
1498
1499 struct http_txn *txn = &s->txn;
1500 struct chunk auth_method;
1501 struct hdr_ctx ctx;
1502 char *h, *p;
1503 int len;
1504
1505#ifdef DEBUG_AUTH
1506 printf("Auth for session %p: %d\n", s, txn->auth.method);
1507#endif
1508
1509 if (txn->auth.method == HTTP_AUTH_WRONG)
1510 return 0;
1511
1512 if (txn->auth.method)
1513 return 1;
1514
1515 txn->auth.method = HTTP_AUTH_WRONG;
1516
1517 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001518
1519 if (txn->flags & TX_USE_PX_CONN) {
1520 h = "Proxy-Authorization";
1521 len = strlen(h);
1522 } else {
1523 h = "Authorization";
1524 len = strlen(h);
1525 }
1526
1527 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001528 return 0;
1529
1530 h = ctx.line + ctx.val;
1531
1532 p = memchr(h, ' ', ctx.vlen);
1533 if (!p || p == h)
1534 return 0;
1535
1536 chunk_initlen(&auth_method, h, 0, p-h);
1537 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1538
1539 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1540
1541 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1542 get_http_auth_buff, BUFSIZE - 1);
1543
1544 if (len < 0)
1545 return 0;
1546
1547
1548 get_http_auth_buff[len] = '\0';
1549
1550 p = strchr(get_http_auth_buff, ':');
1551
1552 if (!p)
1553 return 0;
1554
1555 txn->auth.user = get_http_auth_buff;
1556 *p = '\0';
1557 txn->auth.pass = p+1;
1558
1559 txn->auth.method = HTTP_AUTH_BASIC;
1560 return 1;
1561 }
1562
1563 return 0;
1564}
1565
Willy Tarreau58f10d72006-12-04 02:26:12 +01001566
Willy Tarreau8973c702007-01-21 23:58:29 +01001567/*
1568 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001569 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001570 * when data are missing and recalled at the exact same location with no
1571 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001572 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001573 * fields. Note that msg->som and msg->sol will be initialized after completing
1574 * the first state, so that none of the msg pointers has to be initialized
1575 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001576 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1578{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001579 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 ptr = buf->lr;
1584 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 if (unlikely(ptr >= end))
1587 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001590 /*
1591 * First, states that are specific to the response only.
1592 * We check them first so that request and headers are
1593 * closer to each other (accessed more often).
1594 */
1595 http_msg_rpbefore:
1596 case HTTP_MSG_RPBEFORE:
1597 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001598 /* we have a start of message, but we have to check
1599 * first if we need to remove some CRLF. We can only
1600 * do this when send_max=0.
1601 */
1602 char *beg = buf->w + buf->send_max;
1603 if (beg >= buf->data + buf->size)
1604 beg -= buf->size;
1605 if (unlikely(ptr != beg)) {
1606 if (buf->send_max)
1607 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001608 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001609 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001610 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001611 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001612 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 hdr_idx_init(idx);
1614 state = HTTP_MSG_RPVER;
1615 goto http_msg_rpver;
1616 }
1617
1618 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1619 goto http_msg_invalid;
1620
1621 if (unlikely(*ptr == '\n'))
1622 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1623 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1624 /* stop here */
1625
1626 http_msg_rpbefore_cr:
1627 case HTTP_MSG_RPBEFORE_CR:
1628 EXPECT_LF_HERE(ptr, http_msg_invalid);
1629 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1630 /* stop here */
1631
1632 http_msg_rpver:
1633 case HTTP_MSG_RPVER:
1634 case HTTP_MSG_RPVER_SP:
1635 case HTTP_MSG_RPCODE:
1636 case HTTP_MSG_RPCODE_SP:
1637 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001638 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001639 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001640 if (unlikely(!ptr))
1641 return;
1642
1643 /* we have a full response and we know that we have either a CR
1644 * or an LF at <ptr>.
1645 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001646 //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 +01001647 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1648
1649 msg->sol = ptr;
1650 if (likely(*ptr == '\r'))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1652 goto http_msg_rpline_end;
1653
1654 http_msg_rpline_end:
1655 case HTTP_MSG_RPLINE_END:
1656 /* msg->sol must point to the first of CR or LF. */
1657 EXPECT_LF_HERE(ptr, http_msg_invalid);
1658 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1659 /* stop here */
1660
1661 /*
1662 * Second, states that are specific to the request only
1663 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 http_msg_rqbefore:
1665 case HTTP_MSG_RQBEFORE:
1666 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001667 /* we have a start of message, but we have to check
1668 * first if we need to remove some CRLF. We can only
1669 * do this when send_max=0.
1670 */
1671 char *beg = buf->w + buf->send_max;
1672 if (beg >= buf->data + buf->size)
1673 beg -= buf->size;
1674 if (likely(ptr != beg)) {
1675 if (buf->send_max)
1676 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001677 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001678 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001680 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001681 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001682 /* we will need this when keep-alive will be supported
1683 hdr_idx_init(idx);
1684 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001685 state = HTTP_MSG_RQMETH;
1686 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1690 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 if (unlikely(*ptr == '\n'))
1693 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1694 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001695 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 http_msg_rqbefore_cr:
1698 case HTTP_MSG_RQBEFORE_CR:
1699 EXPECT_LF_HERE(ptr, http_msg_invalid);
1700 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001701 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 http_msg_rqmeth:
1704 case HTTP_MSG_RQMETH:
1705 case HTTP_MSG_RQMETH_SP:
1706 case HTTP_MSG_RQURI:
1707 case HTTP_MSG_RQURI_SP:
1708 case HTTP_MSG_RQVER:
1709 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001710 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 if (unlikely(!ptr))
1712 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 /* we have a full request and we know that we have either a CR
1715 * or an LF at <ptr>.
1716 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001717 //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 +01001718 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 msg->sol = ptr;
1721 if (likely(*ptr == '\r'))
1722 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 http_msg_rqline_end:
1726 case HTTP_MSG_RQLINE_END:
1727 /* check for HTTP/0.9 request : no version information available.
1728 * msg->sol must point to the first of CR or LF.
1729 */
1730 if (unlikely(msg->sl.rq.v_l == 0))
1731 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 EXPECT_LF_HERE(ptr, http_msg_invalid);
1734 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001735 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8973c702007-01-21 23:58:29 +01001737 /*
1738 * Common states below
1739 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 http_msg_hdr_first:
1741 case HTTP_MSG_HDR_FIRST:
1742 msg->sol = ptr;
1743 if (likely(!HTTP_IS_CRLF(*ptr))) {
1744 goto http_msg_hdr_name;
1745 }
1746
1747 if (likely(*ptr == '\r'))
1748 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1749 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 http_msg_hdr_name:
1752 case HTTP_MSG_HDR_NAME:
1753 /* assumes msg->sol points to the first char */
1754 if (likely(HTTP_IS_TOKEN(*ptr)))
1755 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001756
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 if (likely(*ptr == ':')) {
1758 msg->col = ptr - buf->data;
1759 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1760 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001761
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001762 if (likely(msg->err_pos < -1) || *ptr == '\n')
1763 goto http_msg_invalid;
1764
1765 if (msg->err_pos == -1) /* capture error pointer */
1766 msg->err_pos = ptr - buf->data; /* >= 0 now */
1767
1768 /* and we still accept this non-token character */
1769 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 http_msg_hdr_l1_sp:
1772 case HTTP_MSG_HDR_L1_SP:
1773 /* assumes msg->sol points to the first char and msg->col to the colon */
1774 if (likely(HTTP_IS_SPHT(*ptr)))
1775 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 /* header value can be basically anything except CR/LF */
1778 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001779
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001780 if (likely(!HTTP_IS_CRLF(*ptr))) {
1781 goto http_msg_hdr_val;
1782 }
1783
1784 if (likely(*ptr == '\r'))
1785 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1786 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 http_msg_hdr_l1_lf:
1789 case HTTP_MSG_HDR_L1_LF:
1790 EXPECT_LF_HERE(ptr, http_msg_invalid);
1791 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 http_msg_hdr_l1_lws:
1794 case HTTP_MSG_HDR_L1_LWS:
1795 if (likely(HTTP_IS_SPHT(*ptr))) {
1796 /* replace HT,CR,LF with spaces */
1797 for (; buf->data+msg->sov < ptr; msg->sov++)
1798 buf->data[msg->sov] = ' ';
1799 goto http_msg_hdr_l1_sp;
1800 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001801 /* we had a header consisting only in spaces ! */
1802 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 goto http_msg_complete_header;
1804
1805 http_msg_hdr_val:
1806 case HTTP_MSG_HDR_VAL:
1807 /* assumes msg->sol points to the first char, msg->col to the
1808 * colon, and msg->sov points to the first character of the
1809 * value.
1810 */
1811 if (likely(!HTTP_IS_CRLF(*ptr)))
1812 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 msg->eol = ptr;
1815 /* Note: we could also copy eol into ->eoh so that we have the
1816 * real header end in case it ends with lots of LWS, but is this
1817 * really needed ?
1818 */
1819 if (likely(*ptr == '\r'))
1820 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1821 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 http_msg_hdr_l2_lf:
1824 case HTTP_MSG_HDR_L2_LF:
1825 EXPECT_LF_HERE(ptr, http_msg_invalid);
1826 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 http_msg_hdr_l2_lws:
1829 case HTTP_MSG_HDR_L2_LWS:
1830 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1831 /* LWS: replace HT,CR,LF with spaces */
1832 for (; msg->eol < ptr; msg->eol++)
1833 *msg->eol = ' ';
1834 goto http_msg_hdr_val;
1835 }
1836 http_msg_complete_header:
1837 /*
1838 * It was a new header, so the last one is finished.
1839 * Assumes msg->sol points to the first char, msg->col to the
1840 * colon, msg->sov points to the first character of the value
1841 * and msg->eol to the first CR or LF so we know how the line
1842 * ends. We insert last header into the index.
1843 */
1844 /*
1845 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1846 write(2, msg->sol, msg->eol-msg->sol);
1847 fprintf(stderr,"\n");
1848 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001849
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001850 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1851 idx, idx->tail) < 0))
1852 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001853
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 msg->sol = ptr;
1855 if (likely(!HTTP_IS_CRLF(*ptr))) {
1856 goto http_msg_hdr_name;
1857 }
1858
1859 if (likely(*ptr == '\r'))
1860 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1861 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001862
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 http_msg_last_lf:
1864 case HTTP_MSG_LAST_LF:
1865 /* Assumes msg->sol points to the first of either CR or LF */
1866 EXPECT_LF_HERE(ptr, http_msg_invalid);
1867 ptr++;
1868 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001869 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001870 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001871 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001872 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 return;
1874#ifdef DEBUG_FULL
1875 default:
1876 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1877 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001878#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 }
1880 http_msg_ood:
1881 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001882 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 buf->lr = ptr;
1884 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 http_msg_invalid:
1887 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001888 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001889 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 return;
1891}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001892
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001893/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1894 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1895 * nothing is done and 1 is returned.
1896 */
1897static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1898{
1899 int delta;
1900 char *cur_end;
1901
1902 if (msg->sl.rq.v_l != 0)
1903 return 1;
1904
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001905 cur_end = msg->sol + msg->sl.rq.l;
1906 delta = 0;
1907
1908 if (msg->sl.rq.u_l == 0) {
1909 /* if no URI was set, add "/" */
1910 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1911 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001912 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001913 }
1914 /* add HTTP version */
1915 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001916 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001917 cur_end += delta;
1918 cur_end = (char *)http_parse_reqline(msg, req->data,
1919 HTTP_MSG_RQMETH,
1920 msg->sol, cur_end + 1,
1921 NULL, NULL);
1922 if (unlikely(!cur_end))
1923 return 0;
1924
1925 /* we have a full HTTP/1.0 request now and we know that
1926 * we have either a CR or an LF at <ptr>.
1927 */
1928 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1929 return 1;
1930}
1931
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001932/* Parse the Connection: header of an HTTP request, looking for both "close"
1933 * and "keep-alive" values. If a buffer is provided and we already know that
1934 * some headers may safely be removed, we remove them now. The <to_del> flags
1935 * are used for that :
1936 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1937 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1938 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1939 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1940 * harmless combinations may be removed. Do not call that after changes have
1941 * been processed. If unused, the buffer can be NULL, and no data will be
1942 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001943 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001944void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001945{
Willy Tarreau5b154472009-12-21 20:11:07 +01001946 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001947 const char *hdr_val = "Connection";
1948 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001949
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001950 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001951 return;
1952
Willy Tarreau88d349d2010-01-25 12:15:43 +01001953 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1954 hdr_val = "Proxy-Connection";
1955 hdr_len = 16;
1956 }
1957
Willy Tarreau5b154472009-12-21 20:11:07 +01001958 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001959 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001960 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001961 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1962 txn->flags |= TX_HDR_CONN_KAL;
1963 if ((to_del & 2) && buf)
1964 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1965 else
1966 txn->flags |= TX_CON_KAL_SET;
1967 }
1968 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1969 txn->flags |= TX_HDR_CONN_CLO;
1970 if ((to_del & 1) && buf)
1971 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1972 else
1973 txn->flags |= TX_CON_CLO_SET;
1974 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001975 }
1976
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001977 txn->flags |= TX_HDR_CONN_PRS;
1978 return;
1979}
Willy Tarreau5b154472009-12-21 20:11:07 +01001980
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001981/* Apply desired changes on the Connection: header. Values may be removed and/or
1982 * added depending on the <wanted> flags, which are exclusively composed of
1983 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1984 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1985 */
1986void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1987{
1988 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001989 const char *hdr_val = "Connection";
1990 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001991
1992 ctx.idx = 0;
1993
Willy Tarreau88d349d2010-01-25 12:15:43 +01001994
1995 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1996 hdr_val = "Proxy-Connection";
1997 hdr_len = 16;
1998 }
1999
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002000 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002001 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002002 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2003 if (wanted & TX_CON_KAL_SET)
2004 txn->flags |= TX_CON_KAL_SET;
2005 else
2006 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002007 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002008 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2009 if (wanted & TX_CON_CLO_SET)
2010 txn->flags |= TX_CON_CLO_SET;
2011 else
2012 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002013 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002014 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002015
2016 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2017 return;
2018
2019 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2020 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002021 hdr_val = "Connection: close";
2022 hdr_len = 17;
2023 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2024 hdr_val = "Proxy-Connection: close";
2025 hdr_len = 23;
2026 }
2027 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028 }
2029
2030 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2031 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002032 hdr_val = "Connection: keep-alive";
2033 hdr_len = 22;
2034 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2035 hdr_val = "Proxy-Connection: keep-alive";
2036 hdr_len = 28;
2037 }
2038 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002039 }
2040 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002041}
2042
Willy Tarreaud98cf932009-12-27 22:54:55 +01002043/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2044 * first byte of body, and increments msg->sov by the number of bytes parsed,
2045 * so that we know we can forward between ->som and ->sov. Note that due to
2046 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2047 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002048 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002049 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002050 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002052{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053 char *ptr = buf->lr;
2054 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002055 unsigned int chunk = 0;
2056
2057 /* The chunk size is in the following form, though we are only
2058 * interested in the size and CRLF :
2059 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2060 */
2061 while (1) {
2062 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002063 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002064 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002065 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002066 if (c < 0) /* not a hex digit anymore */
2067 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068 if (++ptr >= end)
2069 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002070 if (chunk & 0xF000000) /* overflow will occur */
2071 return -1;
2072 chunk = (chunk << 4) + c;
2073 }
2074
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 /* empty size not allowed */
2076 if (ptr == buf->lr)
2077 return -1;
2078
2079 while (http_is_spht[(unsigned char)*ptr]) {
2080 if (++ptr >= end)
2081 ptr = buf->data;
2082 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002084 }
2085
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 /* Up to there, we know that at least one byte is present at *ptr. Check
2087 * for the end of chunk size.
2088 */
2089 while (1) {
2090 if (likely(HTTP_IS_CRLF(*ptr))) {
2091 /* we now have a CR or an LF at ptr */
2092 if (likely(*ptr == '\r')) {
2093 if (++ptr >= end)
2094 ptr = buf->data;
2095 if (ptr == buf->r)
2096 return 0;
2097 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002098
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099 if (*ptr != '\n')
2100 return -1;
2101 if (++ptr >= end)
2102 ptr = buf->data;
2103 /* done */
2104 break;
2105 }
2106 else if (*ptr == ';') {
2107 /* chunk extension, ends at next CRLF */
2108 if (++ptr >= end)
2109 ptr = buf->data;
2110 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002111 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112
2113 while (!HTTP_IS_CRLF(*ptr)) {
2114 if (++ptr >= end)
2115 ptr = buf->data;
2116 if (ptr == buf->r)
2117 return 0;
2118 }
2119 /* we have a CRLF now, loop above */
2120 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002121 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002123 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002124 }
2125
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126 /* OK we found our CRLF and now <ptr> points to the next byte,
2127 * which may or may not be present. We save that into ->lr and
2128 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002129 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 msg->sov += ptr - buf->lr;
2131 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002132 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002134 return 1;
2135}
2136
Willy Tarreaud98cf932009-12-27 22:54:55 +01002137/* This function skips trailers in the buffer <buf> associated with HTTP
2138 * message <msg>. The first visited position is buf->lr. If the end of
2139 * the trailers is found, it is automatically scheduled to be forwarded,
2140 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2141 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002142 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2143 * zero. If a parse error is encountered, the function returns < 0 and does not
2144 * change anything except maybe buf->lr and msg->sov. Note that the message
2145 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2146 * which implies that all non-trailers data have already been scheduled for
2147 * forwarding, and that the difference between msg->som and msg->sov exactly
2148 * matches the length of trailers already parsed and not forwarded. It is also
2149 * important to note that this function is designed to be able to parse wrapped
2150 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002151 */
2152int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2153{
2154 /* we have buf->lr which points to next line. Look for CRLF. */
2155 while (1) {
2156 char *p1 = NULL, *p2 = NULL;
2157 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002158 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159
2160 /* scan current line and stop at LF or CRLF */
2161 while (1) {
2162 if (ptr == buf->r)
2163 return 0;
2164
2165 if (*ptr == '\n') {
2166 if (!p1)
2167 p1 = ptr;
2168 p2 = ptr;
2169 break;
2170 }
2171
2172 if (*ptr == '\r') {
2173 if (p1)
2174 return -1;
2175 p1 = ptr;
2176 }
2177
2178 ptr++;
2179 if (ptr >= buf->data + buf->size)
2180 ptr = buf->data;
2181 }
2182
2183 /* after LF; point to beginning of next line */
2184 p2++;
2185 if (p2 >= buf->data + buf->size)
2186 p2 = buf->data;
2187
Willy Tarreau638cd022010-01-03 07:42:04 +01002188 bytes = p2 - buf->lr;
2189 if (bytes < 0)
2190 bytes += buf->size;
2191
2192 /* schedule this line for forwarding */
2193 msg->sov += bytes;
2194 if (msg->sov >= buf->size)
2195 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002196
Willy Tarreau638cd022010-01-03 07:42:04 +01002197 if (p1 == buf->lr) {
2198 /* LF/CRLF at beginning of line => end of trailers at p2.
2199 * Everything was scheduled for forwarding, there's nothing
2200 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002201 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002202 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002203 msg->msg_state = HTTP_MSG_DONE;
2204 return 1;
2205 }
2206 /* OK, next line then */
2207 buf->lr = p2;
2208 }
2209}
2210
2211/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2212 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2213 * ->som, buf->lr in order to include this part into the next forwarding phase.
2214 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2215 * not enough data are available, the function does not change anything and
2216 * returns zero. If a parse error is encountered, the function returns < 0 and
2217 * does not change anything. Note: this function is designed to parse wrapped
2218 * CRLF at the end of the buffer.
2219 */
2220int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2221{
2222 char *ptr;
2223 int bytes;
2224
2225 /* NB: we'll check data availabilty at the end. It's not a
2226 * problem because whatever we match first will be checked
2227 * against the correct length.
2228 */
2229 bytes = 1;
2230 ptr = buf->lr;
2231 if (*ptr == '\r') {
2232 bytes++;
2233 ptr++;
2234 if (ptr >= buf->data + buf->size)
2235 ptr = buf->data;
2236 }
2237
2238 if (buf->l < bytes)
2239 return 0;
2240
2241 if (*ptr != '\n')
2242 return -1;
2243
2244 ptr++;
2245 if (ptr >= buf->data + buf->size)
2246 ptr = buf->data;
2247 buf->lr = ptr;
2248 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2249 msg->sov = ptr - buf->data;
2250 msg->som = msg->sov - bytes;
2251 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2252 return 1;
2253}
Willy Tarreau5b154472009-12-21 20:11:07 +01002254
Willy Tarreau83e3af02009-12-28 17:39:57 +01002255void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2256{
2257 char *end = buf->data + buf->size;
2258 int off = buf->data + buf->size - buf->w;
2259
2260 /* two possible cases :
2261 * - the buffer is in one contiguous block, we move it in-place
2262 * - the buffer is in two blocks, we move it via the trash
2263 */
2264 if (buf->l) {
Willy Tarreaub97f1992010-02-25 23:54:31 +01002265 if (buf->r <= buf->w)
Willy Tarreau83e3af02009-12-28 17:39:57 +01002266 /* non-contiguous block */
Willy Tarreaub97f1992010-02-25 23:54:31 +01002267 buffer_bounce_realign(buf);
2268 else
2269 memmove(buf->data, buf->w, buf->l);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002270 }
2271
2272 /* adjust all known pointers */
2273 buf->w = buf->data;
2274 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2275 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2276 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2277 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2278
2279 /* adjust relative pointers */
2280 msg->som = 0;
2281 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2282 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2283 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2284
Willy Tarreau83e3af02009-12-28 17:39:57 +01002285 if (msg->err_pos >= 0) {
2286 msg->err_pos += off;
2287 if (msg->err_pos >= buf->size)
2288 msg->err_pos -= buf->size;
2289 }
2290
2291 buf->flags &= ~BF_FULL;
2292 if (buf->l >= buffer_max_len(buf))
2293 buf->flags |= BF_FULL;
2294}
2295
Willy Tarreaud787e662009-07-07 10:14:51 +02002296/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2297 * processing can continue on next analysers, or zero if it either needs more
2298 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2299 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2300 * when it has nothing left to do, and may remove any analyser when it wants to
2301 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002302 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002303int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002304{
Willy Tarreau59234e92008-11-30 23:51:27 +01002305 /*
2306 * We will parse the partial (or complete) lines.
2307 * We will check the request syntax, and also join multi-line
2308 * headers. An index of all the lines will be elaborated while
2309 * parsing.
2310 *
2311 * For the parsing, we use a 28 states FSM.
2312 *
2313 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002314 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002315 * req->data + msg->eoh = end of processed headers / start of current one
2316 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 * req->lr = first non-visited byte
2318 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002319 *
2320 * At end of parsing, we may perform a capture of the error (if any), and
2321 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2322 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2323 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002327 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002328 struct http_txn *txn = &s->txn;
2329 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002330 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002331
Willy Tarreau6bf17362009-02-24 10:48:35 +01002332 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2333 now_ms, __FUNCTION__,
2334 s,
2335 req,
2336 req->rex, req->wex,
2337 req->flags,
2338 req->l,
2339 req->analysers);
2340
Willy Tarreau52a0c602009-08-16 22:45:38 +02002341 /* we're speaking HTTP here, so let's speak HTTP to the client */
2342 s->srv_error = http_return_srv_error;
2343
Willy Tarreau83e3af02009-12-28 17:39:57 +01002344 /* There's a protected area at the end of the buffer for rewriting
2345 * purposes. We don't want to start to parse the request if the
2346 * protected area is affected, because we may have to move processed
2347 * data later, which is much more complicated.
2348 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002349 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002350 if ((txn->flags & TX_NOT_FIRST) &&
2351 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002352 req->r < req->lr ||
2353 req->r > req->data + req->size - global.tune.maxrewrite)) {
2354 if (req->send_max) {
2355 /* some data has still not left the buffer, wake us once that's done */
2356 buffer_dont_connect(req);
2357 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2358 return 0;
2359 }
2360 if (req->l <= req->size - global.tune.maxrewrite)
2361 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002362 }
2363
Willy Tarreau065e8332010-01-08 00:30:20 +01002364 /* Note that we have the same problem with the response ; we
2365 * may want to send a redirect, error or anything which requires
2366 * some spare space. So we'll ensure that we have at least
2367 * maxrewrite bytes available in the response buffer before
2368 * processing that one. This will only affect pipelined
2369 * keep-alive requests.
2370 */
2371 if ((txn->flags & TX_NOT_FIRST) &&
2372 unlikely((s->rep->flags & BF_FULL) ||
2373 s->rep->r < s->rep->lr ||
2374 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2375 if (s->rep->send_max) {
2376 /* don't let a connection request be initiated */
2377 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002378 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002379 return 0;
2380 }
2381 }
2382
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002383 if (likely(req->lr < req->r))
2384 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002385 }
2386
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 /* 1: we might have to print this header in debug mode */
2388 if (unlikely((global.mode & MODE_DEBUG) &&
2389 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002390 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002392
Willy Tarreau962c3f42010-01-10 00:15:35 +01002393 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 eol = sol + msg->sl.rq.l;
2395 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002396
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 sol += hdr_idx_first_pos(&txn->hdr_idx);
2398 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002399
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 while (cur_idx) {
2401 eol = sol + txn->hdr_idx.v[cur_idx].len;
2402 debug_hdr("clihdr", s, sol, eol);
2403 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2404 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002405 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 }
2407
Willy Tarreau58f10d72006-12-04 02:26:12 +01002408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 /*
2410 * Now we quickly check if we have found a full valid request.
2411 * If not so, we check the FD and buffer states before leaving.
2412 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002413 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002414 * requests are checked first. When waiting for a second request
2415 * on a keep-alive session, if we encounter and error, close, t/o,
2416 * we note the error in the session flags but don't set any state.
2417 * Since the error will be noted there, it will not be counted by
2418 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002419 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002420
Willy Tarreau655dce92009-11-08 13:10:58 +01002421 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002422 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002424 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2426 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002427
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 /* 1: Since we are in header mode, if there's no space
2429 * left for headers, we won't be able to free more
2430 * later, so the session will never terminate. We
2431 * must terminate it now.
2432 */
2433 if (unlikely(req->flags & BF_FULL)) {
2434 /* FIXME: check if URI is set and return Status
2435 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002436 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 goto return_bad_req;
2438 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002439
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 /* 2: have we encountered a read error ? */
2441 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002442 if (!(s->flags & SN_ERR_MASK))
2443 s->flags |= SN_ERR_CLICL;
2444
Willy Tarreaufcffa692010-01-10 14:21:19 +01002445 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002446 goto failed_keep_alive;
2447
Willy Tarreau59234e92008-11-30 23:51:27 +01002448 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002449 if (msg->err_pos >= 0)
2450 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002451 msg->msg_state = HTTP_MSG_ERROR;
2452 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002453
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002454 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002455 if (s->listener->counters)
2456 s->listener->counters->failed_req++;
2457
Willy Tarreau59234e92008-11-30 23:51:27 +01002458 if (!(s->flags & SN_FINST_MASK))
2459 s->flags |= SN_FINST_R;
2460 return 0;
2461 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002462
Willy Tarreau59234e92008-11-30 23:51:27 +01002463 /* 3: has the read timeout expired ? */
2464 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002465 if (!(s->flags & SN_ERR_MASK))
2466 s->flags |= SN_ERR_CLITO;
2467
Willy Tarreaufcffa692010-01-10 14:21:19 +01002468 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002469 goto failed_keep_alive;
2470
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002472 if (msg->err_pos >= 0)
2473 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002474 txn->status = 408;
2475 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2476 msg->msg_state = HTTP_MSG_ERROR;
2477 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002478
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002479 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480 if (s->listener->counters)
2481 s->listener->counters->failed_req++;
2482
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 if (!(s->flags & SN_FINST_MASK))
2484 s->flags |= SN_FINST_R;
2485 return 0;
2486 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 /* 4: have we encountered a close ? */
2489 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002490 if (!(s->flags & SN_ERR_MASK))
2491 s->flags |= SN_ERR_CLICL;
2492
Willy Tarreaufcffa692010-01-10 14:21:19 +01002493 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002494 goto failed_keep_alive;
2495
Willy Tarreau4076a152009-04-02 15:18:36 +02002496 if (msg->err_pos >= 0)
2497 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 txn->status = 400;
2499 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2500 msg->msg_state = HTTP_MSG_ERROR;
2501 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002503 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002504 if (s->listener->counters)
2505 s->listener->counters->failed_req++;
2506
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 if (!(s->flags & SN_FINST_MASK))
2508 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002509 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002510 }
2511
Willy Tarreau520d95e2009-09-19 21:04:57 +02002512 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002513 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002514 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002515
Willy Tarreaufcffa692010-01-10 14:21:19 +01002516 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2517 /* If the client starts to talk, let's fall back to
2518 * request timeout processing.
2519 */
2520 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002521 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002522 }
2523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002525 if (!tick_isset(req->analyse_exp)) {
2526 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2527 (txn->flags & TX_WAIT_NEXT_RQ) &&
2528 tick_isset(s->be->timeout.httpka))
2529 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2530 else
2531 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2532 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002533
Willy Tarreau59234e92008-11-30 23:51:27 +01002534 /* we're not ready yet */
2535 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002536
2537 failed_keep_alive:
2538 /* Here we process low-level errors for keep-alive requests. In
2539 * short, if the request is not the first one and it experiences
2540 * a timeout, read error or shutdown, we just silently close so
2541 * that the client can try again.
2542 */
2543 txn->status = 0;
2544 msg->msg_state = HTTP_MSG_RQBEFORE;
2545 req->analysers = 0;
2546 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002547 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002548 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002549 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002551
Willy Tarreaud787e662009-07-07 10:14:51 +02002552 /* OK now we have a complete HTTP request with indexed headers. Let's
2553 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002554 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2555 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2556 * points to the CRLF of the request line. req->lr points to the first
2557 * byte after the last LF. msg->col and msg->sov point to the first
2558 * byte of data. msg->eol cannot be trusted because it may have been
2559 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002560 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002561
Willy Tarreaub16a5742010-01-10 14:46:16 +01002562 if (txn->flags & TX_WAIT_NEXT_RQ) {
2563 /* kill the pending keep-alive timeout */
2564 txn->flags &= ~TX_WAIT_NEXT_RQ;
2565 req->analyse_exp = TICK_ETERNITY;
2566 }
2567
2568
Willy Tarreaud787e662009-07-07 10:14:51 +02002569 /* Maybe we found in invalid header name while we were configured not
2570 * to block on that, so we have to capture it now.
2571 */
2572 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002573 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2574
Willy Tarreau59234e92008-11-30 23:51:27 +01002575 /*
2576 * 1: identify the method
2577 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002578 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002579
2580 /* we can make use of server redirect on GET and HEAD */
2581 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2582 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002583
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 /*
2585 * 2: check if the URI matches the monitor_uri.
2586 * We have to do this for every request which gets in, because
2587 * the monitor-uri is defined by the frontend.
2588 */
2589 if (unlikely((s->fe->monitor_uri_len != 0) &&
2590 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002591 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002592 s->fe->monitor_uri,
2593 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002594 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002596 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002600
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002602 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2603 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002604
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 ret = acl_pass(ret);
2606 if (cond->pol == ACL_COND_UNLESS)
2607 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 if (ret) {
2610 /* we fail this request, let's return 503 service unavail */
2611 txn->status = 503;
2612 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2613 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002614 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002615 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 /* nothing to fail, let's reply normaly */
2618 txn->status = 200;
2619 stream_int_retnclose(req->prod, &http_200_chunk);
2620 goto return_prx_cond;
2621 }
2622
2623 /*
2624 * 3: Maybe we have to copy the original REQURI for the logs ?
2625 * Note: we cannot log anymore if the request has been
2626 * classified as invalid.
2627 */
2628 if (unlikely(s->logs.logwait & LW_REQ)) {
2629 /* we have a complete HTTP request that we must log */
2630 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2631 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 if (urilen >= REQURI_LEN)
2634 urilen = REQURI_LEN - 1;
2635 memcpy(txn->uri, &req->data[msg->som], urilen);
2636 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002637
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 if (!(s->logs.logwait &= ~LW_REQ))
2639 s->do_log(s);
2640 } else {
2641 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002642 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 }
Willy Tarreau06619262006-12-17 08:37:22 +01002644
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002646 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2647 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002648
Willy Tarreau5b154472009-12-21 20:11:07 +01002649 /* ... and check if the request is HTTP/1.1 or above */
2650 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002651 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2652 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2653 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002654 txn->flags |= TX_REQ_VER_11;
2655
2656 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002657 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002658
Willy Tarreau88d349d2010-01-25 12:15:43 +01002659 /* if the frontend has "option http-use-proxy-header", we'll check if
2660 * we have what looks like a proxied connection instead of a connection,
2661 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2662 * Note that this is *not* RFC-compliant, however browsers and proxies
2663 * happen to do that despite being non-standard :-(
2664 * We consider that a request not beginning with either '/' or '*' is
2665 * a proxied connection, which covers both "scheme://location" and
2666 * CONNECT ip:port.
2667 */
2668 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2669 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2670 txn->flags |= TX_USE_PX_CONN;
2671
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002672 /* transfer length unknown*/
2673 txn->flags &= ~TX_REQ_XFER_LEN;
2674
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 /* 5: we may need to capture headers */
2676 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002677 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002679
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002680 /* 6: determine the transfer-length.
2681 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2682 * the presence of a message-body in a REQUEST and its transfer length
2683 * must be determined that way (in order of precedence) :
2684 * 1. The presence of a message-body in a request is signaled by the
2685 * inclusion of a Content-Length or Transfer-Encoding header field
2686 * in the request's header fields. When a request message contains
2687 * both a message-body of non-zero length and a method that does
2688 * not define any semantics for that request message-body, then an
2689 * origin server SHOULD either ignore the message-body or respond
2690 * with an appropriate error message (e.g., 413). A proxy or
2691 * gateway, when presented the same request, SHOULD either forward
2692 * the request inbound with the message- body or ignore the
2693 * message-body when determining a response.
2694 *
2695 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2696 * and the "chunked" transfer-coding (Section 6.2) is used, the
2697 * transfer-length is defined by the use of this transfer-coding.
2698 * If a Transfer-Encoding header field is present and the "chunked"
2699 * transfer-coding is not present, the transfer-length is defined
2700 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002701 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002702 * 3. If a Content-Length header field is present, its decimal value in
2703 * OCTETs represents both the entity-length and the transfer-length.
2704 * If a message is received with both a Transfer-Encoding header
2705 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002706 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002707 * 4. By the server closing the connection. (Closing the connection
2708 * cannot be used to indicate the end of a request body, since that
2709 * would leave no possibility for the server to send back a response.)
2710 *
2711 * Whenever a transfer-coding is applied to a message-body, the set of
2712 * transfer-codings MUST include "chunked", unless the message indicates
2713 * it is terminated by closing the connection. When the "chunked"
2714 * transfer-coding is used, it MUST be the last transfer-coding applied
2715 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002716 */
2717
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002718 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002719 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002720 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002721 while ((txn->flags & TX_REQ_VER_11) &&
2722 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002723 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2724 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2725 else if (txn->flags & TX_REQ_TE_CHNK) {
2726 /* bad transfer-encoding (chunked followed by something else) */
2727 use_close_only = 1;
2728 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2729 break;
2730 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002731 }
2732
Willy Tarreau32b47f42009-10-18 20:55:02 +02002733 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002734 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002735 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2736 signed long long cl;
2737
2738 if (!ctx.vlen)
2739 goto return_bad_req;
2740
2741 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2742 goto return_bad_req; /* parse failure */
2743
2744 if (cl < 0)
2745 goto return_bad_req;
2746
2747 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2748 goto return_bad_req; /* already specified, was different */
2749
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002750 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751 msg->hdr_content_len = cl;
2752 }
2753
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002754 /* bodyless requests have a known length */
2755 if (!use_close_only)
2756 txn->flags |= TX_REQ_XFER_LEN;
2757
Willy Tarreaud787e662009-07-07 10:14:51 +02002758 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002759 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002760 req->analyse_exp = TICK_ETERNITY;
2761 return 1;
2762
2763 return_bad_req:
2764 /* We centralize bad requests processing here */
2765 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2766 /* we detected a parsing error. We want to archive this request
2767 * in the dedicated proxy area for later troubleshooting.
2768 */
2769 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2770 }
2771
2772 txn->req.msg_state = HTTP_MSG_ERROR;
2773 txn->status = 400;
2774 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002775
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002776 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002777 if (s->listener->counters)
2778 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002779
2780 return_prx_cond:
2781 if (!(s->flags & SN_ERR_MASK))
2782 s->flags |= SN_ERR_PRXCOND;
2783 if (!(s->flags & SN_FINST_MASK))
2784 s->flags |= SN_FINST_R;
2785
2786 req->analysers = 0;
2787 req->analyse_exp = TICK_ETERNITY;
2788 return 0;
2789}
2790
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791/* This stream analyser runs all HTTP request processing which is common to
2792 * frontends and backends, which means blocking ACLs, filters, connection-close,
2793 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002794 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002795 * either needs more data or wants to immediately abort the request (eg: deny,
2796 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002797 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002799{
Willy Tarreaud787e662009-07-07 10:14:51 +02002800 struct http_txn *txn = &s->txn;
2801 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002802 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002803 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002804 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002805 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002806 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002807
Willy Tarreau655dce92009-11-08 13:10:58 +01002808 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002809 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002810 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002811 return 0;
2812 }
2813
Willy Tarreau3a816292009-07-07 10:55:49 +02002814 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002815 req->analyse_exp = TICK_ETERNITY;
2816
2817 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2818 now_ms, __FUNCTION__,
2819 s,
2820 req,
2821 req->rex, req->wex,
2822 req->flags,
2823 req->l,
2824 req->analysers);
2825
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002826 /* first check whether we have some ACLs set to block this request */
2827 list_for_each_entry(cond, &px->block_cond, list) {
2828 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002829
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002830 ret = acl_pass(ret);
2831 if (cond->pol == ACL_COND_UNLESS)
2832 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002833
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002834 if (ret) {
2835 txn->status = 403;
2836 /* let's log the request time */
2837 s->logs.tv_request = now;
2838 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2839 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002841 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002842
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002843 do_stats = stats_check_uri(s, px);
2844
2845 list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002846 int ret = 1;
2847
2848 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
2849 continue;
2850
2851 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002852 if (req_acl->cond) {
2853 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
2854 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01002855
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002856 if (req_acl->cond->pol == ACL_COND_UNLESS)
2857 ret = !ret;
2858 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002859
2860 if (ret) {
2861 req_acl_final = req_acl;
2862 break;
2863 }
2864 }
2865
2866 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
2867 txn->status = 403;
2868 s->logs.tv_request = now;
2869 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2870 goto return_prx_cond;
2871 }
2872
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002873 /* try headers filters */
2874 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002875 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002876 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002877
Willy Tarreau59234e92008-11-30 23:51:27 +01002878 /* has the request been denied ? */
2879 if (txn->flags & TX_CLDENY) {
2880 /* no need to go further */
2881 txn->status = 403;
2882 /* let's log the request time */
2883 s->logs.tv_request = now;
2884 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2885 goto return_prx_cond;
2886 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002887
2888 /* When a connection is tarpitted, we use the tarpit timeout,
2889 * which may be the same as the connect timeout if unspecified.
2890 * If unset, then set it to zero because we really want it to
2891 * eventually expire. We build the tarpit as an analyser.
2892 */
2893 if (txn->flags & TX_CLTARPIT) {
2894 buffer_erase(s->req);
2895 /* wipe the request out so that we can drop the connection early
2896 * if the client closes first.
2897 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002898 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002899 req->analysers = 0; /* remove switching rules etc... */
2900 req->analysers |= AN_REQ_HTTP_TARPIT;
2901 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2902 if (!req->analyse_exp)
2903 req->analyse_exp = tick_add(now_ms, 0);
2904 return 1;
2905 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002906 }
Willy Tarreau06619262006-12-17 08:37:22 +01002907
Willy Tarreau5b154472009-12-21 20:11:07 +01002908 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2909 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002910 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2911 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002912 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2913 * avoid to redo the same work if FE and BE have the same settings (common).
2914 * The method consists in checking if options changed between the two calls
2915 * (implying that either one is non-null, or one of them is non-null and we
2916 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002917 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002918
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002919 del_cl = del_ka = 0;
2920
Willy Tarreaudc008c52010-02-01 16:20:08 +01002921 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2922 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2923 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2924 (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 +01002925 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002926
Willy Tarreau5b154472009-12-21 20:11:07 +01002927 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2928 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002929 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2930 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002931 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002932 tmp = TX_CON_WANT_CLO;
2933
Willy Tarreau5b154472009-12-21 20:11:07 +01002934 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2935 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002936
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002937 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2938 /* parse the Connection header and possibly clean it */
2939 int to_del = 0;
2940 if ((txn->flags & TX_REQ_VER_11) ||
2941 (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)
2942 to_del |= 2; /* remove "keep-alive" */
2943 if (!(txn->flags & TX_REQ_VER_11))
2944 to_del |= 1; /* remove "close" */
2945 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002946 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002947
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002948 /* check if client or config asks for explicit close in KAL/SCL */
2949 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2950 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2951 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2952 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2953 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002954 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2955 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002956 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2957 }
Willy Tarreau78599912009-10-17 20:12:21 +02002958
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002959 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002960 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01002961 if (wl->cond) {
2962 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2963 ret = acl_pass(ret);
2964 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2965 ret = !ret;
2966 if (!ret)
2967 continue;
2968 }
2969
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002970 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002971 goto return_bad_req;
2972 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002973
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002974 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
2975 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002976 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002977
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002978 if (!realm)
2979 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2980
Willy Tarreau844a7e72010-01-31 21:46:18 +01002981 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002982 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2983 txn->status = 401;
2984 stream_int_retnclose(req->prod, &msg);
2985 goto return_prx_cond;
2986 }
2987
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002988 if (do_stats) {
2989 /* We need to provied stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002990 * FIXME!!! that one is rather dangerous, we want to
2991 * make it follow standard rules (eg: clear req->analysers).
2992 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002993
2994 s->logs.tv_request = now;
2995 s->data_source = DATA_SRC_STATS;
2996 s->data_state = DATA_ST_INIT;
2997 s->task->nice = -32; /* small boost for HTTP statistics */
2998 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
2999 s->rep->prod->private = s;
3000 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3001 req->analysers = 0;
3002
3003 return 0;
3004
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003006
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007 /* check whether we have some ACLs set to redirect this request */
3008 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003009 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003010
Willy Tarreauf285f542010-01-03 20:03:03 +01003011 if (rule->cond) {
3012 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3013 ret = acl_pass(ret);
3014 if (rule->cond->pol == ACL_COND_UNLESS)
3015 ret = !ret;
3016 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003017
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003018 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003019 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003021
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003022 /* build redirect message */
3023 switch(rule->code) {
3024 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003025 msg_fmt = HTTP_303;
3026 break;
3027 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003028 msg_fmt = HTTP_301;
3029 break;
3030 case 302:
3031 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003032 msg_fmt = HTTP_302;
3033 break;
3034 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003035
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003036 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003038
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 switch(rule->type) {
3040 case REDIRECT_TYPE_PREFIX: {
3041 const char *path;
3042 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003043
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003044 path = http_get_path(txn);
3045 /* build message using path */
3046 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003047 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3049 int qs = 0;
3050 while (qs < pathlen) {
3051 if (path[qs] == '?') {
3052 pathlen = qs;
3053 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003054 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003056 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003057 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003058 } else {
3059 path = "/";
3060 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003061 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003062
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003063 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003064 goto return_bad_req;
3065
3066 /* add prefix. Note that if prefix == "/", we don't want to
3067 * add anything, otherwise it makes it hard for the user to
3068 * configure a self-redirection.
3069 */
3070 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3072 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073 }
3074
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003075 /* add path */
3076 memcpy(rdr.str + rdr.len, path, pathlen);
3077 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003078
3079 /* append a slash at the end of the location is needed and missing */
3080 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3081 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3082 if (rdr.len > rdr.size - 5)
3083 goto return_bad_req;
3084 rdr.str[rdr.len] = '/';
3085 rdr.len++;
3086 }
3087
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003088 break;
3089 }
3090 case REDIRECT_TYPE_LOCATION:
3091 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003092 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003094
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003095 /* add location */
3096 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3097 rdr.len += rule->rdr_len;
3098 break;
3099 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003100
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003101 if (rule->cookie_len) {
3102 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3103 rdr.len += 14;
3104 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3105 rdr.len += rule->cookie_len;
3106 memcpy(rdr.str + rdr.len, "\r\n", 2);
3107 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003108 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003109
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003110 /* add end of headers and the keep-alive/close status.
3111 * We may choose to set keep-alive if the Location begins
3112 * with a slash, because the client will come back to the
3113 * same server.
3114 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003115 txn->status = rule->code;
3116 /* let's log the request time */
3117 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003118
3119 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3120 (txn->flags & TX_REQ_XFER_LEN) &&
3121 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3122 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3123 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3124 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003125 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003126 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3127 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3128 rdr.len += 30;
3129 } else {
3130 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3131 rdr.len += 24;
3132 }
Willy Tarreau75661452010-01-10 10:35:01 +01003133 }
3134 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3135 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003136 buffer_write(req->prod->ob, rdr.str, rdr.len);
3137 /* "eat" the request */
3138 buffer_ignore(req, msg->sov - msg->som);
3139 msg->som = msg->sov;
3140 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003141 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3142 txn->req.msg_state = HTTP_MSG_CLOSED;
3143 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003144 break;
3145 } else {
3146 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003147 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3148 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3149 rdr.len += 29;
3150 } else {
3151 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3152 rdr.len += 23;
3153 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003154 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003155 goto return_prx_cond;
3156 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003157 }
3158 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003159
Willy Tarreau2be39392010-01-03 17:24:51 +01003160 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3161 * If this happens, then the data will not come immediately, so we must
3162 * send all what we have without waiting. Note that due to the small gain
3163 * in waiting for the body of the request, it's easier to simply put the
3164 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3165 * itself once used.
3166 */
3167 req->flags |= BF_SEND_DONTWAIT;
3168
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003169 /* that's OK for us now, let's move on to next analysers */
3170 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003171
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003172 return_bad_req:
3173 /* We centralize bad requests processing here */
3174 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3175 /* we detected a parsing error. We want to archive this request
3176 * in the dedicated proxy area for later troubleshooting.
3177 */
3178 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3179 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003180
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003181 txn->req.msg_state = HTTP_MSG_ERROR;
3182 txn->status = 400;
3183 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003184
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003185 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003186 if (s->listener->counters)
3187 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003188
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003189 return_prx_cond:
3190 if (!(s->flags & SN_ERR_MASK))
3191 s->flags |= SN_ERR_PRXCOND;
3192 if (!(s->flags & SN_FINST_MASK))
3193 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003194
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003195 req->analysers = 0;
3196 req->analyse_exp = TICK_ETERNITY;
3197 return 0;
3198}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003200/* This function performs all the processing enabled for the current request.
3201 * It returns 1 if the processing can continue on next analysers, or zero if it
3202 * needs more data, encounters an error, or wants to immediately abort the
3203 * request. It relies on buffers flags, and updates s->req->analysers.
3204 */
3205int http_process_request(struct session *s, struct buffer *req, int an_bit)
3206{
3207 struct http_txn *txn = &s->txn;
3208 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003209
Willy Tarreau655dce92009-11-08 13:10:58 +01003210 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003211 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003212 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003213 return 0;
3214 }
3215
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003216 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3217 now_ms, __FUNCTION__,
3218 s,
3219 req,
3220 req->rex, req->wex,
3221 req->flags,
3222 req->l,
3223 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003224
Willy Tarreau59234e92008-11-30 23:51:27 +01003225 /*
3226 * Right now, we know that we have processed the entire headers
3227 * and that unwanted requests have been filtered out. We can do
3228 * whatever we want with the remaining request. Also, now we
3229 * may have separate values for ->fe, ->be.
3230 */
Willy Tarreau06619262006-12-17 08:37:22 +01003231
Willy Tarreau59234e92008-11-30 23:51:27 +01003232 /*
3233 * If HTTP PROXY is set we simply get remote server address
3234 * parsing incoming request.
3235 */
3236 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003237 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003238 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003239
Willy Tarreau59234e92008-11-30 23:51:27 +01003240 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003241 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003242 * Note that doing so might move headers in the request, but
3243 * the fields will stay coherent and the URI will not move.
3244 * This should only be performed in the backend.
3245 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003246 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3248 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003249
Willy Tarreau59234e92008-11-30 23:51:27 +01003250 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003251 * 8: the appsession cookie was looked up very early in 1.2,
3252 * so let's do the same now.
3253 */
3254
3255 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003256 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003257 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003258 }
3259
3260 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 * 9: add X-Forwarded-For if either the frontend or the backend
3262 * asks for it.
3263 */
3264 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3265 if (s->cli_addr.ss_family == AF_INET) {
3266 /* Add an X-Forwarded-For header unless the source IP is
3267 * in the 'except' network range.
3268 */
3269 if ((!s->fe->except_mask.s_addr ||
3270 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3271 != s->fe->except_net.s_addr) &&
3272 (!s->be->except_mask.s_addr ||
3273 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3274 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003275 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 unsigned char *pn;
3277 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003278
3279 /* Note: we rely on the backend to get the header name to be used for
3280 * x-forwarded-for, because the header is really meant for the backends.
3281 * However, if the backend did not specify any option, we have to rely
3282 * on the frontend's header name.
3283 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003284 if (s->be->fwdfor_hdr_len) {
3285 len = s->be->fwdfor_hdr_len;
3286 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003287 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 len = s->fe->fwdfor_hdr_len;
3289 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003290 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003291 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003292
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003293 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003294 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003295 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003296 }
3297 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003298 else if (s->cli_addr.ss_family == AF_INET6) {
3299 /* FIXME: for the sake of completeness, we should also support
3300 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003301 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003302 int len;
3303 char pn[INET6_ADDRSTRLEN];
3304 inet_ntop(AF_INET6,
3305 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3306 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003307
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 /* Note: we rely on the backend to get the header name to be used for
3309 * x-forwarded-for, because the header is really meant for the backends.
3310 * However, if the backend did not specify any option, we have to rely
3311 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003312 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003313 if (s->be->fwdfor_hdr_len) {
3314 len = s->be->fwdfor_hdr_len;
3315 memcpy(trash, s->be->fwdfor_hdr_name, len);
3316 } else {
3317 len = s->fe->fwdfor_hdr_len;
3318 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003319 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003321
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003323 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003324 goto return_bad_req;
3325 }
3326 }
3327
3328 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003329 * 10: add X-Original-To if either the frontend or the backend
3330 * asks for it.
3331 */
3332 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3333
3334 /* FIXME: don't know if IPv6 can handle that case too. */
3335 if (s->cli_addr.ss_family == AF_INET) {
3336 /* Add an X-Original-To header unless the destination IP is
3337 * in the 'except' network range.
3338 */
3339 if (!(s->flags & SN_FRT_ADDR_SET))
3340 get_frt_addr(s);
3341
3342 if ((!s->fe->except_mask_to.s_addr ||
3343 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3344 != s->fe->except_to.s_addr) &&
3345 (!s->be->except_mask_to.s_addr ||
3346 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3347 != s->be->except_to.s_addr)) {
3348 int len;
3349 unsigned char *pn;
3350 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3351
3352 /* Note: we rely on the backend to get the header name to be used for
3353 * x-original-to, because the header is really meant for the backends.
3354 * However, if the backend did not specify any option, we have to rely
3355 * on the frontend's header name.
3356 */
3357 if (s->be->orgto_hdr_len) {
3358 len = s->be->orgto_hdr_len;
3359 memcpy(trash, s->be->orgto_hdr_name, len);
3360 } else {
3361 len = s->fe->orgto_hdr_len;
3362 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003363 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003364 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3365
3366 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003367 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003368 goto return_bad_req;
3369 }
3370 }
3371 }
3372
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003373 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3374 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3375 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3376 unsigned int want_flags = 0;
3377
3378 if (txn->flags & TX_REQ_VER_11) {
3379 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3380 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3381 want_flags |= TX_CON_CLO_SET;
3382 } else {
3383 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
3384 want_flags |= TX_CON_KAL_SET;
3385 }
3386
3387 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3388 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003389 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003390
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003391
Willy Tarreau522d6c02009-12-06 18:49:18 +01003392 /* If we have no server assigned yet and we're balancing on url_param
3393 * with a POST request, we may be interested in checking the body for
3394 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003395 */
3396 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3397 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003398 s->be->url_param_post_limit != 0 &&
3399 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003400 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003401 buffer_dont_connect(req);
3402 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003403 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003404
Willy Tarreaud98cf932009-12-27 22:54:55 +01003405 if (txn->flags & TX_REQ_XFER_LEN)
3406 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003407
Willy Tarreau59234e92008-11-30 23:51:27 +01003408 /*************************************************************
3409 * OK, that's finished for the headers. We have done what we *
3410 * could. Let's switch to the DATA state. *
3411 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003412 req->analyse_exp = TICK_ETERNITY;
3413 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003414
Willy Tarreau59234e92008-11-30 23:51:27 +01003415 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003416 /* OK let's go on with the BODY now */
3417 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003418
Willy Tarreau59234e92008-11-30 23:51:27 +01003419 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003420 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003421 /* we detected a parsing error. We want to archive this request
3422 * in the dedicated proxy area for later troubleshooting.
3423 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003424 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003425 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003426
Willy Tarreau59234e92008-11-30 23:51:27 +01003427 txn->req.msg_state = HTTP_MSG_ERROR;
3428 txn->status = 400;
3429 req->analysers = 0;
3430 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003431
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003432 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003433 if (s->listener->counters)
3434 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003435
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 if (!(s->flags & SN_ERR_MASK))
3437 s->flags |= SN_ERR_PRXCOND;
3438 if (!(s->flags & SN_FINST_MASK))
3439 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003440 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003441}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003442
Willy Tarreau60b85b02008-11-30 23:28:40 +01003443/* This function is an analyser which processes the HTTP tarpit. It always
3444 * returns zero, at the beginning because it prevents any other processing
3445 * from occurring, and at the end because it terminates the request.
3446 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003447int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003448{
3449 struct http_txn *txn = &s->txn;
3450
3451 /* This connection is being tarpitted. The CLIENT side has
3452 * already set the connect expiration date to the right
3453 * timeout. We just have to check that the client is still
3454 * there and that the timeout has not expired.
3455 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003456 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003457 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3458 !tick_is_expired(req->analyse_exp, now_ms))
3459 return 0;
3460
3461 /* We will set the queue timer to the time spent, just for
3462 * logging purposes. We fake a 500 server error, so that the
3463 * attacker will not suspect his connection has been tarpitted.
3464 * It will not cause trouble to the logs because we can exclude
3465 * the tarpitted connections by filtering on the 'PT' status flags.
3466 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003467 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3468
3469 txn->status = 500;
3470 if (req->flags != BF_READ_ERROR)
3471 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3472
3473 req->analysers = 0;
3474 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003475
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003476 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003477 if (s->listener->counters)
3478 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003479
Willy Tarreau60b85b02008-11-30 23:28:40 +01003480 if (!(s->flags & SN_ERR_MASK))
3481 s->flags |= SN_ERR_PRXCOND;
3482 if (!(s->flags & SN_FINST_MASK))
3483 s->flags |= SN_FINST_T;
3484 return 0;
3485}
3486
Willy Tarreaud34af782008-11-30 23:36:37 +01003487/* This function is an analyser which processes the HTTP request body. It looks
3488 * for parameters to be used for the load balancing algorithm (url_param). It
3489 * must only be called after the standard HTTP request processing has occurred,
3490 * because it expects the request to be parsed. It returns zero if it needs to
3491 * read more data, or 1 once it has completed its analysis.
3492 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003493int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003494{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003495 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003496 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003497 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003498
3499 /* We have to parse the HTTP request body to find any required data.
3500 * "balance url_param check_post" should have been the only way to get
3501 * into this. We were brought here after HTTP header analysis, so all
3502 * related structures are ready.
3503 */
3504
Willy Tarreau522d6c02009-12-06 18:49:18 +01003505 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3506 goto missing_data;
3507
3508 if (msg->msg_state < HTTP_MSG_100_SENT) {
3509 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3510 * send an HTTP/1.1 100 Continue intermediate response.
3511 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003512 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003513 struct hdr_ctx ctx;
3514 ctx.idx = 0;
3515 /* Expect is allowed in 1.1, look for it */
3516 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3517 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3518 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3519 }
3520 }
3521 msg->msg_state = HTTP_MSG_100_SENT;
3522 }
3523
3524 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003525 /* we have msg->col and msg->sov which both point to the first
3526 * byte of message body. msg->som still points to the beginning
3527 * of the message. We must save the body in req->lr because it
3528 * survives buffer re-alignments.
3529 */
3530 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003531 if (txn->flags & TX_REQ_TE_CHNK)
3532 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3533 else
3534 msg->msg_state = HTTP_MSG_DATA;
3535 }
3536
3537 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003538 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003539 * set ->sov and ->lr to point to the body and switch to DATA or
3540 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003541 */
3542 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003543
Willy Tarreau115acb92009-12-26 13:56:06 +01003544 if (!ret)
3545 goto missing_data;
3546 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003547 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003548 }
3549
Willy Tarreaud98cf932009-12-27 22:54:55 +01003550 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003551 * We have the first non-header byte in msg->col, which is either the
3552 * beginning of the chunk size or of the data. The first data byte is in
3553 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3554 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003555 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003556
3557 if (msg->hdr_content_len < limit)
3558 limit = msg->hdr_content_len;
3559
Willy Tarreau7c96f672009-12-27 22:47:25 +01003560 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003561 goto http_end;
3562
3563 missing_data:
3564 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003565 if (req->flags & BF_FULL)
3566 goto return_bad_req;
3567
Willy Tarreau522d6c02009-12-06 18:49:18 +01003568 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3569 txn->status = 408;
3570 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3571 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003572 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003573
3574 /* we get here if we need to wait for more data */
3575 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003576 /* Not enough data. We'll re-use the http-request
3577 * timeout here. Ideally, we should set the timeout
3578 * relative to the accept() date. We just set the
3579 * request timeout once at the beginning of the
3580 * request.
3581 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003582 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003583 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003584 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003585 return 0;
3586 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003587
3588 http_end:
3589 /* The situation will not evolve, so let's give up on the analysis. */
3590 s->logs.tv_request = now; /* update the request timer to reflect full request */
3591 req->analysers &= ~an_bit;
3592 req->analyse_exp = TICK_ETERNITY;
3593 return 1;
3594
3595 return_bad_req: /* let's centralize all bad requests */
3596 txn->req.msg_state = HTTP_MSG_ERROR;
3597 txn->status = 400;
3598 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3599
3600 return_err_msg:
3601 req->analysers = 0;
3602 s->fe->counters.failed_req++;
3603 if (s->listener->counters)
3604 s->listener->counters->failed_req++;
3605
3606 if (!(s->flags & SN_ERR_MASK))
3607 s->flags |= SN_ERR_PRXCOND;
3608 if (!(s->flags & SN_FINST_MASK))
3609 s->flags |= SN_FINST_R;
3610 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003611}
3612
Willy Tarreau610ecce2010-01-04 21:15:02 +01003613/* Terminate current transaction and prepare a new one. This is very tricky
3614 * right now but it works.
3615 */
3616void http_end_txn_clean_session(struct session *s)
3617{
3618 /* FIXME: We need a more portable way of releasing a backend's and a
3619 * server's connections. We need a safer way to reinitialize buffer
3620 * flags. We also need a more accurate method for computing per-request
3621 * data.
3622 */
3623 http_silent_debug(__LINE__, s);
3624
3625 s->req->cons->flags |= SI_FL_NOLINGER;
3626 s->req->cons->shutr(s->req->cons);
3627 s->req->cons->shutw(s->req->cons);
3628
3629 http_silent_debug(__LINE__, s);
3630
3631 if (s->flags & SN_BE_ASSIGNED)
3632 s->be->beconn--;
3633
3634 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3635 session_process_counters(s);
3636
3637 if (s->txn.status) {
3638 int n;
3639
3640 n = s->txn.status / 100;
3641 if (n < 1 || n > 5)
3642 n = 0;
3643
3644 if (s->fe->mode == PR_MODE_HTTP)
3645 s->fe->counters.p.http.rsp[n]++;
3646
3647 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3648 (s->be->mode == PR_MODE_HTTP))
3649 s->be->counters.p.http.rsp[n]++;
3650 }
3651
3652 /* don't count other requests' data */
3653 s->logs.bytes_in -= s->req->l - s->req->send_max;
3654 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3655
3656 /* let's do a final log if we need it */
3657 if (s->logs.logwait &&
3658 !(s->flags & SN_MONITOR) &&
3659 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3660 s->do_log(s);
3661 }
3662
3663 s->logs.accept_date = date; /* user-visible date for logging */
3664 s->logs.tv_accept = now; /* corrected date for internal use */
3665 tv_zero(&s->logs.tv_request);
3666 s->logs.t_queue = -1;
3667 s->logs.t_connect = -1;
3668 s->logs.t_data = -1;
3669 s->logs.t_close = 0;
3670 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3671 s->logs.srv_queue_size = 0; /* we will get this number soon */
3672
3673 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3674 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3675
3676 if (s->pend_pos)
3677 pendconn_free(s->pend_pos);
3678
3679 if (s->srv) {
3680 if (s->flags & SN_CURR_SESS) {
3681 s->flags &= ~SN_CURR_SESS;
3682 s->srv->cur_sess--;
3683 }
3684 if (may_dequeue_tasks(s->srv, s->be))
3685 process_srv_queue(s->srv);
3686 }
3687
3688 if (unlikely(s->srv_conn))
3689 sess_change_server(s, NULL);
3690 s->srv = NULL;
3691
3692 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3693 s->req->cons->fd = -1; /* just to help with debugging */
3694 s->req->cons->err_type = SI_ET_NONE;
3695 s->req->cons->err_loc = NULL;
3696 s->req->cons->exp = TICK_ETERNITY;
3697 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003698 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3699 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL);
Willy Tarreau4de91492010-01-22 19:10:05 +01003700 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3702 s->txn.meth = 0;
3703 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003704 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705 if (s->be->options2 & PR_O2_INDEPSTR)
3706 s->req->cons->flags |= SI_FL_INDEP_STR;
3707
3708 /* if the request buffer is not empty, it means we're
3709 * about to process another request, so send pending
3710 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003711 * Just don't do this if the buffer is close to be full,
3712 * because the request will wait for it to flush a little
3713 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003715 if (s->req->l > s->req->send_max) {
3716 if (s->rep->send_max &&
3717 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003718 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3719 s->rep->flags |= BF_EXPECT_MORE;
3720 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003721
3722 /* we're removing the analysers, we MUST re-enable events detection */
3723 buffer_auto_read(s->req);
3724 buffer_auto_close(s->req);
3725 buffer_auto_read(s->rep);
3726 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727
3728 /* make ->lr point to the first non-forwarded byte */
3729 s->req->lr = s->req->w + s->req->send_max;
3730 if (s->req->lr >= s->req->data + s->req->size)
3731 s->req->lr -= s->req->size;
3732 s->rep->lr = s->rep->w + s->rep->send_max;
3733 if (s->rep->lr >= s->rep->data + s->rep->size)
3734 s->rep->lr -= s->req->size;
3735
3736 s->req->analysers |= s->fe->fe_req_ana;
3737 s->rep->analysers = 0;
3738
3739 http_silent_debug(__LINE__, s);
3740}
3741
3742
3743/* This function updates the request state machine according to the response
3744 * state machine and buffer flags. It returns 1 if it changes anything (flag
3745 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3746 * it is only used to find when a request/response couple is complete. Both
3747 * this function and its equivalent should loop until both return zero. It
3748 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3749 */
3750int http_sync_req_state(struct session *s)
3751{
3752 struct buffer *buf = s->req;
3753 struct http_txn *txn = &s->txn;
3754 unsigned int old_flags = buf->flags;
3755 unsigned int old_state = txn->req.msg_state;
3756
3757 http_silent_debug(__LINE__, s);
3758 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3759 return 0;
3760
3761 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003762 /* No need to read anymore, the request was completely parsed.
3763 * We can shut the read side unless we want to abort_on_close.
3764 */
3765 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3766 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767
3768 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3769 goto wait_other_side;
3770
3771 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3772 /* The server has not finished to respond, so we
3773 * don't want to move in order not to upset it.
3774 */
3775 goto wait_other_side;
3776 }
3777
3778 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3779 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003780 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 txn->req.msg_state = HTTP_MSG_TUNNEL;
3782 goto wait_other_side;
3783 }
3784
3785 /* When we get here, it means that both the request and the
3786 * response have finished receiving. Depending on the connection
3787 * mode, we'll have to wait for the last bytes to leave in either
3788 * direction, and sometimes for a close to be effective.
3789 */
3790
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003791 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3792 /* Server-close mode : queue a connection close to the server */
3793 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003795 }
3796 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3797 /* Option forceclose is set, or either side wants to close,
3798 * let's enforce it now that we're not expecting any new
3799 * data to come. The caller knows the session is complete
3800 * once both states are CLOSED.
3801 */
3802 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003803 buffer_shutr_now(buf);
3804 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003806 }
3807 else {
3808 /* The last possible modes are keep-alive and tunnel. Since tunnel
3809 * mode does not set the body analyser, we can't reach this place
3810 * in tunnel mode, so we're left with keep-alive only.
3811 * This mode is currently not implemented, we switch to tunnel mode.
3812 */
3813 buffer_auto_read(buf);
3814 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 }
3816
3817 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3818 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003819 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3820
Willy Tarreau610ecce2010-01-04 21:15:02 +01003821 if (!(buf->flags & BF_OUT_EMPTY)) {
3822 txn->req.msg_state = HTTP_MSG_CLOSING;
3823 goto http_msg_closing;
3824 }
3825 else {
3826 txn->req.msg_state = HTTP_MSG_CLOSED;
3827 goto http_msg_closed;
3828 }
3829 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003830 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003831 }
3832
3833 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3834 http_msg_closing:
3835 /* nothing else to forward, just waiting for the output buffer
3836 * to be empty and for the shutw_now to take effect.
3837 */
3838 if (buf->flags & BF_OUT_EMPTY) {
3839 txn->req.msg_state = HTTP_MSG_CLOSED;
3840 goto http_msg_closed;
3841 }
3842 else if (buf->flags & BF_SHUTW) {
3843 txn->req.msg_state = HTTP_MSG_ERROR;
3844 goto wait_other_side;
3845 }
3846 }
3847
3848 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3849 http_msg_closed:
3850 goto wait_other_side;
3851 }
3852
3853 wait_other_side:
3854 http_silent_debug(__LINE__, s);
3855 return txn->req.msg_state != old_state || buf->flags != old_flags;
3856}
3857
3858
3859/* This function updates the response state machine according to the request
3860 * state machine and buffer flags. It returns 1 if it changes anything (flag
3861 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3862 * it is only used to find when a request/response couple is complete. Both
3863 * this function and its equivalent should loop until both return zero. It
3864 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3865 */
3866int http_sync_res_state(struct session *s)
3867{
3868 struct buffer *buf = s->rep;
3869 struct http_txn *txn = &s->txn;
3870 unsigned int old_flags = buf->flags;
3871 unsigned int old_state = txn->rsp.msg_state;
3872
3873 http_silent_debug(__LINE__, s);
3874 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3875 return 0;
3876
3877 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3878 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003879 * still monitor the server connection for a possible close
3880 * while the request is being uploaded, so we don't disable
3881 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003883 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003884
3885 if (txn->req.msg_state == HTTP_MSG_ERROR)
3886 goto wait_other_side;
3887
3888 if (txn->req.msg_state < HTTP_MSG_DONE) {
3889 /* The client seems to still be sending data, probably
3890 * because we got an error response during an upload.
3891 * We have the choice of either breaking the connection
3892 * or letting it pass through. Let's do the later.
3893 */
3894 goto wait_other_side;
3895 }
3896
3897 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3898 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003899 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3901 goto wait_other_side;
3902 }
3903
3904 /* When we get here, it means that both the request and the
3905 * response have finished receiving. Depending on the connection
3906 * mode, we'll have to wait for the last bytes to leave in either
3907 * direction, and sometimes for a close to be effective.
3908 */
3909
3910 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3911 /* Server-close mode : shut read and wait for the request
3912 * side to close its output buffer. The caller will detect
3913 * when we're in DONE and the other is in CLOSED and will
3914 * catch that for the final cleanup.
3915 */
3916 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3917 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003919 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3920 /* Option forceclose is set, or either side wants to close,
3921 * let's enforce it now that we're not expecting any new
3922 * data to come. The caller knows the session is complete
3923 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003924 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003925 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3926 buffer_shutr_now(buf);
3927 buffer_shutw_now(buf);
3928 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929 }
3930 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003931 /* The last possible modes are keep-alive and tunnel. Since tunnel
3932 * mode does not set the body analyser, we can't reach this place
3933 * in tunnel mode, so we're left with keep-alive only.
3934 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003935 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003936 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003938 }
3939
3940 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3941 /* if we've just closed an output, let's switch */
3942 if (!(buf->flags & BF_OUT_EMPTY)) {
3943 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3944 goto http_msg_closing;
3945 }
3946 else {
3947 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3948 goto http_msg_closed;
3949 }
3950 }
3951 goto wait_other_side;
3952 }
3953
3954 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3955 http_msg_closing:
3956 /* nothing else to forward, just waiting for the output buffer
3957 * to be empty and for the shutw_now to take effect.
3958 */
3959 if (buf->flags & BF_OUT_EMPTY) {
3960 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3961 goto http_msg_closed;
3962 }
3963 else if (buf->flags & BF_SHUTW) {
3964 txn->rsp.msg_state = HTTP_MSG_ERROR;
3965 goto wait_other_side;
3966 }
3967 }
3968
3969 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3970 http_msg_closed:
3971 /* drop any pending data */
3972 buffer_ignore(buf, buf->l - buf->send_max);
3973 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003974 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975 goto wait_other_side;
3976 }
3977
3978 wait_other_side:
3979 http_silent_debug(__LINE__, s);
3980 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3981}
3982
3983
3984/* Resync the request and response state machines. Return 1 if either state
3985 * changes.
3986 */
3987int http_resync_states(struct session *s)
3988{
3989 struct http_txn *txn = &s->txn;
3990 int old_req_state = txn->req.msg_state;
3991 int old_res_state = txn->rsp.msg_state;
3992
3993 http_silent_debug(__LINE__, s);
3994 http_sync_req_state(s);
3995 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003996 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 if (!http_sync_res_state(s))
3998 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01003999 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004000 if (!http_sync_req_state(s))
4001 break;
4002 }
4003 http_silent_debug(__LINE__, s);
4004 /* OK, both state machines agree on a compatible state.
4005 * There are a few cases we're interested in :
4006 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4007 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4008 * directions, so let's simply disable both analysers.
4009 * - HTTP_MSG_CLOSED on the response only means we must abort the
4010 * request.
4011 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4012 * with server-close mode means we've completed one request and we
4013 * must re-initialize the server connection.
4014 */
4015
4016 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4017 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4018 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4019 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4020 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004021 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004022 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004024 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004025 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004027 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4028 txn->rsp.msg_state == HTTP_MSG_ERROR ||
4029 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004030 s->rep->analysers = 0;
4031 buffer_auto_close(s->rep);
4032 buffer_auto_read(s->rep);
4033 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 buffer_abort(s->req);
4035 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004036 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004037 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 }
4039 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4040 txn->rsp.msg_state == HTTP_MSG_DONE &&
4041 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4042 /* server-close: terminate this server connection and
4043 * reinitialize a fresh-new transaction.
4044 */
4045 http_end_txn_clean_session(s);
4046 }
4047
4048 http_silent_debug(__LINE__, s);
4049 return txn->req.msg_state != old_req_state ||
4050 txn->rsp.msg_state != old_res_state;
4051}
4052
Willy Tarreaud98cf932009-12-27 22:54:55 +01004053/* This function is an analyser which forwards request body (including chunk
4054 * sizes if any). It is called as soon as we must forward, even if we forward
4055 * zero byte. The only situation where it must not be called is when we're in
4056 * tunnel mode and we want to forward till the close. It's used both to forward
4057 * remaining data and to resync after end of body. It expects the msg_state to
4058 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4059 * read more data, or 1 once we can go on with next request or end the session.
4060 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4061 * bytes of pending data + the headers if not already done (between som and sov).
4062 * It eventually adjusts som to match sov after the data in between have been sent.
4063 */
4064int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4065{
4066 struct http_txn *txn = &s->txn;
4067 struct http_msg *msg = &s->txn.req;
4068
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004069 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4070 return 0;
4071
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004072 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4073 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
4074 /* Output closed while we were sending data. We must abort. */
4075 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004076 buffer_auto_read(req);
4077 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004078 req->analysers &= ~an_bit;
4079 return 1;
4080 }
4081
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004082 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004083
4084 /* Note that we don't have to send 100-continue back because we don't
4085 * need the data to complete our job, and it's up to the server to
4086 * decide whether to return 100, 417 or anything else in return of
4087 * an "Expect: 100-continue" header.
4088 */
4089
4090 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4091 /* we have msg->col and msg->sov which both point to the first
4092 * byte of message body. msg->som still points to the beginning
4093 * of the message. We must save the body in req->lr because it
4094 * survives buffer re-alignments.
4095 */
4096 req->lr = req->data + msg->sov;
4097 if (txn->flags & TX_REQ_TE_CHNK)
4098 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4099 else {
4100 msg->msg_state = HTTP_MSG_DATA;
4101 }
4102 }
4103
Willy Tarreaud98cf932009-12-27 22:54:55 +01004104 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004106 /* we may have some data pending */
4107 if (msg->hdr_content_len || msg->som != msg->sov) {
4108 int bytes = msg->sov - msg->som;
4109 if (bytes < 0) /* sov may have wrapped at the end */
4110 bytes += req->size;
4111 buffer_forward(req, bytes + msg->hdr_content_len);
4112 msg->hdr_content_len = 0; /* don't forward that again */
4113 msg->som = msg->sov;
4114 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004115
Willy Tarreaucaabe412010-01-03 23:08:28 +01004116 if (msg->msg_state == HTTP_MSG_DATA) {
4117 /* must still forward */
4118 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004119 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004120
4121 /* nothing left to forward */
4122 if (txn->flags & TX_REQ_TE_CHNK)
4123 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004125 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004126 }
4127 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004128 /* read the chunk size and assign it to ->hdr_content_len, then
4129 * set ->sov and ->lr to point to the body and switch to DATA or
4130 * TRAILERS state.
4131 */
4132 int ret = http_parse_chunk_size(req, msg);
4133
4134 if (!ret)
4135 goto missing_data;
4136 else if (ret < 0)
4137 goto return_bad_req;
4138 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004139 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004140 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4141 /* we want the CRLF after the data */
4142 int ret;
4143
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004144 req->lr = req->w + req->send_max;
4145 if (req->lr >= req->data + req->size)
4146 req->lr -= req->size;
4147
Willy Tarreaud98cf932009-12-27 22:54:55 +01004148 ret = http_skip_chunk_crlf(req, msg);
4149
4150 if (ret == 0)
4151 goto missing_data;
4152 else if (ret < 0)
4153 goto return_bad_req;
4154 /* we're in MSG_CHUNK_SIZE now */
4155 }
4156 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4157 int ret = http_forward_trailers(req, msg);
4158
4159 if (ret == 0)
4160 goto missing_data;
4161 else if (ret < 0)
4162 goto return_bad_req;
4163 /* we're in HTTP_MSG_DONE now */
4164 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004165 else {
4166 /* other states, DONE...TUNNEL */
4167 if (http_resync_states(s)) {
4168 /* some state changes occurred, maybe the analyser
4169 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004170 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004171 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4172 goto return_bad_req;
4173 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004174 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004176 }
4177 }
4178
Willy Tarreaud98cf932009-12-27 22:54:55 +01004179 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004180 /* stop waiting for data if the input is closed before the end */
4181 if (req->flags & BF_SHUTR)
4182 goto return_bad_req;
4183
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004184 /* waiting for the last bits to leave the buffer */
4185 if (req->flags & BF_SHUTW)
4186 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004187
4188 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004189 return 0;
4190
Willy Tarreaud98cf932009-12-27 22:54:55 +01004191 return_bad_req: /* let's centralize all bad requests */
4192 txn->req.msg_state = HTTP_MSG_ERROR;
4193 txn->status = 400;
4194 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004195 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004196 req->analysers = 0;
4197 s->fe->counters.failed_req++;
4198 if (s->listener->counters)
4199 s->listener->counters->failed_req++;
4200
4201 if (!(s->flags & SN_ERR_MASK))
4202 s->flags |= SN_ERR_PRXCOND;
4203 if (!(s->flags & SN_FINST_MASK))
4204 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004205 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004206 return 0;
4207}
4208
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004209/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4210 * processing can continue on next analysers, or zero if it either needs more
4211 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4212 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4213 * when it has nothing left to do, and may remove any analyser when it wants to
4214 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004215 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004216int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004217{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004218 struct http_txn *txn = &s->txn;
4219 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004220 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004221 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004222 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004223 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004224
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004225 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 +02004226 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004227 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004228 rep,
4229 rep->rex, rep->wex,
4230 rep->flags,
4231 rep->l,
4232 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004233
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004234 /*
4235 * Now parse the partial (or complete) lines.
4236 * We will check the response syntax, and also join multi-line
4237 * headers. An index of all the lines will be elaborated while
4238 * parsing.
4239 *
4240 * For the parsing, we use a 28 states FSM.
4241 *
4242 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004243 * rep->data + msg->som = beginning of response
4244 * rep->data + msg->eoh = end of processed headers / start of current one
4245 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004246 * rep->lr = first non-visited byte
4247 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004248 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004249 */
4250
Willy Tarreau83e3af02009-12-28 17:39:57 +01004251 /* There's a protected area at the end of the buffer for rewriting
4252 * purposes. We don't want to start to parse the request if the
4253 * protected area is affected, because we may have to move processed
4254 * data later, which is much more complicated.
4255 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004256 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4257 if (unlikely((rep->flags & BF_FULL) ||
4258 rep->r < rep->lr ||
4259 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4260 if (rep->send_max) {
4261 /* some data has still not left the buffer, wake us once that's done */
4262 buffer_dont_close(rep);
4263 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4264 return 0;
4265 }
4266 if (rep->l <= rep->size - global.tune.maxrewrite)
4267 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004268 }
4269
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004270 if (likely(rep->lr < rep->r))
4271 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004272 }
4273
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004274 /* 1: we might have to print this header in debug mode */
4275 if (unlikely((global.mode & MODE_DEBUG) &&
4276 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004277 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004278 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004279
Willy Tarreau962c3f42010-01-10 00:15:35 +01004280 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004281 eol = sol + msg->sl.rq.l;
4282 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004283
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004284 sol += hdr_idx_first_pos(&txn->hdr_idx);
4285 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004286
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004287 while (cur_idx) {
4288 eol = sol + txn->hdr_idx.v[cur_idx].len;
4289 debug_hdr("srvhdr", s, sol, eol);
4290 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4291 cur_idx = txn->hdr_idx.v[cur_idx].next;
4292 }
4293 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004294
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004295 /*
4296 * Now we quickly check if we have found a full valid response.
4297 * If not so, we check the FD and buffer states before leaving.
4298 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004299 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004300 * responses are checked first.
4301 *
4302 * Depending on whether the client is still there or not, we
4303 * may send an error response back or not. Note that normally
4304 * we should only check for HTTP status there, and check I/O
4305 * errors somewhere else.
4306 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004307
Willy Tarreau655dce92009-11-08 13:10:58 +01004308 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004309 /* Invalid response */
4310 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4311 /* we detected a parsing error. We want to archive this response
4312 * in the dedicated proxy area for later troubleshooting.
4313 */
4314 hdr_response_bad:
4315 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4316 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4317
4318 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004319 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004320 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004321 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4322 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004323
Willy Tarreau90deb182010-01-07 00:20:41 +01004324 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004325 rep->analysers = 0;
4326 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004327 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004328 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004329 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4330
4331 if (!(s->flags & SN_ERR_MASK))
4332 s->flags |= SN_ERR_PRXCOND;
4333 if (!(s->flags & SN_FINST_MASK))
4334 s->flags |= SN_FINST_H;
4335
4336 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004337 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004338
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004339 /* too large response does not fit in buffer. */
4340 else if (rep->flags & BF_FULL) {
4341 goto hdr_response_bad;
4342 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004343
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004344 /* read error */
4345 else if (rep->flags & BF_READ_ERROR) {
4346 if (msg->err_pos >= 0)
4347 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004348
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004349 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004350 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004351 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004352 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4353 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004354
Willy Tarreau90deb182010-01-07 00:20:41 +01004355 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004356 rep->analysers = 0;
4357 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004358 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004359 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004360 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004361
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004362 if (!(s->flags & SN_ERR_MASK))
4363 s->flags |= SN_ERR_SRVCL;
4364 if (!(s->flags & SN_FINST_MASK))
4365 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004366 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004367 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004368
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004369 /* read timeout : return a 504 to the client. */
4370 else if (rep->flags & BF_READ_TIMEOUT) {
4371 if (msg->err_pos >= 0)
4372 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004373
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004374 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004375 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004376 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004377 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4378 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004379
Willy Tarreau90deb182010-01-07 00:20:41 +01004380 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004381 rep->analysers = 0;
4382 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004383 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004384 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004385 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004386
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004387 if (!(s->flags & SN_ERR_MASK))
4388 s->flags |= SN_ERR_SRVTO;
4389 if (!(s->flags & SN_FINST_MASK))
4390 s->flags |= SN_FINST_H;
4391 return 0;
4392 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004393
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004394 /* close from server */
4395 else if (rep->flags & BF_SHUTR) {
4396 if (msg->err_pos >= 0)
4397 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004398
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004399 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004400 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004401 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004402 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4403 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004404
Willy Tarreau90deb182010-01-07 00:20:41 +01004405 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004406 rep->analysers = 0;
4407 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004408 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004409 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004410 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004411
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004412 if (!(s->flags & SN_ERR_MASK))
4413 s->flags |= SN_ERR_SRVCL;
4414 if (!(s->flags & SN_FINST_MASK))
4415 s->flags |= SN_FINST_H;
4416 return 0;
4417 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004418
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419 /* write error to client (we don't send any message then) */
4420 else if (rep->flags & BF_WRITE_ERROR) {
4421 if (msg->err_pos >= 0)
4422 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004423
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004424 s->be->counters.failed_resp++;
4425 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004426 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004427
4428 if (!(s->flags & SN_ERR_MASK))
4429 s->flags |= SN_ERR_CLICL;
4430 if (!(s->flags & SN_FINST_MASK))
4431 s->flags |= SN_FINST_H;
4432
4433 /* process_session() will take care of the error */
4434 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004435 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004436
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004437 buffer_dont_close(rep);
4438 return 0;
4439 }
4440
4441 /* More interesting part now : we know that we have a complete
4442 * response which at least looks like HTTP. We have an indicator
4443 * of each header's length, so we can parse them quickly.
4444 */
4445
4446 if (unlikely(msg->err_pos >= 0))
4447 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4448
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004449 /*
4450 * 1: get the status code
4451 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004452 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004453 if (n < 1 || n > 5)
4454 n = 0;
4455 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004456
Willy Tarreau5b154472009-12-21 20:11:07 +01004457 /* check if the response is HTTP/1.1 or above */
4458 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004459 ((msg->sol[5] > '1') ||
4460 ((msg->sol[5] == '1') &&
4461 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004462 txn->flags |= TX_RES_VER_11;
4463
4464 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004465 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004466
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004467 /* transfer length unknown*/
4468 txn->flags &= ~TX_RES_XFER_LEN;
4469
Willy Tarreau962c3f42010-01-10 00:15:35 +01004470 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004472 if (txn->status >= 100 && txn->status < 500)
4473 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4474 else
4475 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4476
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004477 /*
4478 * 2: check for cacheability.
4479 */
4480
4481 switch (txn->status) {
4482 case 200:
4483 case 203:
4484 case 206:
4485 case 300:
4486 case 301:
4487 case 410:
4488 /* RFC2616 @13.4:
4489 * "A response received with a status code of
4490 * 200, 203, 206, 300, 301 or 410 MAY be stored
4491 * by a cache (...) unless a cache-control
4492 * directive prohibits caching."
4493 *
4494 * RFC2616 @9.5: POST method :
4495 * "Responses to this method are not cacheable,
4496 * unless the response includes appropriate
4497 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004498 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004499 if (likely(txn->meth != HTTP_METH_POST) &&
4500 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4501 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4502 break;
4503 default:
4504 break;
4505 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004506
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507 /*
4508 * 3: we may need to capture headers
4509 */
4510 s->logs.logwait &= ~LW_RESP;
4511 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004512 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 txn->rsp.cap, s->fe->rsp_cap);
4514
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004515 /* 4: determine the transfer-length.
4516 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4517 * the presence of a message-body in a RESPONSE and its transfer length
4518 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004519 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004520 * All responses to the HEAD request method MUST NOT include a
4521 * message-body, even though the presence of entity-header fields
4522 * might lead one to believe they do. All 1xx (informational), 204
4523 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4524 * message-body. All other responses do include a message-body,
4525 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004526 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004527 * 1. Any response which "MUST NOT" include a message-body (such as the
4528 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4529 * always terminated by the first empty line after the header fields,
4530 * regardless of the entity-header fields present in the message.
4531 *
4532 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4533 * the "chunked" transfer-coding (Section 6.2) is used, the
4534 * transfer-length is defined by the use of this transfer-coding.
4535 * If a Transfer-Encoding header field is present and the "chunked"
4536 * transfer-coding is not present, the transfer-length is defined by
4537 * the sender closing the connection.
4538 *
4539 * 3. If a Content-Length header field is present, its decimal value in
4540 * OCTETs represents both the entity-length and the transfer-length.
4541 * If a message is received with both a Transfer-Encoding header
4542 * field and a Content-Length header field, the latter MUST be ignored.
4543 *
4544 * 4. If the message uses the media type "multipart/byteranges", and
4545 * the transfer-length is not otherwise specified, then this self-
4546 * delimiting media type defines the transfer-length. This media
4547 * type MUST NOT be used unless the sender knows that the recipient
4548 * can parse it; the presence in a request of a Range header with
4549 * multiple byte-range specifiers from a 1.1 client implies that the
4550 * client can parse multipart/byteranges responses.
4551 *
4552 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004553 */
4554
4555 /* Skip parsing if no content length is possible. The response flags
4556 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004557 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004558 * FIXME: should we parse anyway and return an error on chunked encoding ?
4559 */
4560 if (txn->meth == HTTP_METH_HEAD ||
4561 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004562 txn->status == 204 || txn->status == 304) {
4563 txn->flags |= TX_RES_XFER_LEN;
4564 goto skip_content_length;
4565 }
4566
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004567 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004568 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004569 while ((txn->flags & TX_RES_VER_11) &&
4570 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004571 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4572 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4573 else if (txn->flags & TX_RES_TE_CHNK) {
4574 /* bad transfer-encoding (chunked followed by something else) */
4575 use_close_only = 1;
4576 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4577 break;
4578 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004579 }
4580
4581 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4582 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004583 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004584 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4585 signed long long cl;
4586
4587 if (!ctx.vlen)
4588 goto hdr_response_bad;
4589
4590 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4591 goto hdr_response_bad; /* parse failure */
4592
4593 if (cl < 0)
4594 goto hdr_response_bad;
4595
4596 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4597 goto hdr_response_bad; /* already specified, was different */
4598
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004599 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004600 msg->hdr_content_len = cl;
4601 }
4602
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004603 /* FIXME: we should also implement the multipart/byterange method.
4604 * For now on, we resort to close mode in this case (unknown length).
4605 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004606skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004607
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004608 /* end of job, return OK */
4609 rep->analysers &= ~an_bit;
4610 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004611 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 return 1;
4613}
4614
4615/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004616 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4617 * and updates t->rep->analysers. It might make sense to explode it into several
4618 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 */
4620int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4621{
4622 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 struct http_msg *msg = &txn->rsp;
4624 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004625 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626
4627 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4628 now_ms, __FUNCTION__,
4629 t,
4630 rep,
4631 rep->rex, rep->wex,
4632 rep->flags,
4633 rep->l,
4634 rep->analysers);
4635
Willy Tarreau655dce92009-11-08 13:10:58 +01004636 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 return 0;
4638
4639 rep->analysers &= ~an_bit;
4640 rep->analyse_exp = TICK_ETERNITY;
4641
Willy Tarreau5b154472009-12-21 20:11:07 +01004642 /* Now we have to check if we need to modify the Connection header.
4643 * This is more difficult on the response than it is on the request,
4644 * because we can have two different HTTP versions and we don't know
4645 * how the client will interprete a response. For instance, let's say
4646 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4647 * HTTP/1.1 response without any header. Maybe it will bound itself to
4648 * HTTP/1.0 because it only knows about it, and will consider the lack
4649 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4650 * the lack of header as a keep-alive. Thus we will use two flags
4651 * indicating how a request MAY be understood by the client. In case
4652 * of multiple possibilities, we'll fix the header to be explicit. If
4653 * ambiguous cases such as both close and keepalive are seen, then we
4654 * will fall back to explicit close. Note that we won't take risks with
4655 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004656 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004657 */
4658
Willy Tarreaudc008c52010-02-01 16:20:08 +01004659 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4660 txn->status == 101)) {
4661 /* Either we've established an explicit tunnel, or we're
4662 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004663 * to understand the next protocols. We have to switch to tunnel
4664 * mode, so that we transfer the request and responses then let
4665 * this protocol pass unmodified. When we later implement specific
4666 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004667 * header which contains information about that protocol for
4668 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004669 */
4670 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4671 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004672 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4673 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4674 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004675 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004676
Willy Tarreau60466522010-01-18 19:08:45 +01004677 /* on unknown transfer length, we must close */
4678 if (!(txn->flags & TX_RES_XFER_LEN) &&
4679 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4680 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004681
Willy Tarreau60466522010-01-18 19:08:45 +01004682 /* now adjust header transformations depending on current state */
4683 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4684 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4685 to_del |= 2; /* remove "keep-alive" on any response */
4686 if (!(txn->flags & TX_RES_VER_11))
4687 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004688 }
Willy Tarreau60466522010-01-18 19:08:45 +01004689 else { /* SCL / KAL */
4690 to_del |= 1; /* remove "close" on any response */
4691 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4692 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004693 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004694
Willy Tarreau60466522010-01-18 19:08:45 +01004695 /* Parse and remove some headers from the connection header */
4696 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004697
Willy Tarreau60466522010-01-18 19:08:45 +01004698 /* Some keep-alive responses are converted to Server-close if
4699 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004700 */
Willy Tarreau60466522010-01-18 19:08:45 +01004701 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4702 if ((txn->flags & TX_HDR_CONN_CLO) ||
4703 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4704 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004705 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004706 }
4707
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004708 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004709 /*
4710 * 3: we will have to evaluate the filters.
4711 * As opposed to version 1.2, now they will be evaluated in the
4712 * filters order and not in the header order. This means that
4713 * each filter has to be validated among all headers.
4714 *
4715 * Filters are tried with ->be first, then with ->fe if it is
4716 * different from ->be.
4717 */
4718
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004719 cur_proxy = t->be;
4720 while (1) {
4721 struct proxy *rule_set = cur_proxy;
4722
4723 /* try headers filters */
4724 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004725 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004726 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004727 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004728 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004729 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4730 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004731 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004732 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004733 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004734 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004735 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004736 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004737 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004738 if (!(t->flags & SN_ERR_MASK))
4739 t->flags |= SN_ERR_PRXCOND;
4740 if (!(t->flags & SN_FINST_MASK))
4741 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004742 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004743 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004744 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004745
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004746 /* has the response been denied ? */
4747 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004748 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004749 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004750
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004751 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004752 if (t->listener->counters)
4753 t->listener->counters->denied_resp++;
4754
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004755 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004756 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004757
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004758 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004759 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004760 if (txn->status < 200)
4761 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004762 if (wl->cond) {
4763 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4764 ret = acl_pass(ret);
4765 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4766 ret = !ret;
4767 if (!ret)
4768 continue;
4769 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004770 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004771 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004772 }
4773
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004774 /* check whether we're already working on the frontend */
4775 if (cur_proxy == t->fe)
4776 break;
4777 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004778 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004779
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004780 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004781 * We may be facing a 100-continue response, in which case this
4782 * is not the right response, and we're waiting for the next one.
4783 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004784 * next one.
4785 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004786 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004787 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004788 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004789 msg->msg_state = HTTP_MSG_RPBEFORE;
4790 txn->status = 0;
4791 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4792 return 1;
4793 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004794 else if (unlikely(txn->status < 200))
4795 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004796
4797 /* we don't have any 1xx status code now */
4798
4799 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004800 * 4: check for server cookie.
4801 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004802 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4803 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004804 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004805
Willy Tarreaubaaee002006-06-26 02:48:02 +02004806
Willy Tarreaua15645d2007-03-18 16:22:39 +01004807 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004808 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004809 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004810 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004811 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004812
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004813 /*
4814 * 6: add server cookie in the response if needed
4815 */
4816 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004817 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004818 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004819
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004820 /* the server is known, it's not the one the client requested, we have to
4821 * insert a set-cookie here, except if we want to insert only on POST
4822 * requests and this one isn't. Note that servers which don't have cookies
4823 * (eg: some backup servers) will return a full cookie removal request.
4824 */
4825 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4826 t->be->cookie_name,
4827 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004828
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004829 if (t->be->cookie_domain)
4830 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004831
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004832 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004833 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004834 goto return_bad_resp;
4835 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004836
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004837 /* Here, we will tell an eventual cache on the client side that we don't
4838 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4839 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4840 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4841 */
4842 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004843
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004844 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4845
4846 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004847 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004848 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004849 }
4850 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004851
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004852 /*
4853 * 7: check if result will be cacheable with a cookie.
4854 * We'll block the response if security checks have caught
4855 * nasty things such as a cacheable cookie.
4856 */
4857 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4858 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004859 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004860
4861 /* we're in presence of a cacheable response containing
4862 * a set-cookie header. We'll block it as requested by
4863 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004864 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004865 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004866 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004867
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004868 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004869 if (t->listener->counters)
4870 t->listener->counters->denied_resp++;
4871
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004872 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4873 t->be->id, t->srv?t->srv->id:"<dispatch>");
4874 send_log(t->be, LOG_ALERT,
4875 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4876 t->be->id, t->srv?t->srv->id:"<dispatch>");
4877 goto return_srv_prx_502;
4878 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004879
4880 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004881 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004882 */
Willy Tarreau60466522010-01-18 19:08:45 +01004883 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4884 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4885 unsigned int want_flags = 0;
4886
4887 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4888 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4889 /* we want a keep-alive response here. Keep-alive header
4890 * required if either side is not 1.1.
4891 */
4892 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4893 want_flags |= TX_CON_KAL_SET;
4894 }
4895 else {
4896 /* we want a close response here. Close header required if
4897 * the server is 1.1, regardless of the client.
4898 */
4899 if (txn->flags & TX_RES_VER_11)
4900 want_flags |= TX_CON_CLO_SET;
4901 }
4902
4903 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4904 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004905 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004906
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004907 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01004908 if ((txn->flags & TX_RES_XFER_LEN) ||
4909 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004910 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004911
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004912 /*************************************************************
4913 * OK, that's finished for the headers. We have done what we *
4914 * could. Let's switch to the DATA state. *
4915 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004916
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004917 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004918
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004919 /* if the user wants to log as soon as possible, without counting
4920 * bytes from the server, then this is the right moment. We have
4921 * to temporarily assign bytes_out to log what we currently have.
4922 */
4923 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4924 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4925 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004926 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004927 t->logs.bytes_out = 0;
4928 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004929
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004930 /* Note: we must not try to cheat by jumping directly to DATA,
4931 * otherwise we would not let the client side wake up.
4932 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004933
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004934 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004935 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004936 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004937}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004938
Willy Tarreaud98cf932009-12-27 22:54:55 +01004939/* This function is an analyser which forwards response body (including chunk
4940 * sizes if any). It is called as soon as we must forward, even if we forward
4941 * zero byte. The only situation where it must not be called is when we're in
4942 * tunnel mode and we want to forward till the close. It's used both to forward
4943 * remaining data and to resync after end of body. It expects the msg_state to
4944 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4945 * read more data, or 1 once we can go on with next request or end the session.
4946 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4947 * bytes of pending data + the headers if not already done (between som and sov).
4948 * It eventually adjusts som to match sov after the data in between have been sent.
4949 */
4950int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4951{
4952 struct http_txn *txn = &s->txn;
4953 struct http_msg *msg = &s->txn.rsp;
4954
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004955 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4956 return 0;
4957
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004958 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004959 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004960 !s->req->analysers) {
4961 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4962 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004963 buffer_auto_read(res);
4964 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004965 res->analysers &= ~an_bit;
4966 return 1;
4967 }
4968
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004969 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004970
Willy Tarreaud98cf932009-12-27 22:54:55 +01004971 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4972 /* we have msg->col and msg->sov which both point to the first
4973 * byte of message body. msg->som still points to the beginning
4974 * of the message. We must save the body in req->lr because it
4975 * survives buffer re-alignments.
4976 */
4977 res->lr = res->data + msg->sov;
4978 if (txn->flags & TX_RES_TE_CHNK)
4979 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4980 else {
4981 msg->msg_state = HTTP_MSG_DATA;
4982 }
4983 }
4984
Willy Tarreaud98cf932009-12-27 22:54:55 +01004985 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004987 /* we may have some data pending */
4988 if (msg->hdr_content_len || msg->som != msg->sov) {
4989 int bytes = msg->sov - msg->som;
4990 if (bytes < 0) /* sov may have wrapped at the end */
4991 bytes += res->size;
4992 buffer_forward(res, bytes + msg->hdr_content_len);
4993 msg->hdr_content_len = 0; /* don't forward that again */
4994 msg->som = msg->sov;
4995 }
4996
Willy Tarreaucaabe412010-01-03 23:08:28 +01004997 if (msg->msg_state == HTTP_MSG_DATA) {
4998 /* must still forward */
4999 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005000 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005001
5002 /* nothing left to forward */
5003 if (txn->flags & TX_RES_TE_CHNK)
5004 msg->msg_state = HTTP_MSG_DATA_CRLF;
5005 else
5006 msg->msg_state = HTTP_MSG_DONE;
5007 }
5008 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005009 /* read the chunk size and assign it to ->hdr_content_len, then
5010 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5011 */
5012 int ret = http_parse_chunk_size(res, msg);
5013
5014 if (!ret)
5015 goto missing_data;
5016 else if (ret < 0)
5017 goto return_bad_res;
5018 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005019 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005020 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5021 /* we want the CRLF after the data */
5022 int ret;
5023
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005024 res->lr = res->w + res->send_max;
5025 if (res->lr >= res->data + res->size)
5026 res->lr -= res->size;
5027
Willy Tarreaud98cf932009-12-27 22:54:55 +01005028 ret = http_skip_chunk_crlf(res, msg);
5029
5030 if (!ret)
5031 goto missing_data;
5032 else if (ret < 0)
5033 goto return_bad_res;
5034 /* we're in MSG_CHUNK_SIZE now */
5035 }
5036 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5037 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005038
Willy Tarreaud98cf932009-12-27 22:54:55 +01005039 if (ret == 0)
5040 goto missing_data;
5041 else if (ret < 0)
5042 goto return_bad_res;
5043 /* we're in HTTP_MSG_DONE now */
5044 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045 else {
5046 /* other states, DONE...TUNNEL */
5047 if (http_resync_states(s)) {
5048 http_silent_debug(__LINE__, s);
5049 /* some state changes occurred, maybe the analyser
5050 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005051 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005052 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5053 goto return_bad_res;
5054 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005055 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005056 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005057 }
5058 }
5059
Willy Tarreaud98cf932009-12-27 22:54:55 +01005060 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005061 /* stop waiting for data if the input is closed before the end */
5062 if (res->flags & BF_SHUTR)
5063 goto return_bad_res;
5064
Willy Tarreau610ecce2010-01-04 21:15:02 +01005065 if (!s->req->analysers)
5066 goto return_bad_res;
5067
Willy Tarreaud98cf932009-12-27 22:54:55 +01005068 /* forward the chunk size as well as any pending data */
5069 if (msg->hdr_content_len || msg->som != msg->sov) {
5070 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5071 msg->hdr_content_len = 0; /* don't forward that again */
5072 msg->som = msg->sov;
5073 }
5074
Willy Tarreaud98cf932009-12-27 22:54:55 +01005075 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005076 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005077 return 0;
5078
5079 return_bad_res: /* let's centralize all bad resuests */
5080 txn->rsp.msg_state = HTTP_MSG_ERROR;
5081 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01005082 /* don't send any error message as we're in the body */
5083 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005084 res->analysers = 0;
5085 s->be->counters.failed_resp++;
5086 if (s->srv) {
5087 s->srv->counters.failed_resp++;
5088 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5089 }
5090
5091 if (!(s->flags & SN_ERR_MASK))
5092 s->flags |= SN_ERR_PRXCOND;
5093 if (!(s->flags & SN_FINST_MASK))
5094 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005096 return 0;
5097}
5098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005099/* Iterate the same filter through all request headers.
5100 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005101 * Since it can manage the switch to another backend, it updates the per-proxy
5102 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005103 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005104int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005105{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005106 char term;
5107 char *cur_ptr, *cur_end, *cur_next;
5108 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005109 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005110 struct hdr_idx_elem *cur_hdr;
5111 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005113 last_hdr = 0;
5114
Willy Tarreau962c3f42010-01-10 00:15:35 +01005115 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005116 old_idx = 0;
5117
5118 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005119 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005120 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005121 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005122 (exp->action == ACT_ALLOW ||
5123 exp->action == ACT_DENY ||
5124 exp->action == ACT_TARPIT))
5125 return 0;
5126
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005127 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005128 if (!cur_idx)
5129 break;
5130
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005131 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005132 cur_ptr = cur_next;
5133 cur_end = cur_ptr + cur_hdr->len;
5134 cur_next = cur_end + cur_hdr->cr + 1;
5135
5136 /* Now we have one header between cur_ptr and cur_end,
5137 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005138 */
5139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005140 /* The annoying part is that pattern matching needs
5141 * that we modify the contents to null-terminate all
5142 * strings before testing them.
5143 */
5144
5145 term = *cur_end;
5146 *cur_end = '\0';
5147
5148 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5149 switch (exp->action) {
5150 case ACT_SETBE:
5151 /* It is not possible to jump a second time.
5152 * FIXME: should we return an HTTP/500 here so that
5153 * the admin knows there's a problem ?
5154 */
5155 if (t->be != t->fe)
5156 break;
5157
5158 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005159 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005160 last_hdr = 1;
5161 break;
5162
5163 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005164 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005165 last_hdr = 1;
5166 break;
5167
5168 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005169 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005170 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005171
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005172 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005173 if (t->listener->counters)
5174 t->listener->counters->denied_resp++;
5175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005176 break;
5177
5178 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005179 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005180 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005181
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005182 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005183 if (t->listener->counters)
5184 t->listener->counters->denied_resp++;
5185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005186 break;
5187
5188 case ACT_REPLACE:
5189 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5190 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5191 /* FIXME: if the user adds a newline in the replacement, the
5192 * index will not be recalculated for now, and the new line
5193 * will not be counted as a new header.
5194 */
5195
5196 cur_end += delta;
5197 cur_next += delta;
5198 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005199 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005200 break;
5201
5202 case ACT_REMOVE:
5203 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5204 cur_next += delta;
5205
Willy Tarreaufa355d42009-11-29 18:12:29 +01005206 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005207 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5208 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005209 cur_hdr->len = 0;
5210 cur_end = NULL; /* null-term has been rewritten */
5211 break;
5212
5213 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005215 if (cur_end)
5216 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005217
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005218 /* keep the link from this header to next one in case of later
5219 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005220 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005221 old_idx = cur_idx;
5222 }
5223 return 0;
5224}
5225
5226
5227/* Apply the filter to the request line.
5228 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5229 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005230 * Since it can manage the switch to another backend, it updates the per-proxy
5231 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005232 */
5233int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5234{
5235 char term;
5236 char *cur_ptr, *cur_end;
5237 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005238 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005239 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005240
Willy Tarreau58f10d72006-12-04 02:26:12 +01005241
Willy Tarreau3d300592007-03-18 18:34:41 +01005242 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005243 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005244 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005245 (exp->action == ACT_ALLOW ||
5246 exp->action == ACT_DENY ||
5247 exp->action == ACT_TARPIT))
5248 return 0;
5249 else if (exp->action == ACT_REMOVE)
5250 return 0;
5251
5252 done = 0;
5253
Willy Tarreau962c3f42010-01-10 00:15:35 +01005254 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005255 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005256
5257 /* Now we have the request line between cur_ptr and cur_end */
5258
5259 /* The annoying part is that pattern matching needs
5260 * that we modify the contents to null-terminate all
5261 * strings before testing them.
5262 */
5263
5264 term = *cur_end;
5265 *cur_end = '\0';
5266
5267 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5268 switch (exp->action) {
5269 case ACT_SETBE:
5270 /* It is not possible to jump a second time.
5271 * FIXME: should we return an HTTP/500 here so that
5272 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005273 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005274 if (t->be != t->fe)
5275 break;
5276
5277 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005278 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005279 done = 1;
5280 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005282 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005283 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005284 done = 1;
5285 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005287 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005288 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005289
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005290 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005291 if (t->listener->counters)
5292 t->listener->counters->denied_resp++;
5293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005294 done = 1;
5295 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005297 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005298 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005299
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005300 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005301 if (t->listener->counters)
5302 t->listener->counters->denied_resp++;
5303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005304 done = 1;
5305 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005306
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005307 case ACT_REPLACE:
5308 *cur_end = term; /* restore the string terminator */
5309 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5310 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5311 /* FIXME: if the user adds a newline in the replacement, the
5312 * index will not be recalculated for now, and the new line
5313 * will not be counted as a new header.
5314 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005315
Willy Tarreaufa355d42009-11-29 18:12:29 +01005316 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005317 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005318 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005319 HTTP_MSG_RQMETH,
5320 cur_ptr, cur_end + 1,
5321 NULL, NULL);
5322 if (unlikely(!cur_end))
5323 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005325 /* we have a full request and we know that we have either a CR
5326 * or an LF at <ptr>.
5327 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005328 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5329 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005330 /* there is no point trying this regex on headers */
5331 return 1;
5332 }
5333 }
5334 *cur_end = term; /* restore the string terminator */
5335 return done;
5336}
Willy Tarreau97de6242006-12-27 17:18:38 +01005337
Willy Tarreau58f10d72006-12-04 02:26:12 +01005338
Willy Tarreau58f10d72006-12-04 02:26:12 +01005339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005340/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005341 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005342 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005343 * unparsable request. Since it can manage the switch to another backend, it
5344 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005345 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005346int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005347{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005348 struct http_txn *txn = &s->txn;
5349 struct hdr_exp *exp;
5350
5351 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005352 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005354 /*
5355 * The interleaving of transformations and verdicts
5356 * makes it difficult to decide to continue or stop
5357 * the evaluation.
5358 */
5359
Willy Tarreau6c123b12010-01-28 20:22:06 +01005360 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5361 break;
5362
Willy Tarreau3d300592007-03-18 18:34:41 +01005363 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005364 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005365 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005366 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005367
5368 /* if this filter had a condition, evaluate it now and skip to
5369 * next filter if the condition does not match.
5370 */
5371 if (exp->cond) {
5372 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5373 ret = acl_pass(ret);
5374 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5375 ret = !ret;
5376
5377 if (!ret)
5378 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005379 }
5380
5381 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005382 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005383 if (unlikely(ret < 0))
5384 return -1;
5385
5386 if (likely(ret == 0)) {
5387 /* The filter did not match the request, it can be
5388 * iterated through all headers.
5389 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005390 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005391 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005392 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005393 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005394}
5395
5396
Willy Tarreaua15645d2007-03-18 16:22:39 +01005397
Willy Tarreau58f10d72006-12-04 02:26:12 +01005398/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005399 * Try to retrieve the server associated to the appsession.
5400 * If the server is found, it's assigned to the session.
5401 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005402void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005403 struct http_txn *txn = &t->txn;
5404 appsess *asession = NULL;
5405 char *sessid_temp = NULL;
5406
Cyril Bontéb21570a2009-11-29 20:04:48 +01005407 if (len > t->be->appsession_len) {
5408 len = t->be->appsession_len;
5409 }
5410
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005411 if (t->be->options2 & PR_O2_AS_REQL) {
5412 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005413 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005414 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005415 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005416 }
5417
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005418 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005419 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5420 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5421 return;
5422 }
5423
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005424 memcpy(txn->sessid, buf, len);
5425 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005426 }
5427
5428 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5429 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5430 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5431 return;
5432 }
5433
Cyril Bontéb21570a2009-11-29 20:04:48 +01005434 memcpy(sessid_temp, buf, len);
5435 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005436
5437 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5438 /* free previously allocated memory */
5439 pool_free2(apools.sessid, sessid_temp);
5440
5441 if (asession != NULL) {
5442 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5443 if (!(t->be->options2 & PR_O2_AS_REQL))
5444 asession->request_count++;
5445
5446 if (asession->serverid != NULL) {
5447 struct server *srv = t->be->srv;
5448 while (srv) {
5449 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005450 if ((srv->state & SRV_RUNNING) ||
5451 (t->be->options & PR_O_PERSIST) ||
5452 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005453 /* we found the server and it's usable */
5454 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005455 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005456 t->flags |= SN_DIRECT | SN_ASSIGNED;
5457 t->srv = srv;
5458 break;
5459 } else {
5460 txn->flags &= ~TX_CK_MASK;
5461 txn->flags |= TX_CK_DOWN;
5462 }
5463 }
5464 srv = srv->next;
5465 }
5466 }
5467 }
5468}
5469
5470/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005471 * Manage client-side cookie. It can impact performance by about 2% so it is
5472 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005473 */
5474void manage_client_side_cookies(struct session *t, struct buffer *req)
5475{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005476 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005477 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005478 char *del_colon, *del_cookie, *colon;
5479 int app_cookies;
5480
Willy Tarreau58f10d72006-12-04 02:26:12 +01005481 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005482 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005483
Willy Tarreau2a324282006-12-05 00:05:46 +01005484 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005485 * we start with the start line.
5486 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005487 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005488 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005489
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005490 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005491 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005492 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005493
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005494 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005495 cur_ptr = cur_next;
5496 cur_end = cur_ptr + cur_hdr->len;
5497 cur_next = cur_end + cur_hdr->cr + 1;
5498
5499 /* We have one full header between cur_ptr and cur_end, and the
5500 * next header starts at cur_next. We're only interested in
5501 * "Cookie:" headers.
5502 */
5503
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005504 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5505 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005506 old_idx = cur_idx;
5507 continue;
5508 }
5509
5510 /* Now look for cookies. Conforming to RFC2109, we have to support
5511 * attributes whose name begin with a '$', and associate them with
5512 * the right cookie, if we want to delete this cookie.
5513 * So there are 3 cases for each cookie read :
5514 * 1) it's a special attribute, beginning with a '$' : ignore it.
5515 * 2) it's a server id cookie that we *MAY* want to delete : save
5516 * some pointers on it (last semi-colon, beginning of cookie...)
5517 * 3) it's an application cookie : we *MAY* have to delete a previous
5518 * "special" cookie.
5519 * At the end of loop, if a "special" cookie remains, we may have to
5520 * remove it. If no application cookie persists in the header, we
5521 * *MUST* delete it
5522 */
5523
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005524 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005525
Willy Tarreau58f10d72006-12-04 02:26:12 +01005526 /* del_cookie == NULL => nothing to be deleted */
5527 del_colon = del_cookie = NULL;
5528 app_cookies = 0;
5529
5530 while (p1 < cur_end) {
5531 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005532 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005533 while (p1 < cur_end) {
5534 if (*p1 == ';' || *p1 == ',')
5535 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005536 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005537 break;
5538 p1++;
5539 }
5540
5541 if (p1 == cur_end)
5542 break;
5543
5544 /* p1 is at the beginning of the cookie name */
5545 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005546 while (p2 < cur_end && *p2 != '=') {
5547 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5548 /* oops, the cookie name was truncated, resync */
5549 p1 = p2;
5550 goto resync_name;
5551 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005552 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005553 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005554
5555 if (p2 == cur_end)
5556 break;
5557
5558 p3 = p2 + 1; /* skips the '=' sign */
5559 if (p3 == cur_end)
5560 break;
5561
Willy Tarreau305ae852010-01-03 19:45:54 +01005562 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5563 p5 = p4 = p3;
5564 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5565 if (!isspace((unsigned char)*p5))
5566 p4 = p5 + 1;
5567 p5++;
5568 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005569
5570 /* here, we have the cookie name between p1 and p2,
5571 * and its value between p3 and p4.
5572 * we can process it :
5573 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005574 * Cookie: NAME=VALUE ;
5575 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005576 * | || || +--> p4
5577 * | || |+-------> p3
5578 * | || +--------> p2
5579 * | |+------------> p1
5580 * | +-------------> colon
5581 * +--------------------> cur_ptr
5582 */
5583
5584 if (*p1 == '$') {
5585 /* skip this one */
5586 }
5587 else {
5588 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005589 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005590 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005591 (p4 - p1 >= t->fe->capture_namelen) &&
5592 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005593 int log_len = p4 - p1;
5594
Willy Tarreau086b3b42007-05-13 21:45:51 +02005595 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005596 Alert("HTTP logging : out of memory.\n");
5597 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005598 if (log_len > t->fe->capture_len)
5599 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005600 memcpy(txn->cli_cookie, p1, log_len);
5601 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005602 }
5603 }
5604
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005605 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5606 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005607 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005608 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005609 char *delim;
5610
5611 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5612 * have the server ID betweek p3 and delim, and the original cookie between
5613 * delim+1 and p4. Otherwise, delim==p4 :
5614 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005615 * Cookie: NAME=SRV~VALUE ;
5616 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005617 * | || || | +--> p4
5618 * | || || +--------> delim
5619 * | || |+-----------> p3
5620 * | || +------------> p2
5621 * | |+----------------> p1
5622 * | +-----------------> colon
5623 * +------------------------> cur_ptr
5624 */
5625
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005626 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005627 for (delim = p3; delim < p4; delim++)
5628 if (*delim == COOKIE_DELIM)
5629 break;
5630 }
5631 else
5632 delim = p4;
5633
5634
5635 /* Here, we'll look for the first running server which supports the cookie.
5636 * This allows to share a same cookie between several servers, for example
5637 * to dedicate backup servers to specific servers only.
5638 * However, to prevent clients from sticking to cookie-less backup server
5639 * when they have incidentely learned an empty cookie, we simply ignore
5640 * empty cookies and mark them as invalid.
5641 */
5642 if (delim == p3)
5643 srv = NULL;
5644
5645 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005646 if (srv->cookie && (srv->cklen == delim - p3) &&
5647 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005648 if ((srv->state & SRV_RUNNING) ||
5649 (t->be->options & PR_O_PERSIST) ||
5650 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005651 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005652 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005653 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005654 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005655 t->srv = srv;
5656 break;
5657 } else {
5658 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005659 txn->flags &= ~TX_CK_MASK;
5660 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005661 }
5662 }
5663 srv = srv->next;
5664 }
5665
Willy Tarreau3d300592007-03-18 18:34:41 +01005666 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005667 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005668 txn->flags &= ~TX_CK_MASK;
5669 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005670 }
5671
5672 /* depending on the cookie mode, we may have to either :
5673 * - delete the complete cookie if we're in insert+indirect mode, so that
5674 * the server never sees it ;
5675 * - remove the server id from the cookie value, and tag the cookie as an
5676 * application cookie so that it does not get accidentely removed later,
5677 * if we're in cookie prefix mode
5678 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005679 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005680 int delta; /* negative */
5681
5682 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5683 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005684 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005685 cur_end += delta;
5686 cur_next += delta;
5687 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005688 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005689
5690 del_cookie = del_colon = NULL;
5691 app_cookies++; /* protect the header from deletion */
5692 }
5693 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005694 (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 +01005695 del_cookie = p1;
5696 del_colon = colon;
5697 }
5698 } else {
5699 /* now we know that we must keep this cookie since it's
5700 * not ours. But if we wanted to delete our cookie
5701 * earlier, we cannot remove the complete header, but we
5702 * can remove the previous block itself.
5703 */
5704 app_cookies++;
5705
5706 if (del_cookie != NULL) {
5707 int delta; /* negative */
5708
5709 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5710 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005711 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005712 cur_end += delta;
5713 cur_next += delta;
5714 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005715 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005716 del_cookie = del_colon = NULL;
5717 }
5718 }
5719
Cyril Bontéb21570a2009-11-29 20:04:48 +01005720 if (t->be->appsession_name != NULL) {
5721 int cmp_len, value_len;
5722 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005723
Cyril Bontéb21570a2009-11-29 20:04:48 +01005724 if (t->be->options2 & PR_O2_AS_PFX) {
5725 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5726 value_begin = p1 + t->be->appsession_name_len;
5727 value_len = p4 - p1 - t->be->appsession_name_len;
5728 } else {
5729 cmp_len = p2 - p1;
5730 value_begin = p3;
5731 value_len = p4 - p3;
5732 }
5733
5734 /* let's see if the cookie is our appcookie */
5735 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5736 /* Cool... it's the right one */
5737 manage_client_side_appsession(t, value_begin, value_len);
5738 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005739#if defined(DEBUG_HASH)
5740 Alert("manage_client_side_cookies\n");
5741 appsession_hash_dump(&(t->be->htbl_proxy));
5742#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005743 }/* end if ((t->proxy->appsession_name != NULL) ... */
5744 }
5745
5746 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005747 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005748 } /* while (p1 < cur_end) */
5749
5750 /* There's no more cookie on this line.
5751 * We may have marked the last one(s) for deletion.
5752 * We must do this now in two ways :
5753 * - if there is no app cookie, we simply delete the header ;
5754 * - if there are app cookies, we must delete the end of the
5755 * string properly, including the colon/semi-colon before
5756 * the cookie name.
5757 */
5758 if (del_cookie != NULL) {
5759 int delta;
5760 if (app_cookies) {
5761 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5762 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005763 cur_hdr->len += delta;
5764 } else {
5765 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005766
5767 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005768 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5769 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005770 cur_hdr->len = 0;
5771 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005772 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005773 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005774 }
5775
5776 /* keep the link from this header to next one */
5777 old_idx = cur_idx;
5778 } /* end of cookie processing on this header */
5779}
5780
5781
Willy Tarreaua15645d2007-03-18 16:22:39 +01005782/* Iterate the same filter through all response headers contained in <rtr>.
5783 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5784 */
5785int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5786{
5787 char term;
5788 char *cur_ptr, *cur_end, *cur_next;
5789 int cur_idx, old_idx, last_hdr;
5790 struct http_txn *txn = &t->txn;
5791 struct hdr_idx_elem *cur_hdr;
5792 int len, delta;
5793
5794 last_hdr = 0;
5795
Willy Tarreau962c3f42010-01-10 00:15:35 +01005796 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005797 old_idx = 0;
5798
5799 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005800 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005801 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005802 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005803 (exp->action == ACT_ALLOW ||
5804 exp->action == ACT_DENY))
5805 return 0;
5806
5807 cur_idx = txn->hdr_idx.v[old_idx].next;
5808 if (!cur_idx)
5809 break;
5810
5811 cur_hdr = &txn->hdr_idx.v[cur_idx];
5812 cur_ptr = cur_next;
5813 cur_end = cur_ptr + cur_hdr->len;
5814 cur_next = cur_end + cur_hdr->cr + 1;
5815
5816 /* Now we have one header between cur_ptr and cur_end,
5817 * and the next header starts at cur_next.
5818 */
5819
5820 /* The annoying part is that pattern matching needs
5821 * that we modify the contents to null-terminate all
5822 * strings before testing them.
5823 */
5824
5825 term = *cur_end;
5826 *cur_end = '\0';
5827
5828 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5829 switch (exp->action) {
5830 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005831 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005832 last_hdr = 1;
5833 break;
5834
5835 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005836 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005837 last_hdr = 1;
5838 break;
5839
5840 case ACT_REPLACE:
5841 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5842 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5843 /* FIXME: if the user adds a newline in the replacement, the
5844 * index will not be recalculated for now, and the new line
5845 * will not be counted as a new header.
5846 */
5847
5848 cur_end += delta;
5849 cur_next += delta;
5850 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005851 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005852 break;
5853
5854 case ACT_REMOVE:
5855 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5856 cur_next += delta;
5857
Willy Tarreaufa355d42009-11-29 18:12:29 +01005858 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005859 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5860 txn->hdr_idx.used--;
5861 cur_hdr->len = 0;
5862 cur_end = NULL; /* null-term has been rewritten */
5863 break;
5864
5865 }
5866 }
5867 if (cur_end)
5868 *cur_end = term; /* restore the string terminator */
5869
5870 /* keep the link from this header to next one in case of later
5871 * removal of next header.
5872 */
5873 old_idx = cur_idx;
5874 }
5875 return 0;
5876}
5877
5878
5879/* Apply the filter to the status line in the response buffer <rtr>.
5880 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5881 * or -1 if a replacement resulted in an invalid status line.
5882 */
5883int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5884{
5885 char term;
5886 char *cur_ptr, *cur_end;
5887 int done;
5888 struct http_txn *txn = &t->txn;
5889 int len, delta;
5890
5891
Willy Tarreau3d300592007-03-18 18:34:41 +01005892 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005893 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005894 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005895 (exp->action == ACT_ALLOW ||
5896 exp->action == ACT_DENY))
5897 return 0;
5898 else if (exp->action == ACT_REMOVE)
5899 return 0;
5900
5901 done = 0;
5902
Willy Tarreau962c3f42010-01-10 00:15:35 +01005903 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005904 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5905
5906 /* Now we have the status line between cur_ptr and cur_end */
5907
5908 /* The annoying part is that pattern matching needs
5909 * that we modify the contents to null-terminate all
5910 * strings before testing them.
5911 */
5912
5913 term = *cur_end;
5914 *cur_end = '\0';
5915
5916 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5917 switch (exp->action) {
5918 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005919 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005920 done = 1;
5921 break;
5922
5923 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005924 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005925 done = 1;
5926 break;
5927
5928 case ACT_REPLACE:
5929 *cur_end = term; /* restore the string terminator */
5930 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5931 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5932 /* FIXME: if the user adds a newline in the replacement, the
5933 * index will not be recalculated for now, and the new line
5934 * will not be counted as a new header.
5935 */
5936
Willy Tarreaufa355d42009-11-29 18:12:29 +01005937 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005938 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005940 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005941 cur_ptr, cur_end + 1,
5942 NULL, NULL);
5943 if (unlikely(!cur_end))
5944 return -1;
5945
5946 /* we have a full respnse and we know that we have either a CR
5947 * or an LF at <ptr>.
5948 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005949 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005950 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5951 /* there is no point trying this regex on headers */
5952 return 1;
5953 }
5954 }
5955 *cur_end = term; /* restore the string terminator */
5956 return done;
5957}
5958
5959
5960
5961/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005962 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005963 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5964 * unparsable response.
5965 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005966int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005967{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005968 struct http_txn *txn = &s->txn;
5969 struct hdr_exp *exp;
5970
5971 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005972 int ret;
5973
5974 /*
5975 * The interleaving of transformations and verdicts
5976 * makes it difficult to decide to continue or stop
5977 * the evaluation.
5978 */
5979
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005980 if (txn->flags & TX_SVDENY)
5981 break;
5982
Willy Tarreau3d300592007-03-18 18:34:41 +01005983 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005984 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5985 exp->action == ACT_PASS)) {
5986 exp = exp->next;
5987 continue;
5988 }
5989
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005990 /* if this filter had a condition, evaluate it now and skip to
5991 * next filter if the condition does not match.
5992 */
5993 if (exp->cond) {
5994 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
5995 ret = acl_pass(ret);
5996 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5997 ret = !ret;
5998 if (!ret)
5999 continue;
6000 }
6001
Willy Tarreaua15645d2007-03-18 16:22:39 +01006002 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006003 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006004 if (unlikely(ret < 0))
6005 return -1;
6006
6007 if (likely(ret == 0)) {
6008 /* The filter did not match the response, it can be
6009 * iterated through all headers.
6010 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006011 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006013 }
6014 return 0;
6015}
6016
6017
6018
6019/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006020 * Manage server-side cookies. It can impact performance by about 2% so it is
6021 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006022 */
6023void manage_server_side_cookies(struct session *t, struct buffer *rtr)
6024{
6025 struct http_txn *txn = &t->txn;
6026 char *p1, *p2, *p3, *p4;
6027
Willy Tarreaua15645d2007-03-18 16:22:39 +01006028 char *cur_ptr, *cur_end, *cur_next;
6029 int cur_idx, old_idx, delta;
6030
Willy Tarreaua15645d2007-03-18 16:22:39 +01006031 /* Iterate through the headers.
6032 * we start with the start line.
6033 */
6034 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006035 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006036
6037 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6038 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006039 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006040
6041 cur_hdr = &txn->hdr_idx.v[cur_idx];
6042 cur_ptr = cur_next;
6043 cur_end = cur_ptr + cur_hdr->len;
6044 cur_next = cur_end + cur_hdr->cr + 1;
6045
6046 /* We have one full header between cur_ptr and cur_end, and the
6047 * next header starts at cur_next. We're only interested in
6048 * "Cookie:" headers.
6049 */
6050
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006051 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
6052 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006053 old_idx = cur_idx;
6054 continue;
6055 }
6056
6057 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01006058 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006059
6060
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006061 /* maybe we only wanted to see if there was a set-cookie. Note that
6062 * the cookie capture is declared in the fronend.
6063 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006064 if (t->be->cookie_name == NULL &&
6065 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006066 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006067 return;
6068
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006069 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006070
6071 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006072 if (p1 == cur_end || *p1 == ';') /* end of cookie */
6073 break;
6074
6075 /* p1 is at the beginning of the cookie name */
6076 p2 = p1;
6077
6078 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
6079 p2++;
6080
6081 if (p2 == cur_end || *p2 == ';') /* next cookie */
6082 break;
6083
6084 p3 = p2 + 1; /* skip the '=' sign */
6085 if (p3 == cur_end)
6086 break;
6087
6088 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006089 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01006090 p4++;
6091
6092 /* here, we have the cookie name between p1 and p2,
6093 * and its value between p3 and p4.
6094 * we can process it.
6095 */
6096
6097 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006098 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006099 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006100 (p4 - p1 >= t->fe->capture_namelen) &&
6101 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006102 int log_len = p4 - p1;
6103
Willy Tarreau086b3b42007-05-13 21:45:51 +02006104 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006105 Alert("HTTP logging : out of memory.\n");
6106 }
6107
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006108 if (log_len > t->fe->capture_len)
6109 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006110 memcpy(txn->srv_cookie, p1, log_len);
6111 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006112 }
6113
6114 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006115 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6116 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006117 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01006118 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006119
6120 /* If the cookie is in insert mode on a known server, we'll delete
6121 * this occurrence because we'll insert another one later.
6122 * We'll delete it too if the "indirect" option is set and we're in
6123 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006124 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
6125 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006126 /* this header must be deleted */
6127 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6128 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6129 txn->hdr_idx.used--;
6130 cur_hdr->len = 0;
6131 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006132 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006133
Willy Tarreau3d300592007-03-18 18:34:41 +01006134 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006135 }
6136 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006137 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006138 /* replace bytes p3->p4 with the cookie name associated
6139 * with this server since we know it.
6140 */
6141 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
6142 cur_hdr->len += delta;
6143 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006144 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006145
Willy Tarreau3d300592007-03-18 18:34:41 +01006146 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006147 }
6148 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006149 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006150 /* insert the cookie name associated with this server
6151 * before existing cookie, and insert a delimitor between them..
6152 */
6153 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6154 cur_hdr->len += delta;
6155 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006156 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006157
6158 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006159 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006160 }
6161 }
6162 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006163 else if (t->be->appsession_name != NULL) {
6164 int cmp_len, value_len;
6165 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006166
Cyril Bontéb21570a2009-11-29 20:04:48 +01006167 if (t->be->options2 & PR_O2_AS_PFX) {
6168 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6169 value_begin = p1 + t->be->appsession_name_len;
6170 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6171 } else {
6172 cmp_len = p2 - p1;
6173 value_begin = p3;
6174 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006175 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006176
6177 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
6178 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006179 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006180 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006181 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006182 }
6183 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006184 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006185 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6186 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6187 return;
6188 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006189 memcpy(txn->sessid, value_begin, value_len);
6190 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006191 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006192 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006193 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6194 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006195 /* keep the link from this header to next one */
6196 old_idx = cur_idx;
6197 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006198
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006199 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006200 appsess *asession = NULL;
6201 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006202 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006203 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006204 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006205 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6206 Alert("Not enough Memory process_srv():asession:calloc().\n");
6207 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6208 return;
6209 }
6210 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6211 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6212 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006213 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006214 return;
6215 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006216 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006217 asession->sessid[t->be->appsession_len] = 0;
6218
Willy Tarreau1fac7532010-01-09 19:23:06 +01006219 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006220 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6221 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6222 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006223 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006224 return;
6225 }
6226 asession->serverid[0] = '\0';
6227 memcpy(asession->serverid, t->srv->id, server_id_len);
6228
6229 asession->request_count = 0;
6230 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6231 }
6232
6233 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6234 asession->request_count++;
6235 }
6236
6237#if defined(DEBUG_HASH)
6238 Alert("manage_server_side_cookies\n");
6239 appsession_hash_dump(&(t->be->htbl_proxy));
6240#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006241}
6242
6243
6244
6245/*
6246 * Check if response is cacheable or not. Updates t->flags.
6247 */
6248void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6249{
6250 struct http_txn *txn = &t->txn;
6251 char *p1, *p2;
6252
6253 char *cur_ptr, *cur_end, *cur_next;
6254 int cur_idx;
6255
Willy Tarreau5df51872007-11-25 16:20:08 +01006256 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006257 return;
6258
6259 /* Iterate through the headers.
6260 * we start with the start line.
6261 */
6262 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006263 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006264
6265 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6266 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006267 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006268
6269 cur_hdr = &txn->hdr_idx.v[cur_idx];
6270 cur_ptr = cur_next;
6271 cur_end = cur_ptr + cur_hdr->len;
6272 cur_next = cur_end + cur_hdr->cr + 1;
6273
6274 /* We have one full header between cur_ptr and cur_end, and the
6275 * next header starts at cur_next. We're only interested in
6276 * "Cookie:" headers.
6277 */
6278
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006279 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6280 if (val) {
6281 if ((cur_end - (cur_ptr + val) >= 8) &&
6282 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6283 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6284 return;
6285 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006286 }
6287
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006288 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6289 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006290 continue;
6291
6292 /* OK, right now we know we have a cache-control header at cur_ptr */
6293
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006294 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006295
6296 if (p1 >= cur_end) /* no more info */
6297 continue;
6298
6299 /* p1 is at the beginning of the value */
6300 p2 = p1;
6301
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006302 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006303 p2++;
6304
6305 /* we have a complete value between p1 and p2 */
6306 if (p2 < cur_end && *p2 == '=') {
6307 /* we have something of the form no-cache="set-cookie" */
6308 if ((cur_end - p1 >= 21) &&
6309 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6310 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006311 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006312 continue;
6313 }
6314
6315 /* OK, so we know that either p2 points to the end of string or to a comma */
6316 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6317 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6318 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6319 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006320 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006321 return;
6322 }
6323
6324 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006325 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006326 continue;
6327 }
6328 }
6329}
6330
6331
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332/*
6333 * Try to retrieve a known appsession in the URI, then the associated server.
6334 * If the server is found, it's assigned to the session.
6335 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006336void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006338 char *end_params, *first_param, *cur_param, *next_param;
6339 char separator;
6340 int value_len;
6341
6342 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006344 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006345 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348
Cyril Bontéb21570a2009-11-29 20:04:48 +01006349 first_param = NULL;
6350 switch (mode) {
6351 case PR_O2_AS_M_PP:
6352 first_param = memchr(begin, ';', len);
6353 break;
6354 case PR_O2_AS_M_QS:
6355 first_param = memchr(begin, '?', len);
6356 break;
6357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358
Cyril Bontéb21570a2009-11-29 20:04:48 +01006359 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006361 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006362
Cyril Bontéb21570a2009-11-29 20:04:48 +01006363 switch (mode) {
6364 case PR_O2_AS_M_PP:
6365 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6366 end_params = (char *) begin + len;
6367 }
6368 separator = ';';
6369 break;
6370 case PR_O2_AS_M_QS:
6371 end_params = (char *) begin + len;
6372 separator = '&';
6373 break;
6374 default:
6375 /* unknown mode, shouldn't happen */
6376 return;
6377 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378
Cyril Bontéb21570a2009-11-29 20:04:48 +01006379 cur_param = next_param = end_params;
6380 while (cur_param > first_param) {
6381 cur_param--;
6382 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6383 /* let's see if this is the appsession parameter */
6384 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6385 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6386 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6387 /* Cool... it's the right one */
6388 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6389 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6390 if (value_len > 0) {
6391 manage_client_side_appsession(t, cur_param, value_len);
6392 }
6393 break;
6394 }
6395 next_param = cur_param;
6396 }
6397 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006399 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006400 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006402}
6403
Willy Tarreaub2513902006-12-17 14:52:38 +01006404/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006405 * In a GET or HEAD request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006406 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01006407 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006408 * It is assumed that the request is either a HEAD or GET and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006409 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006410 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006411 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01006412 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006413int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01006414{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006415 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006416 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006417 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006418
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006419 if (!uri_auth)
6420 return 0;
6421
6422 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)
6423 return 0;
6424
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006425 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006427 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006428 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006429 return 0;
6430
Willy Tarreau962c3f42010-01-10 00:15:35 +01006431 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006433 /* the URI is in h */
6434 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006435 return 0;
6436
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006437 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006438 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006439 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006440 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006441 break;
6442 }
6443 h++;
6444 }
6445
6446 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006447 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6448 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006449 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006450 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006451 break;
6452 }
6453 h++;
6454 }
6455 }
6456
Willy Tarreau962c3f42010-01-10 00:15:35 +01006457 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6458 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006459 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006460 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006461 break;
6462 }
6463 h++;
6464 }
6465
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006466 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6467
Willy Tarreaub2513902006-12-17 14:52:38 +01006468 return 1;
6469}
6470
Willy Tarreau4076a152009-04-02 15:18:36 +02006471/*
6472 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006473 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6474 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006475 */
6476void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6477 struct buffer *buf, struct http_msg *msg,
6478 struct proxy *other_end)
6479{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006480 es->len = buf->r - (buf->data + msg->som);
6481 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006482 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006483 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006484 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006485 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006486 es->when = date; // user-visible date
6487 es->sid = s->uniq_id;
6488 es->srv = s->srv;
6489 es->oe = other_end;
6490 es->src = s->cli_addr;
6491}
Willy Tarreaub2513902006-12-17 14:52:38 +01006492
Willy Tarreaubaaee002006-06-26 02:48:02 +02006493/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006494 * Print a debug line with a header
6495 */
6496void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6497{
6498 int len, max;
6499 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006500 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006501 max = end - start;
6502 UBOUND(max, sizeof(trash) - len - 1);
6503 len += strlcpy2(trash + len, start, max + 1);
6504 trash[len++] = '\n';
6505 write(1, trash, len);
6506}
6507
Willy Tarreau0937bc42009-12-22 15:03:09 +01006508/*
6509 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6510 * the required fields are properly allocated and that we only need to (re)init
6511 * them. This should be used before processing any new request.
6512 */
6513void http_init_txn(struct session *s)
6514{
6515 struct http_txn *txn = &s->txn;
6516 struct proxy *fe = s->fe;
6517
6518 txn->flags = 0;
6519 txn->status = -1;
6520
6521 txn->req.sol = txn->req.eol = NULL;
6522 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6523 txn->rsp.sol = txn->rsp.eol = NULL;
6524 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6525 txn->req.hdr_content_len = 0LL;
6526 txn->rsp.hdr_content_len = 0LL;
6527 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6528 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006529
6530 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006531
6532 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6533 if (fe->options2 & PR_O2_REQBUG_OK)
6534 txn->req.err_pos = -1; /* let buggy requests pass */
6535
Willy Tarreau46023632010-01-07 22:51:47 +01006536 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006537 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6538
Willy Tarreau46023632010-01-07 22:51:47 +01006539 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006540 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6541
6542 if (txn->hdr_idx.v)
6543 hdr_idx_init(&txn->hdr_idx);
6544}
6545
6546/* to be used at the end of a transaction */
6547void http_end_txn(struct session *s)
6548{
6549 struct http_txn *txn = &s->txn;
6550
6551 /* these ones will have been dynamically allocated */
6552 pool_free2(pool2_requri, txn->uri);
6553 pool_free2(pool2_capture, txn->cli_cookie);
6554 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006555 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006556
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006557 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006558 txn->uri = NULL;
6559 txn->srv_cookie = NULL;
6560 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006561
6562 if (txn->req.cap) {
6563 struct cap_hdr *h;
6564 for (h = s->fe->req_cap; h; h = h->next)
6565 pool_free2(h->pool, txn->req.cap[h->index]);
6566 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6567 }
6568
6569 if (txn->rsp.cap) {
6570 struct cap_hdr *h;
6571 for (h = s->fe->rsp_cap; h; h = h->next)
6572 pool_free2(h->pool, txn->rsp.cap[h->index]);
6573 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6574 }
6575
Willy Tarreau0937bc42009-12-22 15:03:09 +01006576}
6577
6578/* to be used at the end of a transaction to prepare a new one */
6579void http_reset_txn(struct session *s)
6580{
6581 http_end_txn(s);
6582 http_init_txn(s);
6583
6584 s->be = s->fe;
6585 s->req->analysers = s->listener->analysers;
6586 s->logs.logwait = s->fe->to_log;
6587 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006588 /* re-init store persistence */
6589 s->store_count = 0;
6590
Willy Tarreau0937bc42009-12-22 15:03:09 +01006591 s->pend_pos = NULL;
6592 s->conn_retries = s->be->conn_retries;
6593
6594 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6595
Willy Tarreau739cfba2010-01-25 23:11:14 +01006596 /* We must trim any excess data from the response buffer, because we
6597 * may have blocked an invalid response from a server that we don't
6598 * want to accidentely forward once we disable the analysers, nor do
6599 * we want those data to come along with next response. A typical
6600 * example of such data would be from a buggy server responding to
6601 * a HEAD with some data, or sending more than the advertised
6602 * content-length.
6603 */
6604 if (unlikely(s->rep->l > s->rep->send_max)) {
6605 s->rep->l = s->rep->send_max;
6606 s->rep->r = s->rep->w + s->rep->l;
6607 if (s->rep->r >= s->rep->data + s->rep->size)
6608 s->rep->r -= s->rep->size;
6609 }
6610
Willy Tarreau0937bc42009-12-22 15:03:09 +01006611 s->req->rto = s->fe->timeout.client;
6612 s->req->wto = s->be->timeout.server;
6613 s->req->cto = s->be->timeout.connect;
6614
6615 s->rep->rto = s->be->timeout.server;
6616 s->rep->wto = s->fe->timeout.client;
6617 s->rep->cto = TICK_ETERNITY;
6618
6619 s->req->rex = TICK_ETERNITY;
6620 s->req->wex = TICK_ETERNITY;
6621 s->req->analyse_exp = TICK_ETERNITY;
6622 s->rep->rex = TICK_ETERNITY;
6623 s->rep->wex = TICK_ETERNITY;
6624 s->rep->analyse_exp = TICK_ETERNITY;
6625}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626
Willy Tarreau8797c062007-05-07 00:55:35 +02006627/************************************************************************/
6628/* The code below is dedicated to ACL parsing and matching */
6629/************************************************************************/
6630
6631
6632
6633
6634/* 1. Check on METHOD
6635 * We use the pre-parsed method if it is known, and store its number as an
6636 * integer. If it is unknown, we use the pointer and the length.
6637 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006638static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006639{
6640 int len, meth;
6641
Willy Tarreauae8b7962007-06-09 23:10:04 +02006642 len = strlen(*text);
6643 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006644
6645 pattern->val.i = meth;
6646 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006647 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006648 if (!pattern->ptr.str)
6649 return 0;
6650 pattern->len = len;
6651 }
6652 return 1;
6653}
6654
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006655static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006656acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6657 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006658{
6659 int meth;
6660 struct http_txn *txn = l7;
6661
Willy Tarreaub6866442008-07-14 23:54:42 +02006662 if (!txn)
6663 return 0;
6664
Willy Tarreau655dce92009-11-08 13:10:58 +01006665 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006666 return 0;
6667
Willy Tarreau8797c062007-05-07 00:55:35 +02006668 meth = txn->meth;
6669 test->i = meth;
6670 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006671 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6672 /* ensure the indexes are not affected */
6673 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006674 test->len = txn->req.sl.rq.m_l;
6675 test->ptr = txn->req.sol;
6676 }
6677 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6678 return 1;
6679}
6680
6681static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6682{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006683 int icase;
6684
Willy Tarreau8797c062007-05-07 00:55:35 +02006685 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006686 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006687
6688 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006689 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006690
6691 /* Other method, we must compare the strings */
6692 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006693 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006694
6695 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6696 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6697 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006698 return ACL_PAT_FAIL;
6699 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006700}
6701
6702/* 2. Check on Request/Status Version
6703 * We simply compare strings here.
6704 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006705static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006706{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006707 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006708 if (!pattern->ptr.str)
6709 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006710 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006711 return 1;
6712}
6713
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006714static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006715acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6716 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006717{
6718 struct http_txn *txn = l7;
6719 char *ptr;
6720 int len;
6721
Willy Tarreaub6866442008-07-14 23:54:42 +02006722 if (!txn)
6723 return 0;
6724
Willy Tarreau655dce92009-11-08 13:10:58 +01006725 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006726 return 0;
6727
Willy Tarreau8797c062007-05-07 00:55:35 +02006728 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006729 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006730
6731 while ((len-- > 0) && (*ptr++ != '/'));
6732 if (len <= 0)
6733 return 0;
6734
6735 test->ptr = ptr;
6736 test->len = len;
6737
6738 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6739 return 1;
6740}
6741
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006742static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006743acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6744 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006745{
6746 struct http_txn *txn = l7;
6747 char *ptr;
6748 int len;
6749
Willy Tarreaub6866442008-07-14 23:54:42 +02006750 if (!txn)
6751 return 0;
6752
Willy Tarreau655dce92009-11-08 13:10:58 +01006753 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006754 return 0;
6755
Willy Tarreau8797c062007-05-07 00:55:35 +02006756 len = txn->rsp.sl.st.v_l;
6757 ptr = txn->rsp.sol;
6758
6759 while ((len-- > 0) && (*ptr++ != '/'));
6760 if (len <= 0)
6761 return 0;
6762
6763 test->ptr = ptr;
6764 test->len = len;
6765
6766 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6767 return 1;
6768}
6769
6770/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006771static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006772acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6773 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006774{
6775 struct http_txn *txn = l7;
6776 char *ptr;
6777 int len;
6778
Willy Tarreaub6866442008-07-14 23:54:42 +02006779 if (!txn)
6780 return 0;
6781
Willy Tarreau655dce92009-11-08 13:10:58 +01006782 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006783 return 0;
6784
Willy Tarreau8797c062007-05-07 00:55:35 +02006785 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006786 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006787
6788 test->i = __strl2ui(ptr, len);
6789 test->flags = ACL_TEST_F_VOL_1ST;
6790 return 1;
6791}
6792
6793/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006794static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006795acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6796 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006797{
6798 struct http_txn *txn = l7;
6799
Willy Tarreaub6866442008-07-14 23:54:42 +02006800 if (!txn)
6801 return 0;
6802
Willy Tarreau655dce92009-11-08 13:10:58 +01006803 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006804 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006805
Willy Tarreauc11416f2007-06-17 16:58:38 +02006806 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6807 /* ensure the indexes are not affected */
6808 return 0;
6809
Willy Tarreau8797c062007-05-07 00:55:35 +02006810 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006811 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006812
Willy Tarreauf3d25982007-05-08 22:45:09 +02006813 /* we do not need to set READ_ONLY because the data is in a buffer */
6814 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006815 return 1;
6816}
6817
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006818static int
6819acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6820 struct acl_expr *expr, struct acl_test *test)
6821{
6822 struct http_txn *txn = l7;
6823
Willy Tarreaub6866442008-07-14 23:54:42 +02006824 if (!txn)
6825 return 0;
6826
Willy Tarreau655dce92009-11-08 13:10:58 +01006827 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006828 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006829
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006830 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6831 /* ensure the indexes are not affected */
6832 return 0;
6833
6834 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006835 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006836 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6837 test->i = AF_INET;
6838
6839 /*
6840 * If we are parsing url in frontend space, we prepare backend stage
6841 * to not parse again the same url ! optimization lazyness...
6842 */
6843 if (px->options & PR_O_HTTP_PROXY)
6844 l4->flags |= SN_ADDR_SET;
6845
6846 test->flags = ACL_TEST_F_READ_ONLY;
6847 return 1;
6848}
6849
6850static int
6851acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6852 struct acl_expr *expr, struct acl_test *test)
6853{
6854 struct http_txn *txn = l7;
6855
Willy Tarreaub6866442008-07-14 23:54:42 +02006856 if (!txn)
6857 return 0;
6858
Willy Tarreau655dce92009-11-08 13:10:58 +01006859 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006860 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006861
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006862 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6863 /* ensure the indexes are not affected */
6864 return 0;
6865
6866 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006867 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006868 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6869
6870 if (px->options & PR_O_HTTP_PROXY)
6871 l4->flags |= SN_ADDR_SET;
6872
6873 test->flags = ACL_TEST_F_READ_ONLY;
6874 return 1;
6875}
6876
Willy Tarreauc11416f2007-06-17 16:58:38 +02006877/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6878 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6879 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006880static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006881acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006882 struct acl_expr *expr, struct acl_test *test)
6883{
6884 struct http_txn *txn = l7;
6885 struct hdr_idx *idx = &txn->hdr_idx;
6886 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006887
Willy Tarreaub6866442008-07-14 23:54:42 +02006888 if (!txn)
6889 return 0;
6890
Willy Tarreau33a7e692007-06-10 19:45:56 +02006891 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6892 /* search for header from the beginning */
6893 ctx->idx = 0;
6894
Willy Tarreau33a7e692007-06-10 19:45:56 +02006895 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6896 test->flags |= ACL_TEST_F_FETCH_MORE;
6897 test->flags |= ACL_TEST_F_VOL_HDR;
6898 test->len = ctx->vlen;
6899 test->ptr = (char *)ctx->line + ctx->val;
6900 return 1;
6901 }
6902
6903 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6904 test->flags |= ACL_TEST_F_VOL_HDR;
6905 return 0;
6906}
6907
Willy Tarreau33a7e692007-06-10 19:45:56 +02006908static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006909acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6910 struct acl_expr *expr, struct acl_test *test)
6911{
6912 struct http_txn *txn = l7;
6913
Willy Tarreaub6866442008-07-14 23:54:42 +02006914 if (!txn)
6915 return 0;
6916
Willy Tarreau655dce92009-11-08 13:10:58 +01006917 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006918 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006919
Willy Tarreauc11416f2007-06-17 16:58:38 +02006920 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6921 /* ensure the indexes are not affected */
6922 return 0;
6923
6924 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6925}
6926
6927static int
6928acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6929 struct acl_expr *expr, struct acl_test *test)
6930{
6931 struct http_txn *txn = l7;
6932
Willy Tarreaub6866442008-07-14 23:54:42 +02006933 if (!txn)
6934 return 0;
6935
Willy Tarreau655dce92009-11-08 13:10:58 +01006936 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006937 return 0;
6938
6939 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6940}
6941
6942/* 6. Check on HTTP header count. The number of occurrences is returned.
6943 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6944 */
6945static int
6946acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006947 struct acl_expr *expr, struct acl_test *test)
6948{
6949 struct http_txn *txn = l7;
6950 struct hdr_idx *idx = &txn->hdr_idx;
6951 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006952 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006953
Willy Tarreaub6866442008-07-14 23:54:42 +02006954 if (!txn)
6955 return 0;
6956
Willy Tarreau33a7e692007-06-10 19:45:56 +02006957 ctx.idx = 0;
6958 cnt = 0;
6959 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6960 cnt++;
6961
6962 test->i = cnt;
6963 test->flags = ACL_TEST_F_VOL_HDR;
6964 return 1;
6965}
6966
Willy Tarreauc11416f2007-06-17 16:58:38 +02006967static int
6968acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6969 struct acl_expr *expr, struct acl_test *test)
6970{
6971 struct http_txn *txn = l7;
6972
Willy Tarreaub6866442008-07-14 23:54:42 +02006973 if (!txn)
6974 return 0;
6975
Willy Tarreau655dce92009-11-08 13:10:58 +01006976 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006977 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006978
Willy Tarreauc11416f2007-06-17 16:58:38 +02006979 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6980 /* ensure the indexes are not affected */
6981 return 0;
6982
6983 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6984}
6985
6986static int
6987acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6988 struct acl_expr *expr, struct acl_test *test)
6989{
6990 struct http_txn *txn = l7;
6991
Willy Tarreaub6866442008-07-14 23:54:42 +02006992 if (!txn)
6993 return 0;
6994
Willy Tarreau655dce92009-11-08 13:10:58 +01006995 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006996 return 0;
6997
6998 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6999}
7000
Willy Tarreau33a7e692007-06-10 19:45:56 +02007001/* 7. Check on HTTP header's integer value. The integer value is returned.
7002 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007003 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007004 */
7005static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007006acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007007 struct acl_expr *expr, struct acl_test *test)
7008{
7009 struct http_txn *txn = l7;
7010 struct hdr_idx *idx = &txn->hdr_idx;
7011 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007012
Willy Tarreaub6866442008-07-14 23:54:42 +02007013 if (!txn)
7014 return 0;
7015
Willy Tarreau33a7e692007-06-10 19:45:56 +02007016 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7017 /* search for header from the beginning */
7018 ctx->idx = 0;
7019
Willy Tarreau33a7e692007-06-10 19:45:56 +02007020 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7021 test->flags |= ACL_TEST_F_FETCH_MORE;
7022 test->flags |= ACL_TEST_F_VOL_HDR;
7023 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7024 return 1;
7025 }
7026
7027 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7028 test->flags |= ACL_TEST_F_VOL_HDR;
7029 return 0;
7030}
7031
Willy Tarreauc11416f2007-06-17 16:58:38 +02007032static int
7033acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7034 struct acl_expr *expr, struct acl_test *test)
7035{
7036 struct http_txn *txn = l7;
7037
Willy Tarreaub6866442008-07-14 23:54:42 +02007038 if (!txn)
7039 return 0;
7040
Willy Tarreau655dce92009-11-08 13:10:58 +01007041 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007042 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007043
Willy Tarreauc11416f2007-06-17 16:58:38 +02007044 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7045 /* ensure the indexes are not affected */
7046 return 0;
7047
7048 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7049}
7050
7051static int
7052acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7053 struct acl_expr *expr, struct acl_test *test)
7054{
7055 struct http_txn *txn = l7;
7056
Willy Tarreaub6866442008-07-14 23:54:42 +02007057 if (!txn)
7058 return 0;
7059
Willy Tarreau655dce92009-11-08 13:10:58 +01007060 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007061 return 0;
7062
7063 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7064}
7065
Willy Tarreau106f9792009-09-19 07:54:16 +02007066/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7067 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7068 */
7069static int
7070acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7071 struct acl_expr *expr, struct acl_test *test)
7072{
7073 struct http_txn *txn = l7;
7074 struct hdr_idx *idx = &txn->hdr_idx;
7075 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7076
7077 if (!txn)
7078 return 0;
7079
7080 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7081 /* search for header from the beginning */
7082 ctx->idx = 0;
7083
7084 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7085 test->flags |= ACL_TEST_F_FETCH_MORE;
7086 test->flags |= ACL_TEST_F_VOL_HDR;
7087 /* Same optimization as url_ip */
7088 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7089 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7090 test->ptr = (void *)&l4->srv_addr.sin_addr;
7091 test->i = AF_INET;
7092 return 1;
7093 }
7094
7095 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7096 test->flags |= ACL_TEST_F_VOL_HDR;
7097 return 0;
7098}
7099
7100static int
7101acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7102 struct acl_expr *expr, struct acl_test *test)
7103{
7104 struct http_txn *txn = l7;
7105
7106 if (!txn)
7107 return 0;
7108
Willy Tarreau655dce92009-11-08 13:10:58 +01007109 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007110 return 0;
7111
7112 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7113 /* ensure the indexes are not affected */
7114 return 0;
7115
7116 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7117}
7118
7119static int
7120acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7121 struct acl_expr *expr, struct acl_test *test)
7122{
7123 struct http_txn *txn = l7;
7124
7125 if (!txn)
7126 return 0;
7127
Willy Tarreau655dce92009-11-08 13:10:58 +01007128 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007129 return 0;
7130
7131 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7132}
7133
Willy Tarreau737b0c12007-06-10 21:28:46 +02007134/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7135 * the first '/' after the possible hostname, and ends before the possible '?'.
7136 */
7137static int
7138acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7139 struct acl_expr *expr, struct acl_test *test)
7140{
7141 struct http_txn *txn = l7;
7142 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007143
Willy Tarreaub6866442008-07-14 23:54:42 +02007144 if (!txn)
7145 return 0;
7146
Willy Tarreau655dce92009-11-08 13:10:58 +01007147 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007148 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007149
Willy Tarreauc11416f2007-06-17 16:58:38 +02007150 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7151 /* ensure the indexes are not affected */
7152 return 0;
7153
Willy Tarreau962c3f42010-01-10 00:15:35 +01007154 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007155 ptr = http_get_path(txn);
7156 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007157 return 0;
7158
7159 /* OK, we got the '/' ! */
7160 test->ptr = ptr;
7161
7162 while (ptr < end && *ptr != '?')
7163 ptr++;
7164
7165 test->len = ptr - test->ptr;
7166
7167 /* we do not need to set READ_ONLY because the data is in a buffer */
7168 test->flags = ACL_TEST_F_VOL_1ST;
7169 return 1;
7170}
7171
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007172static int
7173acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7174 struct acl_expr *expr, struct acl_test *test)
7175{
7176 struct buffer *req = s->req;
7177 struct http_txn *txn = &s->txn;
7178 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007179
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007180 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7181 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7182 */
7183
7184 if (!s || !req)
7185 return 0;
7186
Willy Tarreau655dce92009-11-08 13:10:58 +01007187 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007188 /* Already decoded as OK */
7189 test->flags |= ACL_TEST_F_SET_RES_PASS;
7190 return 1;
7191 }
7192
7193 /* Try to decode HTTP request */
7194 if (likely(req->lr < req->r))
7195 http_msg_analyzer(req, msg, &txn->hdr_idx);
7196
Willy Tarreau655dce92009-11-08 13:10:58 +01007197 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007198 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7199 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7200 return 1;
7201 }
7202 /* wait for final state */
7203 test->flags |= ACL_TEST_F_MAY_CHANGE;
7204 return 0;
7205 }
7206
7207 /* OK we got a valid HTTP request. We have some minor preparation to
7208 * perform so that further checks can rely on HTTP tests.
7209 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007210 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007211 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7212 s->flags |= SN_REDIRECTABLE;
7213
7214 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7215 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7216 return 1;
7217 }
7218
7219 test->flags |= ACL_TEST_F_SET_RES_PASS;
7220 return 1;
7221}
7222
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007223static int
7224acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
7225 struct acl_expr *expr, struct acl_test *test)
7226{
7227
7228 if (!s)
7229 return 0;
7230
7231 if (!get_http_auth(s))
7232 return 0;
7233
7234 test->ctx.a[0] = expr->arg.ul;
7235 test->ctx.a[1] = s->txn.auth.user;
7236 test->ctx.a[2] = s->txn.auth.pass;
7237
7238 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
7239
7240 return 1;
7241}
Willy Tarreau8797c062007-05-07 00:55:35 +02007242
7243/************************************************************************/
7244/* All supported keywords must be declared here. */
7245/************************************************************************/
7246
7247/* Note: must not be declared <const> as its list will be overwritten */
7248static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007249 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7250
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007251 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7252 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7253 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7254 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007255
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007256 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7257 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7258 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7259 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7260 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7261 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7262 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7263 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7264 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007265
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007266 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7267 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7268 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7269 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7270 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7271 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7272 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7273 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7274 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7275 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007276 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007277
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007278 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7279 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7280 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7281 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7282 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7283 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7284 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7285 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7286 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007287 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007288
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007289 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7290 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7291 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7292 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7293 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7294 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7295 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007296
Willy Tarreauf3d25982007-05-08 22:45:09 +02007297#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007298 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7299 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7300 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7301 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7302 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7303 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7304 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7305
Willy Tarreau8797c062007-05-07 00:55:35 +02007306 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7307 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7308 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7309 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7310 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7311 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7312 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7313 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007314#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02007315
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007316 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth },
7317 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth },
Willy Tarreau8797c062007-05-07 00:55:35 +02007318 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02007319}};
7320
7321
7322__attribute__((constructor))
7323static void __http_protocol_init(void)
7324{
7325 acl_register_keywords(&acl_kws);
7326}
7327
7328
Willy Tarreau58f10d72006-12-04 02:26:12 +01007329/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007330 * Local variables:
7331 * c-indent-level: 8
7332 * c-basic-offset: 8
7333 * End:
7334 */