blob: aff5ca363dc0722579fe7e3dd5400dc4f08ddb35 [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
Willy Tarreau8096de92010-02-26 11:12:27 +01002262 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002263 */
2264 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002265 int block1 = buf->l;
2266 int block2 = 0;
2267 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002268 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002269 block1 = buf->data + buf->size - buf->w;
2270 block2 = buf->r - buf->data;
2271 }
2272 if (block2)
2273 memcpy(swap_buffer, buf->data, block2);
2274 memmove(buf->data, buf->w, block1);
2275 if (block2)
2276 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002277 }
2278
2279 /* adjust all known pointers */
2280 buf->w = buf->data;
2281 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2282 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2283 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2284 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2285
2286 /* adjust relative pointers */
2287 msg->som = 0;
2288 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2289 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2290 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2291
Willy Tarreau83e3af02009-12-28 17:39:57 +01002292 if (msg->err_pos >= 0) {
2293 msg->err_pos += off;
2294 if (msg->err_pos >= buf->size)
2295 msg->err_pos -= buf->size;
2296 }
2297
2298 buf->flags &= ~BF_FULL;
2299 if (buf->l >= buffer_max_len(buf))
2300 buf->flags |= BF_FULL;
2301}
2302
Willy Tarreaud787e662009-07-07 10:14:51 +02002303/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2304 * processing can continue on next analysers, or zero if it either needs more
2305 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2306 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2307 * when it has nothing left to do, and may remove any analyser when it wants to
2308 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002309 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002310int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002311{
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 /*
2313 * We will parse the partial (or complete) lines.
2314 * We will check the request syntax, and also join multi-line
2315 * headers. An index of all the lines will be elaborated while
2316 * parsing.
2317 *
2318 * For the parsing, we use a 28 states FSM.
2319 *
2320 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002321 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002322 * req->data + msg->eoh = end of processed headers / start of current one
2323 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 * req->lr = first non-visited byte
2325 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002326 *
2327 * At end of parsing, we may perform a capture of the error (if any), and
2328 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2329 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2330 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002332
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002334 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 struct http_txn *txn = &s->txn;
2336 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002337 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002338
Willy Tarreau6bf17362009-02-24 10:48:35 +01002339 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2340 now_ms, __FUNCTION__,
2341 s,
2342 req,
2343 req->rex, req->wex,
2344 req->flags,
2345 req->l,
2346 req->analysers);
2347
Willy Tarreau52a0c602009-08-16 22:45:38 +02002348 /* we're speaking HTTP here, so let's speak HTTP to the client */
2349 s->srv_error = http_return_srv_error;
2350
Willy Tarreau83e3af02009-12-28 17:39:57 +01002351 /* There's a protected area at the end of the buffer for rewriting
2352 * purposes. We don't want to start to parse the request if the
2353 * protected area is affected, because we may have to move processed
2354 * data later, which is much more complicated.
2355 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002356 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002357 if ((txn->flags & TX_NOT_FIRST) &&
2358 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002359 req->r < req->lr ||
2360 req->r > req->data + req->size - global.tune.maxrewrite)) {
2361 if (req->send_max) {
2362 /* some data has still not left the buffer, wake us once that's done */
2363 buffer_dont_connect(req);
2364 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2365 return 0;
2366 }
2367 if (req->l <= req->size - global.tune.maxrewrite)
2368 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002369 }
2370
Willy Tarreau065e8332010-01-08 00:30:20 +01002371 /* Note that we have the same problem with the response ; we
2372 * may want to send a redirect, error or anything which requires
2373 * some spare space. So we'll ensure that we have at least
2374 * maxrewrite bytes available in the response buffer before
2375 * processing that one. This will only affect pipelined
2376 * keep-alive requests.
2377 */
2378 if ((txn->flags & TX_NOT_FIRST) &&
2379 unlikely((s->rep->flags & BF_FULL) ||
2380 s->rep->r < s->rep->lr ||
2381 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2382 if (s->rep->send_max) {
2383 /* don't let a connection request be initiated */
2384 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002385 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002386 return 0;
2387 }
2388 }
2389
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002390 if (likely(req->lr < req->r))
2391 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002392 }
2393
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 /* 1: we might have to print this header in debug mode */
2395 if (unlikely((global.mode & MODE_DEBUG) &&
2396 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002397 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002398 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002399
Willy Tarreau962c3f42010-01-10 00:15:35 +01002400 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 eol = sol + msg->sl.rq.l;
2402 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002403
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 sol += hdr_idx_first_pos(&txn->hdr_idx);
2405 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 while (cur_idx) {
2408 eol = sol + txn->hdr_idx.v[cur_idx].len;
2409 debug_hdr("clihdr", s, sol, eol);
2410 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2411 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002412 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002413 }
2414
Willy Tarreau58f10d72006-12-04 02:26:12 +01002415
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 /*
2417 * Now we quickly check if we have found a full valid request.
2418 * If not so, we check the FD and buffer states before leaving.
2419 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002420 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002421 * requests are checked first. When waiting for a second request
2422 * on a keep-alive session, if we encounter and error, close, t/o,
2423 * we note the error in the session flags but don't set any state.
2424 * Since the error will be noted there, it will not be counted by
2425 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002427
Willy Tarreau655dce92009-11-08 13:10:58 +01002428 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002429 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002431 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2433 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002434
Willy Tarreau59234e92008-11-30 23:51:27 +01002435 /* 1: Since we are in header mode, if there's no space
2436 * left for headers, we won't be able to free more
2437 * later, so the session will never terminate. We
2438 * must terminate it now.
2439 */
2440 if (unlikely(req->flags & BF_FULL)) {
2441 /* FIXME: check if URI is set and return Status
2442 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002443 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 goto return_bad_req;
2445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002446
Willy Tarreau59234e92008-11-30 23:51:27 +01002447 /* 2: have we encountered a read error ? */
2448 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002449 if (!(s->flags & SN_ERR_MASK))
2450 s->flags |= SN_ERR_CLICL;
2451
Willy Tarreaufcffa692010-01-10 14:21:19 +01002452 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002453 goto failed_keep_alive;
2454
Willy Tarreau59234e92008-11-30 23:51:27 +01002455 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002456 if (msg->err_pos >= 0)
2457 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002458 msg->msg_state = HTTP_MSG_ERROR;
2459 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002460
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002461 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002462 if (s->listener->counters)
2463 s->listener->counters->failed_req++;
2464
Willy Tarreau59234e92008-11-30 23:51:27 +01002465 if (!(s->flags & SN_FINST_MASK))
2466 s->flags |= SN_FINST_R;
2467 return 0;
2468 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002469
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 /* 3: has the read timeout expired ? */
2471 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002472 if (!(s->flags & SN_ERR_MASK))
2473 s->flags |= SN_ERR_CLITO;
2474
Willy Tarreaufcffa692010-01-10 14:21:19 +01002475 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002476 goto failed_keep_alive;
2477
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002479 if (msg->err_pos >= 0)
2480 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 txn->status = 408;
2482 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2483 msg->msg_state = HTTP_MSG_ERROR;
2484 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002485
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002486 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002487 if (s->listener->counters)
2488 s->listener->counters->failed_req++;
2489
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 if (!(s->flags & SN_FINST_MASK))
2491 s->flags |= SN_FINST_R;
2492 return 0;
2493 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002494
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 /* 4: have we encountered a close ? */
2496 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002497 if (!(s->flags & SN_ERR_MASK))
2498 s->flags |= SN_ERR_CLICL;
2499
Willy Tarreaufcffa692010-01-10 14:21:19 +01002500 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002501 goto failed_keep_alive;
2502
Willy Tarreau4076a152009-04-02 15:18:36 +02002503 if (msg->err_pos >= 0)
2504 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 txn->status = 400;
2506 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2507 msg->msg_state = HTTP_MSG_ERROR;
2508 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002509
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002510 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002511 if (s->listener->counters)
2512 s->listener->counters->failed_req++;
2513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 if (!(s->flags & SN_FINST_MASK))
2515 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002516 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002517 }
2518
Willy Tarreau520d95e2009-09-19 21:04:57 +02002519 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002520 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002521 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002522
Willy Tarreaufcffa692010-01-10 14:21:19 +01002523 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2524 /* If the client starts to talk, let's fall back to
2525 * request timeout processing.
2526 */
2527 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002528 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002529 }
2530
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002532 if (!tick_isset(req->analyse_exp)) {
2533 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2534 (txn->flags & TX_WAIT_NEXT_RQ) &&
2535 tick_isset(s->be->timeout.httpka))
2536 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2537 else
2538 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2539 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002540
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 /* we're not ready yet */
2542 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002543
2544 failed_keep_alive:
2545 /* Here we process low-level errors for keep-alive requests. In
2546 * short, if the request is not the first one and it experiences
2547 * a timeout, read error or shutdown, we just silently close so
2548 * that the client can try again.
2549 */
2550 txn->status = 0;
2551 msg->msg_state = HTTP_MSG_RQBEFORE;
2552 req->analysers = 0;
2553 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002554 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002555 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002556 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002558
Willy Tarreaud787e662009-07-07 10:14:51 +02002559 /* OK now we have a complete HTTP request with indexed headers. Let's
2560 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002561 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2562 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2563 * points to the CRLF of the request line. req->lr points to the first
2564 * byte after the last LF. msg->col and msg->sov point to the first
2565 * byte of data. msg->eol cannot be trusted because it may have been
2566 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002567 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002568
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002569 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2570
Willy Tarreaub16a5742010-01-10 14:46:16 +01002571 if (txn->flags & TX_WAIT_NEXT_RQ) {
2572 /* kill the pending keep-alive timeout */
2573 txn->flags &= ~TX_WAIT_NEXT_RQ;
2574 req->analyse_exp = TICK_ETERNITY;
2575 }
2576
2577
Willy Tarreaud787e662009-07-07 10:14:51 +02002578 /* Maybe we found in invalid header name while we were configured not
2579 * to block on that, so we have to capture it now.
2580 */
2581 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002582 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2583
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 /*
2585 * 1: identify the method
2586 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002587 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002588
2589 /* we can make use of server redirect on GET and HEAD */
2590 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2591 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /*
2594 * 2: check if the URI matches the monitor_uri.
2595 * We have to do this for every request which gets in, because
2596 * the monitor-uri is defined by the frontend.
2597 */
2598 if (unlikely((s->fe->monitor_uri_len != 0) &&
2599 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002600 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 s->fe->monitor_uri,
2602 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002603 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002605 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002611 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2612 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002613
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 ret = acl_pass(ret);
2615 if (cond->pol == ACL_COND_UNLESS)
2616 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002617
Willy Tarreau59234e92008-11-30 23:51:27 +01002618 if (ret) {
2619 /* we fail this request, let's return 503 service unavail */
2620 txn->status = 503;
2621 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2622 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002623 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002624 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 /* nothing to fail, let's reply normaly */
2627 txn->status = 200;
2628 stream_int_retnclose(req->prod, &http_200_chunk);
2629 goto return_prx_cond;
2630 }
2631
2632 /*
2633 * 3: Maybe we have to copy the original REQURI for the logs ?
2634 * Note: we cannot log anymore if the request has been
2635 * classified as invalid.
2636 */
2637 if (unlikely(s->logs.logwait & LW_REQ)) {
2638 /* we have a complete HTTP request that we must log */
2639 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2640 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002641
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 if (urilen >= REQURI_LEN)
2643 urilen = REQURI_LEN - 1;
2644 memcpy(txn->uri, &req->data[msg->som], urilen);
2645 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002646
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 if (!(s->logs.logwait &= ~LW_REQ))
2648 s->do_log(s);
2649 } else {
2650 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002651 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 }
Willy Tarreau06619262006-12-17 08:37:22 +01002653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002655 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2656 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002657
Willy Tarreau5b154472009-12-21 20:11:07 +01002658 /* ... and check if the request is HTTP/1.1 or above */
2659 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002660 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2661 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2662 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002663 txn->flags |= TX_REQ_VER_11;
2664
2665 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002666 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002667
Willy Tarreau88d349d2010-01-25 12:15:43 +01002668 /* if the frontend has "option http-use-proxy-header", we'll check if
2669 * we have what looks like a proxied connection instead of a connection,
2670 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2671 * Note that this is *not* RFC-compliant, however browsers and proxies
2672 * happen to do that despite being non-standard :-(
2673 * We consider that a request not beginning with either '/' or '*' is
2674 * a proxied connection, which covers both "scheme://location" and
2675 * CONNECT ip:port.
2676 */
2677 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2678 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2679 txn->flags |= TX_USE_PX_CONN;
2680
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002681 /* transfer length unknown*/
2682 txn->flags &= ~TX_REQ_XFER_LEN;
2683
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 /* 5: we may need to capture headers */
2685 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002686 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002688
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002689 /* 6: determine the transfer-length.
2690 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2691 * the presence of a message-body in a REQUEST and its transfer length
2692 * must be determined that way (in order of precedence) :
2693 * 1. The presence of a message-body in a request is signaled by the
2694 * inclusion of a Content-Length or Transfer-Encoding header field
2695 * in the request's header fields. When a request message contains
2696 * both a message-body of non-zero length and a method that does
2697 * not define any semantics for that request message-body, then an
2698 * origin server SHOULD either ignore the message-body or respond
2699 * with an appropriate error message (e.g., 413). A proxy or
2700 * gateway, when presented the same request, SHOULD either forward
2701 * the request inbound with the message- body or ignore the
2702 * message-body when determining a response.
2703 *
2704 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2705 * and the "chunked" transfer-coding (Section 6.2) is used, the
2706 * transfer-length is defined by the use of this transfer-coding.
2707 * If a Transfer-Encoding header field is present and the "chunked"
2708 * transfer-coding is not present, the transfer-length is defined
2709 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002710 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002711 * 3. If a Content-Length header field is present, its decimal value in
2712 * OCTETs represents both the entity-length and the transfer-length.
2713 * If a message is received with both a Transfer-Encoding header
2714 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002715 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002716 * 4. By the server closing the connection. (Closing the connection
2717 * cannot be used to indicate the end of a request body, since that
2718 * would leave no possibility for the server to send back a response.)
2719 *
2720 * Whenever a transfer-coding is applied to a message-body, the set of
2721 * transfer-codings MUST include "chunked", unless the message indicates
2722 * it is terminated by closing the connection. When the "chunked"
2723 * transfer-coding is used, it MUST be the last transfer-coding applied
2724 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002725 */
2726
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002727 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002728 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002729 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002730 while ((txn->flags & TX_REQ_VER_11) &&
2731 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002732 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2733 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2734 else if (txn->flags & TX_REQ_TE_CHNK) {
2735 /* bad transfer-encoding (chunked followed by something else) */
2736 use_close_only = 1;
2737 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2738 break;
2739 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002740 }
2741
Willy Tarreau32b47f42009-10-18 20:55:02 +02002742 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002743 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002744 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2745 signed long long cl;
2746
2747 if (!ctx.vlen)
2748 goto return_bad_req;
2749
2750 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2751 goto return_bad_req; /* parse failure */
2752
2753 if (cl < 0)
2754 goto return_bad_req;
2755
2756 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2757 goto return_bad_req; /* already specified, was different */
2758
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002759 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002760 msg->hdr_content_len = cl;
2761 }
2762
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002763 /* bodyless requests have a known length */
2764 if (!use_close_only)
2765 txn->flags |= TX_REQ_XFER_LEN;
2766
Willy Tarreaud787e662009-07-07 10:14:51 +02002767 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002768 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002769 req->analyse_exp = TICK_ETERNITY;
2770 return 1;
2771
2772 return_bad_req:
2773 /* We centralize bad requests processing here */
2774 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2775 /* we detected a parsing error. We want to archive this request
2776 * in the dedicated proxy area for later troubleshooting.
2777 */
2778 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2779 }
2780
2781 txn->req.msg_state = HTTP_MSG_ERROR;
2782 txn->status = 400;
2783 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002784
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002785 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002786 if (s->listener->counters)
2787 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002788
2789 return_prx_cond:
2790 if (!(s->flags & SN_ERR_MASK))
2791 s->flags |= SN_ERR_PRXCOND;
2792 if (!(s->flags & SN_FINST_MASK))
2793 s->flags |= SN_FINST_R;
2794
2795 req->analysers = 0;
2796 req->analyse_exp = TICK_ETERNITY;
2797 return 0;
2798}
2799
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002800/* This stream analyser runs all HTTP request processing which is common to
2801 * frontends and backends, which means blocking ACLs, filters, connection-close,
2802 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002803 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002804 * either needs more data or wants to immediately abort the request (eg: deny,
2805 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002806 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002807int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002808{
Willy Tarreaud787e662009-07-07 10:14:51 +02002809 struct http_txn *txn = &s->txn;
2810 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002812 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002813 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002814 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002815 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002816
Willy Tarreau655dce92009-11-08 13:10:58 +01002817 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002818 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002819 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002820 return 0;
2821 }
2822
Willy Tarreau3a816292009-07-07 10:55:49 +02002823 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 req->analyse_exp = TICK_ETERNITY;
2825
2826 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2827 now_ms, __FUNCTION__,
2828 s,
2829 req,
2830 req->rex, req->wex,
2831 req->flags,
2832 req->l,
2833 req->analysers);
2834
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002835 /* first check whether we have some ACLs set to block this request */
2836 list_for_each_entry(cond, &px->block_cond, list) {
2837 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002838
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002839 ret = acl_pass(ret);
2840 if (cond->pol == ACL_COND_UNLESS)
2841 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002842
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002843 if (ret) {
2844 txn->status = 403;
2845 /* let's log the request time */
2846 s->logs.tv_request = now;
2847 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2848 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002849 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002850 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002851
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002852 do_stats = stats_check_uri(s, px);
2853
2854 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 +01002855 int ret = 1;
2856
2857 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
2858 continue;
2859
2860 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002861 if (req_acl->cond) {
2862 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
2863 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01002864
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002865 if (req_acl->cond->pol == ACL_COND_UNLESS)
2866 ret = !ret;
2867 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002868
2869 if (ret) {
2870 req_acl_final = req_acl;
2871 break;
2872 }
2873 }
2874
2875 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
2876 txn->status = 403;
2877 s->logs.tv_request = now;
2878 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2879 goto return_prx_cond;
2880 }
2881
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002882 /* try headers filters */
2883 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002884 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002885 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002886
Willy Tarreau59234e92008-11-30 23:51:27 +01002887 /* has the request been denied ? */
2888 if (txn->flags & TX_CLDENY) {
2889 /* no need to go further */
2890 txn->status = 403;
2891 /* let's log the request time */
2892 s->logs.tv_request = now;
2893 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2894 goto return_prx_cond;
2895 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002896
2897 /* When a connection is tarpitted, we use the tarpit timeout,
2898 * which may be the same as the connect timeout if unspecified.
2899 * If unset, then set it to zero because we really want it to
2900 * eventually expire. We build the tarpit as an analyser.
2901 */
2902 if (txn->flags & TX_CLTARPIT) {
2903 buffer_erase(s->req);
2904 /* wipe the request out so that we can drop the connection early
2905 * if the client closes first.
2906 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002907 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002908 req->analysers = 0; /* remove switching rules etc... */
2909 req->analysers |= AN_REQ_HTTP_TARPIT;
2910 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2911 if (!req->analyse_exp)
2912 req->analyse_exp = tick_add(now_ms, 0);
2913 return 1;
2914 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002915 }
Willy Tarreau06619262006-12-17 08:37:22 +01002916
Willy Tarreau5b154472009-12-21 20:11:07 +01002917 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2918 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002919 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2920 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002921 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2922 * avoid to redo the same work if FE and BE have the same settings (common).
2923 * The method consists in checking if options changed between the two calls
2924 * (implying that either one is non-null, or one of them is non-null and we
2925 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002926 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002927
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002928 del_cl = del_ka = 0;
2929
Willy Tarreaudc008c52010-02-01 16:20:08 +01002930 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2931 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2932 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2933 (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 +01002934 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002935
Willy Tarreau5b154472009-12-21 20:11:07 +01002936 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2937 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002938 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2939 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002940 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002941 tmp = TX_CON_WANT_CLO;
2942
Willy Tarreau5b154472009-12-21 20:11:07 +01002943 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2944 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002945
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002946 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2947 /* parse the Connection header and possibly clean it */
2948 int to_del = 0;
2949 if ((txn->flags & TX_REQ_VER_11) ||
2950 (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)
2951 to_del |= 2; /* remove "keep-alive" */
2952 if (!(txn->flags & TX_REQ_VER_11))
2953 to_del |= 1; /* remove "close" */
2954 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002955 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002956
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002957 /* check if client or config asks for explicit close in KAL/SCL */
2958 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2959 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2960 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2961 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2962 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002963 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2964 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002965 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2966 }
Willy Tarreau78599912009-10-17 20:12:21 +02002967
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002968 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002969 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01002970 if (wl->cond) {
2971 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2972 ret = acl_pass(ret);
2973 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2974 ret = !ret;
2975 if (!ret)
2976 continue;
2977 }
2978
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002979 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002980 goto return_bad_req;
2981 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002982
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002983 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
2984 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002985 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002986
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002987 if (!realm)
2988 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2989
Willy Tarreau844a7e72010-01-31 21:46:18 +01002990 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002991 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2992 txn->status = 401;
2993 stream_int_retnclose(req->prod, &msg);
2994 goto return_prx_cond;
2995 }
2996
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002997 if (do_stats) {
2998 /* We need to provied stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999 * FIXME!!! that one is rather dangerous, we want to
3000 * make it follow standard rules (eg: clear req->analysers).
3001 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003002
3003 s->logs.tv_request = now;
3004 s->data_source = DATA_SRC_STATS;
3005 s->data_state = DATA_ST_INIT;
3006 s->task->nice = -32; /* small boost for HTTP statistics */
3007 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3008 s->rep->prod->private = s;
3009 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3010 req->analysers = 0;
3011
3012 return 0;
3013
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003014 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003015
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016 /* check whether we have some ACLs set to redirect this request */
3017 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003018 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003019
Willy Tarreauf285f542010-01-03 20:03:03 +01003020 if (rule->cond) {
3021 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3022 ret = acl_pass(ret);
3023 if (rule->cond->pol == ACL_COND_UNLESS)
3024 ret = !ret;
3025 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003026
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003028 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003029 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003030
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 /* build redirect message */
3032 switch(rule->code) {
3033 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003034 msg_fmt = HTTP_303;
3035 break;
3036 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 msg_fmt = HTTP_301;
3038 break;
3039 case 302:
3040 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003041 msg_fmt = HTTP_302;
3042 break;
3043 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003044
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003045 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 switch(rule->type) {
3049 case REDIRECT_TYPE_PREFIX: {
3050 const char *path;
3051 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 path = http_get_path(txn);
3054 /* build message using path */
3055 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003056 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003057 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3058 int qs = 0;
3059 while (qs < pathlen) {
3060 if (path[qs] == '?') {
3061 pathlen = qs;
3062 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003063 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003064 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003065 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003066 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 } else {
3068 path = "/";
3069 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003070 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003072 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 goto return_bad_req;
3074
3075 /* add prefix. Note that if prefix == "/", we don't want to
3076 * add anything, otherwise it makes it hard for the user to
3077 * configure a self-redirection.
3078 */
3079 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003080 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3081 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082 }
3083
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003084 /* add path */
3085 memcpy(rdr.str + rdr.len, path, pathlen);
3086 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003087
3088 /* append a slash at the end of the location is needed and missing */
3089 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3090 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3091 if (rdr.len > rdr.size - 5)
3092 goto return_bad_req;
3093 rdr.str[rdr.len] = '/';
3094 rdr.len++;
3095 }
3096
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003097 break;
3098 }
3099 case REDIRECT_TYPE_LOCATION:
3100 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003101 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003102 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003103
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104 /* add location */
3105 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3106 rdr.len += rule->rdr_len;
3107 break;
3108 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003109
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 if (rule->cookie_len) {
3111 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3112 rdr.len += 14;
3113 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3114 rdr.len += rule->cookie_len;
3115 memcpy(rdr.str + rdr.len, "\r\n", 2);
3116 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003117 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003118
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003119 /* add end of headers and the keep-alive/close status.
3120 * We may choose to set keep-alive if the Location begins
3121 * with a slash, because the client will come back to the
3122 * same server.
3123 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 txn->status = rule->code;
3125 /* let's log the request time */
3126 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003127
3128 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3129 (txn->flags & TX_REQ_XFER_LEN) &&
3130 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3131 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3132 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3133 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003134 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003135 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3136 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3137 rdr.len += 30;
3138 } else {
3139 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3140 rdr.len += 24;
3141 }
Willy Tarreau75661452010-01-10 10:35:01 +01003142 }
3143 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3144 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003145 buffer_write(req->prod->ob, rdr.str, rdr.len);
3146 /* "eat" the request */
3147 buffer_ignore(req, msg->sov - msg->som);
3148 msg->som = msg->sov;
3149 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003150 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3151 txn->req.msg_state = HTTP_MSG_CLOSED;
3152 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003153 break;
3154 } else {
3155 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003156 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3157 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3158 rdr.len += 29;
3159 } else {
3160 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3161 rdr.len += 23;
3162 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003163 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003164 goto return_prx_cond;
3165 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003166 }
3167 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003168
Willy Tarreau2be39392010-01-03 17:24:51 +01003169 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3170 * If this happens, then the data will not come immediately, so we must
3171 * send all what we have without waiting. Note that due to the small gain
3172 * in waiting for the body of the request, it's easier to simply put the
3173 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3174 * itself once used.
3175 */
3176 req->flags |= BF_SEND_DONTWAIT;
3177
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003178 /* that's OK for us now, let's move on to next analysers */
3179 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003180
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003181 return_bad_req:
3182 /* We centralize bad requests processing here */
3183 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3184 /* we detected a parsing error. We want to archive this request
3185 * in the dedicated proxy area for later troubleshooting.
3186 */
3187 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3188 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003189
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003190 txn->req.msg_state = HTTP_MSG_ERROR;
3191 txn->status = 400;
3192 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003193
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003194 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003195 if (s->listener->counters)
3196 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 return_prx_cond:
3199 if (!(s->flags & SN_ERR_MASK))
3200 s->flags |= SN_ERR_PRXCOND;
3201 if (!(s->flags & SN_FINST_MASK))
3202 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 req->analysers = 0;
3205 req->analyse_exp = TICK_ETERNITY;
3206 return 0;
3207}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003208
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003209/* This function performs all the processing enabled for the current request.
3210 * It returns 1 if the processing can continue on next analysers, or zero if it
3211 * needs more data, encounters an error, or wants to immediately abort the
3212 * request. It relies on buffers flags, and updates s->req->analysers.
3213 */
3214int http_process_request(struct session *s, struct buffer *req, int an_bit)
3215{
3216 struct http_txn *txn = &s->txn;
3217 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003218
Willy Tarreau655dce92009-11-08 13:10:58 +01003219 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003220 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003221 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003222 return 0;
3223 }
3224
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003225 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3226 now_ms, __FUNCTION__,
3227 s,
3228 req,
3229 req->rex, req->wex,
3230 req->flags,
3231 req->l,
3232 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003233
Willy Tarreau59234e92008-11-30 23:51:27 +01003234 /*
3235 * Right now, we know that we have processed the entire headers
3236 * and that unwanted requests have been filtered out. We can do
3237 * whatever we want with the remaining request. Also, now we
3238 * may have separate values for ->fe, ->be.
3239 */
Willy Tarreau06619262006-12-17 08:37:22 +01003240
Willy Tarreau59234e92008-11-30 23:51:27 +01003241 /*
3242 * If HTTP PROXY is set we simply get remote server address
3243 * parsing incoming request.
3244 */
3245 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003246 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003248
Willy Tarreau59234e92008-11-30 23:51:27 +01003249 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003250 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003251 * Note that doing so might move headers in the request, but
3252 * the fields will stay coherent and the URI will not move.
3253 * This should only be performed in the backend.
3254 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003255 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3257 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003258
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003260 * 8: the appsession cookie was looked up very early in 1.2,
3261 * so let's do the same now.
3262 */
3263
3264 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003265 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003266 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003267 }
3268
3269 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 * 9: add X-Forwarded-For if either the frontend or the backend
3271 * asks for it.
3272 */
3273 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3274 if (s->cli_addr.ss_family == AF_INET) {
3275 /* Add an X-Forwarded-For header unless the source IP is
3276 * in the 'except' network range.
3277 */
3278 if ((!s->fe->except_mask.s_addr ||
3279 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3280 != s->fe->except_net.s_addr) &&
3281 (!s->be->except_mask.s_addr ||
3282 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3283 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003284 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 unsigned char *pn;
3286 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003287
3288 /* Note: we rely on the backend to get the header name to be used for
3289 * x-forwarded-for, because the header is really meant for the backends.
3290 * However, if the backend did not specify any option, we have to rely
3291 * on the frontend's header name.
3292 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003293 if (s->be->fwdfor_hdr_len) {
3294 len = s->be->fwdfor_hdr_len;
3295 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003296 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003297 len = s->fe->fwdfor_hdr_len;
3298 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003299 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003300 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003301
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003302 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003303 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003304 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003305 }
3306 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003307 else if (s->cli_addr.ss_family == AF_INET6) {
3308 /* FIXME: for the sake of completeness, we should also support
3309 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003310 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003311 int len;
3312 char pn[INET6_ADDRSTRLEN];
3313 inet_ntop(AF_INET6,
3314 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3315 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003316
Willy Tarreau59234e92008-11-30 23:51:27 +01003317 /* Note: we rely on the backend to get the header name to be used for
3318 * x-forwarded-for, because the header is really meant for the backends.
3319 * However, if the backend did not specify any option, we have to rely
3320 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003321 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 if (s->be->fwdfor_hdr_len) {
3323 len = s->be->fwdfor_hdr_len;
3324 memcpy(trash, s->be->fwdfor_hdr_name, len);
3325 } else {
3326 len = s->fe->fwdfor_hdr_len;
3327 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003328 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003329 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003330
Willy Tarreau59234e92008-11-30 23:51:27 +01003331 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003332 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 goto return_bad_req;
3334 }
3335 }
3336
3337 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003338 * 10: add X-Original-To if either the frontend or the backend
3339 * asks for it.
3340 */
3341 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3342
3343 /* FIXME: don't know if IPv6 can handle that case too. */
3344 if (s->cli_addr.ss_family == AF_INET) {
3345 /* Add an X-Original-To header unless the destination IP is
3346 * in the 'except' network range.
3347 */
3348 if (!(s->flags & SN_FRT_ADDR_SET))
3349 get_frt_addr(s);
3350
3351 if ((!s->fe->except_mask_to.s_addr ||
3352 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3353 != s->fe->except_to.s_addr) &&
3354 (!s->be->except_mask_to.s_addr ||
3355 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3356 != s->be->except_to.s_addr)) {
3357 int len;
3358 unsigned char *pn;
3359 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3360
3361 /* Note: we rely on the backend to get the header name to be used for
3362 * x-original-to, because the header is really meant for the backends.
3363 * However, if the backend did not specify any option, we have to rely
3364 * on the frontend's header name.
3365 */
3366 if (s->be->orgto_hdr_len) {
3367 len = s->be->orgto_hdr_len;
3368 memcpy(trash, s->be->orgto_hdr_name, len);
3369 } else {
3370 len = s->fe->orgto_hdr_len;
3371 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003372 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003373 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3374
3375 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003376 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003377 goto return_bad_req;
3378 }
3379 }
3380 }
3381
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003382 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3383 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3384 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3385 unsigned int want_flags = 0;
3386
3387 if (txn->flags & TX_REQ_VER_11) {
3388 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3389 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3390 want_flags |= TX_CON_CLO_SET;
3391 } else {
3392 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
3393 want_flags |= TX_CON_KAL_SET;
3394 }
3395
3396 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3397 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003398 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003399
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003400
Willy Tarreau522d6c02009-12-06 18:49:18 +01003401 /* If we have no server assigned yet and we're balancing on url_param
3402 * with a POST request, we may be interested in checking the body for
3403 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003404 */
3405 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3406 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003407 s->be->url_param_post_limit != 0 &&
3408 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003409 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003410 buffer_dont_connect(req);
3411 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413
Willy Tarreaud98cf932009-12-27 22:54:55 +01003414 if (txn->flags & TX_REQ_XFER_LEN)
3415 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003416
Willy Tarreau59234e92008-11-30 23:51:27 +01003417 /*************************************************************
3418 * OK, that's finished for the headers. We have done what we *
3419 * could. Let's switch to the DATA state. *
3420 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003421 req->analyse_exp = TICK_ETERNITY;
3422 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003423
Willy Tarreau59234e92008-11-30 23:51:27 +01003424 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003425 /* OK let's go on with the BODY now */
3426 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003427
Willy Tarreau59234e92008-11-30 23:51:27 +01003428 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003429 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003430 /* we detected a parsing error. We want to archive this request
3431 * in the dedicated proxy area for later troubleshooting.
3432 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003433 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003434 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003435
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 txn->req.msg_state = HTTP_MSG_ERROR;
3437 txn->status = 400;
3438 req->analysers = 0;
3439 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003440
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003441 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003442 if (s->listener->counters)
3443 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003444
Willy Tarreau59234e92008-11-30 23:51:27 +01003445 if (!(s->flags & SN_ERR_MASK))
3446 s->flags |= SN_ERR_PRXCOND;
3447 if (!(s->flags & SN_FINST_MASK))
3448 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003449 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003450}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003451
Willy Tarreau60b85b02008-11-30 23:28:40 +01003452/* This function is an analyser which processes the HTTP tarpit. It always
3453 * returns zero, at the beginning because it prevents any other processing
3454 * from occurring, and at the end because it terminates the request.
3455 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003456int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003457{
3458 struct http_txn *txn = &s->txn;
3459
3460 /* This connection is being tarpitted. The CLIENT side has
3461 * already set the connect expiration date to the right
3462 * timeout. We just have to check that the client is still
3463 * there and that the timeout has not expired.
3464 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003465 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003466 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3467 !tick_is_expired(req->analyse_exp, now_ms))
3468 return 0;
3469
3470 /* We will set the queue timer to the time spent, just for
3471 * logging purposes. We fake a 500 server error, so that the
3472 * attacker will not suspect his connection has been tarpitted.
3473 * It will not cause trouble to the logs because we can exclude
3474 * the tarpitted connections by filtering on the 'PT' status flags.
3475 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003476 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3477
3478 txn->status = 500;
3479 if (req->flags != BF_READ_ERROR)
3480 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3481
3482 req->analysers = 0;
3483 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003484
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003485 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003486 if (s->listener->counters)
3487 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003488
Willy Tarreau60b85b02008-11-30 23:28:40 +01003489 if (!(s->flags & SN_ERR_MASK))
3490 s->flags |= SN_ERR_PRXCOND;
3491 if (!(s->flags & SN_FINST_MASK))
3492 s->flags |= SN_FINST_T;
3493 return 0;
3494}
3495
Willy Tarreaud34af782008-11-30 23:36:37 +01003496/* This function is an analyser which processes the HTTP request body. It looks
3497 * for parameters to be used for the load balancing algorithm (url_param). It
3498 * must only be called after the standard HTTP request processing has occurred,
3499 * because it expects the request to be parsed. It returns zero if it needs to
3500 * read more data, or 1 once it has completed its analysis.
3501 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003502int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003503{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003504 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003505 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003506 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003507
3508 /* We have to parse the HTTP request body to find any required data.
3509 * "balance url_param check_post" should have been the only way to get
3510 * into this. We were brought here after HTTP header analysis, so all
3511 * related structures are ready.
3512 */
3513
Willy Tarreau522d6c02009-12-06 18:49:18 +01003514 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3515 goto missing_data;
3516
3517 if (msg->msg_state < HTTP_MSG_100_SENT) {
3518 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3519 * send an HTTP/1.1 100 Continue intermediate response.
3520 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003521 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003522 struct hdr_ctx ctx;
3523 ctx.idx = 0;
3524 /* Expect is allowed in 1.1, look for it */
3525 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3526 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3527 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3528 }
3529 }
3530 msg->msg_state = HTTP_MSG_100_SENT;
3531 }
3532
3533 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003534 /* we have msg->col and msg->sov which both point to the first
3535 * byte of message body. msg->som still points to the beginning
3536 * of the message. We must save the body in req->lr because it
3537 * survives buffer re-alignments.
3538 */
3539 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003540 if (txn->flags & TX_REQ_TE_CHNK)
3541 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3542 else
3543 msg->msg_state = HTTP_MSG_DATA;
3544 }
3545
3546 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003547 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003548 * set ->sov and ->lr to point to the body and switch to DATA or
3549 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003550 */
3551 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003552
Willy Tarreau115acb92009-12-26 13:56:06 +01003553 if (!ret)
3554 goto missing_data;
3555 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003556 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003557 }
3558
Willy Tarreaud98cf932009-12-27 22:54:55 +01003559 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003560 * We have the first non-header byte in msg->col, which is either the
3561 * beginning of the chunk size or of the data. The first data byte is in
3562 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3563 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003564 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003565
3566 if (msg->hdr_content_len < limit)
3567 limit = msg->hdr_content_len;
3568
Willy Tarreau7c96f672009-12-27 22:47:25 +01003569 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003570 goto http_end;
3571
3572 missing_data:
3573 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003574 if (req->flags & BF_FULL)
3575 goto return_bad_req;
3576
Willy Tarreau522d6c02009-12-06 18:49:18 +01003577 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3578 txn->status = 408;
3579 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3580 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003581 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003582
3583 /* we get here if we need to wait for more data */
3584 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003585 /* Not enough data. We'll re-use the http-request
3586 * timeout here. Ideally, we should set the timeout
3587 * relative to the accept() date. We just set the
3588 * request timeout once at the beginning of the
3589 * request.
3590 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003591 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003592 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003593 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003594 return 0;
3595 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003596
3597 http_end:
3598 /* The situation will not evolve, so let's give up on the analysis. */
3599 s->logs.tv_request = now; /* update the request timer to reflect full request */
3600 req->analysers &= ~an_bit;
3601 req->analyse_exp = TICK_ETERNITY;
3602 return 1;
3603
3604 return_bad_req: /* let's centralize all bad requests */
3605 txn->req.msg_state = HTTP_MSG_ERROR;
3606 txn->status = 400;
3607 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3608
3609 return_err_msg:
3610 req->analysers = 0;
3611 s->fe->counters.failed_req++;
3612 if (s->listener->counters)
3613 s->listener->counters->failed_req++;
3614
3615 if (!(s->flags & SN_ERR_MASK))
3616 s->flags |= SN_ERR_PRXCOND;
3617 if (!(s->flags & SN_FINST_MASK))
3618 s->flags |= SN_FINST_R;
3619 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003620}
3621
Willy Tarreau610ecce2010-01-04 21:15:02 +01003622/* Terminate current transaction and prepare a new one. This is very tricky
3623 * right now but it works.
3624 */
3625void http_end_txn_clean_session(struct session *s)
3626{
3627 /* FIXME: We need a more portable way of releasing a backend's and a
3628 * server's connections. We need a safer way to reinitialize buffer
3629 * flags. We also need a more accurate method for computing per-request
3630 * data.
3631 */
3632 http_silent_debug(__LINE__, s);
3633
3634 s->req->cons->flags |= SI_FL_NOLINGER;
3635 s->req->cons->shutr(s->req->cons);
3636 s->req->cons->shutw(s->req->cons);
3637
3638 http_silent_debug(__LINE__, s);
3639
3640 if (s->flags & SN_BE_ASSIGNED)
3641 s->be->beconn--;
3642
3643 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3644 session_process_counters(s);
3645
3646 if (s->txn.status) {
3647 int n;
3648
3649 n = s->txn.status / 100;
3650 if (n < 1 || n > 5)
3651 n = 0;
3652
3653 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003654 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003655
Willy Tarreau24657792010-02-26 10:30:28 +01003656 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003657 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003658 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003659 }
3660
3661 /* don't count other requests' data */
3662 s->logs.bytes_in -= s->req->l - s->req->send_max;
3663 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3664
3665 /* let's do a final log if we need it */
3666 if (s->logs.logwait &&
3667 !(s->flags & SN_MONITOR) &&
3668 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3669 s->do_log(s);
3670 }
3671
3672 s->logs.accept_date = date; /* user-visible date for logging */
3673 s->logs.tv_accept = now; /* corrected date for internal use */
3674 tv_zero(&s->logs.tv_request);
3675 s->logs.t_queue = -1;
3676 s->logs.t_connect = -1;
3677 s->logs.t_data = -1;
3678 s->logs.t_close = 0;
3679 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3680 s->logs.srv_queue_size = 0; /* we will get this number soon */
3681
3682 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3683 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3684
3685 if (s->pend_pos)
3686 pendconn_free(s->pend_pos);
3687
3688 if (s->srv) {
3689 if (s->flags & SN_CURR_SESS) {
3690 s->flags &= ~SN_CURR_SESS;
3691 s->srv->cur_sess--;
3692 }
3693 if (may_dequeue_tasks(s->srv, s->be))
3694 process_srv_queue(s->srv);
3695 }
3696
3697 if (unlikely(s->srv_conn))
3698 sess_change_server(s, NULL);
3699 s->srv = NULL;
3700
3701 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3702 s->req->cons->fd = -1; /* just to help with debugging */
3703 s->req->cons->err_type = SI_ET_NONE;
3704 s->req->cons->err_loc = NULL;
3705 s->req->cons->exp = TICK_ETERNITY;
3706 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003707 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3708 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 +01003709 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003710 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3711 s->txn.meth = 0;
3712 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003713 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714 if (s->be->options2 & PR_O2_INDEPSTR)
3715 s->req->cons->flags |= SI_FL_INDEP_STR;
3716
3717 /* if the request buffer is not empty, it means we're
3718 * about to process another request, so send pending
3719 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003720 * Just don't do this if the buffer is close to be full,
3721 * because the request will wait for it to flush a little
3722 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003723 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003724 if (s->req->l > s->req->send_max) {
3725 if (s->rep->send_max &&
3726 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003727 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3728 s->rep->flags |= BF_EXPECT_MORE;
3729 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003730
3731 /* we're removing the analysers, we MUST re-enable events detection */
3732 buffer_auto_read(s->req);
3733 buffer_auto_close(s->req);
3734 buffer_auto_read(s->rep);
3735 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736
3737 /* make ->lr point to the first non-forwarded byte */
3738 s->req->lr = s->req->w + s->req->send_max;
3739 if (s->req->lr >= s->req->data + s->req->size)
3740 s->req->lr -= s->req->size;
3741 s->rep->lr = s->rep->w + s->rep->send_max;
3742 if (s->rep->lr >= s->rep->data + s->rep->size)
3743 s->rep->lr -= s->req->size;
3744
3745 s->req->analysers |= s->fe->fe_req_ana;
3746 s->rep->analysers = 0;
3747
3748 http_silent_debug(__LINE__, s);
3749}
3750
3751
3752/* This function updates the request state machine according to the response
3753 * state machine and buffer flags. It returns 1 if it changes anything (flag
3754 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3755 * it is only used to find when a request/response couple is complete. Both
3756 * this function and its equivalent should loop until both return zero. It
3757 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3758 */
3759int http_sync_req_state(struct session *s)
3760{
3761 struct buffer *buf = s->req;
3762 struct http_txn *txn = &s->txn;
3763 unsigned int old_flags = buf->flags;
3764 unsigned int old_state = txn->req.msg_state;
3765
3766 http_silent_debug(__LINE__, s);
3767 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3768 return 0;
3769
3770 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003771 /* No need to read anymore, the request was completely parsed.
3772 * We can shut the read side unless we want to abort_on_close.
3773 */
3774 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3775 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776
3777 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3778 goto wait_other_side;
3779
3780 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3781 /* The server has not finished to respond, so we
3782 * don't want to move in order not to upset it.
3783 */
3784 goto wait_other_side;
3785 }
3786
3787 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3788 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003789 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003790 txn->req.msg_state = HTTP_MSG_TUNNEL;
3791 goto wait_other_side;
3792 }
3793
3794 /* When we get here, it means that both the request and the
3795 * response have finished receiving. Depending on the connection
3796 * mode, we'll have to wait for the last bytes to leave in either
3797 * direction, and sometimes for a close to be effective.
3798 */
3799
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003800 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3801 /* Server-close mode : queue a connection close to the server */
3802 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003803 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003804 }
3805 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3806 /* Option forceclose is set, or either side wants to close,
3807 * let's enforce it now that we're not expecting any new
3808 * data to come. The caller knows the session is complete
3809 * once both states are CLOSED.
3810 */
3811 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812 buffer_shutr_now(buf);
3813 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003815 }
3816 else {
3817 /* The last possible modes are keep-alive and tunnel. Since tunnel
3818 * mode does not set the body analyser, we can't reach this place
3819 * in tunnel mode, so we're left with keep-alive only.
3820 * This mode is currently not implemented, we switch to tunnel mode.
3821 */
3822 buffer_auto_read(buf);
3823 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824 }
3825
3826 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3827 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003828 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3829
Willy Tarreau610ecce2010-01-04 21:15:02 +01003830 if (!(buf->flags & BF_OUT_EMPTY)) {
3831 txn->req.msg_state = HTTP_MSG_CLOSING;
3832 goto http_msg_closing;
3833 }
3834 else {
3835 txn->req.msg_state = HTTP_MSG_CLOSED;
3836 goto http_msg_closed;
3837 }
3838 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003839 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003840 }
3841
3842 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3843 http_msg_closing:
3844 /* nothing else to forward, just waiting for the output buffer
3845 * to be empty and for the shutw_now to take effect.
3846 */
3847 if (buf->flags & BF_OUT_EMPTY) {
3848 txn->req.msg_state = HTTP_MSG_CLOSED;
3849 goto http_msg_closed;
3850 }
3851 else if (buf->flags & BF_SHUTW) {
3852 txn->req.msg_state = HTTP_MSG_ERROR;
3853 goto wait_other_side;
3854 }
3855 }
3856
3857 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3858 http_msg_closed:
3859 goto wait_other_side;
3860 }
3861
3862 wait_other_side:
3863 http_silent_debug(__LINE__, s);
3864 return txn->req.msg_state != old_state || buf->flags != old_flags;
3865}
3866
3867
3868/* This function updates the response state machine according to the request
3869 * state machine and buffer flags. It returns 1 if it changes anything (flag
3870 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3871 * it is only used to find when a request/response couple is complete. Both
3872 * this function and its equivalent should loop until both return zero. It
3873 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3874 */
3875int http_sync_res_state(struct session *s)
3876{
3877 struct buffer *buf = s->rep;
3878 struct http_txn *txn = &s->txn;
3879 unsigned int old_flags = buf->flags;
3880 unsigned int old_state = txn->rsp.msg_state;
3881
3882 http_silent_debug(__LINE__, s);
3883 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3884 return 0;
3885
3886 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3887 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003888 * still monitor the server connection for a possible close
3889 * while the request is being uploaded, so we don't disable
3890 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003892 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003893
3894 if (txn->req.msg_state == HTTP_MSG_ERROR)
3895 goto wait_other_side;
3896
3897 if (txn->req.msg_state < HTTP_MSG_DONE) {
3898 /* The client seems to still be sending data, probably
3899 * because we got an error response during an upload.
3900 * We have the choice of either breaking the connection
3901 * or letting it pass through. Let's do the later.
3902 */
3903 goto wait_other_side;
3904 }
3905
3906 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3907 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003908 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003909 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3910 goto wait_other_side;
3911 }
3912
3913 /* When we get here, it means that both the request and the
3914 * response have finished receiving. Depending on the connection
3915 * mode, we'll have to wait for the last bytes to leave in either
3916 * direction, and sometimes for a close to be effective.
3917 */
3918
3919 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3920 /* Server-close mode : shut read and wait for the request
3921 * side to close its output buffer. The caller will detect
3922 * when we're in DONE and the other is in CLOSED and will
3923 * catch that for the final cleanup.
3924 */
3925 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3926 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003927 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003928 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3929 /* Option forceclose is set, or either side wants to close,
3930 * let's enforce it now that we're not expecting any new
3931 * data to come. The caller knows the session is complete
3932 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003933 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003934 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3935 buffer_shutr_now(buf);
3936 buffer_shutw_now(buf);
3937 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003938 }
3939 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003940 /* The last possible modes are keep-alive and tunnel. Since tunnel
3941 * mode does not set the body analyser, we can't reach this place
3942 * in tunnel mode, so we're left with keep-alive only.
3943 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003944 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003945 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003946 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 }
3948
3949 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3950 /* if we've just closed an output, let's switch */
3951 if (!(buf->flags & BF_OUT_EMPTY)) {
3952 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3953 goto http_msg_closing;
3954 }
3955 else {
3956 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3957 goto http_msg_closed;
3958 }
3959 }
3960 goto wait_other_side;
3961 }
3962
3963 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3964 http_msg_closing:
3965 /* nothing else to forward, just waiting for the output buffer
3966 * to be empty and for the shutw_now to take effect.
3967 */
3968 if (buf->flags & BF_OUT_EMPTY) {
3969 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3970 goto http_msg_closed;
3971 }
3972 else if (buf->flags & BF_SHUTW) {
3973 txn->rsp.msg_state = HTTP_MSG_ERROR;
3974 goto wait_other_side;
3975 }
3976 }
3977
3978 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3979 http_msg_closed:
3980 /* drop any pending data */
3981 buffer_ignore(buf, buf->l - buf->send_max);
3982 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003983 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003984 goto wait_other_side;
3985 }
3986
3987 wait_other_side:
3988 http_silent_debug(__LINE__, s);
3989 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
3990}
3991
3992
3993/* Resync the request and response state machines. Return 1 if either state
3994 * changes.
3995 */
3996int http_resync_states(struct session *s)
3997{
3998 struct http_txn *txn = &s->txn;
3999 int old_req_state = txn->req.msg_state;
4000 int old_res_state = txn->rsp.msg_state;
4001
4002 http_silent_debug(__LINE__, s);
4003 http_sync_req_state(s);
4004 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004005 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004006 if (!http_sync_res_state(s))
4007 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004008 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 if (!http_sync_req_state(s))
4010 break;
4011 }
4012 http_silent_debug(__LINE__, s);
4013 /* OK, both state machines agree on a compatible state.
4014 * There are a few cases we're interested in :
4015 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4016 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4017 * directions, so let's simply disable both analysers.
4018 * - HTTP_MSG_CLOSED on the response only means we must abort the
4019 * request.
4020 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4021 * with server-close mode means we've completed one request and we
4022 * must re-initialize the server connection.
4023 */
4024
4025 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4026 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4027 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4028 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4029 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004030 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004031 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004032 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004033 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004034 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004036 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4037 txn->rsp.msg_state == HTTP_MSG_ERROR ||
4038 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004039 s->rep->analysers = 0;
4040 buffer_auto_close(s->rep);
4041 buffer_auto_read(s->rep);
4042 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043 buffer_abort(s->req);
4044 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004045 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 }
4048 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4049 txn->rsp.msg_state == HTTP_MSG_DONE &&
4050 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4051 /* server-close: terminate this server connection and
4052 * reinitialize a fresh-new transaction.
4053 */
4054 http_end_txn_clean_session(s);
4055 }
4056
4057 http_silent_debug(__LINE__, s);
4058 return txn->req.msg_state != old_req_state ||
4059 txn->rsp.msg_state != old_res_state;
4060}
4061
Willy Tarreaud98cf932009-12-27 22:54:55 +01004062/* This function is an analyser which forwards request body (including chunk
4063 * sizes if any). It is called as soon as we must forward, even if we forward
4064 * zero byte. The only situation where it must not be called is when we're in
4065 * tunnel mode and we want to forward till the close. It's used both to forward
4066 * remaining data and to resync after end of body. It expects the msg_state to
4067 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4068 * read more data, or 1 once we can go on with next request or end the session.
4069 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4070 * bytes of pending data + the headers if not already done (between som and sov).
4071 * It eventually adjusts som to match sov after the data in between have been sent.
4072 */
4073int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4074{
4075 struct http_txn *txn = &s->txn;
4076 struct http_msg *msg = &s->txn.req;
4077
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004078 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4079 return 0;
4080
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004081 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4082 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
4083 /* Output closed while we were sending data. We must abort. */
4084 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004085 buffer_auto_read(req);
4086 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004087 req->analysers &= ~an_bit;
4088 return 1;
4089 }
4090
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004091 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004092
4093 /* Note that we don't have to send 100-continue back because we don't
4094 * need the data to complete our job, and it's up to the server to
4095 * decide whether to return 100, 417 or anything else in return of
4096 * an "Expect: 100-continue" header.
4097 */
4098
4099 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4100 /* we have msg->col and msg->sov which both point to the first
4101 * byte of message body. msg->som still points to the beginning
4102 * of the message. We must save the body in req->lr because it
4103 * survives buffer re-alignments.
4104 */
4105 req->lr = req->data + msg->sov;
4106 if (txn->flags & TX_REQ_TE_CHNK)
4107 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4108 else {
4109 msg->msg_state = HTTP_MSG_DATA;
4110 }
4111 }
4112
Willy Tarreaud98cf932009-12-27 22:54:55 +01004113 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004114 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004115 /* we may have some data pending */
4116 if (msg->hdr_content_len || msg->som != msg->sov) {
4117 int bytes = msg->sov - msg->som;
4118 if (bytes < 0) /* sov may have wrapped at the end */
4119 bytes += req->size;
4120 buffer_forward(req, bytes + msg->hdr_content_len);
4121 msg->hdr_content_len = 0; /* don't forward that again */
4122 msg->som = msg->sov;
4123 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004124
Willy Tarreaucaabe412010-01-03 23:08:28 +01004125 if (msg->msg_state == HTTP_MSG_DATA) {
4126 /* must still forward */
4127 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004128 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004129
4130 /* nothing left to forward */
4131 if (txn->flags & TX_REQ_TE_CHNK)
4132 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004134 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004135 }
4136 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004137 /* read the chunk size and assign it to ->hdr_content_len, then
4138 * set ->sov and ->lr to point to the body and switch to DATA or
4139 * TRAILERS state.
4140 */
4141 int ret = http_parse_chunk_size(req, msg);
4142
4143 if (!ret)
4144 goto missing_data;
4145 else if (ret < 0)
4146 goto return_bad_req;
4147 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004148 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004149 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4150 /* we want the CRLF after the data */
4151 int ret;
4152
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004153 req->lr = req->w + req->send_max;
4154 if (req->lr >= req->data + req->size)
4155 req->lr -= req->size;
4156
Willy Tarreaud98cf932009-12-27 22:54:55 +01004157 ret = http_skip_chunk_crlf(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 MSG_CHUNK_SIZE now */
4164 }
4165 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4166 int ret = http_forward_trailers(req, msg);
4167
4168 if (ret == 0)
4169 goto missing_data;
4170 else if (ret < 0)
4171 goto return_bad_req;
4172 /* we're in HTTP_MSG_DONE now */
4173 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004174 else {
4175 /* other states, DONE...TUNNEL */
4176 if (http_resync_states(s)) {
4177 /* some state changes occurred, maybe the analyser
4178 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004179 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004180 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4181 goto return_bad_req;
4182 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004183 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004185 }
4186 }
4187
Willy Tarreaud98cf932009-12-27 22:54:55 +01004188 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004189 /* stop waiting for data if the input is closed before the end */
4190 if (req->flags & BF_SHUTR)
4191 goto return_bad_req;
4192
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004193 /* waiting for the last bits to leave the buffer */
4194 if (req->flags & BF_SHUTW)
4195 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196
4197 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004198 return 0;
4199
Willy Tarreaud98cf932009-12-27 22:54:55 +01004200 return_bad_req: /* let's centralize all bad requests */
4201 txn->req.msg_state = HTTP_MSG_ERROR;
4202 txn->status = 400;
4203 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004204 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 +01004205 req->analysers = 0;
4206 s->fe->counters.failed_req++;
4207 if (s->listener->counters)
4208 s->listener->counters->failed_req++;
4209
4210 if (!(s->flags & SN_ERR_MASK))
4211 s->flags |= SN_ERR_PRXCOND;
4212 if (!(s->flags & SN_FINST_MASK))
4213 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004214 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004215 return 0;
4216}
4217
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004218/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4219 * processing can continue on next analysers, or zero if it either needs more
4220 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4221 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4222 * when it has nothing left to do, and may remove any analyser when it wants to
4223 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004224 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004225int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004226{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004227 struct http_txn *txn = &s->txn;
4228 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004229 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004230 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004231 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004232 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004233
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004234 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 +02004235 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004236 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004237 rep,
4238 rep->rex, rep->wex,
4239 rep->flags,
4240 rep->l,
4241 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004242
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004243 /*
4244 * Now parse the partial (or complete) lines.
4245 * We will check the response syntax, and also join multi-line
4246 * headers. An index of all the lines will be elaborated while
4247 * parsing.
4248 *
4249 * For the parsing, we use a 28 states FSM.
4250 *
4251 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004252 * rep->data + msg->som = beginning of response
4253 * rep->data + msg->eoh = end of processed headers / start of current one
4254 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004255 * rep->lr = first non-visited byte
4256 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004257 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004258 */
4259
Willy Tarreau83e3af02009-12-28 17:39:57 +01004260 /* There's a protected area at the end of the buffer for rewriting
4261 * purposes. We don't want to start to parse the request if the
4262 * protected area is affected, because we may have to move processed
4263 * data later, which is much more complicated.
4264 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004265 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4266 if (unlikely((rep->flags & BF_FULL) ||
4267 rep->r < rep->lr ||
4268 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4269 if (rep->send_max) {
4270 /* some data has still not left the buffer, wake us once that's done */
4271 buffer_dont_close(rep);
4272 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4273 return 0;
4274 }
4275 if (rep->l <= rep->size - global.tune.maxrewrite)
4276 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004277 }
4278
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004279 if (likely(rep->lr < rep->r))
4280 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004281 }
4282
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004283 /* 1: we might have to print this header in debug mode */
4284 if (unlikely((global.mode & MODE_DEBUG) &&
4285 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004286 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004287 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004288
Willy Tarreau962c3f42010-01-10 00:15:35 +01004289 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004290 eol = sol + msg->sl.rq.l;
4291 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004292
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004293 sol += hdr_idx_first_pos(&txn->hdr_idx);
4294 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004295
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004296 while (cur_idx) {
4297 eol = sol + txn->hdr_idx.v[cur_idx].len;
4298 debug_hdr("srvhdr", s, sol, eol);
4299 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4300 cur_idx = txn->hdr_idx.v[cur_idx].next;
4301 }
4302 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004303
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004304 /*
4305 * Now we quickly check if we have found a full valid response.
4306 * If not so, we check the FD and buffer states before leaving.
4307 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004308 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004309 * responses are checked first.
4310 *
4311 * Depending on whether the client is still there or not, we
4312 * may send an error response back or not. Note that normally
4313 * we should only check for HTTP status there, and check I/O
4314 * errors somewhere else.
4315 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004316
Willy Tarreau655dce92009-11-08 13:10:58 +01004317 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004318 /* Invalid response */
4319 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4320 /* we detected a parsing error. We want to archive this response
4321 * in the dedicated proxy area for later troubleshooting.
4322 */
4323 hdr_response_bad:
4324 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4325 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4326
4327 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004328 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004329 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004330 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4331 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004332
Willy Tarreau90deb182010-01-07 00:20:41 +01004333 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004334 rep->analysers = 0;
4335 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004336 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004337 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004338 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4339
4340 if (!(s->flags & SN_ERR_MASK))
4341 s->flags |= SN_ERR_PRXCOND;
4342 if (!(s->flags & SN_FINST_MASK))
4343 s->flags |= SN_FINST_H;
4344
4345 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004346 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004347
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004348 /* too large response does not fit in buffer. */
4349 else if (rep->flags & BF_FULL) {
4350 goto hdr_response_bad;
4351 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004352
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004353 /* read error */
4354 else if (rep->flags & BF_READ_ERROR) {
4355 if (msg->err_pos >= 0)
4356 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004357
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004358 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004359 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004360 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004361 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4362 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004363
Willy Tarreau90deb182010-01-07 00:20:41 +01004364 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004365 rep->analysers = 0;
4366 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004367 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004368 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004369 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004370
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004371 if (!(s->flags & SN_ERR_MASK))
4372 s->flags |= SN_ERR_SRVCL;
4373 if (!(s->flags & SN_FINST_MASK))
4374 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004375 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004376 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004377
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004378 /* read timeout : return a 504 to the client. */
4379 else if (rep->flags & BF_READ_TIMEOUT) {
4380 if (msg->err_pos >= 0)
4381 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004382
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004383 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004384 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004385 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004386 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4387 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004388
Willy Tarreau90deb182010-01-07 00:20:41 +01004389 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004390 rep->analysers = 0;
4391 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004392 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004393 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004394 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004395
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004396 if (!(s->flags & SN_ERR_MASK))
4397 s->flags |= SN_ERR_SRVTO;
4398 if (!(s->flags & SN_FINST_MASK))
4399 s->flags |= SN_FINST_H;
4400 return 0;
4401 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004402
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004403 /* close from server */
4404 else if (rep->flags & BF_SHUTR) {
4405 if (msg->err_pos >= 0)
4406 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004407
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004408 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004409 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004410 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004411 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4412 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004413
Willy Tarreau90deb182010-01-07 00:20:41 +01004414 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004415 rep->analysers = 0;
4416 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004417 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004418 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004420
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004421 if (!(s->flags & SN_ERR_MASK))
4422 s->flags |= SN_ERR_SRVCL;
4423 if (!(s->flags & SN_FINST_MASK))
4424 s->flags |= SN_FINST_H;
4425 return 0;
4426 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004427
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004428 /* write error to client (we don't send any message then) */
4429 else if (rep->flags & BF_WRITE_ERROR) {
4430 if (msg->err_pos >= 0)
4431 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004432
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004433 s->be->counters.failed_resp++;
4434 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004435 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004436
4437 if (!(s->flags & SN_ERR_MASK))
4438 s->flags |= SN_ERR_CLICL;
4439 if (!(s->flags & SN_FINST_MASK))
4440 s->flags |= SN_FINST_H;
4441
4442 /* process_session() will take care of the error */
4443 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004444 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004445
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446 buffer_dont_close(rep);
4447 return 0;
4448 }
4449
4450 /* More interesting part now : we know that we have a complete
4451 * response which at least looks like HTTP. We have an indicator
4452 * of each header's length, so we can parse them quickly.
4453 */
4454
4455 if (unlikely(msg->err_pos >= 0))
4456 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4457
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 /*
4459 * 1: get the status code
4460 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004461 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004462 if (n < 1 || n > 5)
4463 n = 0;
4464 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004465
Willy Tarreau5b154472009-12-21 20:11:07 +01004466 /* check if the response is HTTP/1.1 or above */
4467 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004468 ((msg->sol[5] > '1') ||
4469 ((msg->sol[5] == '1') &&
4470 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004471 txn->flags |= TX_RES_VER_11;
4472
4473 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004474 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 +01004475
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004476 /* transfer length unknown*/
4477 txn->flags &= ~TX_RES_XFER_LEN;
4478
Willy Tarreau962c3f42010-01-10 00:15:35 +01004479 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004481 if (txn->status >= 100 && txn->status < 500)
4482 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4483 else
4484 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4485
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004486 /*
4487 * 2: check for cacheability.
4488 */
4489
4490 switch (txn->status) {
4491 case 200:
4492 case 203:
4493 case 206:
4494 case 300:
4495 case 301:
4496 case 410:
4497 /* RFC2616 @13.4:
4498 * "A response received with a status code of
4499 * 200, 203, 206, 300, 301 or 410 MAY be stored
4500 * by a cache (...) unless a cache-control
4501 * directive prohibits caching."
4502 *
4503 * RFC2616 @9.5: POST method :
4504 * "Responses to this method are not cacheable,
4505 * unless the response includes appropriate
4506 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004507 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004508 if (likely(txn->meth != HTTP_METH_POST) &&
4509 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4510 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4511 break;
4512 default:
4513 break;
4514 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004515
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 /*
4517 * 3: we may need to capture headers
4518 */
4519 s->logs.logwait &= ~LW_RESP;
4520 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004521 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004522 txn->rsp.cap, s->fe->rsp_cap);
4523
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004524 /* 4: determine the transfer-length.
4525 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4526 * the presence of a message-body in a RESPONSE and its transfer length
4527 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004528 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004529 * All responses to the HEAD request method MUST NOT include a
4530 * message-body, even though the presence of entity-header fields
4531 * might lead one to believe they do. All 1xx (informational), 204
4532 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4533 * message-body. All other responses do include a message-body,
4534 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004535 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004536 * 1. Any response which "MUST NOT" include a message-body (such as the
4537 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4538 * always terminated by the first empty line after the header fields,
4539 * regardless of the entity-header fields present in the message.
4540 *
4541 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4542 * the "chunked" transfer-coding (Section 6.2) is used, the
4543 * transfer-length is defined by the use of this transfer-coding.
4544 * If a Transfer-Encoding header field is present and the "chunked"
4545 * transfer-coding is not present, the transfer-length is defined by
4546 * the sender closing the connection.
4547 *
4548 * 3. If a Content-Length header field is present, its decimal value in
4549 * OCTETs represents both the entity-length and the transfer-length.
4550 * If a message is received with both a Transfer-Encoding header
4551 * field and a Content-Length header field, the latter MUST be ignored.
4552 *
4553 * 4. If the message uses the media type "multipart/byteranges", and
4554 * the transfer-length is not otherwise specified, then this self-
4555 * delimiting media type defines the transfer-length. This media
4556 * type MUST NOT be used unless the sender knows that the recipient
4557 * can parse it; the presence in a request of a Range header with
4558 * multiple byte-range specifiers from a 1.1 client implies that the
4559 * client can parse multipart/byteranges responses.
4560 *
4561 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004562 */
4563
4564 /* Skip parsing if no content length is possible. The response flags
4565 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004566 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004567 * FIXME: should we parse anyway and return an error on chunked encoding ?
4568 */
4569 if (txn->meth == HTTP_METH_HEAD ||
4570 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004571 txn->status == 204 || txn->status == 304) {
4572 txn->flags |= TX_RES_XFER_LEN;
4573 goto skip_content_length;
4574 }
4575
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004576 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004577 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004578 while ((txn->flags & TX_RES_VER_11) &&
4579 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004580 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4581 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4582 else if (txn->flags & TX_RES_TE_CHNK) {
4583 /* bad transfer-encoding (chunked followed by something else) */
4584 use_close_only = 1;
4585 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4586 break;
4587 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004588 }
4589
4590 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4591 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004592 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004593 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4594 signed long long cl;
4595
4596 if (!ctx.vlen)
4597 goto hdr_response_bad;
4598
4599 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4600 goto hdr_response_bad; /* parse failure */
4601
4602 if (cl < 0)
4603 goto hdr_response_bad;
4604
4605 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4606 goto hdr_response_bad; /* already specified, was different */
4607
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004608 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004609 msg->hdr_content_len = cl;
4610 }
4611
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004612 /* FIXME: we should also implement the multipart/byterange method.
4613 * For now on, we resort to close mode in this case (unknown length).
4614 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004615skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004616
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 /* end of job, return OK */
4618 rep->analysers &= ~an_bit;
4619 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004620 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621 return 1;
4622}
4623
4624/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004625 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4626 * and updates t->rep->analysers. It might make sense to explode it into several
4627 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628 */
4629int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4630{
4631 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 struct http_msg *msg = &txn->rsp;
4633 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004634 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635
4636 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4637 now_ms, __FUNCTION__,
4638 t,
4639 rep,
4640 rep->rex, rep->wex,
4641 rep->flags,
4642 rep->l,
4643 rep->analysers);
4644
Willy Tarreau655dce92009-11-08 13:10:58 +01004645 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 return 0;
4647
4648 rep->analysers &= ~an_bit;
4649 rep->analyse_exp = TICK_ETERNITY;
4650
Willy Tarreau5b154472009-12-21 20:11:07 +01004651 /* Now we have to check if we need to modify the Connection header.
4652 * This is more difficult on the response than it is on the request,
4653 * because we can have two different HTTP versions and we don't know
4654 * how the client will interprete a response. For instance, let's say
4655 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4656 * HTTP/1.1 response without any header. Maybe it will bound itself to
4657 * HTTP/1.0 because it only knows about it, and will consider the lack
4658 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4659 * the lack of header as a keep-alive. Thus we will use two flags
4660 * indicating how a request MAY be understood by the client. In case
4661 * of multiple possibilities, we'll fix the header to be explicit. If
4662 * ambiguous cases such as both close and keepalive are seen, then we
4663 * will fall back to explicit close. Note that we won't take risks with
4664 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004665 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004666 */
4667
Willy Tarreaudc008c52010-02-01 16:20:08 +01004668 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4669 txn->status == 101)) {
4670 /* Either we've established an explicit tunnel, or we're
4671 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004672 * to understand the next protocols. We have to switch to tunnel
4673 * mode, so that we transfer the request and responses then let
4674 * this protocol pass unmodified. When we later implement specific
4675 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004676 * header which contains information about that protocol for
4677 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004678 */
4679 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4680 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004681 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4682 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4683 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004684 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004685
Willy Tarreau60466522010-01-18 19:08:45 +01004686 /* on unknown transfer length, we must close */
4687 if (!(txn->flags & TX_RES_XFER_LEN) &&
4688 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4689 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004690
Willy Tarreau60466522010-01-18 19:08:45 +01004691 /* now adjust header transformations depending on current state */
4692 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4693 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4694 to_del |= 2; /* remove "keep-alive" on any response */
4695 if (!(txn->flags & TX_RES_VER_11))
4696 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004697 }
Willy Tarreau60466522010-01-18 19:08:45 +01004698 else { /* SCL / KAL */
4699 to_del |= 1; /* remove "close" on any response */
4700 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4701 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004702 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004703
Willy Tarreau60466522010-01-18 19:08:45 +01004704 /* Parse and remove some headers from the connection header */
4705 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004706
Willy Tarreau60466522010-01-18 19:08:45 +01004707 /* Some keep-alive responses are converted to Server-close if
4708 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004709 */
Willy Tarreau60466522010-01-18 19:08:45 +01004710 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4711 if ((txn->flags & TX_HDR_CONN_CLO) ||
4712 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4713 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004714 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004715 }
4716
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004717 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004718 /*
4719 * 3: we will have to evaluate the filters.
4720 * As opposed to version 1.2, now they will be evaluated in the
4721 * filters order and not in the header order. This means that
4722 * each filter has to be validated among all headers.
4723 *
4724 * Filters are tried with ->be first, then with ->fe if it is
4725 * different from ->be.
4726 */
4727
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004728 cur_proxy = t->be;
4729 while (1) {
4730 struct proxy *rule_set = cur_proxy;
4731
4732 /* try headers filters */
4733 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004734 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004735 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004736 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004737 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004738 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4739 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004740 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004741 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004742 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004743 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004744 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004745 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004746 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004747 if (!(t->flags & SN_ERR_MASK))
4748 t->flags |= SN_ERR_PRXCOND;
4749 if (!(t->flags & SN_FINST_MASK))
4750 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004751 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004752 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004753 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004754
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004755 /* has the response been denied ? */
4756 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004757 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004758 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004759
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004760 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004761 if (t->listener->counters)
4762 t->listener->counters->denied_resp++;
4763
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004764 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004765 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004767 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004768 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004769 if (txn->status < 200)
4770 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004771 if (wl->cond) {
4772 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4773 ret = acl_pass(ret);
4774 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4775 ret = !ret;
4776 if (!ret)
4777 continue;
4778 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004779 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004780 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004781 }
4782
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004783 /* check whether we're already working on the frontend */
4784 if (cur_proxy == t->fe)
4785 break;
4786 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004787 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004788
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004789 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004790 * We may be facing a 100-continue response, in which case this
4791 * is not the right response, and we're waiting for the next one.
4792 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004793 * next one.
4794 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004795 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004796 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004797 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004798 msg->msg_state = HTTP_MSG_RPBEFORE;
4799 txn->status = 0;
4800 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4801 return 1;
4802 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004803 else if (unlikely(txn->status < 200))
4804 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004805
4806 /* we don't have any 1xx status code now */
4807
4808 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004809 * 4: check for server cookie.
4810 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004811 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4812 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004813 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004814
Willy Tarreaubaaee002006-06-26 02:48:02 +02004815
Willy Tarreaua15645d2007-03-18 16:22:39 +01004816 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004817 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004818 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004819 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004820 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004821
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004822 /*
4823 * 6: add server cookie in the response if needed
4824 */
4825 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004826 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004827 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004828
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004829 /* the server is known, it's not the one the client requested, we have to
4830 * insert a set-cookie here, except if we want to insert only on POST
4831 * requests and this one isn't. Note that servers which don't have cookies
4832 * (eg: some backup servers) will return a full cookie removal request.
4833 */
4834 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4835 t->be->cookie_name,
4836 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004837
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004838 if (t->be->cookie_domain)
4839 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004840
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004841 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004842 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004843 goto return_bad_resp;
4844 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004845
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004846 /* Here, we will tell an eventual cache on the client side that we don't
4847 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4848 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4849 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4850 */
4851 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004852
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004853 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4854
4855 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004856 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004857 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004858 }
4859 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004860
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004861 /*
4862 * 7: check if result will be cacheable with a cookie.
4863 * We'll block the response if security checks have caught
4864 * nasty things such as a cacheable cookie.
4865 */
4866 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4867 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004868 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004869
4870 /* we're in presence of a cacheable response containing
4871 * a set-cookie header. We'll block it as requested by
4872 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004873 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004874 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004875 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004876
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004877 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004878 if (t->listener->counters)
4879 t->listener->counters->denied_resp++;
4880
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004881 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4882 t->be->id, t->srv?t->srv->id:"<dispatch>");
4883 send_log(t->be, LOG_ALERT,
4884 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4885 t->be->id, t->srv?t->srv->id:"<dispatch>");
4886 goto return_srv_prx_502;
4887 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004888
4889 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004890 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004891 */
Willy Tarreau60466522010-01-18 19:08:45 +01004892 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4893 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4894 unsigned int want_flags = 0;
4895
4896 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4897 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4898 /* we want a keep-alive response here. Keep-alive header
4899 * required if either side is not 1.1.
4900 */
4901 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4902 want_flags |= TX_CON_KAL_SET;
4903 }
4904 else {
4905 /* we want a close response here. Close header required if
4906 * the server is 1.1, regardless of the client.
4907 */
4908 if (txn->flags & TX_RES_VER_11)
4909 want_flags |= TX_CON_CLO_SET;
4910 }
4911
4912 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4913 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004914 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004915
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004916 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01004917 if ((txn->flags & TX_RES_XFER_LEN) ||
4918 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004919 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004920
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004921 /*************************************************************
4922 * OK, that's finished for the headers. We have done what we *
4923 * could. Let's switch to the DATA state. *
4924 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004925
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004926 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004927
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004928 /* if the user wants to log as soon as possible, without counting
4929 * bytes from the server, then this is the right moment. We have
4930 * to temporarily assign bytes_out to log what we currently have.
4931 */
4932 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4933 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4934 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004935 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004936 t->logs.bytes_out = 0;
4937 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004938
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004939 /* Note: we must not try to cheat by jumping directly to DATA,
4940 * otherwise we would not let the client side wake up.
4941 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004942
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004943 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004944 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004945 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004946}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004947
Willy Tarreaud98cf932009-12-27 22:54:55 +01004948/* This function is an analyser which forwards response body (including chunk
4949 * sizes if any). It is called as soon as we must forward, even if we forward
4950 * zero byte. The only situation where it must not be called is when we're in
4951 * tunnel mode and we want to forward till the close. It's used both to forward
4952 * remaining data and to resync after end of body. It expects the msg_state to
4953 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4954 * read more data, or 1 once we can go on with next request or end the session.
4955 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4956 * bytes of pending data + the headers if not already done (between som and sov).
4957 * It eventually adjusts som to match sov after the data in between have been sent.
4958 */
4959int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4960{
4961 struct http_txn *txn = &s->txn;
4962 struct http_msg *msg = &s->txn.rsp;
4963
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004964 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4965 return 0;
4966
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004967 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004968 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004969 !s->req->analysers) {
4970 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4971 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004972 buffer_auto_read(res);
4973 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004974 res->analysers &= ~an_bit;
4975 return 1;
4976 }
4977
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004978 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004979
Willy Tarreaud98cf932009-12-27 22:54:55 +01004980 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4981 /* we have msg->col and msg->sov which both point to the first
4982 * byte of message body. msg->som still points to the beginning
4983 * of the message. We must save the body in req->lr because it
4984 * survives buffer re-alignments.
4985 */
4986 res->lr = res->data + msg->sov;
4987 if (txn->flags & TX_RES_TE_CHNK)
4988 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4989 else {
4990 msg->msg_state = HTTP_MSG_DATA;
4991 }
4992 }
4993
Willy Tarreaud98cf932009-12-27 22:54:55 +01004994 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004996 /* we may have some data pending */
4997 if (msg->hdr_content_len || msg->som != msg->sov) {
4998 int bytes = msg->sov - msg->som;
4999 if (bytes < 0) /* sov may have wrapped at the end */
5000 bytes += res->size;
5001 buffer_forward(res, bytes + msg->hdr_content_len);
5002 msg->hdr_content_len = 0; /* don't forward that again */
5003 msg->som = msg->sov;
5004 }
5005
Willy Tarreaucaabe412010-01-03 23:08:28 +01005006 if (msg->msg_state == HTTP_MSG_DATA) {
5007 /* must still forward */
5008 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005009 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005010
5011 /* nothing left to forward */
5012 if (txn->flags & TX_RES_TE_CHNK)
5013 msg->msg_state = HTTP_MSG_DATA_CRLF;
5014 else
5015 msg->msg_state = HTTP_MSG_DONE;
5016 }
5017 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005018 /* read the chunk size and assign it to ->hdr_content_len, then
5019 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5020 */
5021 int ret = http_parse_chunk_size(res, msg);
5022
5023 if (!ret)
5024 goto missing_data;
5025 else if (ret < 0)
5026 goto return_bad_res;
5027 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005028 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005029 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5030 /* we want the CRLF after the data */
5031 int ret;
5032
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005033 res->lr = res->w + res->send_max;
5034 if (res->lr >= res->data + res->size)
5035 res->lr -= res->size;
5036
Willy Tarreaud98cf932009-12-27 22:54:55 +01005037 ret = http_skip_chunk_crlf(res, msg);
5038
5039 if (!ret)
5040 goto missing_data;
5041 else if (ret < 0)
5042 goto return_bad_res;
5043 /* we're in MSG_CHUNK_SIZE now */
5044 }
5045 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5046 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005047
Willy Tarreaud98cf932009-12-27 22:54:55 +01005048 if (ret == 0)
5049 goto missing_data;
5050 else if (ret < 0)
5051 goto return_bad_res;
5052 /* we're in HTTP_MSG_DONE now */
5053 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005054 else {
5055 /* other states, DONE...TUNNEL */
5056 if (http_resync_states(s)) {
5057 http_silent_debug(__LINE__, s);
5058 /* some state changes occurred, maybe the analyser
5059 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005060 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5062 goto return_bad_res;
5063 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005064 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005065 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005066 }
5067 }
5068
Willy Tarreaud98cf932009-12-27 22:54:55 +01005069 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005070 /* stop waiting for data if the input is closed before the end */
5071 if (res->flags & BF_SHUTR)
5072 goto return_bad_res;
5073
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 if (!s->req->analysers)
5075 goto return_bad_res;
5076
Willy Tarreaud98cf932009-12-27 22:54:55 +01005077 /* forward the chunk size as well as any pending data */
5078 if (msg->hdr_content_len || msg->som != msg->sov) {
5079 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5080 msg->hdr_content_len = 0; /* don't forward that again */
5081 msg->som = msg->sov;
5082 }
5083
Willy Tarreaud98cf932009-12-27 22:54:55 +01005084 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005085 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005086 return 0;
5087
5088 return_bad_res: /* let's centralize all bad resuests */
5089 txn->rsp.msg_state = HTTP_MSG_ERROR;
5090 txn->status = 502;
Willy Tarreau148d0992010-01-10 10:21:21 +01005091 /* don't send any error message as we're in the body */
5092 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005093 res->analysers = 0;
5094 s->be->counters.failed_resp++;
5095 if (s->srv) {
5096 s->srv->counters.failed_resp++;
5097 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5098 }
5099
5100 if (!(s->flags & SN_ERR_MASK))
5101 s->flags |= SN_ERR_PRXCOND;
5102 if (!(s->flags & SN_FINST_MASK))
5103 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005104 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005105 return 0;
5106}
5107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005108/* Iterate the same filter through all request headers.
5109 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005110 * Since it can manage the switch to another backend, it updates the per-proxy
5111 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005112 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005113int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005114{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005115 char term;
5116 char *cur_ptr, *cur_end, *cur_next;
5117 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005118 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005119 struct hdr_idx_elem *cur_hdr;
5120 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005122 last_hdr = 0;
5123
Willy Tarreau962c3f42010-01-10 00:15:35 +01005124 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005125 old_idx = 0;
5126
5127 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005128 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005129 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005130 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005131 (exp->action == ACT_ALLOW ||
5132 exp->action == ACT_DENY ||
5133 exp->action == ACT_TARPIT))
5134 return 0;
5135
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005136 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005137 if (!cur_idx)
5138 break;
5139
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005140 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005141 cur_ptr = cur_next;
5142 cur_end = cur_ptr + cur_hdr->len;
5143 cur_next = cur_end + cur_hdr->cr + 1;
5144
5145 /* Now we have one header between cur_ptr and cur_end,
5146 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005147 */
5148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005149 /* The annoying part is that pattern matching needs
5150 * that we modify the contents to null-terminate all
5151 * strings before testing them.
5152 */
5153
5154 term = *cur_end;
5155 *cur_end = '\0';
5156
5157 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5158 switch (exp->action) {
5159 case ACT_SETBE:
5160 /* It is not possible to jump a second time.
5161 * FIXME: should we return an HTTP/500 here so that
5162 * the admin knows there's a problem ?
5163 */
5164 if (t->be != t->fe)
5165 break;
5166
5167 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005168 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005169 last_hdr = 1;
5170 break;
5171
5172 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005173 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005174 last_hdr = 1;
5175 break;
5176
5177 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005178 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005179 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005180
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005181 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005182 if (t->listener->counters)
5183 t->listener->counters->denied_resp++;
5184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005185 break;
5186
5187 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005188 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005189 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005190
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005191 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005192 if (t->listener->counters)
5193 t->listener->counters->denied_resp++;
5194
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005195 break;
5196
5197 case ACT_REPLACE:
5198 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5199 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5200 /* FIXME: if the user adds a newline in the replacement, the
5201 * index will not be recalculated for now, and the new line
5202 * will not be counted as a new header.
5203 */
5204
5205 cur_end += delta;
5206 cur_next += delta;
5207 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005208 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005209 break;
5210
5211 case ACT_REMOVE:
5212 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5213 cur_next += delta;
5214
Willy Tarreaufa355d42009-11-29 18:12:29 +01005215 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005216 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5217 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005218 cur_hdr->len = 0;
5219 cur_end = NULL; /* null-term has been rewritten */
5220 break;
5221
5222 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005223 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005224 if (cur_end)
5225 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005227 /* keep the link from this header to next one in case of later
5228 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005229 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005230 old_idx = cur_idx;
5231 }
5232 return 0;
5233}
5234
5235
5236/* Apply the filter to the request line.
5237 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5238 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005239 * Since it can manage the switch to another backend, it updates the per-proxy
5240 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005241 */
5242int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5243{
5244 char term;
5245 char *cur_ptr, *cur_end;
5246 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005247 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005248 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249
Willy Tarreau58f10d72006-12-04 02:26:12 +01005250
Willy Tarreau3d300592007-03-18 18:34:41 +01005251 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005252 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005253 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005254 (exp->action == ACT_ALLOW ||
5255 exp->action == ACT_DENY ||
5256 exp->action == ACT_TARPIT))
5257 return 0;
5258 else if (exp->action == ACT_REMOVE)
5259 return 0;
5260
5261 done = 0;
5262
Willy Tarreau962c3f42010-01-10 00:15:35 +01005263 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005264 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005265
5266 /* Now we have the request line between cur_ptr and cur_end */
5267
5268 /* The annoying part is that pattern matching needs
5269 * that we modify the contents to null-terminate all
5270 * strings before testing them.
5271 */
5272
5273 term = *cur_end;
5274 *cur_end = '\0';
5275
5276 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5277 switch (exp->action) {
5278 case ACT_SETBE:
5279 /* It is not possible to jump a second time.
5280 * FIXME: should we return an HTTP/500 here so that
5281 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005282 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005283 if (t->be != t->fe)
5284 break;
5285
5286 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005287 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005288 done = 1;
5289 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005291 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005292 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005293 done = 1;
5294 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005296 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005297 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005298
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005299 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005300 if (t->listener->counters)
5301 t->listener->counters->denied_resp++;
5302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005303 done = 1;
5304 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005306 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005307 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005308
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005309 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005310 if (t->listener->counters)
5311 t->listener->counters->denied_resp++;
5312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005313 done = 1;
5314 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005316 case ACT_REPLACE:
5317 *cur_end = term; /* restore the string terminator */
5318 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5319 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5320 /* FIXME: if the user adds a newline in the replacement, the
5321 * index will not be recalculated for now, and the new line
5322 * will not be counted as a new header.
5323 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005324
Willy Tarreaufa355d42009-11-29 18:12:29 +01005325 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005326 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005327 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005328 HTTP_MSG_RQMETH,
5329 cur_ptr, cur_end + 1,
5330 NULL, NULL);
5331 if (unlikely(!cur_end))
5332 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005334 /* we have a full request and we know that we have either a CR
5335 * or an LF at <ptr>.
5336 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005337 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5338 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005339 /* there is no point trying this regex on headers */
5340 return 1;
5341 }
5342 }
5343 *cur_end = term; /* restore the string terminator */
5344 return done;
5345}
Willy Tarreau97de6242006-12-27 17:18:38 +01005346
Willy Tarreau58f10d72006-12-04 02:26:12 +01005347
Willy Tarreau58f10d72006-12-04 02:26:12 +01005348
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005349/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005350 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005351 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005352 * unparsable request. Since it can manage the switch to another backend, it
5353 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005354 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005355int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005356{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005357 struct http_txn *txn = &s->txn;
5358 struct hdr_exp *exp;
5359
5360 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005361 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005363 /*
5364 * The interleaving of transformations and verdicts
5365 * makes it difficult to decide to continue or stop
5366 * the evaluation.
5367 */
5368
Willy Tarreau6c123b12010-01-28 20:22:06 +01005369 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5370 break;
5371
Willy Tarreau3d300592007-03-18 18:34:41 +01005372 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005373 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005374 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005375 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005376
5377 /* if this filter had a condition, evaluate it now and skip to
5378 * next filter if the condition does not match.
5379 */
5380 if (exp->cond) {
5381 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5382 ret = acl_pass(ret);
5383 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5384 ret = !ret;
5385
5386 if (!ret)
5387 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005388 }
5389
5390 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005391 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005392 if (unlikely(ret < 0))
5393 return -1;
5394
5395 if (likely(ret == 0)) {
5396 /* The filter did not match the request, it can be
5397 * iterated through all headers.
5398 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005399 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005400 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005401 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005402 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005403}
5404
5405
Willy Tarreaua15645d2007-03-18 16:22:39 +01005406
Willy Tarreau58f10d72006-12-04 02:26:12 +01005407/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005408 * Try to retrieve the server associated to the appsession.
5409 * If the server is found, it's assigned to the session.
5410 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005411void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005412 struct http_txn *txn = &t->txn;
5413 appsess *asession = NULL;
5414 char *sessid_temp = NULL;
5415
Cyril Bontéb21570a2009-11-29 20:04:48 +01005416 if (len > t->be->appsession_len) {
5417 len = t->be->appsession_len;
5418 }
5419
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005420 if (t->be->options2 & PR_O2_AS_REQL) {
5421 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005422 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005423 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005424 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005425 }
5426
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005427 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005428 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5429 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5430 return;
5431 }
5432
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005433 memcpy(txn->sessid, buf, len);
5434 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005435 }
5436
5437 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5438 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5439 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5440 return;
5441 }
5442
Cyril Bontéb21570a2009-11-29 20:04:48 +01005443 memcpy(sessid_temp, buf, len);
5444 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005445
5446 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5447 /* free previously allocated memory */
5448 pool_free2(apools.sessid, sessid_temp);
5449
5450 if (asession != NULL) {
5451 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5452 if (!(t->be->options2 & PR_O2_AS_REQL))
5453 asession->request_count++;
5454
5455 if (asession->serverid != NULL) {
5456 struct server *srv = t->be->srv;
5457 while (srv) {
5458 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005459 if ((srv->state & SRV_RUNNING) ||
5460 (t->be->options & PR_O_PERSIST) ||
5461 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005462 /* we found the server and it's usable */
5463 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005464 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005465 t->flags |= SN_DIRECT | SN_ASSIGNED;
5466 t->srv = srv;
5467 break;
5468 } else {
5469 txn->flags &= ~TX_CK_MASK;
5470 txn->flags |= TX_CK_DOWN;
5471 }
5472 }
5473 srv = srv->next;
5474 }
5475 }
5476 }
5477}
5478
5479/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005480 * Manage client-side cookie. It can impact performance by about 2% so it is
5481 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005482 */
5483void manage_client_side_cookies(struct session *t, struct buffer *req)
5484{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005485 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005486 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005487 char *del_colon, *del_cookie, *colon;
5488 int app_cookies;
5489
Willy Tarreau58f10d72006-12-04 02:26:12 +01005490 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005491 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005492
Willy Tarreau2a324282006-12-05 00:05:46 +01005493 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005494 * we start with the start line.
5495 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005496 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005497 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005498
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005499 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005500 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005501 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005502
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005503 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005504 cur_ptr = cur_next;
5505 cur_end = cur_ptr + cur_hdr->len;
5506 cur_next = cur_end + cur_hdr->cr + 1;
5507
5508 /* We have one full header between cur_ptr and cur_end, and the
5509 * next header starts at cur_next. We're only interested in
5510 * "Cookie:" headers.
5511 */
5512
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005513 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5514 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005515 old_idx = cur_idx;
5516 continue;
5517 }
5518
5519 /* Now look for cookies. Conforming to RFC2109, we have to support
5520 * attributes whose name begin with a '$', and associate them with
5521 * the right cookie, if we want to delete this cookie.
5522 * So there are 3 cases for each cookie read :
5523 * 1) it's a special attribute, beginning with a '$' : ignore it.
5524 * 2) it's a server id cookie that we *MAY* want to delete : save
5525 * some pointers on it (last semi-colon, beginning of cookie...)
5526 * 3) it's an application cookie : we *MAY* have to delete a previous
5527 * "special" cookie.
5528 * At the end of loop, if a "special" cookie remains, we may have to
5529 * remove it. If no application cookie persists in the header, we
5530 * *MUST* delete it
5531 */
5532
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005533 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005534
Willy Tarreau58f10d72006-12-04 02:26:12 +01005535 /* del_cookie == NULL => nothing to be deleted */
5536 del_colon = del_cookie = NULL;
5537 app_cookies = 0;
5538
5539 while (p1 < cur_end) {
5540 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005541 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005542 while (p1 < cur_end) {
5543 if (*p1 == ';' || *p1 == ',')
5544 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005545 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005546 break;
5547 p1++;
5548 }
5549
5550 if (p1 == cur_end)
5551 break;
5552
5553 /* p1 is at the beginning of the cookie name */
5554 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005555 while (p2 < cur_end && *p2 != '=') {
5556 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5557 /* oops, the cookie name was truncated, resync */
5558 p1 = p2;
5559 goto resync_name;
5560 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005561 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005562 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005563
5564 if (p2 == cur_end)
5565 break;
5566
5567 p3 = p2 + 1; /* skips the '=' sign */
5568 if (p3 == cur_end)
5569 break;
5570
Willy Tarreau305ae852010-01-03 19:45:54 +01005571 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5572 p5 = p4 = p3;
5573 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5574 if (!isspace((unsigned char)*p5))
5575 p4 = p5 + 1;
5576 p5++;
5577 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005578
5579 /* here, we have the cookie name between p1 and p2,
5580 * and its value between p3 and p4.
5581 * we can process it :
5582 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005583 * Cookie: NAME=VALUE ;
5584 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005585 * | || || +--> p4
5586 * | || |+-------> p3
5587 * | || +--------> p2
5588 * | |+------------> p1
5589 * | +-------------> colon
5590 * +--------------------> cur_ptr
5591 */
5592
5593 if (*p1 == '$') {
5594 /* skip this one */
5595 }
5596 else {
5597 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005598 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005599 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005600 (p4 - p1 >= t->fe->capture_namelen) &&
5601 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005602 int log_len = p4 - p1;
5603
Willy Tarreau086b3b42007-05-13 21:45:51 +02005604 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005605 Alert("HTTP logging : out of memory.\n");
5606 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005607 if (log_len > t->fe->capture_len)
5608 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005609 memcpy(txn->cli_cookie, p1, log_len);
5610 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005611 }
5612 }
5613
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005614 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5615 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005616 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005617 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005618 char *delim;
5619
5620 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5621 * have the server ID betweek p3 and delim, and the original cookie between
5622 * delim+1 and p4. Otherwise, delim==p4 :
5623 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005624 * Cookie: NAME=SRV~VALUE ;
5625 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005626 * | || || | +--> p4
5627 * | || || +--------> delim
5628 * | || |+-----------> p3
5629 * | || +------------> p2
5630 * | |+----------------> p1
5631 * | +-----------------> colon
5632 * +------------------------> cur_ptr
5633 */
5634
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005635 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005636 for (delim = p3; delim < p4; delim++)
5637 if (*delim == COOKIE_DELIM)
5638 break;
5639 }
5640 else
5641 delim = p4;
5642
5643
5644 /* Here, we'll look for the first running server which supports the cookie.
5645 * This allows to share a same cookie between several servers, for example
5646 * to dedicate backup servers to specific servers only.
5647 * However, to prevent clients from sticking to cookie-less backup server
5648 * when they have incidentely learned an empty cookie, we simply ignore
5649 * empty cookies and mark them as invalid.
5650 */
5651 if (delim == p3)
5652 srv = NULL;
5653
5654 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005655 if (srv->cookie && (srv->cklen == delim - p3) &&
5656 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005657 if ((srv->state & SRV_RUNNING) ||
5658 (t->be->options & PR_O_PERSIST) ||
5659 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005660 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005661 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005662 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005663 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005664 t->srv = srv;
5665 break;
5666 } else {
5667 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005668 txn->flags &= ~TX_CK_MASK;
5669 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005670 }
5671 }
5672 srv = srv->next;
5673 }
5674
Willy Tarreau3d300592007-03-18 18:34:41 +01005675 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005676 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005677 txn->flags &= ~TX_CK_MASK;
5678 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005679 }
5680
5681 /* depending on the cookie mode, we may have to either :
5682 * - delete the complete cookie if we're in insert+indirect mode, so that
5683 * the server never sees it ;
5684 * - remove the server id from the cookie value, and tag the cookie as an
5685 * application cookie so that it does not get accidentely removed later,
5686 * if we're in cookie prefix mode
5687 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005688 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005689 int delta; /* negative */
5690
5691 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5692 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005693 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005694 cur_end += delta;
5695 cur_next += delta;
5696 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005697 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005698
5699 del_cookie = del_colon = NULL;
5700 app_cookies++; /* protect the header from deletion */
5701 }
5702 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005703 (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 +01005704 del_cookie = p1;
5705 del_colon = colon;
5706 }
5707 } else {
5708 /* now we know that we must keep this cookie since it's
5709 * not ours. But if we wanted to delete our cookie
5710 * earlier, we cannot remove the complete header, but we
5711 * can remove the previous block itself.
5712 */
5713 app_cookies++;
5714
5715 if (del_cookie != NULL) {
5716 int delta; /* negative */
5717
5718 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5719 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005720 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005721 cur_end += delta;
5722 cur_next += delta;
5723 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005724 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005725 del_cookie = del_colon = NULL;
5726 }
5727 }
5728
Cyril Bontéb21570a2009-11-29 20:04:48 +01005729 if (t->be->appsession_name != NULL) {
5730 int cmp_len, value_len;
5731 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005732
Cyril Bontéb21570a2009-11-29 20:04:48 +01005733 if (t->be->options2 & PR_O2_AS_PFX) {
5734 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5735 value_begin = p1 + t->be->appsession_name_len;
5736 value_len = p4 - p1 - t->be->appsession_name_len;
5737 } else {
5738 cmp_len = p2 - p1;
5739 value_begin = p3;
5740 value_len = p4 - p3;
5741 }
5742
5743 /* let's see if the cookie is our appcookie */
5744 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5745 /* Cool... it's the right one */
5746 manage_client_side_appsession(t, value_begin, value_len);
5747 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005748#if defined(DEBUG_HASH)
5749 Alert("manage_client_side_cookies\n");
5750 appsession_hash_dump(&(t->be->htbl_proxy));
5751#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005752 }/* end if ((t->proxy->appsession_name != NULL) ... */
5753 }
5754
5755 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005756 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005757 } /* while (p1 < cur_end) */
5758
5759 /* There's no more cookie on this line.
5760 * We may have marked the last one(s) for deletion.
5761 * We must do this now in two ways :
5762 * - if there is no app cookie, we simply delete the header ;
5763 * - if there are app cookies, we must delete the end of the
5764 * string properly, including the colon/semi-colon before
5765 * the cookie name.
5766 */
5767 if (del_cookie != NULL) {
5768 int delta;
5769 if (app_cookies) {
5770 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5771 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005772 cur_hdr->len += delta;
5773 } else {
5774 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005775
5776 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005777 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5778 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005779 cur_hdr->len = 0;
5780 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005781 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005782 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005783 }
5784
5785 /* keep the link from this header to next one */
5786 old_idx = cur_idx;
5787 } /* end of cookie processing on this header */
5788}
5789
5790
Willy Tarreaua15645d2007-03-18 16:22:39 +01005791/* Iterate the same filter through all response headers contained in <rtr>.
5792 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5793 */
5794int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5795{
5796 char term;
5797 char *cur_ptr, *cur_end, *cur_next;
5798 int cur_idx, old_idx, last_hdr;
5799 struct http_txn *txn = &t->txn;
5800 struct hdr_idx_elem *cur_hdr;
5801 int len, delta;
5802
5803 last_hdr = 0;
5804
Willy Tarreau962c3f42010-01-10 00:15:35 +01005805 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005806 old_idx = 0;
5807
5808 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005809 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005810 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005811 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005812 (exp->action == ACT_ALLOW ||
5813 exp->action == ACT_DENY))
5814 return 0;
5815
5816 cur_idx = txn->hdr_idx.v[old_idx].next;
5817 if (!cur_idx)
5818 break;
5819
5820 cur_hdr = &txn->hdr_idx.v[cur_idx];
5821 cur_ptr = cur_next;
5822 cur_end = cur_ptr + cur_hdr->len;
5823 cur_next = cur_end + cur_hdr->cr + 1;
5824
5825 /* Now we have one header between cur_ptr and cur_end,
5826 * and the next header starts at cur_next.
5827 */
5828
5829 /* The annoying part is that pattern matching needs
5830 * that we modify the contents to null-terminate all
5831 * strings before testing them.
5832 */
5833
5834 term = *cur_end;
5835 *cur_end = '\0';
5836
5837 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5838 switch (exp->action) {
5839 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005840 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005841 last_hdr = 1;
5842 break;
5843
5844 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005845 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005846 last_hdr = 1;
5847 break;
5848
5849 case ACT_REPLACE:
5850 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5851 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5852 /* FIXME: if the user adds a newline in the replacement, the
5853 * index will not be recalculated for now, and the new line
5854 * will not be counted as a new header.
5855 */
5856
5857 cur_end += delta;
5858 cur_next += delta;
5859 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005860 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005861 break;
5862
5863 case ACT_REMOVE:
5864 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5865 cur_next += delta;
5866
Willy Tarreaufa355d42009-11-29 18:12:29 +01005867 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5869 txn->hdr_idx.used--;
5870 cur_hdr->len = 0;
5871 cur_end = NULL; /* null-term has been rewritten */
5872 break;
5873
5874 }
5875 }
5876 if (cur_end)
5877 *cur_end = term; /* restore the string terminator */
5878
5879 /* keep the link from this header to next one in case of later
5880 * removal of next header.
5881 */
5882 old_idx = cur_idx;
5883 }
5884 return 0;
5885}
5886
5887
5888/* Apply the filter to the status line in the response buffer <rtr>.
5889 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5890 * or -1 if a replacement resulted in an invalid status line.
5891 */
5892int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5893{
5894 char term;
5895 char *cur_ptr, *cur_end;
5896 int done;
5897 struct http_txn *txn = &t->txn;
5898 int len, delta;
5899
5900
Willy Tarreau3d300592007-03-18 18:34:41 +01005901 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005902 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005903 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005904 (exp->action == ACT_ALLOW ||
5905 exp->action == ACT_DENY))
5906 return 0;
5907 else if (exp->action == ACT_REMOVE)
5908 return 0;
5909
5910 done = 0;
5911
Willy Tarreau962c3f42010-01-10 00:15:35 +01005912 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005913 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5914
5915 /* Now we have the status line between cur_ptr and cur_end */
5916
5917 /* The annoying part is that pattern matching needs
5918 * that we modify the contents to null-terminate all
5919 * strings before testing them.
5920 */
5921
5922 term = *cur_end;
5923 *cur_end = '\0';
5924
5925 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5926 switch (exp->action) {
5927 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005928 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005929 done = 1;
5930 break;
5931
5932 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005933 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005934 done = 1;
5935 break;
5936
5937 case ACT_REPLACE:
5938 *cur_end = term; /* restore the string terminator */
5939 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5940 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5941 /* FIXME: if the user adds a newline in the replacement, the
5942 * index will not be recalculated for now, and the new line
5943 * will not be counted as a new header.
5944 */
5945
Willy Tarreaufa355d42009-11-29 18:12:29 +01005946 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005947 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005949 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005950 cur_ptr, cur_end + 1,
5951 NULL, NULL);
5952 if (unlikely(!cur_end))
5953 return -1;
5954
5955 /* we have a full respnse and we know that we have either a CR
5956 * or an LF at <ptr>.
5957 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005958 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5960 /* there is no point trying this regex on headers */
5961 return 1;
5962 }
5963 }
5964 *cur_end = term; /* restore the string terminator */
5965 return done;
5966}
5967
5968
5969
5970/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005971 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005972 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5973 * unparsable response.
5974 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005975int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005976{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005977 struct http_txn *txn = &s->txn;
5978 struct hdr_exp *exp;
5979
5980 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005981 int ret;
5982
5983 /*
5984 * The interleaving of transformations and verdicts
5985 * makes it difficult to decide to continue or stop
5986 * the evaluation.
5987 */
5988
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005989 if (txn->flags & TX_SVDENY)
5990 break;
5991
Willy Tarreau3d300592007-03-18 18:34:41 +01005992 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005993 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5994 exp->action == ACT_PASS)) {
5995 exp = exp->next;
5996 continue;
5997 }
5998
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005999 /* if this filter had a condition, evaluate it now and skip to
6000 * next filter if the condition does not match.
6001 */
6002 if (exp->cond) {
6003 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6004 ret = acl_pass(ret);
6005 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6006 ret = !ret;
6007 if (!ret)
6008 continue;
6009 }
6010
Willy Tarreaua15645d2007-03-18 16:22:39 +01006011 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006012 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006013 if (unlikely(ret < 0))
6014 return -1;
6015
6016 if (likely(ret == 0)) {
6017 /* The filter did not match the response, it can be
6018 * iterated through all headers.
6019 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006020 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006021 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006022 }
6023 return 0;
6024}
6025
6026
6027
6028/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006029 * Manage server-side cookies. It can impact performance by about 2% so it is
6030 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006031 */
6032void manage_server_side_cookies(struct session *t, struct buffer *rtr)
6033{
6034 struct http_txn *txn = &t->txn;
6035 char *p1, *p2, *p3, *p4;
6036
Willy Tarreaua15645d2007-03-18 16:22:39 +01006037 char *cur_ptr, *cur_end, *cur_next;
6038 int cur_idx, old_idx, delta;
6039
Willy Tarreaua15645d2007-03-18 16:22:39 +01006040 /* Iterate through the headers.
6041 * we start with the start line.
6042 */
6043 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006044 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006045
6046 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6047 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006048 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006049
6050 cur_hdr = &txn->hdr_idx.v[cur_idx];
6051 cur_ptr = cur_next;
6052 cur_end = cur_ptr + cur_hdr->len;
6053 cur_next = cur_end + cur_hdr->cr + 1;
6054
6055 /* We have one full header between cur_ptr and cur_end, and the
6056 * next header starts at cur_next. We're only interested in
6057 * "Cookie:" headers.
6058 */
6059
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006060 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
6061 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006062 old_idx = cur_idx;
6063 continue;
6064 }
6065
6066 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01006067 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006068
6069
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006070 /* maybe we only wanted to see if there was a set-cookie. Note that
6071 * the cookie capture is declared in the fronend.
6072 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006073 if (t->be->cookie_name == NULL &&
6074 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006075 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006076 return;
6077
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006078 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006079
6080 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006081 if (p1 == cur_end || *p1 == ';') /* end of cookie */
6082 break;
6083
6084 /* p1 is at the beginning of the cookie name */
6085 p2 = p1;
6086
6087 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
6088 p2++;
6089
6090 if (p2 == cur_end || *p2 == ';') /* next cookie */
6091 break;
6092
6093 p3 = p2 + 1; /* skip the '=' sign */
6094 if (p3 == cur_end)
6095 break;
6096
6097 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006098 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01006099 p4++;
6100
6101 /* here, we have the cookie name between p1 and p2,
6102 * and its value between p3 and p4.
6103 * we can process it.
6104 */
6105
6106 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006107 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006108 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006109 (p4 - p1 >= t->fe->capture_namelen) &&
6110 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006111 int log_len = p4 - p1;
6112
Willy Tarreau086b3b42007-05-13 21:45:51 +02006113 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006114 Alert("HTTP logging : out of memory.\n");
6115 }
6116
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006117 if (log_len > t->fe->capture_len)
6118 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006119 memcpy(txn->srv_cookie, p1, log_len);
6120 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006121 }
6122
6123 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006124 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6125 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006126 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01006127 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006128
6129 /* If the cookie is in insert mode on a known server, we'll delete
6130 * this occurrence because we'll insert another one later.
6131 * We'll delete it too if the "indirect" option is set and we're in
6132 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006133 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
6134 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006135 /* this header must be deleted */
6136 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6137 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6138 txn->hdr_idx.used--;
6139 cur_hdr->len = 0;
6140 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006141 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006142
Willy Tarreau3d300592007-03-18 18:34:41 +01006143 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006144 }
6145 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006146 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006147 /* replace bytes p3->p4 with the cookie name associated
6148 * with this server since we know it.
6149 */
6150 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
6151 cur_hdr->len += delta;
6152 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006153 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006154
Willy Tarreau3d300592007-03-18 18:34:41 +01006155 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006156 }
6157 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006158 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006159 /* insert the cookie name associated with this server
6160 * before existing cookie, and insert a delimitor between them..
6161 */
6162 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6163 cur_hdr->len += delta;
6164 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006165 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006166
6167 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006168 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006169 }
6170 }
6171 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006172 else if (t->be->appsession_name != NULL) {
6173 int cmp_len, value_len;
6174 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006175
Cyril Bontéb21570a2009-11-29 20:04:48 +01006176 if (t->be->options2 & PR_O2_AS_PFX) {
6177 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6178 value_begin = p1 + t->be->appsession_name_len;
6179 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6180 } else {
6181 cmp_len = p2 - p1;
6182 value_begin = p3;
6183 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006184 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006185
6186 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
6187 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006188 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006189 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006190 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006191 }
6192 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006193 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006194 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6195 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6196 return;
6197 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006198 memcpy(txn->sessid, value_begin, value_len);
6199 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006200 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006201 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006202 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6203 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006204 /* keep the link from this header to next one */
6205 old_idx = cur_idx;
6206 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006207
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006208 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006209 appsess *asession = NULL;
6210 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006211 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006212 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006213 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006214 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6215 Alert("Not enough Memory process_srv():asession:calloc().\n");
6216 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6217 return;
6218 }
6219 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6220 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6221 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006222 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006223 return;
6224 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006225 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006226 asession->sessid[t->be->appsession_len] = 0;
6227
Willy Tarreau1fac7532010-01-09 19:23:06 +01006228 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006229 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6230 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6231 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006232 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006233 return;
6234 }
6235 asession->serverid[0] = '\0';
6236 memcpy(asession->serverid, t->srv->id, server_id_len);
6237
6238 asession->request_count = 0;
6239 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6240 }
6241
6242 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6243 asession->request_count++;
6244 }
6245
6246#if defined(DEBUG_HASH)
6247 Alert("manage_server_side_cookies\n");
6248 appsession_hash_dump(&(t->be->htbl_proxy));
6249#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006250}
6251
6252
6253
6254/*
6255 * Check if response is cacheable or not. Updates t->flags.
6256 */
6257void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6258{
6259 struct http_txn *txn = &t->txn;
6260 char *p1, *p2;
6261
6262 char *cur_ptr, *cur_end, *cur_next;
6263 int cur_idx;
6264
Willy Tarreau5df51872007-11-25 16:20:08 +01006265 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006266 return;
6267
6268 /* Iterate through the headers.
6269 * we start with the start line.
6270 */
6271 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006272 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006273
6274 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6275 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006276 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006277
6278 cur_hdr = &txn->hdr_idx.v[cur_idx];
6279 cur_ptr = cur_next;
6280 cur_end = cur_ptr + cur_hdr->len;
6281 cur_next = cur_end + cur_hdr->cr + 1;
6282
6283 /* We have one full header between cur_ptr and cur_end, and the
6284 * next header starts at cur_next. We're only interested in
6285 * "Cookie:" headers.
6286 */
6287
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006288 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6289 if (val) {
6290 if ((cur_end - (cur_ptr + val) >= 8) &&
6291 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6292 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6293 return;
6294 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006295 }
6296
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006297 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6298 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006299 continue;
6300
6301 /* OK, right now we know we have a cache-control header at cur_ptr */
6302
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006303 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006304
6305 if (p1 >= cur_end) /* no more info */
6306 continue;
6307
6308 /* p1 is at the beginning of the value */
6309 p2 = p1;
6310
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006311 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006312 p2++;
6313
6314 /* we have a complete value between p1 and p2 */
6315 if (p2 < cur_end && *p2 == '=') {
6316 /* we have something of the form no-cache="set-cookie" */
6317 if ((cur_end - p1 >= 21) &&
6318 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6319 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006320 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006321 continue;
6322 }
6323
6324 /* OK, so we know that either p2 points to the end of string or to a comma */
6325 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6326 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6327 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6328 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006329 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006330 return;
6331 }
6332
6333 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006334 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006335 continue;
6336 }
6337 }
6338}
6339
6340
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341/*
6342 * Try to retrieve a known appsession in the URI, then the associated server.
6343 * If the server is found, it's assigned to the session.
6344 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006345void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006347 char *end_params, *first_param, *cur_param, *next_param;
6348 char separator;
6349 int value_len;
6350
6351 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006353 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006354 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006357
Cyril Bontéb21570a2009-11-29 20:04:48 +01006358 first_param = NULL;
6359 switch (mode) {
6360 case PR_O2_AS_M_PP:
6361 first_param = memchr(begin, ';', len);
6362 break;
6363 case PR_O2_AS_M_QS:
6364 first_param = memchr(begin, '?', len);
6365 break;
6366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006367
Cyril Bontéb21570a2009-11-29 20:04:48 +01006368 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006369 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006370 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006371
Cyril Bontéb21570a2009-11-29 20:04:48 +01006372 switch (mode) {
6373 case PR_O2_AS_M_PP:
6374 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6375 end_params = (char *) begin + len;
6376 }
6377 separator = ';';
6378 break;
6379 case PR_O2_AS_M_QS:
6380 end_params = (char *) begin + len;
6381 separator = '&';
6382 break;
6383 default:
6384 /* unknown mode, shouldn't happen */
6385 return;
6386 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006387
Cyril Bontéb21570a2009-11-29 20:04:48 +01006388 cur_param = next_param = end_params;
6389 while (cur_param > first_param) {
6390 cur_param--;
6391 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6392 /* let's see if this is the appsession parameter */
6393 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6394 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6395 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6396 /* Cool... it's the right one */
6397 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6398 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6399 if (value_len > 0) {
6400 manage_client_side_appsession(t, cur_param, value_len);
6401 }
6402 break;
6403 }
6404 next_param = cur_param;
6405 }
6406 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006407#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006408 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006409 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006410#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411}
6412
Willy Tarreaub2513902006-12-17 14:52:38 +01006413/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006414 * In a GET or HEAD request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006415 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01006416 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006417 * It is assumed that the request is either a HEAD or GET and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006418 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006419 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006420 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01006421 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006422int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01006423{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006424 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006425 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006427
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006428 if (!uri_auth)
6429 return 0;
6430
6431 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)
6432 return 0;
6433
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006434 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006436 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006437 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006438 return 0;
6439
Willy Tarreau962c3f42010-01-10 00:15:35 +01006440 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006442 /* the URI is in h */
6443 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006444 return 0;
6445
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006446 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006447 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006448 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006449 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006450 break;
6451 }
6452 h++;
6453 }
6454
6455 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006456 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6457 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006458 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006459 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006460 break;
6461 }
6462 h++;
6463 }
6464 }
6465
Willy Tarreau962c3f42010-01-10 00:15:35 +01006466 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6467 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006468 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006469 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006470 break;
6471 }
6472 h++;
6473 }
6474
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006475 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6476
Willy Tarreaub2513902006-12-17 14:52:38 +01006477 return 1;
6478}
6479
Willy Tarreau4076a152009-04-02 15:18:36 +02006480/*
6481 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006482 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6483 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006484 */
6485void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6486 struct buffer *buf, struct http_msg *msg,
6487 struct proxy *other_end)
6488{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006489 es->len = buf->r - (buf->data + msg->som);
6490 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006491 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006492 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006493 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006494 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006495 es->when = date; // user-visible date
6496 es->sid = s->uniq_id;
6497 es->srv = s->srv;
6498 es->oe = other_end;
6499 es->src = s->cli_addr;
6500}
Willy Tarreaub2513902006-12-17 14:52:38 +01006501
Willy Tarreaubaaee002006-06-26 02:48:02 +02006502/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503 * Print a debug line with a header
6504 */
6505void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6506{
6507 int len, max;
6508 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006509 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006510 max = end - start;
6511 UBOUND(max, sizeof(trash) - len - 1);
6512 len += strlcpy2(trash + len, start, max + 1);
6513 trash[len++] = '\n';
6514 write(1, trash, len);
6515}
6516
Willy Tarreau0937bc42009-12-22 15:03:09 +01006517/*
6518 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6519 * the required fields are properly allocated and that we only need to (re)init
6520 * them. This should be used before processing any new request.
6521 */
6522void http_init_txn(struct session *s)
6523{
6524 struct http_txn *txn = &s->txn;
6525 struct proxy *fe = s->fe;
6526
6527 txn->flags = 0;
6528 txn->status = -1;
6529
6530 txn->req.sol = txn->req.eol = NULL;
6531 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6532 txn->rsp.sol = txn->rsp.eol = NULL;
6533 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6534 txn->req.hdr_content_len = 0LL;
6535 txn->rsp.hdr_content_len = 0LL;
6536 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6537 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006538
6539 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006540
6541 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6542 if (fe->options2 & PR_O2_REQBUG_OK)
6543 txn->req.err_pos = -1; /* let buggy requests pass */
6544
Willy Tarreau46023632010-01-07 22:51:47 +01006545 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006546 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6547
Willy Tarreau46023632010-01-07 22:51:47 +01006548 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006549 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6550
6551 if (txn->hdr_idx.v)
6552 hdr_idx_init(&txn->hdr_idx);
6553}
6554
6555/* to be used at the end of a transaction */
6556void http_end_txn(struct session *s)
6557{
6558 struct http_txn *txn = &s->txn;
6559
6560 /* these ones will have been dynamically allocated */
6561 pool_free2(pool2_requri, txn->uri);
6562 pool_free2(pool2_capture, txn->cli_cookie);
6563 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006564 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006565
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006566 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006567 txn->uri = NULL;
6568 txn->srv_cookie = NULL;
6569 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006570
6571 if (txn->req.cap) {
6572 struct cap_hdr *h;
6573 for (h = s->fe->req_cap; h; h = h->next)
6574 pool_free2(h->pool, txn->req.cap[h->index]);
6575 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6576 }
6577
6578 if (txn->rsp.cap) {
6579 struct cap_hdr *h;
6580 for (h = s->fe->rsp_cap; h; h = h->next)
6581 pool_free2(h->pool, txn->rsp.cap[h->index]);
6582 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6583 }
6584
Willy Tarreau0937bc42009-12-22 15:03:09 +01006585}
6586
6587/* to be used at the end of a transaction to prepare a new one */
6588void http_reset_txn(struct session *s)
6589{
6590 http_end_txn(s);
6591 http_init_txn(s);
6592
6593 s->be = s->fe;
6594 s->req->analysers = s->listener->analysers;
6595 s->logs.logwait = s->fe->to_log;
6596 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006597 /* re-init store persistence */
6598 s->store_count = 0;
6599
Willy Tarreau0937bc42009-12-22 15:03:09 +01006600 s->pend_pos = NULL;
6601 s->conn_retries = s->be->conn_retries;
6602
6603 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6604
Willy Tarreau739cfba2010-01-25 23:11:14 +01006605 /* We must trim any excess data from the response buffer, because we
6606 * may have blocked an invalid response from a server that we don't
6607 * want to accidentely forward once we disable the analysers, nor do
6608 * we want those data to come along with next response. A typical
6609 * example of such data would be from a buggy server responding to
6610 * a HEAD with some data, or sending more than the advertised
6611 * content-length.
6612 */
6613 if (unlikely(s->rep->l > s->rep->send_max)) {
6614 s->rep->l = s->rep->send_max;
6615 s->rep->r = s->rep->w + s->rep->l;
6616 if (s->rep->r >= s->rep->data + s->rep->size)
6617 s->rep->r -= s->rep->size;
6618 }
6619
Willy Tarreau0937bc42009-12-22 15:03:09 +01006620 s->req->rto = s->fe->timeout.client;
6621 s->req->wto = s->be->timeout.server;
6622 s->req->cto = s->be->timeout.connect;
6623
6624 s->rep->rto = s->be->timeout.server;
6625 s->rep->wto = s->fe->timeout.client;
6626 s->rep->cto = TICK_ETERNITY;
6627
6628 s->req->rex = TICK_ETERNITY;
6629 s->req->wex = TICK_ETERNITY;
6630 s->req->analyse_exp = TICK_ETERNITY;
6631 s->rep->rex = TICK_ETERNITY;
6632 s->rep->wex = TICK_ETERNITY;
6633 s->rep->analyse_exp = TICK_ETERNITY;
6634}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006635
Willy Tarreau8797c062007-05-07 00:55:35 +02006636/************************************************************************/
6637/* The code below is dedicated to ACL parsing and matching */
6638/************************************************************************/
6639
6640
6641
6642
6643/* 1. Check on METHOD
6644 * We use the pre-parsed method if it is known, and store its number as an
6645 * integer. If it is unknown, we use the pointer and the length.
6646 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006647static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006648{
6649 int len, meth;
6650
Willy Tarreauae8b7962007-06-09 23:10:04 +02006651 len = strlen(*text);
6652 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006653
6654 pattern->val.i = meth;
6655 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006656 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006657 if (!pattern->ptr.str)
6658 return 0;
6659 pattern->len = len;
6660 }
6661 return 1;
6662}
6663
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006664static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006665acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6666 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006667{
6668 int meth;
6669 struct http_txn *txn = l7;
6670
Willy Tarreaub6866442008-07-14 23:54:42 +02006671 if (!txn)
6672 return 0;
6673
Willy Tarreau655dce92009-11-08 13:10:58 +01006674 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006675 return 0;
6676
Willy Tarreau8797c062007-05-07 00:55:35 +02006677 meth = txn->meth;
6678 test->i = meth;
6679 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006680 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6681 /* ensure the indexes are not affected */
6682 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006683 test->len = txn->req.sl.rq.m_l;
6684 test->ptr = txn->req.sol;
6685 }
6686 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6687 return 1;
6688}
6689
6690static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6691{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006692 int icase;
6693
Willy Tarreau8797c062007-05-07 00:55:35 +02006694 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006695 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006696
6697 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006698 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006699
6700 /* Other method, we must compare the strings */
6701 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006702 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006703
6704 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6705 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6706 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006707 return ACL_PAT_FAIL;
6708 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006709}
6710
6711/* 2. Check on Request/Status Version
6712 * We simply compare strings here.
6713 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006714static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006715{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006716 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006717 if (!pattern->ptr.str)
6718 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006719 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006720 return 1;
6721}
6722
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006723static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006724acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6725 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006726{
6727 struct http_txn *txn = l7;
6728 char *ptr;
6729 int len;
6730
Willy Tarreaub6866442008-07-14 23:54:42 +02006731 if (!txn)
6732 return 0;
6733
Willy Tarreau655dce92009-11-08 13:10:58 +01006734 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006735 return 0;
6736
Willy Tarreau8797c062007-05-07 00:55:35 +02006737 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006738 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006739
6740 while ((len-- > 0) && (*ptr++ != '/'));
6741 if (len <= 0)
6742 return 0;
6743
6744 test->ptr = ptr;
6745 test->len = len;
6746
6747 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6748 return 1;
6749}
6750
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006751static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006752acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6753 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006754{
6755 struct http_txn *txn = l7;
6756 char *ptr;
6757 int len;
6758
Willy Tarreaub6866442008-07-14 23:54:42 +02006759 if (!txn)
6760 return 0;
6761
Willy Tarreau655dce92009-11-08 13:10:58 +01006762 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006763 return 0;
6764
Willy Tarreau8797c062007-05-07 00:55:35 +02006765 len = txn->rsp.sl.st.v_l;
6766 ptr = txn->rsp.sol;
6767
6768 while ((len-- > 0) && (*ptr++ != '/'));
6769 if (len <= 0)
6770 return 0;
6771
6772 test->ptr = ptr;
6773 test->len = len;
6774
6775 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6776 return 1;
6777}
6778
6779/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006780static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006781acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6782 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006783{
6784 struct http_txn *txn = l7;
6785 char *ptr;
6786 int len;
6787
Willy Tarreaub6866442008-07-14 23:54:42 +02006788 if (!txn)
6789 return 0;
6790
Willy Tarreau655dce92009-11-08 13:10:58 +01006791 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006792 return 0;
6793
Willy Tarreau8797c062007-05-07 00:55:35 +02006794 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006795 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006796
6797 test->i = __strl2ui(ptr, len);
6798 test->flags = ACL_TEST_F_VOL_1ST;
6799 return 1;
6800}
6801
6802/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006803static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006804acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6805 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006806{
6807 struct http_txn *txn = l7;
6808
Willy Tarreaub6866442008-07-14 23:54:42 +02006809 if (!txn)
6810 return 0;
6811
Willy Tarreau655dce92009-11-08 13:10:58 +01006812 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006813 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006814
Willy Tarreauc11416f2007-06-17 16:58:38 +02006815 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6816 /* ensure the indexes are not affected */
6817 return 0;
6818
Willy Tarreau8797c062007-05-07 00:55:35 +02006819 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006820 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006821
Willy Tarreauf3d25982007-05-08 22:45:09 +02006822 /* we do not need to set READ_ONLY because the data is in a buffer */
6823 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006824 return 1;
6825}
6826
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006827static int
6828acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6829 struct acl_expr *expr, struct acl_test *test)
6830{
6831 struct http_txn *txn = l7;
6832
Willy Tarreaub6866442008-07-14 23:54:42 +02006833 if (!txn)
6834 return 0;
6835
Willy Tarreau655dce92009-11-08 13:10:58 +01006836 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006837 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006838
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006839 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6840 /* ensure the indexes are not affected */
6841 return 0;
6842
6843 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006844 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 +01006845 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6846 test->i = AF_INET;
6847
6848 /*
6849 * If we are parsing url in frontend space, we prepare backend stage
6850 * to not parse again the same url ! optimization lazyness...
6851 */
6852 if (px->options & PR_O_HTTP_PROXY)
6853 l4->flags |= SN_ADDR_SET;
6854
6855 test->flags = ACL_TEST_F_READ_ONLY;
6856 return 1;
6857}
6858
6859static int
6860acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6861 struct acl_expr *expr, struct acl_test *test)
6862{
6863 struct http_txn *txn = l7;
6864
Willy Tarreaub6866442008-07-14 23:54:42 +02006865 if (!txn)
6866 return 0;
6867
Willy Tarreau655dce92009-11-08 13:10:58 +01006868 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006869 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006870
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006871 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6872 /* ensure the indexes are not affected */
6873 return 0;
6874
6875 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006876 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 +01006877 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6878
6879 if (px->options & PR_O_HTTP_PROXY)
6880 l4->flags |= SN_ADDR_SET;
6881
6882 test->flags = ACL_TEST_F_READ_ONLY;
6883 return 1;
6884}
6885
Willy Tarreauc11416f2007-06-17 16:58:38 +02006886/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6887 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6888 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006889static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006890acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006891 struct acl_expr *expr, struct acl_test *test)
6892{
6893 struct http_txn *txn = l7;
6894 struct hdr_idx *idx = &txn->hdr_idx;
6895 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006896
Willy Tarreaub6866442008-07-14 23:54:42 +02006897 if (!txn)
6898 return 0;
6899
Willy Tarreau33a7e692007-06-10 19:45:56 +02006900 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6901 /* search for header from the beginning */
6902 ctx->idx = 0;
6903
Willy Tarreau33a7e692007-06-10 19:45:56 +02006904 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6905 test->flags |= ACL_TEST_F_FETCH_MORE;
6906 test->flags |= ACL_TEST_F_VOL_HDR;
6907 test->len = ctx->vlen;
6908 test->ptr = (char *)ctx->line + ctx->val;
6909 return 1;
6910 }
6911
6912 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6913 test->flags |= ACL_TEST_F_VOL_HDR;
6914 return 0;
6915}
6916
Willy Tarreau33a7e692007-06-10 19:45:56 +02006917static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006918acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6919 struct acl_expr *expr, struct acl_test *test)
6920{
6921 struct http_txn *txn = l7;
6922
Willy Tarreaub6866442008-07-14 23:54:42 +02006923 if (!txn)
6924 return 0;
6925
Willy Tarreau655dce92009-11-08 13:10:58 +01006926 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006927 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006928
Willy Tarreauc11416f2007-06-17 16:58:38 +02006929 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6930 /* ensure the indexes are not affected */
6931 return 0;
6932
6933 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6934}
6935
6936static int
6937acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6938 struct acl_expr *expr, struct acl_test *test)
6939{
6940 struct http_txn *txn = l7;
6941
Willy Tarreaub6866442008-07-14 23:54:42 +02006942 if (!txn)
6943 return 0;
6944
Willy Tarreau655dce92009-11-08 13:10:58 +01006945 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006946 return 0;
6947
6948 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6949}
6950
6951/* 6. Check on HTTP header count. The number of occurrences is returned.
6952 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6953 */
6954static int
6955acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006956 struct acl_expr *expr, struct acl_test *test)
6957{
6958 struct http_txn *txn = l7;
6959 struct hdr_idx *idx = &txn->hdr_idx;
6960 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006961 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006962
Willy Tarreaub6866442008-07-14 23:54:42 +02006963 if (!txn)
6964 return 0;
6965
Willy Tarreau33a7e692007-06-10 19:45:56 +02006966 ctx.idx = 0;
6967 cnt = 0;
6968 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6969 cnt++;
6970
6971 test->i = cnt;
6972 test->flags = ACL_TEST_F_VOL_HDR;
6973 return 1;
6974}
6975
Willy Tarreauc11416f2007-06-17 16:58:38 +02006976static int
6977acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6978 struct acl_expr *expr, struct acl_test *test)
6979{
6980 struct http_txn *txn = l7;
6981
Willy Tarreaub6866442008-07-14 23:54:42 +02006982 if (!txn)
6983 return 0;
6984
Willy Tarreau655dce92009-11-08 13:10:58 +01006985 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006986 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006987
Willy Tarreauc11416f2007-06-17 16:58:38 +02006988 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6989 /* ensure the indexes are not affected */
6990 return 0;
6991
6992 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6993}
6994
6995static int
6996acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6997 struct acl_expr *expr, struct acl_test *test)
6998{
6999 struct http_txn *txn = l7;
7000
Willy Tarreaub6866442008-07-14 23:54:42 +02007001 if (!txn)
7002 return 0;
7003
Willy Tarreau655dce92009-11-08 13:10:58 +01007004 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007005 return 0;
7006
7007 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7008}
7009
Willy Tarreau33a7e692007-06-10 19:45:56 +02007010/* 7. Check on HTTP header's integer value. The integer value is returned.
7011 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007012 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007013 */
7014static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007015acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007016 struct acl_expr *expr, struct acl_test *test)
7017{
7018 struct http_txn *txn = l7;
7019 struct hdr_idx *idx = &txn->hdr_idx;
7020 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007021
Willy Tarreaub6866442008-07-14 23:54:42 +02007022 if (!txn)
7023 return 0;
7024
Willy Tarreau33a7e692007-06-10 19:45:56 +02007025 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7026 /* search for header from the beginning */
7027 ctx->idx = 0;
7028
Willy Tarreau33a7e692007-06-10 19:45:56 +02007029 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7030 test->flags |= ACL_TEST_F_FETCH_MORE;
7031 test->flags |= ACL_TEST_F_VOL_HDR;
7032 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7033 return 1;
7034 }
7035
7036 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7037 test->flags |= ACL_TEST_F_VOL_HDR;
7038 return 0;
7039}
7040
Willy Tarreauc11416f2007-06-17 16:58:38 +02007041static int
7042acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7043 struct acl_expr *expr, struct acl_test *test)
7044{
7045 struct http_txn *txn = l7;
7046
Willy Tarreaub6866442008-07-14 23:54:42 +02007047 if (!txn)
7048 return 0;
7049
Willy Tarreau655dce92009-11-08 13:10:58 +01007050 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007051 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007052
Willy Tarreauc11416f2007-06-17 16:58:38 +02007053 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7054 /* ensure the indexes are not affected */
7055 return 0;
7056
7057 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7058}
7059
7060static int
7061acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7062 struct acl_expr *expr, struct acl_test *test)
7063{
7064 struct http_txn *txn = l7;
7065
Willy Tarreaub6866442008-07-14 23:54:42 +02007066 if (!txn)
7067 return 0;
7068
Willy Tarreau655dce92009-11-08 13:10:58 +01007069 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007070 return 0;
7071
7072 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7073}
7074
Willy Tarreau106f9792009-09-19 07:54:16 +02007075/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7076 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7077 */
7078static int
7079acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7080 struct acl_expr *expr, struct acl_test *test)
7081{
7082 struct http_txn *txn = l7;
7083 struct hdr_idx *idx = &txn->hdr_idx;
7084 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7085
7086 if (!txn)
7087 return 0;
7088
7089 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7090 /* search for header from the beginning */
7091 ctx->idx = 0;
7092
7093 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7094 test->flags |= ACL_TEST_F_FETCH_MORE;
7095 test->flags |= ACL_TEST_F_VOL_HDR;
7096 /* Same optimization as url_ip */
7097 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7098 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7099 test->ptr = (void *)&l4->srv_addr.sin_addr;
7100 test->i = AF_INET;
7101 return 1;
7102 }
7103
7104 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7105 test->flags |= ACL_TEST_F_VOL_HDR;
7106 return 0;
7107}
7108
7109static int
7110acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7111 struct acl_expr *expr, struct acl_test *test)
7112{
7113 struct http_txn *txn = l7;
7114
7115 if (!txn)
7116 return 0;
7117
Willy Tarreau655dce92009-11-08 13:10:58 +01007118 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007119 return 0;
7120
7121 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7122 /* ensure the indexes are not affected */
7123 return 0;
7124
7125 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7126}
7127
7128static int
7129acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7130 struct acl_expr *expr, struct acl_test *test)
7131{
7132 struct http_txn *txn = l7;
7133
7134 if (!txn)
7135 return 0;
7136
Willy Tarreau655dce92009-11-08 13:10:58 +01007137 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007138 return 0;
7139
7140 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7141}
7142
Willy Tarreau737b0c12007-06-10 21:28:46 +02007143/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7144 * the first '/' after the possible hostname, and ends before the possible '?'.
7145 */
7146static int
7147acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7148 struct acl_expr *expr, struct acl_test *test)
7149{
7150 struct http_txn *txn = l7;
7151 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007152
Willy Tarreaub6866442008-07-14 23:54:42 +02007153 if (!txn)
7154 return 0;
7155
Willy Tarreau655dce92009-11-08 13:10:58 +01007156 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007157 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007158
Willy Tarreauc11416f2007-06-17 16:58:38 +02007159 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7160 /* ensure the indexes are not affected */
7161 return 0;
7162
Willy Tarreau962c3f42010-01-10 00:15:35 +01007163 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007164 ptr = http_get_path(txn);
7165 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007166 return 0;
7167
7168 /* OK, we got the '/' ! */
7169 test->ptr = ptr;
7170
7171 while (ptr < end && *ptr != '?')
7172 ptr++;
7173
7174 test->len = ptr - test->ptr;
7175
7176 /* we do not need to set READ_ONLY because the data is in a buffer */
7177 test->flags = ACL_TEST_F_VOL_1ST;
7178 return 1;
7179}
7180
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007181static int
7182acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7183 struct acl_expr *expr, struct acl_test *test)
7184{
7185 struct buffer *req = s->req;
7186 struct http_txn *txn = &s->txn;
7187 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007188
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007189 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7190 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7191 */
7192
7193 if (!s || !req)
7194 return 0;
7195
Willy Tarreau655dce92009-11-08 13:10:58 +01007196 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007197 /* Already decoded as OK */
7198 test->flags |= ACL_TEST_F_SET_RES_PASS;
7199 return 1;
7200 }
7201
7202 /* Try to decode HTTP request */
7203 if (likely(req->lr < req->r))
7204 http_msg_analyzer(req, msg, &txn->hdr_idx);
7205
Willy Tarreau655dce92009-11-08 13:10:58 +01007206 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007207 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7208 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7209 return 1;
7210 }
7211 /* wait for final state */
7212 test->flags |= ACL_TEST_F_MAY_CHANGE;
7213 return 0;
7214 }
7215
7216 /* OK we got a valid HTTP request. We have some minor preparation to
7217 * perform so that further checks can rely on HTTP tests.
7218 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007219 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007220 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7221 s->flags |= SN_REDIRECTABLE;
7222
7223 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7224 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7225 return 1;
7226 }
7227
7228 test->flags |= ACL_TEST_F_SET_RES_PASS;
7229 return 1;
7230}
7231
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007232static int
7233acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
7234 struct acl_expr *expr, struct acl_test *test)
7235{
7236
7237 if (!s)
7238 return 0;
7239
7240 if (!get_http_auth(s))
7241 return 0;
7242
7243 test->ctx.a[0] = expr->arg.ul;
7244 test->ctx.a[1] = s->txn.auth.user;
7245 test->ctx.a[2] = s->txn.auth.pass;
7246
7247 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
7248
7249 return 1;
7250}
Willy Tarreau8797c062007-05-07 00:55:35 +02007251
7252/************************************************************************/
7253/* All supported keywords must be declared here. */
7254/************************************************************************/
7255
7256/* Note: must not be declared <const> as its list will be overwritten */
7257static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007258 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7259
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007260 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7261 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7262 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7263 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007264
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007265 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7266 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7267 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7268 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7269 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7270 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7271 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7272 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7273 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007274
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007275 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7276 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7277 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7278 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7279 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7280 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7281 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7282 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7283 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7284 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007285 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007286
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007287 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7288 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7289 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7290 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7291 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7292 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7293 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7294 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7295 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007296 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007297
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007298 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7299 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7300 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7301 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7302 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7303 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7304 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007305
Willy Tarreauf3d25982007-05-08 22:45:09 +02007306#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007307 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7308 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7309 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7310 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7311 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7312 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7313 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7314
Willy Tarreau8797c062007-05-07 00:55:35 +02007315 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7316 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7317 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7318 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7319 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7320 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7321 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7322 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007323#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02007324
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007325 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth },
7326 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth },
Willy Tarreau8797c062007-05-07 00:55:35 +02007327 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02007328}};
7329
7330
7331__attribute__((constructor))
7332static void __http_protocol_init(void)
7333{
7334 acl_register_keywords(&acl_kws);
7335}
7336
7337
Willy Tarreau58f10d72006-12-04 02:26:12 +01007338/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007339 * Local variables:
7340 * c-indent-level: 8
7341 * c-basic-offset: 8
7342 * End:
7343 */