blob: 694e98dc5e9cd71c2ed5e4b8a6b825268e876b8c [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;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001021 if (txn->req.cap[hdr] != NULL) {
1022 *(h++) = ' ';
1023 *(h++) = '\"';
1024 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1025 '#', hdr_encode_map, txn->req.cap[hdr]);
1026 *(h++) = '\"';
1027 } else {
1028 memcpy(h, " \"-\"", 4);
1029 h += 4;
1030 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001031 }
1032 }
1033
1034 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1035 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1036 if (h >= sizeof (tmpline) + tmpline - 4)
1037 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001038 if (txn->rsp.cap[hdr] != NULL) {
1039 *(h++) = ' ';
1040 *(h++) = '\"';
1041 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1042 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1043 *(h++) = '\"';
1044 } else {
1045 memcpy(h, " \"-\"", 4);
1046 h += 4;
1047 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001048 }
1049 }
1050
1051trunc:
1052 *h = '\0';
1053
1054 level = LOG_INFO;
1055 if (err && (fe->options2 & PR_O2_LOGERRORS))
1056 level = LOG_ERR;
1057
1058 send_log(prx_log, level, "%s\n", tmpline);
1059
1060 s->logs.logwait = 0;
1061}
1062
Willy Tarreau42250582007-04-01 01:30:43 +02001063/*
1064 * send a log for the session when we have enough info about it.
1065 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001067void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001068{
1069 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1070 struct proxy *fe = s->fe;
1071 struct proxy *be = s->be;
1072 struct proxy *prx_log;
1073 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001074 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001075 char *uri, *h;
1076 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001077 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001078 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001079 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001080 int hdr;
1081
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001082 /* if we don't want to log normal traffic, return now */
1083 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1084 (s->conn_retries != be->conn_retries) ||
1085 txn->status >= 500;
1086 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1087 return;
1088
Willy Tarreau42250582007-04-01 01:30:43 +02001089 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1090 return;
1091 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092
Emeric Brun3a058f32009-06-30 18:26:00 +02001093 if (prx_log->options2 & PR_O2_CLFLOG)
1094 return http_sess_clflog(s);
1095
Willy Tarreau42250582007-04-01 01:30:43 +02001096 if (s->cli_addr.ss_family == AF_INET)
1097 inet_ntop(AF_INET,
1098 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1099 pn, sizeof(pn));
1100 else
1101 inet_ntop(AF_INET6,
1102 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1103 pn, sizeof(pn));
1104
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001105 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001106
1107 /* FIXME: let's limit ourselves to frontend logging for now. */
1108 tolog = fe->to_log;
1109
1110 h = tmpline;
1111 if (fe->to_log & LW_REQHDR &&
1112 txn->req.cap &&
1113 (h < tmpline + sizeof(tmpline) - 10)) {
1114 *(h++) = ' ';
1115 *(h++) = '{';
1116 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1117 if (hdr)
1118 *(h++) = '|';
1119 if (txn->req.cap[hdr] != NULL)
1120 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1121 '#', hdr_encode_map, txn->req.cap[hdr]);
1122 }
1123 *(h++) = '}';
1124 }
1125
1126 if (fe->to_log & LW_RSPHDR &&
1127 txn->rsp.cap &&
1128 (h < tmpline + sizeof(tmpline) - 7)) {
1129 *(h++) = ' ';
1130 *(h++) = '{';
1131 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1132 if (hdr)
1133 *(h++) = '|';
1134 if (txn->rsp.cap[hdr] != NULL)
1135 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1136 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1137 }
1138 *(h++) = '}';
1139 }
1140
1141 if (h < tmpline + sizeof(tmpline) - 4) {
1142 *(h++) = ' ';
1143 *(h++) = '"';
1144 uri = txn->uri ? txn->uri : "<BADREQ>";
1145 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1146 '#', url_encode_map, uri);
1147 *(h++) = '"';
1148 }
1149 *h = '\0';
1150
1151 svid = (tolog & LW_SVID) ?
1152 (s->data_source != DATA_SRC_STATS) ?
1153 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1154
Willy Tarreau70089872008-06-13 21:12:51 +02001155 t_request = -1;
1156 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1157 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1158
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001159 level = LOG_INFO;
1160 if (err && (fe->options2 & PR_O2_LOGERRORS))
1161 level = LOG_ERR;
1162
1163 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001164 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001165 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1166 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001167 pn,
1168 (s->cli_addr.ss_family == AF_INET) ?
1169 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1170 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001171 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001172 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001173 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001174 t_request,
1175 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001176 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1177 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1178 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1179 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001180 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001181 txn->cli_cookie ? txn->cli_cookie : "-",
1182 txn->srv_cookie ? txn->srv_cookie : "-",
1183 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1184 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1185 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1186 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1187 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001188 (s->flags & SN_REDISP)?"+":"",
1189 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001190 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1191
1192 s->logs.logwait = 0;
1193}
1194
Willy Tarreau117f59e2007-03-04 18:17:17 +01001195
1196/*
1197 * Capture headers from message starting at <som> according to header list
1198 * <cap_hdr>, and fill the <idx> structure appropriately.
1199 */
1200void capture_headers(char *som, struct hdr_idx *idx,
1201 char **cap, struct cap_hdr *cap_hdr)
1202{
1203 char *eol, *sol, *col, *sov;
1204 int cur_idx;
1205 struct cap_hdr *h;
1206 int len;
1207
1208 sol = som + hdr_idx_first_pos(idx);
1209 cur_idx = hdr_idx_first_idx(idx);
1210
1211 while (cur_idx) {
1212 eol = sol + idx->v[cur_idx].len;
1213
1214 col = sol;
1215 while (col < eol && *col != ':')
1216 col++;
1217
1218 sov = col + 1;
1219 while (sov < eol && http_is_lws[(unsigned char)*sov])
1220 sov++;
1221
1222 for (h = cap_hdr; h; h = h->next) {
1223 if ((h->namelen == col - sol) &&
1224 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1225 if (cap[h->index] == NULL)
1226 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001227 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228
1229 if (cap[h->index] == NULL) {
1230 Alert("HTTP capture : out of memory.\n");
1231 continue;
1232 }
1233
1234 len = eol - sov;
1235 if (len > h->len)
1236 len = h->len;
1237
1238 memcpy(cap[h->index], sov, len);
1239 cap[h->index][len]=0;
1240 }
1241 }
1242 sol = eol + idx->v[cur_idx].cr + 1;
1243 cur_idx = idx->v[cur_idx].next;
1244 }
1245}
1246
1247
Willy Tarreau42250582007-04-01 01:30:43 +02001248/* either we find an LF at <ptr> or we jump to <bad>.
1249 */
1250#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1251
1252/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1253 * otherwise to <http_msg_ood> with <state> set to <st>.
1254 */
1255#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1256 ptr++; \
1257 if (likely(ptr < end)) \
1258 goto good; \
1259 else { \
1260 state = (st); \
1261 goto http_msg_ood; \
1262 } \
1263 } while (0)
1264
1265
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001267 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1269 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1270 * will give undefined results.
1271 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1272 * and that msg->sol points to the beginning of the response.
1273 * If a complete line is found (which implies that at least one CR or LF is
1274 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1275 * returned indicating an incomplete line (which does not mean that parts have
1276 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1277 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1278 * upon next call.
1279 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001280 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1282 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001285const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1286 unsigned int state, const char *ptr, const char *end,
1287 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001288{
Willy Tarreau8973c702007-01-21 23:58:29 +01001289 switch (state) {
1290 http_msg_rpver:
1291 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001292 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1294
1295 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001296 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1298 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001299 state = HTTP_MSG_ERROR;
1300 break;
1301
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 http_msg_rpver_sp:
1303 case HTTP_MSG_RPVER_SP:
1304 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001305 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 goto http_msg_rpcode;
1307 }
1308 if (likely(HTTP_IS_SPHT(*ptr)))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1310 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001311 state = HTTP_MSG_ERROR;
1312 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001313
1314 http_msg_rpcode:
1315 case HTTP_MSG_RPCODE:
1316 if (likely(!HTTP_IS_LWS(*ptr)))
1317 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1318
1319 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001320 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1322 }
1323
1324 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001325 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 http_msg_rsp_reason:
1327 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001328 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 msg->sl.st.r_l = 0;
1330 goto http_msg_rpline_eol;
1331
1332 http_msg_rpcode_sp:
1333 case HTTP_MSG_RPCODE_SP:
1334 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001335 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 goto http_msg_rpreason;
1337 }
1338 if (likely(HTTP_IS_SPHT(*ptr)))
1339 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1340 /* so it's a CR/LF, so there is no reason phrase */
1341 goto http_msg_rsp_reason;
1342
1343 http_msg_rpreason:
1344 case HTTP_MSG_RPREASON:
1345 if (likely(!HTTP_IS_CRLF(*ptr)))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001347 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 http_msg_rpline_eol:
1349 /* We have seen the end of line. Note that we do not
1350 * necessarily have the \n yet, but at least we know that we
1351 * have EITHER \r OR \n, otherwise the response would not be
1352 * complete. We can then record the response length and return
1353 * to the caller which will be able to register it.
1354 */
1355 msg->sl.st.l = ptr - msg->sol;
1356 return ptr;
1357
1358#ifdef DEBUG_FULL
1359 default:
1360 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1361 exit(1);
1362#endif
1363 }
1364
1365 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001366 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 if (ret_state)
1368 *ret_state = state;
1369 if (ret_ptr)
1370 *ret_ptr = (char *)ptr;
1371 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001372}
1373
Willy Tarreau8973c702007-01-21 23:58:29 +01001374/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 * This function parses a request line between <ptr> and <end>, starting with
1376 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1377 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1378 * will give undefined results.
1379 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1380 * and that msg->sol points to the beginning of the request.
1381 * If a complete line is found (which implies that at least one CR or LF is
1382 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1383 * returned indicating an incomplete line (which does not mean that parts have
1384 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1385 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1386 * upon next call.
1387 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001388 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1390 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001391 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001392 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001393const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1394 unsigned int state, const char *ptr, const char *end,
1395 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001396{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 switch (state) {
1398 http_msg_rqmeth:
1399 case HTTP_MSG_RQMETH:
1400 if (likely(HTTP_IS_TOKEN(*ptr)))
1401 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001404 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1406 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (likely(HTTP_IS_CRLF(*ptr))) {
1409 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001410 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001412 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001414 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001416 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 msg->sl.rq.v_l = 0;
1418 goto http_msg_rqline_eol;
1419 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001420 state = HTTP_MSG_ERROR;
1421 break;
1422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 http_msg_rqmeth_sp:
1424 case HTTP_MSG_RQMETH_SP:
1425 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001426 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 goto http_msg_rquri;
1428 }
1429 if (likely(HTTP_IS_SPHT(*ptr)))
1430 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1431 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1432 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 http_msg_rquri:
1435 case HTTP_MSG_RQURI:
1436 if (likely(!HTTP_IS_LWS(*ptr)))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001440 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1445 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 http_msg_rquri_sp:
1448 case HTTP_MSG_RQURI_SP:
1449 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001450 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 goto http_msg_rqver;
1452 }
1453 if (likely(HTTP_IS_SPHT(*ptr)))
1454 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1455 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1456 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 http_msg_rqver:
1459 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001460 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001462
1463 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001464 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001465 http_msg_rqline_eol:
1466 /* We have seen the end of line. Note that we do not
1467 * necessarily have the \n yet, but at least we know that we
1468 * have EITHER \r OR \n, otherwise the request would not be
1469 * complete. We can then record the request length and return
1470 * to the caller which will be able to register it.
1471 */
1472 msg->sl.rq.l = ptr - msg->sol;
1473 return ptr;
1474 }
1475
1476 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001477 state = HTTP_MSG_ERROR;
1478 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480#ifdef DEBUG_FULL
1481 default:
1482 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1483 exit(1);
1484#endif
1485 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001488 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 if (ret_state)
1490 *ret_state = state;
1491 if (ret_ptr)
1492 *ret_ptr = (char *)ptr;
1493 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001495
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001496/*
1497 * Returns the data from Authorization header. Function may be called more
1498 * than once so data is stored in txn->auth_data. When no header is found
1499 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1500 * searching again for something we are unable to find anyway.
1501 */
1502
1503char get_http_auth_buff[BUFSIZE];
1504
1505int
1506get_http_auth(struct session *s)
1507{
1508
1509 struct http_txn *txn = &s->txn;
1510 struct chunk auth_method;
1511 struct hdr_ctx ctx;
1512 char *h, *p;
1513 int len;
1514
1515#ifdef DEBUG_AUTH
1516 printf("Auth for session %p: %d\n", s, txn->auth.method);
1517#endif
1518
1519 if (txn->auth.method == HTTP_AUTH_WRONG)
1520 return 0;
1521
1522 if (txn->auth.method)
1523 return 1;
1524
1525 txn->auth.method = HTTP_AUTH_WRONG;
1526
1527 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001528
1529 if (txn->flags & TX_USE_PX_CONN) {
1530 h = "Proxy-Authorization";
1531 len = strlen(h);
1532 } else {
1533 h = "Authorization";
1534 len = strlen(h);
1535 }
1536
1537 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538 return 0;
1539
1540 h = ctx.line + ctx.val;
1541
1542 p = memchr(h, ' ', ctx.vlen);
1543 if (!p || p == h)
1544 return 0;
1545
1546 chunk_initlen(&auth_method, h, 0, p-h);
1547 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1548
1549 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1550
1551 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1552 get_http_auth_buff, BUFSIZE - 1);
1553
1554 if (len < 0)
1555 return 0;
1556
1557
1558 get_http_auth_buff[len] = '\0';
1559
1560 p = strchr(get_http_auth_buff, ':');
1561
1562 if (!p)
1563 return 0;
1564
1565 txn->auth.user = get_http_auth_buff;
1566 *p = '\0';
1567 txn->auth.pass = p+1;
1568
1569 txn->auth.method = HTTP_AUTH_BASIC;
1570 return 1;
1571 }
1572
1573 return 0;
1574}
1575
Willy Tarreau58f10d72006-12-04 02:26:12 +01001576
Willy Tarreau8973c702007-01-21 23:58:29 +01001577/*
1578 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001579 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001580 * when data are missing and recalled at the exact same location with no
1581 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001582 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001583 * fields. Note that msg->som and msg->sol will be initialized after completing
1584 * the first state, so that none of the msg pointers has to be initialized
1585 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001586 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1588{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001589 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001592 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 ptr = buf->lr;
1594 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 if (unlikely(ptr >= end))
1597 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 /*
1601 * First, states that are specific to the response only.
1602 * We check them first so that request and headers are
1603 * closer to each other (accessed more often).
1604 */
1605 http_msg_rpbefore:
1606 case HTTP_MSG_RPBEFORE:
1607 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001608 /* we have a start of message, but we have to check
1609 * first if we need to remove some CRLF. We can only
1610 * do this when send_max=0.
1611 */
1612 char *beg = buf->w + buf->send_max;
1613 if (beg >= buf->data + buf->size)
1614 beg -= buf->size;
1615 if (unlikely(ptr != beg)) {
1616 if (buf->send_max)
1617 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001618 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001619 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001620 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001621 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001622 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001623 hdr_idx_init(idx);
1624 state = HTTP_MSG_RPVER;
1625 goto http_msg_rpver;
1626 }
1627
1628 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1629 goto http_msg_invalid;
1630
1631 if (unlikely(*ptr == '\n'))
1632 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1634 /* stop here */
1635
1636 http_msg_rpbefore_cr:
1637 case HTTP_MSG_RPBEFORE_CR:
1638 EXPECT_LF_HERE(ptr, http_msg_invalid);
1639 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1640 /* stop here */
1641
1642 http_msg_rpver:
1643 case HTTP_MSG_RPVER:
1644 case HTTP_MSG_RPVER_SP:
1645 case HTTP_MSG_RPCODE:
1646 case HTTP_MSG_RPCODE_SP:
1647 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001648 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001649 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001650 if (unlikely(!ptr))
1651 return;
1652
1653 /* we have a full response and we know that we have either a CR
1654 * or an LF at <ptr>.
1655 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001656 //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 +01001657 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1658
1659 msg->sol = ptr;
1660 if (likely(*ptr == '\r'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1662 goto http_msg_rpline_end;
1663
1664 http_msg_rpline_end:
1665 case HTTP_MSG_RPLINE_END:
1666 /* msg->sol must point to the first of CR or LF. */
1667 EXPECT_LF_HERE(ptr, http_msg_invalid);
1668 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1669 /* stop here */
1670
1671 /*
1672 * Second, states that are specific to the request only
1673 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 http_msg_rqbefore:
1675 case HTTP_MSG_RQBEFORE:
1676 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001677 /* we have a start of message, but we have to check
1678 * first if we need to remove some CRLF. We can only
1679 * do this when send_max=0.
1680 */
1681 char *beg = buf->w + buf->send_max;
1682 if (beg >= buf->data + buf->size)
1683 beg -= buf->size;
1684 if (likely(ptr != beg)) {
1685 if (buf->send_max)
1686 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001687 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001688 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001690 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001691 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001692 /* we will need this when keep-alive will be supported
1693 hdr_idx_init(idx);
1694 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001695 state = HTTP_MSG_RQMETH;
1696 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1700 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 if (unlikely(*ptr == '\n'))
1703 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1704 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001705 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001706
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 http_msg_rqbefore_cr:
1708 case HTTP_MSG_RQBEFORE_CR:
1709 EXPECT_LF_HERE(ptr, http_msg_invalid);
1710 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001711 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 http_msg_rqmeth:
1714 case HTTP_MSG_RQMETH:
1715 case HTTP_MSG_RQMETH_SP:
1716 case HTTP_MSG_RQURI:
1717 case HTTP_MSG_RQURI_SP:
1718 case HTTP_MSG_RQVER:
1719 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001720 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 if (unlikely(!ptr))
1722 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 /* we have a full request and we know that we have either a CR
1725 * or an LF at <ptr>.
1726 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001727 //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 +01001728 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 msg->sol = ptr;
1731 if (likely(*ptr == '\r'))
1732 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001734
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 http_msg_rqline_end:
1736 case HTTP_MSG_RQLINE_END:
1737 /* check for HTTP/0.9 request : no version information available.
1738 * msg->sol must point to the first of CR or LF.
1739 */
1740 if (unlikely(msg->sl.rq.v_l == 0))
1741 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001742
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 EXPECT_LF_HERE(ptr, http_msg_invalid);
1744 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001745 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001746
Willy Tarreau8973c702007-01-21 23:58:29 +01001747 /*
1748 * Common states below
1749 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 http_msg_hdr_first:
1751 case HTTP_MSG_HDR_FIRST:
1752 msg->sol = ptr;
1753 if (likely(!HTTP_IS_CRLF(*ptr))) {
1754 goto http_msg_hdr_name;
1755 }
1756
1757 if (likely(*ptr == '\r'))
1758 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1759 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 http_msg_hdr_name:
1762 case HTTP_MSG_HDR_NAME:
1763 /* assumes msg->sol points to the first char */
1764 if (likely(HTTP_IS_TOKEN(*ptr)))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 if (likely(*ptr == ':')) {
1768 msg->col = ptr - buf->data;
1769 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1770 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001771
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001772 if (likely(msg->err_pos < -1) || *ptr == '\n')
1773 goto http_msg_invalid;
1774
1775 if (msg->err_pos == -1) /* capture error pointer */
1776 msg->err_pos = ptr - buf->data; /* >= 0 now */
1777
1778 /* and we still accept this non-token character */
1779 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001781 http_msg_hdr_l1_sp:
1782 case HTTP_MSG_HDR_L1_SP:
1783 /* assumes msg->sol points to the first char and msg->col to the colon */
1784 if (likely(HTTP_IS_SPHT(*ptr)))
1785 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 /* header value can be basically anything except CR/LF */
1788 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 if (likely(!HTTP_IS_CRLF(*ptr))) {
1791 goto http_msg_hdr_val;
1792 }
1793
1794 if (likely(*ptr == '\r'))
1795 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1796 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001797
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 http_msg_hdr_l1_lf:
1799 case HTTP_MSG_HDR_L1_LF:
1800 EXPECT_LF_HERE(ptr, http_msg_invalid);
1801 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001802
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 http_msg_hdr_l1_lws:
1804 case HTTP_MSG_HDR_L1_LWS:
1805 if (likely(HTTP_IS_SPHT(*ptr))) {
1806 /* replace HT,CR,LF with spaces */
1807 for (; buf->data+msg->sov < ptr; msg->sov++)
1808 buf->data[msg->sov] = ' ';
1809 goto http_msg_hdr_l1_sp;
1810 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001811 /* we had a header consisting only in spaces ! */
1812 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 goto http_msg_complete_header;
1814
1815 http_msg_hdr_val:
1816 case HTTP_MSG_HDR_VAL:
1817 /* assumes msg->sol points to the first char, msg->col to the
1818 * colon, and msg->sov points to the first character of the
1819 * value.
1820 */
1821 if (likely(!HTTP_IS_CRLF(*ptr)))
1822 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 msg->eol = ptr;
1825 /* Note: we could also copy eol into ->eoh so that we have the
1826 * real header end in case it ends with lots of LWS, but is this
1827 * really needed ?
1828 */
1829 if (likely(*ptr == '\r'))
1830 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1831 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 http_msg_hdr_l2_lf:
1834 case HTTP_MSG_HDR_L2_LF:
1835 EXPECT_LF_HERE(ptr, http_msg_invalid);
1836 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 http_msg_hdr_l2_lws:
1839 case HTTP_MSG_HDR_L2_LWS:
1840 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1841 /* LWS: replace HT,CR,LF with spaces */
1842 for (; msg->eol < ptr; msg->eol++)
1843 *msg->eol = ' ';
1844 goto http_msg_hdr_val;
1845 }
1846 http_msg_complete_header:
1847 /*
1848 * It was a new header, so the last one is finished.
1849 * Assumes msg->sol points to the first char, msg->col to the
1850 * colon, msg->sov points to the first character of the value
1851 * and msg->eol to the first CR or LF so we know how the line
1852 * ends. We insert last header into the index.
1853 */
1854 /*
1855 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1856 write(2, msg->sol, msg->eol-msg->sol);
1857 fprintf(stderr,"\n");
1858 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001859
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1861 idx, idx->tail) < 0))
1862 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001863
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 msg->sol = ptr;
1865 if (likely(!HTTP_IS_CRLF(*ptr))) {
1866 goto http_msg_hdr_name;
1867 }
1868
1869 if (likely(*ptr == '\r'))
1870 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1871 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 http_msg_last_lf:
1874 case HTTP_MSG_LAST_LF:
1875 /* Assumes msg->sol points to the first of either CR or LF */
1876 EXPECT_LF_HERE(ptr, http_msg_invalid);
1877 ptr++;
1878 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001879 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001881 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001882 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 return;
1884#ifdef DEBUG_FULL
1885 default:
1886 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1887 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001888#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 }
1890 http_msg_ood:
1891 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001892 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 buf->lr = ptr;
1894 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 http_msg_invalid:
1897 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001898 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001899 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 return;
1901}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001902
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001903/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1904 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1905 * nothing is done and 1 is returned.
1906 */
1907static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1908{
1909 int delta;
1910 char *cur_end;
1911
1912 if (msg->sl.rq.v_l != 0)
1913 return 1;
1914
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001915 cur_end = msg->sol + msg->sl.rq.l;
1916 delta = 0;
1917
1918 if (msg->sl.rq.u_l == 0) {
1919 /* if no URI was set, add "/" */
1920 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1921 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001922 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001923 }
1924 /* add HTTP version */
1925 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001926 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001927 cur_end += delta;
1928 cur_end = (char *)http_parse_reqline(msg, req->data,
1929 HTTP_MSG_RQMETH,
1930 msg->sol, cur_end + 1,
1931 NULL, NULL);
1932 if (unlikely(!cur_end))
1933 return 0;
1934
1935 /* we have a full HTTP/1.0 request now and we know that
1936 * we have either a CR or an LF at <ptr>.
1937 */
1938 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1939 return 1;
1940}
1941
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001942/* Parse the Connection: header of an HTTP request, looking for both "close"
1943 * and "keep-alive" values. If a buffer is provided and we already know that
1944 * some headers may safely be removed, we remove them now. The <to_del> flags
1945 * are used for that :
1946 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1947 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1948 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1949 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1950 * harmless combinations may be removed. Do not call that after changes have
1951 * been processed. If unused, the buffer can be NULL, and no data will be
1952 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001953 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001954void 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 +01001955{
Willy Tarreau5b154472009-12-21 20:11:07 +01001956 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001957 const char *hdr_val = "Connection";
1958 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001959
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001960 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001961 return;
1962
Willy Tarreau88d349d2010-01-25 12:15:43 +01001963 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1964 hdr_val = "Proxy-Connection";
1965 hdr_len = 16;
1966 }
1967
Willy Tarreau5b154472009-12-21 20:11:07 +01001968 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001969 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001970 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1972 txn->flags |= TX_HDR_CONN_KAL;
1973 if ((to_del & 2) && buf)
1974 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1975 else
1976 txn->flags |= TX_CON_KAL_SET;
1977 }
1978 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1979 txn->flags |= TX_HDR_CONN_CLO;
1980 if ((to_del & 1) && buf)
1981 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1982 else
1983 txn->flags |= TX_CON_CLO_SET;
1984 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001985 }
1986
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001987 txn->flags |= TX_HDR_CONN_PRS;
1988 return;
1989}
Willy Tarreau5b154472009-12-21 20:11:07 +01001990
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001991/* Apply desired changes on the Connection: header. Values may be removed and/or
1992 * added depending on the <wanted> flags, which are exclusively composed of
1993 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1994 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1995 */
1996void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1997{
1998 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001999 const char *hdr_val = "Connection";
2000 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002001
2002 ctx.idx = 0;
2003
Willy Tarreau88d349d2010-01-25 12:15:43 +01002004
2005 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2006 hdr_val = "Proxy-Connection";
2007 hdr_len = 16;
2008 }
2009
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002010 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002011 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002012 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2013 if (wanted & TX_CON_KAL_SET)
2014 txn->flags |= TX_CON_KAL_SET;
2015 else
2016 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002017 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002018 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2019 if (wanted & TX_CON_CLO_SET)
2020 txn->flags |= TX_CON_CLO_SET;
2021 else
2022 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002023 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002024 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025
2026 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2027 return;
2028
2029 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2030 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031 hdr_val = "Connection: close";
2032 hdr_len = 17;
2033 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2034 hdr_val = "Proxy-Connection: close";
2035 hdr_len = 23;
2036 }
2037 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002038 }
2039
2040 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2041 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002042 hdr_val = "Connection: keep-alive";
2043 hdr_len = 22;
2044 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2045 hdr_val = "Proxy-Connection: keep-alive";
2046 hdr_len = 28;
2047 }
2048 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002049 }
2050 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002051}
2052
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2054 * first byte of body, and increments msg->sov by the number of bytes parsed,
2055 * so that we know we can forward between ->som and ->sov. Note that due to
2056 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2057 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002058 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002060 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002062{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002063 char *ptr = buf->lr;
2064 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002065 unsigned int chunk = 0;
2066
2067 /* The chunk size is in the following form, though we are only
2068 * interested in the size and CRLF :
2069 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2070 */
2071 while (1) {
2072 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002073 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002074 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 if (c < 0) /* not a hex digit anymore */
2077 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002078 if (++ptr >= end)
2079 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080 if (chunk & 0xF000000) /* overflow will occur */
2081 return -1;
2082 chunk = (chunk << 4) + c;
2083 }
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 /* empty size not allowed */
2086 if (ptr == buf->lr)
2087 return -1;
2088
2089 while (http_is_spht[(unsigned char)*ptr]) {
2090 if (++ptr >= end)
2091 ptr = buf->data;
2092 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002093 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002094 }
2095
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 /* Up to there, we know that at least one byte is present at *ptr. Check
2097 * for the end of chunk size.
2098 */
2099 while (1) {
2100 if (likely(HTTP_IS_CRLF(*ptr))) {
2101 /* we now have a CR or an LF at ptr */
2102 if (likely(*ptr == '\r')) {
2103 if (++ptr >= end)
2104 ptr = buf->data;
2105 if (ptr == buf->r)
2106 return 0;
2107 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002108
Willy Tarreaud98cf932009-12-27 22:54:55 +01002109 if (*ptr != '\n')
2110 return -1;
2111 if (++ptr >= end)
2112 ptr = buf->data;
2113 /* done */
2114 break;
2115 }
2116 else if (*ptr == ';') {
2117 /* chunk extension, ends at next CRLF */
2118 if (++ptr >= end)
2119 ptr = buf->data;
2120 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002121 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122
2123 while (!HTTP_IS_CRLF(*ptr)) {
2124 if (++ptr >= end)
2125 ptr = buf->data;
2126 if (ptr == buf->r)
2127 return 0;
2128 }
2129 /* we have a CRLF now, loop above */
2130 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002131 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002132 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002133 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002134 }
2135
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 /* OK we found our CRLF and now <ptr> points to the next byte,
2137 * which may or may not be present. We save that into ->lr and
2138 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002139 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002140 msg->sov += ptr - buf->lr;
2141 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002142 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002144 return 1;
2145}
2146
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147/* This function skips trailers in the buffer <buf> associated with HTTP
2148 * message <msg>. The first visited position is buf->lr. If the end of
2149 * the trailers is found, it is automatically scheduled to be forwarded,
2150 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2151 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002152 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2153 * zero. If a parse error is encountered, the function returns < 0 and does not
2154 * change anything except maybe buf->lr and msg->sov. Note that the message
2155 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2156 * which implies that all non-trailers data have already been scheduled for
2157 * forwarding, and that the difference between msg->som and msg->sov exactly
2158 * matches the length of trailers already parsed and not forwarded. It is also
2159 * important to note that this function is designed to be able to parse wrapped
2160 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002161 */
2162int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2163{
2164 /* we have buf->lr which points to next line. Look for CRLF. */
2165 while (1) {
2166 char *p1 = NULL, *p2 = NULL;
2167 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002168 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002169
2170 /* scan current line and stop at LF or CRLF */
2171 while (1) {
2172 if (ptr == buf->r)
2173 return 0;
2174
2175 if (*ptr == '\n') {
2176 if (!p1)
2177 p1 = ptr;
2178 p2 = ptr;
2179 break;
2180 }
2181
2182 if (*ptr == '\r') {
2183 if (p1)
2184 return -1;
2185 p1 = ptr;
2186 }
2187
2188 ptr++;
2189 if (ptr >= buf->data + buf->size)
2190 ptr = buf->data;
2191 }
2192
2193 /* after LF; point to beginning of next line */
2194 p2++;
2195 if (p2 >= buf->data + buf->size)
2196 p2 = buf->data;
2197
Willy Tarreau638cd022010-01-03 07:42:04 +01002198 bytes = p2 - buf->lr;
2199 if (bytes < 0)
2200 bytes += buf->size;
2201
2202 /* schedule this line for forwarding */
2203 msg->sov += bytes;
2204 if (msg->sov >= buf->size)
2205 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002206
Willy Tarreau638cd022010-01-03 07:42:04 +01002207 if (p1 == buf->lr) {
2208 /* LF/CRLF at beginning of line => end of trailers at p2.
2209 * Everything was scheduled for forwarding, there's nothing
2210 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002211 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002212 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002213 msg->msg_state = HTTP_MSG_DONE;
2214 return 1;
2215 }
2216 /* OK, next line then */
2217 buf->lr = p2;
2218 }
2219}
2220
2221/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2222 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2223 * ->som, buf->lr in order to include this part into the next forwarding phase.
2224 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2225 * not enough data are available, the function does not change anything and
2226 * returns zero. If a parse error is encountered, the function returns < 0 and
2227 * does not change anything. Note: this function is designed to parse wrapped
2228 * CRLF at the end of the buffer.
2229 */
2230int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2231{
2232 char *ptr;
2233 int bytes;
2234
2235 /* NB: we'll check data availabilty at the end. It's not a
2236 * problem because whatever we match first will be checked
2237 * against the correct length.
2238 */
2239 bytes = 1;
2240 ptr = buf->lr;
2241 if (*ptr == '\r') {
2242 bytes++;
2243 ptr++;
2244 if (ptr >= buf->data + buf->size)
2245 ptr = buf->data;
2246 }
2247
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002248 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002249 return 0;
2250
2251 if (*ptr != '\n')
2252 return -1;
2253
2254 ptr++;
2255 if (ptr >= buf->data + buf->size)
2256 ptr = buf->data;
2257 buf->lr = ptr;
2258 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2259 msg->sov = ptr - buf->data;
2260 msg->som = msg->sov - bytes;
2261 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2262 return 1;
2263}
Willy Tarreau5b154472009-12-21 20:11:07 +01002264
Willy Tarreau83e3af02009-12-28 17:39:57 +01002265void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2266{
2267 char *end = buf->data + buf->size;
2268 int off = buf->data + buf->size - buf->w;
2269
2270 /* two possible cases :
2271 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002272 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002273 */
2274 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002275 int block1 = buf->l;
2276 int block2 = 0;
2277 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002278 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002279 block1 = buf->data + buf->size - buf->w;
2280 block2 = buf->r - buf->data;
2281 }
2282 if (block2)
2283 memcpy(swap_buffer, buf->data, block2);
2284 memmove(buf->data, buf->w, block1);
2285 if (block2)
2286 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002287 }
2288
2289 /* adjust all known pointers */
2290 buf->w = buf->data;
2291 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2292 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2293 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2294 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2295
2296 /* adjust relative pointers */
2297 msg->som = 0;
2298 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2299 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2300 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2301
Willy Tarreau83e3af02009-12-28 17:39:57 +01002302 if (msg->err_pos >= 0) {
2303 msg->err_pos += off;
2304 if (msg->err_pos >= buf->size)
2305 msg->err_pos -= buf->size;
2306 }
2307
2308 buf->flags &= ~BF_FULL;
2309 if (buf->l >= buffer_max_len(buf))
2310 buf->flags |= BF_FULL;
2311}
2312
Willy Tarreaud787e662009-07-07 10:14:51 +02002313/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2314 * processing can continue on next analysers, or zero if it either needs more
2315 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2316 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2317 * when it has nothing left to do, and may remove any analyser when it wants to
2318 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002319 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002320int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002321{
Willy Tarreau59234e92008-11-30 23:51:27 +01002322 /*
2323 * We will parse the partial (or complete) lines.
2324 * We will check the request syntax, and also join multi-line
2325 * headers. An index of all the lines will be elaborated while
2326 * parsing.
2327 *
2328 * For the parsing, we use a 28 states FSM.
2329 *
2330 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002331 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002332 * req->data + msg->eoh = end of processed headers / start of current one
2333 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 * req->lr = first non-visited byte
2335 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002336 *
2337 * At end of parsing, we may perform a capture of the error (if any), and
2338 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2339 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2340 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002342
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002344 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 struct http_txn *txn = &s->txn;
2346 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002347 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002348
Willy Tarreau6bf17362009-02-24 10:48:35 +01002349 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2350 now_ms, __FUNCTION__,
2351 s,
2352 req,
2353 req->rex, req->wex,
2354 req->flags,
2355 req->l,
2356 req->analysers);
2357
Willy Tarreau52a0c602009-08-16 22:45:38 +02002358 /* we're speaking HTTP here, so let's speak HTTP to the client */
2359 s->srv_error = http_return_srv_error;
2360
Willy Tarreau83e3af02009-12-28 17:39:57 +01002361 /* There's a protected area at the end of the buffer for rewriting
2362 * purposes. We don't want to start to parse the request if the
2363 * protected area is affected, because we may have to move processed
2364 * data later, which is much more complicated.
2365 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002366 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002367 if ((txn->flags & TX_NOT_FIRST) &&
2368 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002369 req->r < req->lr ||
2370 req->r > req->data + req->size - global.tune.maxrewrite)) {
2371 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002372 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2373 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002374 /* some data has still not left the buffer, wake us once that's done */
2375 buffer_dont_connect(req);
2376 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2377 return 0;
2378 }
2379 if (req->l <= req->size - global.tune.maxrewrite)
2380 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002381 }
2382
Willy Tarreau065e8332010-01-08 00:30:20 +01002383 /* Note that we have the same problem with the response ; we
2384 * may want to send a redirect, error or anything which requires
2385 * some spare space. So we'll ensure that we have at least
2386 * maxrewrite bytes available in the response buffer before
2387 * processing that one. This will only affect pipelined
2388 * keep-alive requests.
2389 */
2390 if ((txn->flags & TX_NOT_FIRST) &&
2391 unlikely((s->rep->flags & BF_FULL) ||
2392 s->rep->r < s->rep->lr ||
2393 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2394 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002395 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2396 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002397 /* don't let a connection request be initiated */
2398 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002399 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002400 return 0;
2401 }
2402 }
2403
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002404 if (likely(req->lr < req->r))
2405 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002406 }
2407
Willy Tarreau59234e92008-11-30 23:51:27 +01002408 /* 1: we might have to print this header in debug mode */
2409 if (unlikely((global.mode & MODE_DEBUG) &&
2410 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002411 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002412 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002413
Willy Tarreau962c3f42010-01-10 00:15:35 +01002414 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002415 eol = sol + msg->sl.rq.l;
2416 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002417
Willy Tarreau59234e92008-11-30 23:51:27 +01002418 sol += hdr_idx_first_pos(&txn->hdr_idx);
2419 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002420
Willy Tarreau59234e92008-11-30 23:51:27 +01002421 while (cur_idx) {
2422 eol = sol + txn->hdr_idx.v[cur_idx].len;
2423 debug_hdr("clihdr", s, sol, eol);
2424 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2425 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002426 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 }
2428
Willy Tarreau58f10d72006-12-04 02:26:12 +01002429
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 /*
2431 * Now we quickly check if we have found a full valid request.
2432 * If not so, we check the FD and buffer states before leaving.
2433 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002434 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002435 * requests are checked first. When waiting for a second request
2436 * on a keep-alive session, if we encounter and error, close, t/o,
2437 * we note the error in the session flags but don't set any state.
2438 * Since the error will be noted there, it will not be counted by
2439 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002441
Willy Tarreau655dce92009-11-08 13:10:58 +01002442 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002443 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002445 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002446 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2447 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002448 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002449 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002450
Willy Tarreau59234e92008-11-30 23:51:27 +01002451 /* 1: Since we are in header mode, if there's no space
2452 * left for headers, we won't be able to free more
2453 * later, so the session will never terminate. We
2454 * must terminate it now.
2455 */
2456 if (unlikely(req->flags & BF_FULL)) {
2457 /* FIXME: check if URI is set and return Status
2458 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002459 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002460 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 goto return_bad_req;
2462 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002463
Willy Tarreau59234e92008-11-30 23:51:27 +01002464 /* 2: have we encountered a read error ? */
2465 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002466 if (!(s->flags & SN_ERR_MASK))
2467 s->flags |= SN_ERR_CLICL;
2468
Willy Tarreaufcffa692010-01-10 14:21:19 +01002469 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002470 goto failed_keep_alive;
2471
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002473 if (msg->err_pos >= 0)
2474 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002475 msg->msg_state = HTTP_MSG_ERROR;
2476 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002477
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002478 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002479 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480 if (s->listener->counters)
2481 s->listener->counters->failed_req++;
2482
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 if (!(s->flags & SN_FINST_MASK))
2484 s->flags |= SN_FINST_R;
2485 return 0;
2486 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 /* 3: has the read timeout expired ? */
2489 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002490 if (!(s->flags & SN_ERR_MASK))
2491 s->flags |= SN_ERR_CLITO;
2492
Willy Tarreaufcffa692010-01-10 14:21:19 +01002493 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002494 goto failed_keep_alive;
2495
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002497 if (msg->err_pos >= 0)
2498 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 txn->status = 408;
2500 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2501 msg->msg_state = HTTP_MSG_ERROR;
2502 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002503
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002504 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002505 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002506 if (s->listener->counters)
2507 s->listener->counters->failed_req++;
2508
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 if (!(s->flags & SN_FINST_MASK))
2510 s->flags |= SN_FINST_R;
2511 return 0;
2512 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* 4: have we encountered a close ? */
2515 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002516 if (!(s->flags & SN_ERR_MASK))
2517 s->flags |= SN_ERR_CLICL;
2518
Willy Tarreaufcffa692010-01-10 14:21:19 +01002519 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002520 goto failed_keep_alive;
2521
Willy Tarreau4076a152009-04-02 15:18:36 +02002522 if (msg->err_pos >= 0)
2523 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 txn->status = 400;
2525 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2526 msg->msg_state = HTTP_MSG_ERROR;
2527 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002528
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002529 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002530 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002531 if (s->listener->counters)
2532 s->listener->counters->failed_req++;
2533
Willy Tarreau59234e92008-11-30 23:51:27 +01002534 if (!(s->flags & SN_FINST_MASK))
2535 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002536 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002537 }
2538
Willy Tarreau520d95e2009-09-19 21:04:57 +02002539 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002540 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002541 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002542
Willy Tarreaufcffa692010-01-10 14:21:19 +01002543 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2544 /* If the client starts to talk, let's fall back to
2545 * request timeout processing.
2546 */
2547 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002548 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002549 }
2550
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002552 if (!tick_isset(req->analyse_exp)) {
2553 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2554 (txn->flags & TX_WAIT_NEXT_RQ) &&
2555 tick_isset(s->be->timeout.httpka))
2556 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2557 else
2558 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2559 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002560
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 /* we're not ready yet */
2562 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002563
2564 failed_keep_alive:
2565 /* Here we process low-level errors for keep-alive requests. In
2566 * short, if the request is not the first one and it experiences
2567 * a timeout, read error or shutdown, we just silently close so
2568 * that the client can try again.
2569 */
2570 txn->status = 0;
2571 msg->msg_state = HTTP_MSG_RQBEFORE;
2572 req->analysers = 0;
2573 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002574 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002575 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002576 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002577 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002578
Willy Tarreaud787e662009-07-07 10:14:51 +02002579 /* OK now we have a complete HTTP request with indexed headers. Let's
2580 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002581 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2582 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2583 * points to the CRLF of the request line. req->lr points to the first
2584 * byte after the last LF. msg->col and msg->sov point to the first
2585 * byte of data. msg->eol cannot be trusted because it may have been
2586 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002587 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002588
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002589 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2590
Willy Tarreaub16a5742010-01-10 14:46:16 +01002591 if (txn->flags & TX_WAIT_NEXT_RQ) {
2592 /* kill the pending keep-alive timeout */
2593 txn->flags &= ~TX_WAIT_NEXT_RQ;
2594 req->analyse_exp = TICK_ETERNITY;
2595 }
2596
2597
Willy Tarreaud787e662009-07-07 10:14:51 +02002598 /* Maybe we found in invalid header name while we were configured not
2599 * to block on that, so we have to capture it now.
2600 */
2601 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002602 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 /*
2605 * 1: identify the method
2606 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002607 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002608
2609 /* we can make use of server redirect on GET and HEAD */
2610 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2611 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002612
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 /*
2614 * 2: check if the URI matches the monitor_uri.
2615 * We have to do this for every request which gets in, because
2616 * the monitor-uri is defined by the frontend.
2617 */
2618 if (unlikely((s->fe->monitor_uri_len != 0) &&
2619 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002620 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 s->fe->monitor_uri,
2622 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002623 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002625 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002627
Willy Tarreau59234e92008-11-30 23:51:27 +01002628 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002629
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002631 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2632 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002633
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 ret = acl_pass(ret);
2635 if (cond->pol == ACL_COND_UNLESS)
2636 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002637
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 if (ret) {
2639 /* we fail this request, let's return 503 service unavail */
2640 txn->status = 503;
2641 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2642 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002643 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002644 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002645
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 /* nothing to fail, let's reply normaly */
2647 txn->status = 200;
2648 stream_int_retnclose(req->prod, &http_200_chunk);
2649 goto return_prx_cond;
2650 }
2651
2652 /*
2653 * 3: Maybe we have to copy the original REQURI for the logs ?
2654 * Note: we cannot log anymore if the request has been
2655 * classified as invalid.
2656 */
2657 if (unlikely(s->logs.logwait & LW_REQ)) {
2658 /* we have a complete HTTP request that we must log */
2659 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2660 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002661
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 if (urilen >= REQURI_LEN)
2663 urilen = REQURI_LEN - 1;
2664 memcpy(txn->uri, &req->data[msg->som], urilen);
2665 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002666
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 if (!(s->logs.logwait &= ~LW_REQ))
2668 s->do_log(s);
2669 } else {
2670 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002671 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 }
Willy Tarreau06619262006-12-17 08:37:22 +01002673
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002675 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2676 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002677
Willy Tarreau5b154472009-12-21 20:11:07 +01002678 /* ... and check if the request is HTTP/1.1 or above */
2679 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002680 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2681 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2682 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002683 txn->flags |= TX_REQ_VER_11;
2684
2685 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002686 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002687
Willy Tarreau88d349d2010-01-25 12:15:43 +01002688 /* if the frontend has "option http-use-proxy-header", we'll check if
2689 * we have what looks like a proxied connection instead of a connection,
2690 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2691 * Note that this is *not* RFC-compliant, however browsers and proxies
2692 * happen to do that despite being non-standard :-(
2693 * We consider that a request not beginning with either '/' or '*' is
2694 * a proxied connection, which covers both "scheme://location" and
2695 * CONNECT ip:port.
2696 */
2697 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2698 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2699 txn->flags |= TX_USE_PX_CONN;
2700
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002701 /* transfer length unknown*/
2702 txn->flags &= ~TX_REQ_XFER_LEN;
2703
Willy Tarreau59234e92008-11-30 23:51:27 +01002704 /* 5: we may need to capture headers */
2705 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002706 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002707 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002708
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002709 /* 6: determine the transfer-length.
2710 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2711 * the presence of a message-body in a REQUEST and its transfer length
2712 * must be determined that way (in order of precedence) :
2713 * 1. The presence of a message-body in a request is signaled by the
2714 * inclusion of a Content-Length or Transfer-Encoding header field
2715 * in the request's header fields. When a request message contains
2716 * both a message-body of non-zero length and a method that does
2717 * not define any semantics for that request message-body, then an
2718 * origin server SHOULD either ignore the message-body or respond
2719 * with an appropriate error message (e.g., 413). A proxy or
2720 * gateway, when presented the same request, SHOULD either forward
2721 * the request inbound with the message- body or ignore the
2722 * message-body when determining a response.
2723 *
2724 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2725 * and the "chunked" transfer-coding (Section 6.2) is used, the
2726 * transfer-length is defined by the use of this transfer-coding.
2727 * If a Transfer-Encoding header field is present and the "chunked"
2728 * transfer-coding is not present, the transfer-length is defined
2729 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002730 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002731 * 3. If a Content-Length header field is present, its decimal value in
2732 * OCTETs represents both the entity-length and the transfer-length.
2733 * If a message is received with both a Transfer-Encoding header
2734 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002735 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002736 * 4. By the server closing the connection. (Closing the connection
2737 * cannot be used to indicate the end of a request body, since that
2738 * would leave no possibility for the server to send back a response.)
2739 *
2740 * Whenever a transfer-coding is applied to a message-body, the set of
2741 * transfer-codings MUST include "chunked", unless the message indicates
2742 * it is terminated by closing the connection. When the "chunked"
2743 * transfer-coding is used, it MUST be the last transfer-coding applied
2744 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002745 */
2746
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002747 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002748 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002749 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002750 while ((txn->flags & TX_REQ_VER_11) &&
2751 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002752 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2753 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2754 else if (txn->flags & TX_REQ_TE_CHNK) {
2755 /* bad transfer-encoding (chunked followed by something else) */
2756 use_close_only = 1;
2757 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2758 break;
2759 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002760 }
2761
Willy Tarreau32b47f42009-10-18 20:55:02 +02002762 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002763 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002764 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2765 signed long long cl;
2766
2767 if (!ctx.vlen)
2768 goto return_bad_req;
2769
2770 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2771 goto return_bad_req; /* parse failure */
2772
2773 if (cl < 0)
2774 goto return_bad_req;
2775
2776 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2777 goto return_bad_req; /* already specified, was different */
2778
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002779 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002780 msg->hdr_content_len = cl;
2781 }
2782
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002783 /* bodyless requests have a known length */
2784 if (!use_close_only)
2785 txn->flags |= TX_REQ_XFER_LEN;
2786
Willy Tarreaud787e662009-07-07 10:14:51 +02002787 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002788 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002789 req->analyse_exp = TICK_ETERNITY;
2790 return 1;
2791
2792 return_bad_req:
2793 /* We centralize bad requests processing here */
2794 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2795 /* we detected a parsing error. We want to archive this request
2796 * in the dedicated proxy area for later troubleshooting.
2797 */
2798 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2799 }
2800
2801 txn->req.msg_state = HTTP_MSG_ERROR;
2802 txn->status = 400;
2803 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002804
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002805 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002806 if (s->listener->counters)
2807 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002808
2809 return_prx_cond:
2810 if (!(s->flags & SN_ERR_MASK))
2811 s->flags |= SN_ERR_PRXCOND;
2812 if (!(s->flags & SN_FINST_MASK))
2813 s->flags |= SN_FINST_R;
2814
2815 req->analysers = 0;
2816 req->analyse_exp = TICK_ETERNITY;
2817 return 0;
2818}
2819
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820/* This stream analyser runs all HTTP request processing which is common to
2821 * frontends and backends, which means blocking ACLs, filters, connection-close,
2822 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002823 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002824 * either needs more data or wants to immediately abort the request (eg: deny,
2825 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002826 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002827int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002828{
Willy Tarreaud787e662009-07-07 10:14:51 +02002829 struct http_txn *txn = &s->txn;
2830 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002831 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002832 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002834 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002835 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002836
Willy Tarreau655dce92009-11-08 13:10:58 +01002837 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002838 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002839 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002840 return 0;
2841 }
2842
Willy Tarreau3a816292009-07-07 10:55:49 +02002843 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002844 req->analyse_exp = TICK_ETERNITY;
2845
2846 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2847 now_ms, __FUNCTION__,
2848 s,
2849 req,
2850 req->rex, req->wex,
2851 req->flags,
2852 req->l,
2853 req->analysers);
2854
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002855 /* first check whether we have some ACLs set to block this request */
2856 list_for_each_entry(cond, &px->block_cond, list) {
2857 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002858
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002859 ret = acl_pass(ret);
2860 if (cond->pol == ACL_COND_UNLESS)
2861 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002862
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002863 if (ret) {
2864 txn->status = 403;
2865 /* let's log the request time */
2866 s->logs.tv_request = now;
2867 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2868 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002870 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002871
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002872 do_stats = stats_check_uri(s, px);
2873
2874 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 +01002875 int ret = 1;
2876
2877 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
2878 continue;
2879
2880 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002881 if (req_acl->cond) {
2882 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
2883 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01002884
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002885 if (req_acl->cond->pol == ACL_COND_UNLESS)
2886 ret = !ret;
2887 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002888
2889 if (ret) {
2890 req_acl_final = req_acl;
2891 break;
2892 }
2893 }
2894
2895 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
2896 txn->status = 403;
2897 s->logs.tv_request = now;
2898 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2899 goto return_prx_cond;
2900 }
2901
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002902 /* try headers filters */
2903 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002904 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002905 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002906
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 /* has the request been denied ? */
2908 if (txn->flags & TX_CLDENY) {
2909 /* no need to go further */
2910 txn->status = 403;
2911 /* let's log the request time */
2912 s->logs.tv_request = now;
2913 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2914 goto return_prx_cond;
2915 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002916
2917 /* When a connection is tarpitted, we use the tarpit timeout,
2918 * which may be the same as the connect timeout if unspecified.
2919 * If unset, then set it to zero because we really want it to
2920 * eventually expire. We build the tarpit as an analyser.
2921 */
2922 if (txn->flags & TX_CLTARPIT) {
2923 buffer_erase(s->req);
2924 /* wipe the request out so that we can drop the connection early
2925 * if the client closes first.
2926 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002927 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002928 req->analysers = 0; /* remove switching rules etc... */
2929 req->analysers |= AN_REQ_HTTP_TARPIT;
2930 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2931 if (!req->analyse_exp)
2932 req->analyse_exp = tick_add(now_ms, 0);
2933 return 1;
2934 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002935 }
Willy Tarreau06619262006-12-17 08:37:22 +01002936
Willy Tarreau5b154472009-12-21 20:11:07 +01002937 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2938 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002939 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2940 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002941 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2942 * avoid to redo the same work if FE and BE have the same settings (common).
2943 * The method consists in checking if options changed between the two calls
2944 * (implying that either one is non-null, or one of them is non-null and we
2945 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002946 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002947
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002948 del_cl = del_ka = 0;
2949
Willy Tarreaudc008c52010-02-01 16:20:08 +01002950 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2951 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2952 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2953 (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 +01002954 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002955
Willy Tarreau5b154472009-12-21 20:11:07 +01002956 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2957 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002958 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2959 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002960 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002961 tmp = TX_CON_WANT_CLO;
2962
Willy Tarreau5b154472009-12-21 20:11:07 +01002963 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2964 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002965
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002966 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2967 /* parse the Connection header and possibly clean it */
2968 int to_del = 0;
2969 if ((txn->flags & TX_REQ_VER_11) ||
2970 (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)
2971 to_del |= 2; /* remove "keep-alive" */
2972 if (!(txn->flags & TX_REQ_VER_11))
2973 to_del |= 1; /* remove "close" */
2974 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002975 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002976
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002977 /* check if client or config asks for explicit close in KAL/SCL */
2978 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2979 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2980 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2981 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2982 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002983 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2984 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002985 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2986 }
Willy Tarreau78599912009-10-17 20:12:21 +02002987
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002988 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002989 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01002990 if (wl->cond) {
2991 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2992 ret = acl_pass(ret);
2993 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2994 ret = !ret;
2995 if (!ret)
2996 continue;
2997 }
2998
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002999 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003000 goto return_bad_req;
3001 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003002
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003003 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3004 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003005 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003006
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003007 if (!realm)
3008 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3009
Willy Tarreau844a7e72010-01-31 21:46:18 +01003010 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003011 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3012 txn->status = 401;
3013 stream_int_retnclose(req->prod, &msg);
3014 goto return_prx_cond;
3015 }
3016
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003017 if (do_stats) {
3018 /* We need to provied stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003019 * FIXME!!! that one is rather dangerous, we want to
3020 * make it follow standard rules (eg: clear req->analysers).
3021 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003022
3023 s->logs.tv_request = now;
3024 s->data_source = DATA_SRC_STATS;
3025 s->data_state = DATA_ST_INIT;
3026 s->task->nice = -32; /* small boost for HTTP statistics */
3027 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3028 s->rep->prod->private = s;
3029 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3030 req->analysers = 0;
3031
3032 return 0;
3033
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003034 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 /* check whether we have some ACLs set to redirect this request */
3037 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003038 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003039
Willy Tarreauf285f542010-01-03 20:03:03 +01003040 if (rule->cond) {
3041 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3042 ret = acl_pass(ret);
3043 if (rule->cond->pol == ACL_COND_UNLESS)
3044 ret = !ret;
3045 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003046
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003048 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 /* build redirect message */
3052 switch(rule->code) {
3053 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 msg_fmt = HTTP_303;
3055 break;
3056 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003057 msg_fmt = HTTP_301;
3058 break;
3059 case 302:
3060 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003061 msg_fmt = HTTP_302;
3062 break;
3063 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003064
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003065 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003067
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003068 switch(rule->type) {
3069 case REDIRECT_TYPE_PREFIX: {
3070 const char *path;
3071 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 path = http_get_path(txn);
3074 /* build message using path */
3075 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003076 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003077 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3078 int qs = 0;
3079 while (qs < pathlen) {
3080 if (path[qs] == '?') {
3081 pathlen = qs;
3082 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003083 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003084 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003085 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003086 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003087 } else {
3088 path = "/";
3089 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003090 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003091
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003092 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 goto return_bad_req;
3094
3095 /* add prefix. Note that if prefix == "/", we don't want to
3096 * add anything, otherwise it makes it hard for the user to
3097 * configure a self-redirection.
3098 */
3099 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003100 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3101 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003102 }
3103
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104 /* add path */
3105 memcpy(rdr.str + rdr.len, path, pathlen);
3106 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003107
3108 /* append a slash at the end of the location is needed and missing */
3109 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3110 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3111 if (rdr.len > rdr.size - 5)
3112 goto return_bad_req;
3113 rdr.str[rdr.len] = '/';
3114 rdr.len++;
3115 }
3116
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003117 break;
3118 }
3119 case REDIRECT_TYPE_LOCATION:
3120 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003121 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003123
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 /* add location */
3125 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3126 rdr.len += rule->rdr_len;
3127 break;
3128 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003129
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 if (rule->cookie_len) {
3131 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3132 rdr.len += 14;
3133 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3134 rdr.len += rule->cookie_len;
3135 memcpy(rdr.str + rdr.len, "\r\n", 2);
3136 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003137 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003138
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003139 /* add end of headers and the keep-alive/close status.
3140 * We may choose to set keep-alive if the Location begins
3141 * with a slash, because the client will come back to the
3142 * same server.
3143 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003144 txn->status = rule->code;
3145 /* let's log the request time */
3146 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003147
3148 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3149 (txn->flags & TX_REQ_XFER_LEN) &&
3150 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3151 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3152 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3153 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003154 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003155 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3156 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3157 rdr.len += 30;
3158 } else {
3159 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3160 rdr.len += 24;
3161 }
Willy Tarreau75661452010-01-10 10:35:01 +01003162 }
3163 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3164 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003165 buffer_write(req->prod->ob, rdr.str, rdr.len);
3166 /* "eat" the request */
3167 buffer_ignore(req, msg->sov - msg->som);
3168 msg->som = msg->sov;
3169 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003170 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3171 txn->req.msg_state = HTTP_MSG_CLOSED;
3172 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003173 break;
3174 } else {
3175 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003176 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3177 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3178 rdr.len += 29;
3179 } else {
3180 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3181 rdr.len += 23;
3182 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003183 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003184 goto return_prx_cond;
3185 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003186 }
3187 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003188
Willy Tarreau2be39392010-01-03 17:24:51 +01003189 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3190 * If this happens, then the data will not come immediately, so we must
3191 * send all what we have without waiting. Note that due to the small gain
3192 * in waiting for the body of the request, it's easier to simply put the
3193 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3194 * itself once used.
3195 */
3196 req->flags |= BF_SEND_DONTWAIT;
3197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 /* that's OK for us now, let's move on to next analysers */
3199 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003200
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003201 return_bad_req:
3202 /* We centralize bad requests processing here */
3203 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3204 /* we detected a parsing error. We want to archive this request
3205 * in the dedicated proxy area for later troubleshooting.
3206 */
3207 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3208 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 txn->req.msg_state = HTTP_MSG_ERROR;
3211 txn->status = 400;
3212 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003213
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003214 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003215 if (s->listener->counters)
3216 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003217
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 return_prx_cond:
3219 if (!(s->flags & SN_ERR_MASK))
3220 s->flags |= SN_ERR_PRXCOND;
3221 if (!(s->flags & SN_FINST_MASK))
3222 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003223
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003224 req->analysers = 0;
3225 req->analyse_exp = TICK_ETERNITY;
3226 return 0;
3227}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003228
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003229/* This function performs all the processing enabled for the current request.
3230 * It returns 1 if the processing can continue on next analysers, or zero if it
3231 * needs more data, encounters an error, or wants to immediately abort the
3232 * request. It relies on buffers flags, and updates s->req->analysers.
3233 */
3234int http_process_request(struct session *s, struct buffer *req, int an_bit)
3235{
3236 struct http_txn *txn = &s->txn;
3237 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003238
Willy Tarreau655dce92009-11-08 13:10:58 +01003239 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003240 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003241 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003242 return 0;
3243 }
3244
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003245 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3246 now_ms, __FUNCTION__,
3247 s,
3248 req,
3249 req->rex, req->wex,
3250 req->flags,
3251 req->l,
3252 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003253
Willy Tarreau59234e92008-11-30 23:51:27 +01003254 /*
3255 * Right now, we know that we have processed the entire headers
3256 * and that unwanted requests have been filtered out. We can do
3257 * whatever we want with the remaining request. Also, now we
3258 * may have separate values for ->fe, ->be.
3259 */
Willy Tarreau06619262006-12-17 08:37:22 +01003260
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 /*
3262 * If HTTP PROXY is set we simply get remote server address
3263 * parsing incoming request.
3264 */
3265 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003266 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003268
Willy Tarreau59234e92008-11-30 23:51:27 +01003269 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003270 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003271 * Note that doing so might move headers in the request, but
3272 * the fields will stay coherent and the URI will not move.
3273 * This should only be performed in the backend.
3274 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003275 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3277 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003278
Willy Tarreau59234e92008-11-30 23:51:27 +01003279 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003280 * 8: the appsession cookie was looked up very early in 1.2,
3281 * so let's do the same now.
3282 */
3283
3284 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003285 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003286 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003287 }
3288
3289 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003290 * 9: add X-Forwarded-For if either the frontend or the backend
3291 * asks for it.
3292 */
3293 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3294 if (s->cli_addr.ss_family == AF_INET) {
3295 /* Add an X-Forwarded-For header unless the source IP is
3296 * in the 'except' network range.
3297 */
3298 if ((!s->fe->except_mask.s_addr ||
3299 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3300 != s->fe->except_net.s_addr) &&
3301 (!s->be->except_mask.s_addr ||
3302 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3303 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003304 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 unsigned char *pn;
3306 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003307
3308 /* Note: we rely on the backend to get the header name to be used for
3309 * x-forwarded-for, because the header is really meant for the backends.
3310 * However, if the backend did not specify any option, we have to rely
3311 * on the frontend's header name.
3312 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003313 if (s->be->fwdfor_hdr_len) {
3314 len = s->be->fwdfor_hdr_len;
3315 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003316 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003317 len = s->fe->fwdfor_hdr_len;
3318 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003319 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003321
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003322 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003323 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003324 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003325 }
3326 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003327 else if (s->cli_addr.ss_family == AF_INET6) {
3328 /* FIXME: for the sake of completeness, we should also support
3329 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003330 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003331 int len;
3332 char pn[INET6_ADDRSTRLEN];
3333 inet_ntop(AF_INET6,
3334 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3335 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003336
Willy Tarreau59234e92008-11-30 23:51:27 +01003337 /* Note: we rely on the backend to get the header name to be used for
3338 * x-forwarded-for, because the header is really meant for the backends.
3339 * However, if the backend did not specify any option, we have to rely
3340 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003341 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003342 if (s->be->fwdfor_hdr_len) {
3343 len = s->be->fwdfor_hdr_len;
3344 memcpy(trash, s->be->fwdfor_hdr_name, len);
3345 } else {
3346 len = s->fe->fwdfor_hdr_len;
3347 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003348 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003349 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003350
Willy Tarreau59234e92008-11-30 23:51:27 +01003351 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003352 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003353 goto return_bad_req;
3354 }
3355 }
3356
3357 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003358 * 10: add X-Original-To if either the frontend or the backend
3359 * asks for it.
3360 */
3361 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3362
3363 /* FIXME: don't know if IPv6 can handle that case too. */
3364 if (s->cli_addr.ss_family == AF_INET) {
3365 /* Add an X-Original-To header unless the destination IP is
3366 * in the 'except' network range.
3367 */
3368 if (!(s->flags & SN_FRT_ADDR_SET))
3369 get_frt_addr(s);
3370
3371 if ((!s->fe->except_mask_to.s_addr ||
3372 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3373 != s->fe->except_to.s_addr) &&
3374 (!s->be->except_mask_to.s_addr ||
3375 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3376 != s->be->except_to.s_addr)) {
3377 int len;
3378 unsigned char *pn;
3379 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3380
3381 /* Note: we rely on the backend to get the header name to be used for
3382 * x-original-to, because the header is really meant for the backends.
3383 * However, if the backend did not specify any option, we have to rely
3384 * on the frontend's header name.
3385 */
3386 if (s->be->orgto_hdr_len) {
3387 len = s->be->orgto_hdr_len;
3388 memcpy(trash, s->be->orgto_hdr_name, len);
3389 } else {
3390 len = s->fe->orgto_hdr_len;
3391 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003392 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003393 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3394
3395 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003396 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003397 goto return_bad_req;
3398 }
3399 }
3400 }
3401
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003402 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3403 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3404 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3405 unsigned int want_flags = 0;
3406
3407 if (txn->flags & TX_REQ_VER_11) {
3408 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3409 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3410 want_flags |= TX_CON_CLO_SET;
3411 } else {
3412 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
3413 want_flags |= TX_CON_KAL_SET;
3414 }
3415
3416 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3417 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003418 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003419
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003420
Willy Tarreau522d6c02009-12-06 18:49:18 +01003421 /* If we have no server assigned yet and we're balancing on url_param
3422 * with a POST request, we may be interested in checking the body for
3423 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003424 */
3425 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3426 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003427 s->be->url_param_post_limit != 0 &&
3428 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003429 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003430 buffer_dont_connect(req);
3431 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003432 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003433
Willy Tarreaud98cf932009-12-27 22:54:55 +01003434 if (txn->flags & TX_REQ_XFER_LEN)
3435 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003436
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 /*************************************************************
3438 * OK, that's finished for the headers. We have done what we *
3439 * could. Let's switch to the DATA state. *
3440 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003441 req->analyse_exp = TICK_ETERNITY;
3442 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003443
Willy Tarreau59234e92008-11-30 23:51:27 +01003444 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003445 /* OK let's go on with the BODY now */
3446 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003447
Willy Tarreau59234e92008-11-30 23:51:27 +01003448 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003449 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003450 /* we detected a parsing error. We want to archive this request
3451 * in the dedicated proxy area for later troubleshooting.
3452 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003453 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003454 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003455
Willy Tarreau59234e92008-11-30 23:51:27 +01003456 txn->req.msg_state = HTTP_MSG_ERROR;
3457 txn->status = 400;
3458 req->analysers = 0;
3459 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003460
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003461 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003462 if (s->listener->counters)
3463 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003464
Willy Tarreau59234e92008-11-30 23:51:27 +01003465 if (!(s->flags & SN_ERR_MASK))
3466 s->flags |= SN_ERR_PRXCOND;
3467 if (!(s->flags & SN_FINST_MASK))
3468 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003469 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003470}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003471
Willy Tarreau60b85b02008-11-30 23:28:40 +01003472/* This function is an analyser which processes the HTTP tarpit. It always
3473 * returns zero, at the beginning because it prevents any other processing
3474 * from occurring, and at the end because it terminates the request.
3475 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003476int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003477{
3478 struct http_txn *txn = &s->txn;
3479
3480 /* This connection is being tarpitted. The CLIENT side has
3481 * already set the connect expiration date to the right
3482 * timeout. We just have to check that the client is still
3483 * there and that the timeout has not expired.
3484 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003485 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003486 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3487 !tick_is_expired(req->analyse_exp, now_ms))
3488 return 0;
3489
3490 /* We will set the queue timer to the time spent, just for
3491 * logging purposes. We fake a 500 server error, so that the
3492 * attacker will not suspect his connection has been tarpitted.
3493 * It will not cause trouble to the logs because we can exclude
3494 * the tarpitted connections by filtering on the 'PT' status flags.
3495 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003496 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3497
3498 txn->status = 500;
3499 if (req->flags != BF_READ_ERROR)
3500 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3501
3502 req->analysers = 0;
3503 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003504
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003505 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003506 if (s->listener->counters)
3507 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003508
Willy Tarreau60b85b02008-11-30 23:28:40 +01003509 if (!(s->flags & SN_ERR_MASK))
3510 s->flags |= SN_ERR_PRXCOND;
3511 if (!(s->flags & SN_FINST_MASK))
3512 s->flags |= SN_FINST_T;
3513 return 0;
3514}
3515
Willy Tarreaud34af782008-11-30 23:36:37 +01003516/* This function is an analyser which processes the HTTP request body. It looks
3517 * for parameters to be used for the load balancing algorithm (url_param). It
3518 * must only be called after the standard HTTP request processing has occurred,
3519 * because it expects the request to be parsed. It returns zero if it needs to
3520 * read more data, or 1 once it has completed its analysis.
3521 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003522int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003523{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003524 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003525 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003526 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003527
3528 /* We have to parse the HTTP request body to find any required data.
3529 * "balance url_param check_post" should have been the only way to get
3530 * into this. We were brought here after HTTP header analysis, so all
3531 * related structures are ready.
3532 */
3533
Willy Tarreau522d6c02009-12-06 18:49:18 +01003534 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3535 goto missing_data;
3536
3537 if (msg->msg_state < HTTP_MSG_100_SENT) {
3538 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3539 * send an HTTP/1.1 100 Continue intermediate response.
3540 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003541 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003542 struct hdr_ctx ctx;
3543 ctx.idx = 0;
3544 /* Expect is allowed in 1.1, look for it */
3545 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3546 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3547 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3548 }
3549 }
3550 msg->msg_state = HTTP_MSG_100_SENT;
3551 }
3552
3553 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003554 /* we have msg->col and msg->sov which both point to the first
3555 * byte of message body. msg->som still points to the beginning
3556 * of the message. We must save the body in req->lr because it
3557 * survives buffer re-alignments.
3558 */
3559 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003560 if (txn->flags & TX_REQ_TE_CHNK)
3561 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3562 else
3563 msg->msg_state = HTTP_MSG_DATA;
3564 }
3565
3566 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003567 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003568 * set ->sov and ->lr to point to the body and switch to DATA or
3569 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003570 */
3571 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003572
Willy Tarreau115acb92009-12-26 13:56:06 +01003573 if (!ret)
3574 goto missing_data;
3575 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003576 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003577 }
3578
Willy Tarreaud98cf932009-12-27 22:54:55 +01003579 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003580 * We have the first non-header byte in msg->col, which is either the
3581 * beginning of the chunk size or of the data. The first data byte is in
3582 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3583 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003584 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003585
3586 if (msg->hdr_content_len < limit)
3587 limit = msg->hdr_content_len;
3588
Willy Tarreau7c96f672009-12-27 22:47:25 +01003589 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003590 goto http_end;
3591
3592 missing_data:
3593 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003594 if (req->flags & BF_FULL)
3595 goto return_bad_req;
3596
Willy Tarreau522d6c02009-12-06 18:49:18 +01003597 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3598 txn->status = 408;
3599 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3600 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003601 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003602
3603 /* we get here if we need to wait for more data */
3604 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003605 /* Not enough data. We'll re-use the http-request
3606 * timeout here. Ideally, we should set the timeout
3607 * relative to the accept() date. We just set the
3608 * request timeout once at the beginning of the
3609 * request.
3610 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003611 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003612 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003613 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003614 return 0;
3615 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003616
3617 http_end:
3618 /* The situation will not evolve, so let's give up on the analysis. */
3619 s->logs.tv_request = now; /* update the request timer to reflect full request */
3620 req->analysers &= ~an_bit;
3621 req->analyse_exp = TICK_ETERNITY;
3622 return 1;
3623
3624 return_bad_req: /* let's centralize all bad requests */
3625 txn->req.msg_state = HTTP_MSG_ERROR;
3626 txn->status = 400;
3627 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3628
3629 return_err_msg:
3630 req->analysers = 0;
3631 s->fe->counters.failed_req++;
3632 if (s->listener->counters)
3633 s->listener->counters->failed_req++;
3634
3635 if (!(s->flags & SN_ERR_MASK))
3636 s->flags |= SN_ERR_PRXCOND;
3637 if (!(s->flags & SN_FINST_MASK))
3638 s->flags |= SN_FINST_R;
3639 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003640}
3641
Willy Tarreau610ecce2010-01-04 21:15:02 +01003642/* Terminate current transaction and prepare a new one. This is very tricky
3643 * right now but it works.
3644 */
3645void http_end_txn_clean_session(struct session *s)
3646{
3647 /* FIXME: We need a more portable way of releasing a backend's and a
3648 * server's connections. We need a safer way to reinitialize buffer
3649 * flags. We also need a more accurate method for computing per-request
3650 * data.
3651 */
3652 http_silent_debug(__LINE__, s);
3653
3654 s->req->cons->flags |= SI_FL_NOLINGER;
3655 s->req->cons->shutr(s->req->cons);
3656 s->req->cons->shutw(s->req->cons);
3657
3658 http_silent_debug(__LINE__, s);
3659
3660 if (s->flags & SN_BE_ASSIGNED)
3661 s->be->beconn--;
3662
3663 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3664 session_process_counters(s);
3665
3666 if (s->txn.status) {
3667 int n;
3668
3669 n = s->txn.status / 100;
3670 if (n < 1 || n > 5)
3671 n = 0;
3672
3673 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003674 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003675
Willy Tarreau24657792010-02-26 10:30:28 +01003676 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003677 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003678 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003679 }
3680
3681 /* don't count other requests' data */
3682 s->logs.bytes_in -= s->req->l - s->req->send_max;
3683 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3684
3685 /* let's do a final log if we need it */
3686 if (s->logs.logwait &&
3687 !(s->flags & SN_MONITOR) &&
3688 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3689 s->do_log(s);
3690 }
3691
3692 s->logs.accept_date = date; /* user-visible date for logging */
3693 s->logs.tv_accept = now; /* corrected date for internal use */
3694 tv_zero(&s->logs.tv_request);
3695 s->logs.t_queue = -1;
3696 s->logs.t_connect = -1;
3697 s->logs.t_data = -1;
3698 s->logs.t_close = 0;
3699 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3700 s->logs.srv_queue_size = 0; /* we will get this number soon */
3701
3702 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3703 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3704
3705 if (s->pend_pos)
3706 pendconn_free(s->pend_pos);
3707
3708 if (s->srv) {
3709 if (s->flags & SN_CURR_SESS) {
3710 s->flags &= ~SN_CURR_SESS;
3711 s->srv->cur_sess--;
3712 }
3713 if (may_dequeue_tasks(s->srv, s->be))
3714 process_srv_queue(s->srv);
3715 }
3716
3717 if (unlikely(s->srv_conn))
3718 sess_change_server(s, NULL);
3719 s->srv = NULL;
3720
3721 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3722 s->req->cons->fd = -1; /* just to help with debugging */
3723 s->req->cons->err_type = SI_ET_NONE;
3724 s->req->cons->err_loc = NULL;
3725 s->req->cons->exp = TICK_ETERNITY;
3726 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003727 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3728 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 +01003729 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003730 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3731 s->txn.meth = 0;
3732 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003733 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003734 if (s->be->options2 & PR_O2_INDEPSTR)
3735 s->req->cons->flags |= SI_FL_INDEP_STR;
3736
3737 /* if the request buffer is not empty, it means we're
3738 * about to process another request, so send pending
3739 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003740 * Just don't do this if the buffer is close to be full,
3741 * because the request will wait for it to flush a little
3742 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003744 if (s->req->l > s->req->send_max) {
3745 if (s->rep->send_max &&
3746 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003747 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3748 s->rep->flags |= BF_EXPECT_MORE;
3749 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003750
3751 /* we're removing the analysers, we MUST re-enable events detection */
3752 buffer_auto_read(s->req);
3753 buffer_auto_close(s->req);
3754 buffer_auto_read(s->rep);
3755 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756
3757 /* make ->lr point to the first non-forwarded byte */
3758 s->req->lr = s->req->w + s->req->send_max;
3759 if (s->req->lr >= s->req->data + s->req->size)
3760 s->req->lr -= s->req->size;
3761 s->rep->lr = s->rep->w + s->rep->send_max;
3762 if (s->rep->lr >= s->rep->data + s->rep->size)
3763 s->rep->lr -= s->req->size;
3764
3765 s->req->analysers |= s->fe->fe_req_ana;
3766 s->rep->analysers = 0;
3767
3768 http_silent_debug(__LINE__, s);
3769}
3770
3771
3772/* This function updates the request state machine according to the response
3773 * state machine and buffer flags. It returns 1 if it changes anything (flag
3774 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3775 * it is only used to find when a request/response couple is complete. Both
3776 * this function and its equivalent should loop until both return zero. It
3777 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3778 */
3779int http_sync_req_state(struct session *s)
3780{
3781 struct buffer *buf = s->req;
3782 struct http_txn *txn = &s->txn;
3783 unsigned int old_flags = buf->flags;
3784 unsigned int old_state = txn->req.msg_state;
3785
3786 http_silent_debug(__LINE__, s);
3787 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3788 return 0;
3789
3790 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003791 /* No need to read anymore, the request was completely parsed.
3792 * We can shut the read side unless we want to abort_on_close.
3793 */
3794 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3795 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796
3797 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3798 goto wait_other_side;
3799
3800 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3801 /* The server has not finished to respond, so we
3802 * don't want to move in order not to upset it.
3803 */
3804 goto wait_other_side;
3805 }
3806
3807 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3808 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003809 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810 txn->req.msg_state = HTTP_MSG_TUNNEL;
3811 goto wait_other_side;
3812 }
3813
3814 /* When we get here, it means that both the request and the
3815 * response have finished receiving. Depending on the connection
3816 * mode, we'll have to wait for the last bytes to leave in either
3817 * direction, and sometimes for a close to be effective.
3818 */
3819
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003820 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3821 /* Server-close mode : queue a connection close to the server */
3822 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003823 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003824 }
3825 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3826 /* Option forceclose is set, or either side wants to close,
3827 * let's enforce it now that we're not expecting any new
3828 * data to come. The caller knows the session is complete
3829 * once both states are CLOSED.
3830 */
3831 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832 buffer_shutr_now(buf);
3833 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003834 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003835 }
3836 else {
3837 /* The last possible modes are keep-alive and tunnel. Since tunnel
3838 * mode does not set the body analyser, we can't reach this place
3839 * in tunnel mode, so we're left with keep-alive only.
3840 * This mode is currently not implemented, we switch to tunnel mode.
3841 */
3842 buffer_auto_read(buf);
3843 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003844 }
3845
3846 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3847 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003848 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3849
Willy Tarreau610ecce2010-01-04 21:15:02 +01003850 if (!(buf->flags & BF_OUT_EMPTY)) {
3851 txn->req.msg_state = HTTP_MSG_CLOSING;
3852 goto http_msg_closing;
3853 }
3854 else {
3855 txn->req.msg_state = HTTP_MSG_CLOSED;
3856 goto http_msg_closed;
3857 }
3858 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003859 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003860 }
3861
3862 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3863 http_msg_closing:
3864 /* nothing else to forward, just waiting for the output buffer
3865 * to be empty and for the shutw_now to take effect.
3866 */
3867 if (buf->flags & BF_OUT_EMPTY) {
3868 txn->req.msg_state = HTTP_MSG_CLOSED;
3869 goto http_msg_closed;
3870 }
3871 else if (buf->flags & BF_SHUTW) {
3872 txn->req.msg_state = HTTP_MSG_ERROR;
3873 goto wait_other_side;
3874 }
3875 }
3876
3877 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3878 http_msg_closed:
3879 goto wait_other_side;
3880 }
3881
3882 wait_other_side:
3883 http_silent_debug(__LINE__, s);
3884 return txn->req.msg_state != old_state || buf->flags != old_flags;
3885}
3886
3887
3888/* This function updates the response state machine according to the request
3889 * state machine and buffer flags. It returns 1 if it changes anything (flag
3890 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3891 * it is only used to find when a request/response couple is complete. Both
3892 * this function and its equivalent should loop until both return zero. It
3893 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3894 */
3895int http_sync_res_state(struct session *s)
3896{
3897 struct buffer *buf = s->rep;
3898 struct http_txn *txn = &s->txn;
3899 unsigned int old_flags = buf->flags;
3900 unsigned int old_state = txn->rsp.msg_state;
3901
3902 http_silent_debug(__LINE__, s);
3903 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3904 return 0;
3905
3906 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3907 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003908 * still monitor the server connection for a possible close
3909 * while the request is being uploaded, so we don't disable
3910 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003911 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003912 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003913
3914 if (txn->req.msg_state == HTTP_MSG_ERROR)
3915 goto wait_other_side;
3916
3917 if (txn->req.msg_state < HTTP_MSG_DONE) {
3918 /* The client seems to still be sending data, probably
3919 * because we got an error response during an upload.
3920 * We have the choice of either breaking the connection
3921 * or letting it pass through. Let's do the later.
3922 */
3923 goto wait_other_side;
3924 }
3925
3926 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3927 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003928 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3930 goto wait_other_side;
3931 }
3932
3933 /* When we get here, it means that both the request and the
3934 * response have finished receiving. Depending on the connection
3935 * mode, we'll have to wait for the last bytes to leave in either
3936 * direction, and sometimes for a close to be effective.
3937 */
3938
3939 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3940 /* Server-close mode : shut read and wait for the request
3941 * side to close its output buffer. The caller will detect
3942 * when we're in DONE and the other is in CLOSED and will
3943 * catch that for the final cleanup.
3944 */
3945 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3946 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003948 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3949 /* Option forceclose is set, or either side wants to close,
3950 * let's enforce it now that we're not expecting any new
3951 * data to come. The caller knows the session is complete
3952 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003953 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003954 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3955 buffer_shutr_now(buf);
3956 buffer_shutw_now(buf);
3957 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003958 }
3959 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003960 /* The last possible modes are keep-alive and tunnel. Since tunnel
3961 * mode does not set the body analyser, we can't reach this place
3962 * in tunnel mode, so we're left with keep-alive only.
3963 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003964 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003965 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003966 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967 }
3968
3969 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3970 /* if we've just closed an output, let's switch */
3971 if (!(buf->flags & BF_OUT_EMPTY)) {
3972 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3973 goto http_msg_closing;
3974 }
3975 else {
3976 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3977 goto http_msg_closed;
3978 }
3979 }
3980 goto wait_other_side;
3981 }
3982
3983 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3984 http_msg_closing:
3985 /* nothing else to forward, just waiting for the output buffer
3986 * to be empty and for the shutw_now to take effect.
3987 */
3988 if (buf->flags & BF_OUT_EMPTY) {
3989 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3990 goto http_msg_closed;
3991 }
3992 else if (buf->flags & BF_SHUTW) {
3993 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01003994 s->be->counters.cli_aborts++;
3995 if (s->srv)
3996 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 goto wait_other_side;
3998 }
3999 }
4000
4001 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4002 http_msg_closed:
4003 /* drop any pending data */
4004 buffer_ignore(buf, buf->l - buf->send_max);
4005 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004006 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 goto wait_other_side;
4008 }
4009
4010 wait_other_side:
4011 http_silent_debug(__LINE__, s);
4012 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4013}
4014
4015
4016/* Resync the request and response state machines. Return 1 if either state
4017 * changes.
4018 */
4019int http_resync_states(struct session *s)
4020{
4021 struct http_txn *txn = &s->txn;
4022 int old_req_state = txn->req.msg_state;
4023 int old_res_state = txn->rsp.msg_state;
4024
4025 http_silent_debug(__LINE__, s);
4026 http_sync_req_state(s);
4027 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004028 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004029 if (!http_sync_res_state(s))
4030 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004031 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004032 if (!http_sync_req_state(s))
4033 break;
4034 }
4035 http_silent_debug(__LINE__, s);
4036 /* OK, both state machines agree on a compatible state.
4037 * There are a few cases we're interested in :
4038 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4039 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4040 * directions, so let's simply disable both analysers.
4041 * - HTTP_MSG_CLOSED on the response only means we must abort the
4042 * request.
4043 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4044 * with server-close mode means we've completed one request and we
4045 * must re-initialize the server connection.
4046 */
4047
4048 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4049 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4050 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4051 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4052 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004053 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004054 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004055 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004056 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004057 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004058 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004059 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4060 txn->rsp.msg_state == HTTP_MSG_ERROR ||
4061 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004062 s->rep->analysers = 0;
4063 buffer_auto_close(s->rep);
4064 buffer_auto_read(s->rep);
4065 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004066 buffer_abort(s->req);
4067 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004068 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004069 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004070 }
4071 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4072 txn->rsp.msg_state == HTTP_MSG_DONE &&
4073 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4074 /* server-close: terminate this server connection and
4075 * reinitialize a fresh-new transaction.
4076 */
4077 http_end_txn_clean_session(s);
4078 }
4079
4080 http_silent_debug(__LINE__, s);
4081 return txn->req.msg_state != old_req_state ||
4082 txn->rsp.msg_state != old_res_state;
4083}
4084
Willy Tarreaud98cf932009-12-27 22:54:55 +01004085/* This function is an analyser which forwards request body (including chunk
4086 * sizes if any). It is called as soon as we must forward, even if we forward
4087 * zero byte. The only situation where it must not be called is when we're in
4088 * tunnel mode and we want to forward till the close. It's used both to forward
4089 * remaining data and to resync after end of body. It expects the msg_state to
4090 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4091 * read more data, or 1 once we can go on with next request or end the session.
4092 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4093 * bytes of pending data + the headers if not already done (between som and sov).
4094 * It eventually adjusts som to match sov after the data in between have been sent.
4095 */
4096int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4097{
4098 struct http_txn *txn = &s->txn;
4099 struct http_msg *msg = &s->txn.req;
4100
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004101 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4102 return 0;
4103
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004104 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4105 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
4106 /* Output closed while we were sending data. We must abort. */
4107 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004108 buffer_auto_read(req);
4109 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004110 req->analysers &= ~an_bit;
4111 return 1;
4112 }
4113
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004114 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004115
4116 /* Note that we don't have to send 100-continue back because we don't
4117 * need the data to complete our job, and it's up to the server to
4118 * decide whether to return 100, 417 or anything else in return of
4119 * an "Expect: 100-continue" header.
4120 */
4121
4122 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4123 /* we have msg->col and msg->sov which both point to the first
4124 * byte of message body. msg->som still points to the beginning
4125 * of the message. We must save the body in req->lr because it
4126 * survives buffer re-alignments.
4127 */
4128 req->lr = req->data + msg->sov;
4129 if (txn->flags & TX_REQ_TE_CHNK)
4130 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4131 else {
4132 msg->msg_state = HTTP_MSG_DATA;
4133 }
4134 }
4135
Willy Tarreaud98cf932009-12-27 22:54:55 +01004136 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004138 /* we may have some data pending */
4139 if (msg->hdr_content_len || msg->som != msg->sov) {
4140 int bytes = msg->sov - msg->som;
4141 if (bytes < 0) /* sov may have wrapped at the end */
4142 bytes += req->size;
4143 buffer_forward(req, bytes + msg->hdr_content_len);
4144 msg->hdr_content_len = 0; /* don't forward that again */
4145 msg->som = msg->sov;
4146 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004147
Willy Tarreaucaabe412010-01-03 23:08:28 +01004148 if (msg->msg_state == HTTP_MSG_DATA) {
4149 /* must still forward */
4150 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004151 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004152
4153 /* nothing left to forward */
4154 if (txn->flags & TX_REQ_TE_CHNK)
4155 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004157 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004158 }
4159 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004160 /* read the chunk size and assign it to ->hdr_content_len, then
4161 * set ->sov and ->lr to point to the body and switch to DATA or
4162 * TRAILERS state.
4163 */
4164 int ret = http_parse_chunk_size(req, msg);
4165
4166 if (!ret)
4167 goto missing_data;
4168 else if (ret < 0)
4169 goto return_bad_req;
4170 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004171 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004172 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4173 /* we want the CRLF after the data */
4174 int ret;
4175
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004176 req->lr = req->w + req->send_max;
4177 if (req->lr >= req->data + req->size)
4178 req->lr -= req->size;
4179
Willy Tarreaud98cf932009-12-27 22:54:55 +01004180 ret = http_skip_chunk_crlf(req, msg);
4181
4182 if (ret == 0)
4183 goto missing_data;
4184 else if (ret < 0)
4185 goto return_bad_req;
4186 /* we're in MSG_CHUNK_SIZE now */
4187 }
4188 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4189 int ret = http_forward_trailers(req, msg);
4190
4191 if (ret == 0)
4192 goto missing_data;
4193 else if (ret < 0)
4194 goto return_bad_req;
4195 /* we're in HTTP_MSG_DONE now */
4196 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197 else {
4198 /* other states, DONE...TUNNEL */
4199 if (http_resync_states(s)) {
4200 /* some state changes occurred, maybe the analyser
4201 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004202 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4204 goto return_bad_req;
4205 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004206 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004208 }
4209 }
4210
Willy Tarreaud98cf932009-12-27 22:54:55 +01004211 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004212 /* stop waiting for data if the input is closed before the end */
4213 if (req->flags & BF_SHUTR)
4214 goto return_bad_req;
4215
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004216 /* waiting for the last bits to leave the buffer */
4217 if (req->flags & BF_SHUTW)
4218 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219
4220 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004221 return 0;
4222
Willy Tarreaud98cf932009-12-27 22:54:55 +01004223 return_bad_req: /* let's centralize all bad requests */
4224 txn->req.msg_state = HTTP_MSG_ERROR;
4225 txn->status = 400;
4226 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004227 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 +01004228 req->analysers = 0;
4229 s->fe->counters.failed_req++;
4230 if (s->listener->counters)
4231 s->listener->counters->failed_req++;
4232
4233 if (!(s->flags & SN_ERR_MASK))
4234 s->flags |= SN_ERR_PRXCOND;
4235 if (!(s->flags & SN_FINST_MASK))
4236 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004237 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004238 return 0;
4239}
4240
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004241/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4242 * processing can continue on next analysers, or zero if it either needs more
4243 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4244 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4245 * when it has nothing left to do, and may remove any analyser when it wants to
4246 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004247 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004248int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004249{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004250 struct http_txn *txn = &s->txn;
4251 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004252 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004253 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004254 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004255 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004256
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004257 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 +02004258 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004259 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004260 rep,
4261 rep->rex, rep->wex,
4262 rep->flags,
4263 rep->l,
4264 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004265
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004266 /*
4267 * Now parse the partial (or complete) lines.
4268 * We will check the response syntax, and also join multi-line
4269 * headers. An index of all the lines will be elaborated while
4270 * parsing.
4271 *
4272 * For the parsing, we use a 28 states FSM.
4273 *
4274 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004275 * rep->data + msg->som = beginning of response
4276 * rep->data + msg->eoh = end of processed headers / start of current one
4277 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004278 * rep->lr = first non-visited byte
4279 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004280 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004281 */
4282
Willy Tarreau83e3af02009-12-28 17:39:57 +01004283 /* There's a protected area at the end of the buffer for rewriting
4284 * purposes. We don't want to start to parse the request if the
4285 * protected area is affected, because we may have to move processed
4286 * data later, which is much more complicated.
4287 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004288 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4289 if (unlikely((rep->flags & BF_FULL) ||
4290 rep->r < rep->lr ||
4291 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4292 if (rep->send_max) {
4293 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004294 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4295 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004296 buffer_dont_close(rep);
4297 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4298 return 0;
4299 }
4300 if (rep->l <= rep->size - global.tune.maxrewrite)
4301 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004302 }
4303
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004304 if (likely(rep->lr < rep->r))
4305 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004306 }
4307
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004308 /* 1: we might have to print this header in debug mode */
4309 if (unlikely((global.mode & MODE_DEBUG) &&
4310 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004311 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004312 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004313
Willy Tarreau962c3f42010-01-10 00:15:35 +01004314 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004315 eol = sol + msg->sl.rq.l;
4316 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004317
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004318 sol += hdr_idx_first_pos(&txn->hdr_idx);
4319 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004320
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004321 while (cur_idx) {
4322 eol = sol + txn->hdr_idx.v[cur_idx].len;
4323 debug_hdr("srvhdr", s, sol, eol);
4324 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4325 cur_idx = txn->hdr_idx.v[cur_idx].next;
4326 }
4327 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004328
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004329 /*
4330 * Now we quickly check if we have found a full valid response.
4331 * If not so, we check the FD and buffer states before leaving.
4332 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004333 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004334 * responses are checked first.
4335 *
4336 * Depending on whether the client is still there or not, we
4337 * may send an error response back or not. Note that normally
4338 * we should only check for HTTP status there, and check I/O
4339 * errors somewhere else.
4340 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004341
Willy Tarreau655dce92009-11-08 13:10:58 +01004342 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004343 /* Invalid response */
4344 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4345 /* we detected a parsing error. We want to archive this response
4346 * in the dedicated proxy area for later troubleshooting.
4347 */
4348 hdr_response_bad:
4349 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4350 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4351
4352 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004353 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004354 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004355 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4356 }
Willy Tarreau64648412010-03-05 10:41:54 +01004357 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004358 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004359 rep->analysers = 0;
4360 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004361 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004362 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004363 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4364
4365 if (!(s->flags & SN_ERR_MASK))
4366 s->flags |= SN_ERR_PRXCOND;
4367 if (!(s->flags & SN_FINST_MASK))
4368 s->flags |= SN_FINST_H;
4369
4370 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004371 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004372
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004373 /* too large response does not fit in buffer. */
4374 else if (rep->flags & BF_FULL) {
4375 goto hdr_response_bad;
4376 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004377
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004378 /* read error */
4379 else if (rep->flags & BF_READ_ERROR) {
4380 if (msg->err_pos >= 0)
4381 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004382
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_ERROR);
4387 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004388
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 = 502;
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_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004395
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004396 if (!(s->flags & SN_ERR_MASK))
4397 s->flags |= SN_ERR_SRVCL;
4398 if (!(s->flags & SN_FINST_MASK))
4399 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004400 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004401 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004402
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004403 /* read timeout : return a 504 to the client. */
4404 else if (rep->flags & BF_READ_TIMEOUT) {
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_READ_TIMEOUT);
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 = 504;
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_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004420
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004421 if (!(s->flags & SN_ERR_MASK))
4422 s->flags |= SN_ERR_SRVTO;
4423 if (!(s->flags & SN_FINST_MASK))
4424 s->flags |= SN_FINST_H;
4425 return 0;
4426 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004427
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004428 /* close from server */
4429 else if (rep->flags & BF_SHUTR) {
4430 if (msg->err_pos >= 0)
4431 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004432
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004433 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004434 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004435 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004436 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4437 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004438
Willy Tarreau90deb182010-01-07 00:20:41 +01004439 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004440 rep->analysers = 0;
4441 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004442 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004443 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004444 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004445
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446 if (!(s->flags & SN_ERR_MASK))
4447 s->flags |= SN_ERR_SRVCL;
4448 if (!(s->flags & SN_FINST_MASK))
4449 s->flags |= SN_FINST_H;
4450 return 0;
4451 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004452
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004453 /* write error to client (we don't send any message then) */
4454 else if (rep->flags & BF_WRITE_ERROR) {
4455 if (msg->err_pos >= 0)
4456 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004457
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 s->be->counters.failed_resp++;
4459 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004460 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004461
4462 if (!(s->flags & SN_ERR_MASK))
4463 s->flags |= SN_ERR_CLICL;
4464 if (!(s->flags & SN_FINST_MASK))
4465 s->flags |= SN_FINST_H;
4466
4467 /* process_session() will take care of the error */
4468 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004469 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004470
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471 buffer_dont_close(rep);
4472 return 0;
4473 }
4474
4475 /* More interesting part now : we know that we have a complete
4476 * response which at least looks like HTTP. We have an indicator
4477 * of each header's length, so we can parse them quickly.
4478 */
4479
4480 if (unlikely(msg->err_pos >= 0))
4481 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4482
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004483 /*
4484 * 1: get the status code
4485 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004486 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004487 if (n < 1 || n > 5)
4488 n = 0;
4489 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004490
Willy Tarreau5b154472009-12-21 20:11:07 +01004491 /* check if the response is HTTP/1.1 or above */
4492 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004493 ((msg->sol[5] > '1') ||
4494 ((msg->sol[5] == '1') &&
4495 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004496 txn->flags |= TX_RES_VER_11;
4497
4498 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004499 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 +01004500
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004501 /* transfer length unknown*/
4502 txn->flags &= ~TX_RES_XFER_LEN;
4503
Willy Tarreau962c3f42010-01-10 00:15:35 +01004504 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505
Willy Tarreau39650402010-03-15 19:44:39 +01004506 /* Adjust server's health based on status code. Note: status codes 501
4507 * and 505 are triggered on demand by client request, so we must not
4508 * count them as server failures.
4509 */
4510 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004511 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4512 else
4513 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4514
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004515 /*
4516 * 2: check for cacheability.
4517 */
4518
4519 switch (txn->status) {
4520 case 200:
4521 case 203:
4522 case 206:
4523 case 300:
4524 case 301:
4525 case 410:
4526 /* RFC2616 @13.4:
4527 * "A response received with a status code of
4528 * 200, 203, 206, 300, 301 or 410 MAY be stored
4529 * by a cache (...) unless a cache-control
4530 * directive prohibits caching."
4531 *
4532 * RFC2616 @9.5: POST method :
4533 * "Responses to this method are not cacheable,
4534 * unless the response includes appropriate
4535 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004536 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 if (likely(txn->meth != HTTP_METH_POST) &&
4538 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4539 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4540 break;
4541 default:
4542 break;
4543 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004544
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 /*
4546 * 3: we may need to capture headers
4547 */
4548 s->logs.logwait &= ~LW_RESP;
4549 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004550 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551 txn->rsp.cap, s->fe->rsp_cap);
4552
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004553 /* 4: determine the transfer-length.
4554 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4555 * the presence of a message-body in a RESPONSE and its transfer length
4556 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004557 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004558 * All responses to the HEAD request method MUST NOT include a
4559 * message-body, even though the presence of entity-header fields
4560 * might lead one to believe they do. All 1xx (informational), 204
4561 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4562 * message-body. All other responses do include a message-body,
4563 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004564 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004565 * 1. Any response which "MUST NOT" include a message-body (such as the
4566 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4567 * always terminated by the first empty line after the header fields,
4568 * regardless of the entity-header fields present in the message.
4569 *
4570 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4571 * the "chunked" transfer-coding (Section 6.2) is used, the
4572 * transfer-length is defined by the use of this transfer-coding.
4573 * If a Transfer-Encoding header field is present and the "chunked"
4574 * transfer-coding is not present, the transfer-length is defined by
4575 * the sender closing the connection.
4576 *
4577 * 3. If a Content-Length header field is present, its decimal value in
4578 * OCTETs represents both the entity-length and the transfer-length.
4579 * If a message is received with both a Transfer-Encoding header
4580 * field and a Content-Length header field, the latter MUST be ignored.
4581 *
4582 * 4. If the message uses the media type "multipart/byteranges", and
4583 * the transfer-length is not otherwise specified, then this self-
4584 * delimiting media type defines the transfer-length. This media
4585 * type MUST NOT be used unless the sender knows that the recipient
4586 * can parse it; the presence in a request of a Range header with
4587 * multiple byte-range specifiers from a 1.1 client implies that the
4588 * client can parse multipart/byteranges responses.
4589 *
4590 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004591 */
4592
4593 /* Skip parsing if no content length is possible. The response flags
4594 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004595 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004596 * FIXME: should we parse anyway and return an error on chunked encoding ?
4597 */
4598 if (txn->meth == HTTP_METH_HEAD ||
4599 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004600 txn->status == 204 || txn->status == 304) {
4601 txn->flags |= TX_RES_XFER_LEN;
4602 goto skip_content_length;
4603 }
4604
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004605 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004606 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004607 while ((txn->flags & TX_RES_VER_11) &&
4608 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004609 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4610 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4611 else if (txn->flags & TX_RES_TE_CHNK) {
4612 /* bad transfer-encoding (chunked followed by something else) */
4613 use_close_only = 1;
4614 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4615 break;
4616 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004617 }
4618
4619 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4620 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004621 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004622 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4623 signed long long cl;
4624
4625 if (!ctx.vlen)
4626 goto hdr_response_bad;
4627
4628 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4629 goto hdr_response_bad; /* parse failure */
4630
4631 if (cl < 0)
4632 goto hdr_response_bad;
4633
4634 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4635 goto hdr_response_bad; /* already specified, was different */
4636
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004637 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004638 msg->hdr_content_len = cl;
4639 }
4640
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004641 /* FIXME: we should also implement the multipart/byterange method.
4642 * For now on, we resort to close mode in this case (unknown length).
4643 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004644skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004645
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 /* end of job, return OK */
4647 rep->analysers &= ~an_bit;
4648 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004649 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004650 return 1;
4651}
4652
4653/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004654 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4655 * and updates t->rep->analysers. It might make sense to explode it into several
4656 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657 */
4658int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4659{
4660 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 struct http_msg *msg = &txn->rsp;
4662 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004663 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664
4665 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4666 now_ms, __FUNCTION__,
4667 t,
4668 rep,
4669 rep->rex, rep->wex,
4670 rep->flags,
4671 rep->l,
4672 rep->analysers);
4673
Willy Tarreau655dce92009-11-08 13:10:58 +01004674 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004675 return 0;
4676
4677 rep->analysers &= ~an_bit;
4678 rep->analyse_exp = TICK_ETERNITY;
4679
Willy Tarreau5b154472009-12-21 20:11:07 +01004680 /* Now we have to check if we need to modify the Connection header.
4681 * This is more difficult on the response than it is on the request,
4682 * because we can have two different HTTP versions and we don't know
4683 * how the client will interprete a response. For instance, let's say
4684 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4685 * HTTP/1.1 response without any header. Maybe it will bound itself to
4686 * HTTP/1.0 because it only knows about it, and will consider the lack
4687 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4688 * the lack of header as a keep-alive. Thus we will use two flags
4689 * indicating how a request MAY be understood by the client. In case
4690 * of multiple possibilities, we'll fix the header to be explicit. If
4691 * ambiguous cases such as both close and keepalive are seen, then we
4692 * will fall back to explicit close. Note that we won't take risks with
4693 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004694 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004695 */
4696
Willy Tarreaudc008c52010-02-01 16:20:08 +01004697 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4698 txn->status == 101)) {
4699 /* Either we've established an explicit tunnel, or we're
4700 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004701 * to understand the next protocols. We have to switch to tunnel
4702 * mode, so that we transfer the request and responses then let
4703 * this protocol pass unmodified. When we later implement specific
4704 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004705 * header which contains information about that protocol for
4706 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004707 */
4708 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4709 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004710 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4711 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4712 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004713 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004714
Willy Tarreau60466522010-01-18 19:08:45 +01004715 /* on unknown transfer length, we must close */
4716 if (!(txn->flags & TX_RES_XFER_LEN) &&
4717 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4718 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004719
Willy Tarreau60466522010-01-18 19:08:45 +01004720 /* now adjust header transformations depending on current state */
4721 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4722 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4723 to_del |= 2; /* remove "keep-alive" on any response */
4724 if (!(txn->flags & TX_RES_VER_11))
4725 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004726 }
Willy Tarreau60466522010-01-18 19:08:45 +01004727 else { /* SCL / KAL */
4728 to_del |= 1; /* remove "close" on any response */
4729 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4730 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004731 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004732
Willy Tarreau60466522010-01-18 19:08:45 +01004733 /* Parse and remove some headers from the connection header */
4734 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004735
Willy Tarreau60466522010-01-18 19:08:45 +01004736 /* Some keep-alive responses are converted to Server-close if
4737 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004738 */
Willy Tarreau60466522010-01-18 19:08:45 +01004739 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4740 if ((txn->flags & TX_HDR_CONN_CLO) ||
4741 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4742 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004743 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004744 }
4745
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004747 /*
4748 * 3: we will have to evaluate the filters.
4749 * As opposed to version 1.2, now they will be evaluated in the
4750 * filters order and not in the header order. This means that
4751 * each filter has to be validated among all headers.
4752 *
4753 * Filters are tried with ->be first, then with ->fe if it is
4754 * different from ->be.
4755 */
4756
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004757 cur_proxy = t->be;
4758 while (1) {
4759 struct proxy *rule_set = cur_proxy;
4760
4761 /* try headers filters */
4762 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004763 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004764 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004765 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004766 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004767 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4768 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004769 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004770 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004771 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004772 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004773 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004774 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004775 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004776 if (!(t->flags & SN_ERR_MASK))
4777 t->flags |= SN_ERR_PRXCOND;
4778 if (!(t->flags & SN_FINST_MASK))
4779 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004780 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004781 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004782 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004783
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004784 /* has the response been denied ? */
4785 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004786 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004787 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004788
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004789 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004790 if (t->listener->counters)
4791 t->listener->counters->denied_resp++;
4792
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004793 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004794 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004795
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004796 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004797 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004798 if (txn->status < 200)
4799 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004800 if (wl->cond) {
4801 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4802 ret = acl_pass(ret);
4803 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4804 ret = !ret;
4805 if (!ret)
4806 continue;
4807 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004808 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004809 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004810 }
4811
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004812 /* check whether we're already working on the frontend */
4813 if (cur_proxy == t->fe)
4814 break;
4815 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004817
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004818 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004819 * We may be facing a 100-continue response, in which case this
4820 * is not the right response, and we're waiting for the next one.
4821 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004822 * next one.
4823 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004824 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004825 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004826 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004827 msg->msg_state = HTTP_MSG_RPBEFORE;
4828 txn->status = 0;
4829 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4830 return 1;
4831 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004832 else if (unlikely(txn->status < 200))
4833 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004834
4835 /* we don't have any 1xx status code now */
4836
4837 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004838 * 4: check for server cookie.
4839 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004840 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4841 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004842 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004843
Willy Tarreaubaaee002006-06-26 02:48:02 +02004844
Willy Tarreaua15645d2007-03-18 16:22:39 +01004845 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004846 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004847 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004848 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004849 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004850
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004851 /*
4852 * 6: add server cookie in the response if needed
4853 */
4854 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004855 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004856 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004857
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004858 /* the server is known, it's not the one the client requested, we have to
4859 * insert a set-cookie here, except if we want to insert only on POST
4860 * requests and this one isn't. Note that servers which don't have cookies
4861 * (eg: some backup servers) will return a full cookie removal request.
4862 */
4863 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4864 t->be->cookie_name,
4865 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004866
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004867 if (t->be->cookie_domain)
4868 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004869
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004870 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004871 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004872 goto return_bad_resp;
4873 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004874
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004875 /* Here, we will tell an eventual cache on the client side that we don't
4876 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4877 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4878 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4879 */
4880 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004881
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004882 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4883
4884 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004885 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004886 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004887 }
4888 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004889
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004890 /*
4891 * 7: check if result will be cacheable with a cookie.
4892 * We'll block the response if security checks have caught
4893 * nasty things such as a cacheable cookie.
4894 */
4895 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4896 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004897 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004898
4899 /* we're in presence of a cacheable response containing
4900 * a set-cookie header. We'll block it as requested by
4901 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004902 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004903 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004904 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004905
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004906 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004907 if (t->listener->counters)
4908 t->listener->counters->denied_resp++;
4909
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004910 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4911 t->be->id, t->srv?t->srv->id:"<dispatch>");
4912 send_log(t->be, LOG_ALERT,
4913 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4914 t->be->id, t->srv?t->srv->id:"<dispatch>");
4915 goto return_srv_prx_502;
4916 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004917
4918 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004919 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004920 */
Willy Tarreau60466522010-01-18 19:08:45 +01004921 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4922 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4923 unsigned int want_flags = 0;
4924
4925 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4926 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4927 /* we want a keep-alive response here. Keep-alive header
4928 * required if either side is not 1.1.
4929 */
4930 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4931 want_flags |= TX_CON_KAL_SET;
4932 }
4933 else {
4934 /* we want a close response here. Close header required if
4935 * the server is 1.1, regardless of the client.
4936 */
4937 if (txn->flags & TX_RES_VER_11)
4938 want_flags |= TX_CON_CLO_SET;
4939 }
4940
4941 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4942 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004943 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004944
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004945 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01004946 if ((txn->flags & TX_RES_XFER_LEN) ||
4947 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004948 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004949
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004950 /*************************************************************
4951 * OK, that's finished for the headers. We have done what we *
4952 * could. Let's switch to the DATA state. *
4953 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004954
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004955 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004956
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004957 /* if the user wants to log as soon as possible, without counting
4958 * bytes from the server, then this is the right moment. We have
4959 * to temporarily assign bytes_out to log what we currently have.
4960 */
4961 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4962 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4963 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004964 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004965 t->logs.bytes_out = 0;
4966 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004967
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 /* Note: we must not try to cheat by jumping directly to DATA,
4969 * otherwise we would not let the client side wake up.
4970 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004971
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004972 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004973 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004974 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004975}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004976
Willy Tarreaud98cf932009-12-27 22:54:55 +01004977/* This function is an analyser which forwards response body (including chunk
4978 * sizes if any). It is called as soon as we must forward, even if we forward
4979 * zero byte. The only situation where it must not be called is when we're in
4980 * tunnel mode and we want to forward till the close. It's used both to forward
4981 * remaining data and to resync after end of body. It expects the msg_state to
4982 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4983 * read more data, or 1 once we can go on with next request or end the session.
4984 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4985 * bytes of pending data + the headers if not already done (between som and sov).
4986 * It eventually adjusts som to match sov after the data in between have been sent.
4987 */
4988int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4989{
4990 struct http_txn *txn = &s->txn;
4991 struct http_msg *msg = &s->txn.rsp;
4992
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004993 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4994 return 0;
4995
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004996 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004997 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004998 !s->req->analysers) {
4999 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
5000 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01005001 buffer_auto_read(res);
5002 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005003 res->analysers &= ~an_bit;
5004 return 1;
5005 }
5006
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005007 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005008
Willy Tarreaud98cf932009-12-27 22:54:55 +01005009 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5010 /* we have msg->col and msg->sov which both point to the first
5011 * byte of message body. msg->som still points to the beginning
5012 * of the message. We must save the body in req->lr because it
5013 * survives buffer re-alignments.
5014 */
5015 res->lr = res->data + msg->sov;
5016 if (txn->flags & TX_RES_TE_CHNK)
5017 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5018 else {
5019 msg->msg_state = HTTP_MSG_DATA;
5020 }
5021 }
5022
Willy Tarreaud98cf932009-12-27 22:54:55 +01005023 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005024 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005025 /* we may have some data pending */
5026 if (msg->hdr_content_len || msg->som != msg->sov) {
5027 int bytes = msg->sov - msg->som;
5028 if (bytes < 0) /* sov may have wrapped at the end */
5029 bytes += res->size;
5030 buffer_forward(res, bytes + msg->hdr_content_len);
5031 msg->hdr_content_len = 0; /* don't forward that again */
5032 msg->som = msg->sov;
5033 }
5034
Willy Tarreaucaabe412010-01-03 23:08:28 +01005035 if (msg->msg_state == HTTP_MSG_DATA) {
5036 /* must still forward */
5037 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005038 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005039
5040 /* nothing left to forward */
5041 if (txn->flags & TX_RES_TE_CHNK)
5042 msg->msg_state = HTTP_MSG_DATA_CRLF;
5043 else
5044 msg->msg_state = HTTP_MSG_DONE;
5045 }
5046 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005047 /* read the chunk size and assign it to ->hdr_content_len, then
5048 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5049 */
5050 int ret = http_parse_chunk_size(res, msg);
5051
5052 if (!ret)
5053 goto missing_data;
5054 else if (ret < 0)
5055 goto return_bad_res;
5056 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005057 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005058 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5059 /* we want the CRLF after the data */
5060 int ret;
5061
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005062 res->lr = res->w + res->send_max;
5063 if (res->lr >= res->data + res->size)
5064 res->lr -= res->size;
5065
Willy Tarreaud98cf932009-12-27 22:54:55 +01005066 ret = http_skip_chunk_crlf(res, msg);
5067
5068 if (!ret)
5069 goto missing_data;
5070 else if (ret < 0)
5071 goto return_bad_res;
5072 /* we're in MSG_CHUNK_SIZE now */
5073 }
5074 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5075 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005076
Willy Tarreaud98cf932009-12-27 22:54:55 +01005077 if (ret == 0)
5078 goto missing_data;
5079 else if (ret < 0)
5080 goto return_bad_res;
5081 /* we're in HTTP_MSG_DONE now */
5082 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005083 else {
5084 /* other states, DONE...TUNNEL */
5085 if (http_resync_states(s)) {
5086 http_silent_debug(__LINE__, s);
5087 /* some state changes occurred, maybe the analyser
5088 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005089 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5091 goto return_bad_res;
5092 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005093 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005094 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005095 }
5096 }
5097
Willy Tarreaud98cf932009-12-27 22:54:55 +01005098 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005099 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005100 if (res->flags & BF_SHUTR) {
5101 if (!(s->flags & SN_ERR_MASK))
5102 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005103 s->be->counters.srv_aborts++;
5104 if (s->srv)
5105 s->srv->counters.srv_aborts++;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005106 goto return_bad_res;
Willy Tarreau40dba092010-03-04 18:14:51 +01005107 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005108
Willy Tarreau40dba092010-03-04 18:14:51 +01005109 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005110 if (!s->req->analysers)
5111 goto return_bad_res;
5112
Willy Tarreaud98cf932009-12-27 22:54:55 +01005113 /* forward the chunk size as well as any pending data */
5114 if (msg->hdr_content_len || msg->som != msg->sov) {
5115 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5116 msg->hdr_content_len = 0; /* don't forward that again */
5117 msg->som = msg->sov;
5118 }
5119
Willy Tarreaud98cf932009-12-27 22:54:55 +01005120 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005122 return 0;
5123
Willy Tarreau40dba092010-03-04 18:14:51 +01005124 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005125 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005126 /* don't send any error message as we're in the body */
5127 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005128 res->analysers = 0;
5129 s->be->counters.failed_resp++;
5130 if (s->srv) {
5131 s->srv->counters.failed_resp++;
5132 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5133 }
5134
5135 if (!(s->flags & SN_ERR_MASK))
5136 s->flags |= SN_ERR_PRXCOND;
5137 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005138 s->flags |= SN_FINST_D;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005140 return 0;
5141}
5142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005143/* Iterate the same filter through all request headers.
5144 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005145 * Since it can manage the switch to another backend, it updates the per-proxy
5146 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005147 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005148int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005149{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005150 char term;
5151 char *cur_ptr, *cur_end, *cur_next;
5152 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005153 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005154 struct hdr_idx_elem *cur_hdr;
5155 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005157 last_hdr = 0;
5158
Willy Tarreau962c3f42010-01-10 00:15:35 +01005159 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005160 old_idx = 0;
5161
5162 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005163 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005164 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005165 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005166 (exp->action == ACT_ALLOW ||
5167 exp->action == ACT_DENY ||
5168 exp->action == ACT_TARPIT))
5169 return 0;
5170
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005171 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005172 if (!cur_idx)
5173 break;
5174
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005175 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005176 cur_ptr = cur_next;
5177 cur_end = cur_ptr + cur_hdr->len;
5178 cur_next = cur_end + cur_hdr->cr + 1;
5179
5180 /* Now we have one header between cur_ptr and cur_end,
5181 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005182 */
5183
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005184 /* The annoying part is that pattern matching needs
5185 * that we modify the contents to null-terminate all
5186 * strings before testing them.
5187 */
5188
5189 term = *cur_end;
5190 *cur_end = '\0';
5191
5192 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5193 switch (exp->action) {
5194 case ACT_SETBE:
5195 /* It is not possible to jump a second time.
5196 * FIXME: should we return an HTTP/500 here so that
5197 * the admin knows there's a problem ?
5198 */
5199 if (t->be != t->fe)
5200 break;
5201
5202 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005203 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005204 last_hdr = 1;
5205 break;
5206
5207 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005208 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005209 last_hdr = 1;
5210 break;
5211
5212 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005213 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005214 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005215
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005216 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005217 if (t->listener->counters)
5218 t->listener->counters->denied_resp++;
5219
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005220 break;
5221
5222 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005223 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005224 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005225
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005226 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005227 if (t->listener->counters)
5228 t->listener->counters->denied_resp++;
5229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005230 break;
5231
5232 case ACT_REPLACE:
5233 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5234 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5235 /* FIXME: if the user adds a newline in the replacement, the
5236 * index will not be recalculated for now, and the new line
5237 * will not be counted as a new header.
5238 */
5239
5240 cur_end += delta;
5241 cur_next += delta;
5242 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005243 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005244 break;
5245
5246 case ACT_REMOVE:
5247 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5248 cur_next += delta;
5249
Willy Tarreaufa355d42009-11-29 18:12:29 +01005250 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005251 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5252 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005253 cur_hdr->len = 0;
5254 cur_end = NULL; /* null-term has been rewritten */
5255 break;
5256
5257 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005259 if (cur_end)
5260 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005262 /* keep the link from this header to next one in case of later
5263 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005264 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005265 old_idx = cur_idx;
5266 }
5267 return 0;
5268}
5269
5270
5271/* Apply the filter to the request line.
5272 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5273 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005274 * Since it can manage the switch to another backend, it updates the per-proxy
5275 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005276 */
5277int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5278{
5279 char term;
5280 char *cur_ptr, *cur_end;
5281 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005282 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005283 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005284
Willy Tarreau58f10d72006-12-04 02:26:12 +01005285
Willy Tarreau3d300592007-03-18 18:34:41 +01005286 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005287 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005288 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005289 (exp->action == ACT_ALLOW ||
5290 exp->action == ACT_DENY ||
5291 exp->action == ACT_TARPIT))
5292 return 0;
5293 else if (exp->action == ACT_REMOVE)
5294 return 0;
5295
5296 done = 0;
5297
Willy Tarreau962c3f42010-01-10 00:15:35 +01005298 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005299 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005300
5301 /* Now we have the request line between cur_ptr and cur_end */
5302
5303 /* The annoying part is that pattern matching needs
5304 * that we modify the contents to null-terminate all
5305 * strings before testing them.
5306 */
5307
5308 term = *cur_end;
5309 *cur_end = '\0';
5310
5311 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5312 switch (exp->action) {
5313 case ACT_SETBE:
5314 /* It is not possible to jump a second time.
5315 * FIXME: should we return an HTTP/500 here so that
5316 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005317 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005318 if (t->be != t->fe)
5319 break;
5320
5321 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005322 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005323 done = 1;
5324 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005326 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005327 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005328 done = 1;
5329 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005331 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005332 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005333
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005334 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005335 if (t->listener->counters)
5336 t->listener->counters->denied_resp++;
5337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005338 done = 1;
5339 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005341 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005342 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005343
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005344 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005345 if (t->listener->counters)
5346 t->listener->counters->denied_resp++;
5347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005348 done = 1;
5349 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005351 case ACT_REPLACE:
5352 *cur_end = term; /* restore the string terminator */
5353 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5354 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5355 /* FIXME: if the user adds a newline in the replacement, the
5356 * index will not be recalculated for now, and the new line
5357 * will not be counted as a new header.
5358 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005359
Willy Tarreaufa355d42009-11-29 18:12:29 +01005360 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005361 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005362 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005363 HTTP_MSG_RQMETH,
5364 cur_ptr, cur_end + 1,
5365 NULL, NULL);
5366 if (unlikely(!cur_end))
5367 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005369 /* we have a full request and we know that we have either a CR
5370 * or an LF at <ptr>.
5371 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005372 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5373 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005374 /* there is no point trying this regex on headers */
5375 return 1;
5376 }
5377 }
5378 *cur_end = term; /* restore the string terminator */
5379 return done;
5380}
Willy Tarreau97de6242006-12-27 17:18:38 +01005381
Willy Tarreau58f10d72006-12-04 02:26:12 +01005382
Willy Tarreau58f10d72006-12-04 02:26:12 +01005383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005384/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005385 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005386 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005387 * unparsable request. Since it can manage the switch to another backend, it
5388 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005389 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005390int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005391{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005392 struct http_txn *txn = &s->txn;
5393 struct hdr_exp *exp;
5394
5395 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005396 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005398 /*
5399 * The interleaving of transformations and verdicts
5400 * makes it difficult to decide to continue or stop
5401 * the evaluation.
5402 */
5403
Willy Tarreau6c123b12010-01-28 20:22:06 +01005404 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5405 break;
5406
Willy Tarreau3d300592007-03-18 18:34:41 +01005407 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005408 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005409 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005410 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005411
5412 /* if this filter had a condition, evaluate it now and skip to
5413 * next filter if the condition does not match.
5414 */
5415 if (exp->cond) {
5416 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5417 ret = acl_pass(ret);
5418 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5419 ret = !ret;
5420
5421 if (!ret)
5422 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005423 }
5424
5425 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005426 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005427 if (unlikely(ret < 0))
5428 return -1;
5429
5430 if (likely(ret == 0)) {
5431 /* The filter did not match the request, it can be
5432 * iterated through all headers.
5433 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005434 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005436 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005437 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005438}
5439
5440
Willy Tarreaua15645d2007-03-18 16:22:39 +01005441
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005443 * Try to retrieve the server associated to the appsession.
5444 * If the server is found, it's assigned to the session.
5445 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005446void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005447 struct http_txn *txn = &t->txn;
5448 appsess *asession = NULL;
5449 char *sessid_temp = NULL;
5450
Cyril Bontéb21570a2009-11-29 20:04:48 +01005451 if (len > t->be->appsession_len) {
5452 len = t->be->appsession_len;
5453 }
5454
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005455 if (t->be->options2 & PR_O2_AS_REQL) {
5456 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005457 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005458 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005459 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005460 }
5461
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005462 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005463 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5464 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5465 return;
5466 }
5467
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005468 memcpy(txn->sessid, buf, len);
5469 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005470 }
5471
5472 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5473 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5474 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5475 return;
5476 }
5477
Cyril Bontéb21570a2009-11-29 20:04:48 +01005478 memcpy(sessid_temp, buf, len);
5479 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005480
5481 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5482 /* free previously allocated memory */
5483 pool_free2(apools.sessid, sessid_temp);
5484
5485 if (asession != NULL) {
5486 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5487 if (!(t->be->options2 & PR_O2_AS_REQL))
5488 asession->request_count++;
5489
5490 if (asession->serverid != NULL) {
5491 struct server *srv = t->be->srv;
5492 while (srv) {
5493 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005494 if ((srv->state & SRV_RUNNING) ||
5495 (t->be->options & PR_O_PERSIST) ||
5496 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005497 /* we found the server and it's usable */
5498 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005499 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005500 t->flags |= SN_DIRECT | SN_ASSIGNED;
5501 t->srv = srv;
5502 break;
5503 } else {
5504 txn->flags &= ~TX_CK_MASK;
5505 txn->flags |= TX_CK_DOWN;
5506 }
5507 }
5508 srv = srv->next;
5509 }
5510 }
5511 }
5512}
5513
5514/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005515 * Manage client-side cookie. It can impact performance by about 2% so it is
5516 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005517 */
5518void manage_client_side_cookies(struct session *t, struct buffer *req)
5519{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005520 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005521 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005522 char *del_colon, *del_cookie, *colon;
5523 int app_cookies;
5524
Willy Tarreau58f10d72006-12-04 02:26:12 +01005525 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005526 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005527
Willy Tarreau2a324282006-12-05 00:05:46 +01005528 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005529 * we start with the start line.
5530 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005531 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005532 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005533
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005534 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005535 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005536 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005537
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005538 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005539 cur_ptr = cur_next;
5540 cur_end = cur_ptr + cur_hdr->len;
5541 cur_next = cur_end + cur_hdr->cr + 1;
5542
5543 /* We have one full header between cur_ptr and cur_end, and the
5544 * next header starts at cur_next. We're only interested in
5545 * "Cookie:" headers.
5546 */
5547
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005548 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5549 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005550 old_idx = cur_idx;
5551 continue;
5552 }
5553
5554 /* Now look for cookies. Conforming to RFC2109, we have to support
5555 * attributes whose name begin with a '$', and associate them with
5556 * the right cookie, if we want to delete this cookie.
5557 * So there are 3 cases for each cookie read :
5558 * 1) it's a special attribute, beginning with a '$' : ignore it.
5559 * 2) it's a server id cookie that we *MAY* want to delete : save
5560 * some pointers on it (last semi-colon, beginning of cookie...)
5561 * 3) it's an application cookie : we *MAY* have to delete a previous
5562 * "special" cookie.
5563 * At the end of loop, if a "special" cookie remains, we may have to
5564 * remove it. If no application cookie persists in the header, we
5565 * *MUST* delete it
5566 */
5567
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005568 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005569
Willy Tarreau58f10d72006-12-04 02:26:12 +01005570 /* del_cookie == NULL => nothing to be deleted */
5571 del_colon = del_cookie = NULL;
5572 app_cookies = 0;
5573
5574 while (p1 < cur_end) {
5575 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005576 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005577 while (p1 < cur_end) {
5578 if (*p1 == ';' || *p1 == ',')
5579 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005580 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005581 break;
5582 p1++;
5583 }
5584
5585 if (p1 == cur_end)
5586 break;
5587
5588 /* p1 is at the beginning of the cookie name */
5589 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005590 while (p2 < cur_end && *p2 != '=') {
5591 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5592 /* oops, the cookie name was truncated, resync */
5593 p1 = p2;
5594 goto resync_name;
5595 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005596 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005598
5599 if (p2 == cur_end)
5600 break;
5601
5602 p3 = p2 + 1; /* skips the '=' sign */
5603 if (p3 == cur_end)
5604 break;
5605
Willy Tarreau305ae852010-01-03 19:45:54 +01005606 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5607 p5 = p4 = p3;
5608 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5609 if (!isspace((unsigned char)*p5))
5610 p4 = p5 + 1;
5611 p5++;
5612 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005613
5614 /* here, we have the cookie name between p1 and p2,
5615 * and its value between p3 and p4.
5616 * we can process it :
5617 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005618 * Cookie: NAME=VALUE ;
5619 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005620 * | || || +--> p4
5621 * | || |+-------> p3
5622 * | || +--------> p2
5623 * | |+------------> p1
5624 * | +-------------> colon
5625 * +--------------------> cur_ptr
5626 */
5627
5628 if (*p1 == '$') {
5629 /* skip this one */
5630 }
5631 else {
5632 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005633 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005634 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005635 (p4 - p1 >= t->fe->capture_namelen) &&
5636 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005637 int log_len = p4 - p1;
5638
Willy Tarreau086b3b42007-05-13 21:45:51 +02005639 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005640 Alert("HTTP logging : out of memory.\n");
5641 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005642 if (log_len > t->fe->capture_len)
5643 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005644 memcpy(txn->cli_cookie, p1, log_len);
5645 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005646 }
5647 }
5648
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005649 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5650 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005651 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005652 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005653 char *delim;
5654
5655 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5656 * have the server ID betweek p3 and delim, and the original cookie between
5657 * delim+1 and p4. Otherwise, delim==p4 :
5658 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005659 * Cookie: NAME=SRV~VALUE ;
5660 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005661 * | || || | +--> p4
5662 * | || || +--------> delim
5663 * | || |+-----------> p3
5664 * | || +------------> p2
5665 * | |+----------------> p1
5666 * | +-----------------> colon
5667 * +------------------------> cur_ptr
5668 */
5669
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005670 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005671 for (delim = p3; delim < p4; delim++)
5672 if (*delim == COOKIE_DELIM)
5673 break;
5674 }
5675 else
5676 delim = p4;
5677
5678
5679 /* Here, we'll look for the first running server which supports the cookie.
5680 * This allows to share a same cookie between several servers, for example
5681 * to dedicate backup servers to specific servers only.
5682 * However, to prevent clients from sticking to cookie-less backup server
5683 * when they have incidentely learned an empty cookie, we simply ignore
5684 * empty cookies and mark them as invalid.
5685 */
5686 if (delim == p3)
5687 srv = NULL;
5688
5689 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005690 if (srv->cookie && (srv->cklen == delim - p3) &&
5691 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005692 if ((srv->state & SRV_RUNNING) ||
5693 (t->be->options & PR_O_PERSIST) ||
5694 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005696 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005697 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005698 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005699 t->srv = srv;
5700 break;
5701 } else {
5702 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005703 txn->flags &= ~TX_CK_MASK;
5704 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005705 }
5706 }
5707 srv = srv->next;
5708 }
5709
Willy Tarreau3d300592007-03-18 18:34:41 +01005710 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005711 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005712 txn->flags &= ~TX_CK_MASK;
5713 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005714 }
5715
5716 /* depending on the cookie mode, we may have to either :
5717 * - delete the complete cookie if we're in insert+indirect mode, so that
5718 * the server never sees it ;
5719 * - remove the server id from the cookie value, and tag the cookie as an
5720 * application cookie so that it does not get accidentely removed later,
5721 * if we're in cookie prefix mode
5722 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005723 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005724 int delta; /* negative */
5725
5726 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5727 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005728 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005729 cur_end += delta;
5730 cur_next += delta;
5731 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005732 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005733
5734 del_cookie = del_colon = NULL;
5735 app_cookies++; /* protect the header from deletion */
5736 }
5737 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005738 (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 +01005739 del_cookie = p1;
5740 del_colon = colon;
5741 }
5742 } else {
5743 /* now we know that we must keep this cookie since it's
5744 * not ours. But if we wanted to delete our cookie
5745 * earlier, we cannot remove the complete header, but we
5746 * can remove the previous block itself.
5747 */
5748 app_cookies++;
5749
5750 if (del_cookie != NULL) {
5751 int delta; /* negative */
5752
5753 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5754 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005755 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005756 cur_end += delta;
5757 cur_next += delta;
5758 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005759 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005760 del_cookie = del_colon = NULL;
5761 }
5762 }
5763
Cyril Bontéb21570a2009-11-29 20:04:48 +01005764 if (t->be->appsession_name != NULL) {
5765 int cmp_len, value_len;
5766 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005767
Cyril Bontéb21570a2009-11-29 20:04:48 +01005768 if (t->be->options2 & PR_O2_AS_PFX) {
5769 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5770 value_begin = p1 + t->be->appsession_name_len;
5771 value_len = p4 - p1 - t->be->appsession_name_len;
5772 } else {
5773 cmp_len = p2 - p1;
5774 value_begin = p3;
5775 value_len = p4 - p3;
5776 }
5777
5778 /* let's see if the cookie is our appcookie */
5779 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5780 /* Cool... it's the right one */
5781 manage_client_side_appsession(t, value_begin, value_len);
5782 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005783#if defined(DEBUG_HASH)
5784 Alert("manage_client_side_cookies\n");
5785 appsession_hash_dump(&(t->be->htbl_proxy));
5786#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005787 }/* end if ((t->proxy->appsession_name != NULL) ... */
5788 }
5789
5790 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005791 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005792 } /* while (p1 < cur_end) */
5793
5794 /* There's no more cookie on this line.
5795 * We may have marked the last one(s) for deletion.
5796 * We must do this now in two ways :
5797 * - if there is no app cookie, we simply delete the header ;
5798 * - if there are app cookies, we must delete the end of the
5799 * string properly, including the colon/semi-colon before
5800 * the cookie name.
5801 */
5802 if (del_cookie != NULL) {
5803 int delta;
5804 if (app_cookies) {
5805 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5806 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005807 cur_hdr->len += delta;
5808 } else {
5809 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005810
5811 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005812 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5813 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005814 cur_hdr->len = 0;
5815 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005816 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005817 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005818 }
5819
5820 /* keep the link from this header to next one */
5821 old_idx = cur_idx;
5822 } /* end of cookie processing on this header */
5823}
5824
5825
Willy Tarreaua15645d2007-03-18 16:22:39 +01005826/* Iterate the same filter through all response headers contained in <rtr>.
5827 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5828 */
5829int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5830{
5831 char term;
5832 char *cur_ptr, *cur_end, *cur_next;
5833 int cur_idx, old_idx, last_hdr;
5834 struct http_txn *txn = &t->txn;
5835 struct hdr_idx_elem *cur_hdr;
5836 int len, delta;
5837
5838 last_hdr = 0;
5839
Willy Tarreau962c3f42010-01-10 00:15:35 +01005840 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005841 old_idx = 0;
5842
5843 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005844 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005845 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005846 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005847 (exp->action == ACT_ALLOW ||
5848 exp->action == ACT_DENY))
5849 return 0;
5850
5851 cur_idx = txn->hdr_idx.v[old_idx].next;
5852 if (!cur_idx)
5853 break;
5854
5855 cur_hdr = &txn->hdr_idx.v[cur_idx];
5856 cur_ptr = cur_next;
5857 cur_end = cur_ptr + cur_hdr->len;
5858 cur_next = cur_end + cur_hdr->cr + 1;
5859
5860 /* Now we have one header between cur_ptr and cur_end,
5861 * and the next header starts at cur_next.
5862 */
5863
5864 /* The annoying part is that pattern matching needs
5865 * that we modify the contents to null-terminate all
5866 * strings before testing them.
5867 */
5868
5869 term = *cur_end;
5870 *cur_end = '\0';
5871
5872 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5873 switch (exp->action) {
5874 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005875 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005876 last_hdr = 1;
5877 break;
5878
5879 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005880 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005881 last_hdr = 1;
5882 break;
5883
5884 case ACT_REPLACE:
5885 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5886 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5887 /* FIXME: if the user adds a newline in the replacement, the
5888 * index will not be recalculated for now, and the new line
5889 * will not be counted as a new header.
5890 */
5891
5892 cur_end += delta;
5893 cur_next += delta;
5894 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005895 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005896 break;
5897
5898 case ACT_REMOVE:
5899 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5900 cur_next += delta;
5901
Willy Tarreaufa355d42009-11-29 18:12:29 +01005902 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005903 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5904 txn->hdr_idx.used--;
5905 cur_hdr->len = 0;
5906 cur_end = NULL; /* null-term has been rewritten */
5907 break;
5908
5909 }
5910 }
5911 if (cur_end)
5912 *cur_end = term; /* restore the string terminator */
5913
5914 /* keep the link from this header to next one in case of later
5915 * removal of next header.
5916 */
5917 old_idx = cur_idx;
5918 }
5919 return 0;
5920}
5921
5922
5923/* Apply the filter to the status line in the response buffer <rtr>.
5924 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5925 * or -1 if a replacement resulted in an invalid status line.
5926 */
5927int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5928{
5929 char term;
5930 char *cur_ptr, *cur_end;
5931 int done;
5932 struct http_txn *txn = &t->txn;
5933 int len, delta;
5934
5935
Willy Tarreau3d300592007-03-18 18:34:41 +01005936 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005937 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005938 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939 (exp->action == ACT_ALLOW ||
5940 exp->action == ACT_DENY))
5941 return 0;
5942 else if (exp->action == ACT_REMOVE)
5943 return 0;
5944
5945 done = 0;
5946
Willy Tarreau962c3f42010-01-10 00:15:35 +01005947 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5949
5950 /* Now we have the status line between cur_ptr and cur_end */
5951
5952 /* The annoying part is that pattern matching needs
5953 * that we modify the contents to null-terminate all
5954 * strings before testing them.
5955 */
5956
5957 term = *cur_end;
5958 *cur_end = '\0';
5959
5960 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5961 switch (exp->action) {
5962 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005963 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005964 done = 1;
5965 break;
5966
5967 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005968 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005969 done = 1;
5970 break;
5971
5972 case ACT_REPLACE:
5973 *cur_end = term; /* restore the string terminator */
5974 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5975 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5976 /* FIXME: if the user adds a newline in the replacement, the
5977 * index will not be recalculated for now, and the new line
5978 * will not be counted as a new header.
5979 */
5980
Willy Tarreaufa355d42009-11-29 18:12:29 +01005981 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005982 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005983 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005984 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005985 cur_ptr, cur_end + 1,
5986 NULL, NULL);
5987 if (unlikely(!cur_end))
5988 return -1;
5989
5990 /* we have a full respnse and we know that we have either a CR
5991 * or an LF at <ptr>.
5992 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005993 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005994 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5995 /* there is no point trying this regex on headers */
5996 return 1;
5997 }
5998 }
5999 *cur_end = term; /* restore the string terminator */
6000 return done;
6001}
6002
6003
6004
6005/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006006 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006007 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6008 * unparsable response.
6009 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006010int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006011{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006012 struct http_txn *txn = &s->txn;
6013 struct hdr_exp *exp;
6014
6015 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006016 int ret;
6017
6018 /*
6019 * The interleaving of transformations and verdicts
6020 * makes it difficult to decide to continue or stop
6021 * the evaluation.
6022 */
6023
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006024 if (txn->flags & TX_SVDENY)
6025 break;
6026
Willy Tarreau3d300592007-03-18 18:34:41 +01006027 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006028 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6029 exp->action == ACT_PASS)) {
6030 exp = exp->next;
6031 continue;
6032 }
6033
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006034 /* if this filter had a condition, evaluate it now and skip to
6035 * next filter if the condition does not match.
6036 */
6037 if (exp->cond) {
6038 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6039 ret = acl_pass(ret);
6040 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6041 ret = !ret;
6042 if (!ret)
6043 continue;
6044 }
6045
Willy Tarreaua15645d2007-03-18 16:22:39 +01006046 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006047 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006048 if (unlikely(ret < 0))
6049 return -1;
6050
6051 if (likely(ret == 0)) {
6052 /* The filter did not match the response, it can be
6053 * iterated through all headers.
6054 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006055 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006057 }
6058 return 0;
6059}
6060
6061
6062
6063/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006064 * Manage server-side cookies. It can impact performance by about 2% so it is
6065 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006066 */
6067void manage_server_side_cookies(struct session *t, struct buffer *rtr)
6068{
6069 struct http_txn *txn = &t->txn;
6070 char *p1, *p2, *p3, *p4;
6071
Willy Tarreaua15645d2007-03-18 16:22:39 +01006072 char *cur_ptr, *cur_end, *cur_next;
6073 int cur_idx, old_idx, delta;
6074
Willy Tarreaua15645d2007-03-18 16:22:39 +01006075 /* Iterate through the headers.
6076 * we start with the start line.
6077 */
6078 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006079 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006080
6081 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6082 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006083 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006084
6085 cur_hdr = &txn->hdr_idx.v[cur_idx];
6086 cur_ptr = cur_next;
6087 cur_end = cur_ptr + cur_hdr->len;
6088 cur_next = cur_end + cur_hdr->cr + 1;
6089
6090 /* We have one full header between cur_ptr and cur_end, and the
6091 * next header starts at cur_next. We're only interested in
6092 * "Cookie:" headers.
6093 */
6094
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006095 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
6096 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006097 old_idx = cur_idx;
6098 continue;
6099 }
6100
6101 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01006102 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006103
6104
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006105 /* maybe we only wanted to see if there was a set-cookie. Note that
6106 * the cookie capture is declared in the fronend.
6107 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006108 if (t->be->cookie_name == NULL &&
6109 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006110 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006111 return;
6112
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006113 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006114
6115 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006116 if (p1 == cur_end || *p1 == ';') /* end of cookie */
6117 break;
6118
6119 /* p1 is at the beginning of the cookie name */
6120 p2 = p1;
6121
6122 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
6123 p2++;
6124
6125 if (p2 == cur_end || *p2 == ';') /* next cookie */
6126 break;
6127
6128 p3 = p2 + 1; /* skip the '=' sign */
6129 if (p3 == cur_end)
6130 break;
6131
6132 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006133 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01006134 p4++;
6135
6136 /* here, we have the cookie name between p1 and p2,
6137 * and its value between p3 and p4.
6138 * we can process it.
6139 */
6140
6141 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006142 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006143 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006144 (p4 - p1 >= t->fe->capture_namelen) &&
6145 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006146 int log_len = p4 - p1;
6147
Willy Tarreau086b3b42007-05-13 21:45:51 +02006148 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006149 Alert("HTTP logging : out of memory.\n");
6150 }
6151
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006152 if (log_len > t->fe->capture_len)
6153 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006154 memcpy(txn->srv_cookie, p1, log_len);
6155 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006156 }
6157
6158 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006159 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6160 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006161 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01006162 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006163
6164 /* If the cookie is in insert mode on a known server, we'll delete
6165 * this occurrence because we'll insert another one later.
6166 * We'll delete it too if the "indirect" option is set and we're in
6167 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006168 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
6169 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006170 /* this header must be deleted */
6171 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6172 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6173 txn->hdr_idx.used--;
6174 cur_hdr->len = 0;
6175 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006176 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006177
Willy Tarreau3d300592007-03-18 18:34:41 +01006178 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006179 }
6180 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006181 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006182 /* replace bytes p3->p4 with the cookie name associated
6183 * with this server since we know it.
6184 */
6185 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
6186 cur_hdr->len += delta;
6187 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006188 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006189
Willy Tarreau3d300592007-03-18 18:34:41 +01006190 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006191 }
6192 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006193 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006194 /* insert the cookie name associated with this server
6195 * before existing cookie, and insert a delimitor between them..
6196 */
6197 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6198 cur_hdr->len += delta;
6199 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006200 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006201
6202 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006203 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006204 }
6205 }
6206 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006207 else if (t->be->appsession_name != NULL) {
6208 int cmp_len, value_len;
6209 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006210
Cyril Bontéb21570a2009-11-29 20:04:48 +01006211 if (t->be->options2 & PR_O2_AS_PFX) {
6212 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6213 value_begin = p1 + t->be->appsession_name_len;
6214 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6215 } else {
6216 cmp_len = p2 - p1;
6217 value_begin = p3;
6218 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006219 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006220
6221 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
6222 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006223 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006224 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006225 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006226 }
6227 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006228 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006229 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6230 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6231 return;
6232 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006233 memcpy(txn->sessid, value_begin, value_len);
6234 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006235 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006236 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006237 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6238 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006239 /* keep the link from this header to next one */
6240 old_idx = cur_idx;
6241 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006242
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006243 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006244 appsess *asession = NULL;
6245 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006246 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006247 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006248 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006249 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6250 Alert("Not enough Memory process_srv():asession:calloc().\n");
6251 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6252 return;
6253 }
6254 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6255 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6256 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006257 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006258 return;
6259 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006260 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006261 asession->sessid[t->be->appsession_len] = 0;
6262
Willy Tarreau1fac7532010-01-09 19:23:06 +01006263 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006264 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6265 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6266 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006267 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006268 return;
6269 }
6270 asession->serverid[0] = '\0';
6271 memcpy(asession->serverid, t->srv->id, server_id_len);
6272
6273 asession->request_count = 0;
6274 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6275 }
6276
6277 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6278 asession->request_count++;
6279 }
6280
6281#if defined(DEBUG_HASH)
6282 Alert("manage_server_side_cookies\n");
6283 appsession_hash_dump(&(t->be->htbl_proxy));
6284#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006285}
6286
6287
6288
6289/*
6290 * Check if response is cacheable or not. Updates t->flags.
6291 */
6292void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6293{
6294 struct http_txn *txn = &t->txn;
6295 char *p1, *p2;
6296
6297 char *cur_ptr, *cur_end, *cur_next;
6298 int cur_idx;
6299
Willy Tarreau5df51872007-11-25 16:20:08 +01006300 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006301 return;
6302
6303 /* Iterate through the headers.
6304 * we start with the start line.
6305 */
6306 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006307 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006308
6309 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6310 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006311 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006312
6313 cur_hdr = &txn->hdr_idx.v[cur_idx];
6314 cur_ptr = cur_next;
6315 cur_end = cur_ptr + cur_hdr->len;
6316 cur_next = cur_end + cur_hdr->cr + 1;
6317
6318 /* We have one full header between cur_ptr and cur_end, and the
6319 * next header starts at cur_next. We're only interested in
6320 * "Cookie:" headers.
6321 */
6322
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006323 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6324 if (val) {
6325 if ((cur_end - (cur_ptr + val) >= 8) &&
6326 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6327 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6328 return;
6329 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006330 }
6331
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006332 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6333 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006334 continue;
6335
6336 /* OK, right now we know we have a cache-control header at cur_ptr */
6337
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006338 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006339
6340 if (p1 >= cur_end) /* no more info */
6341 continue;
6342
6343 /* p1 is at the beginning of the value */
6344 p2 = p1;
6345
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006346 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006347 p2++;
6348
6349 /* we have a complete value between p1 and p2 */
6350 if (p2 < cur_end && *p2 == '=') {
6351 /* we have something of the form no-cache="set-cookie" */
6352 if ((cur_end - p1 >= 21) &&
6353 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6354 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006355 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006356 continue;
6357 }
6358
6359 /* OK, so we know that either p2 points to the end of string or to a comma */
6360 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6361 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6362 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6363 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006364 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006365 return;
6366 }
6367
6368 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006369 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006370 continue;
6371 }
6372 }
6373}
6374
6375
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376/*
6377 * Try to retrieve a known appsession in the URI, then the associated server.
6378 * If the server is found, it's assigned to the session.
6379 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006382 char *end_params, *first_param, *cur_param, *next_param;
6383 char separator;
6384 int value_len;
6385
6386 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006387
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006388 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006389 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006390 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006391 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006392
Cyril Bontéb21570a2009-11-29 20:04:48 +01006393 first_param = NULL;
6394 switch (mode) {
6395 case PR_O2_AS_M_PP:
6396 first_param = memchr(begin, ';', len);
6397 break;
6398 case PR_O2_AS_M_QS:
6399 first_param = memchr(begin, '?', len);
6400 break;
6401 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006402
Cyril Bontéb21570a2009-11-29 20:04:48 +01006403 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006404 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006406
Cyril Bontéb21570a2009-11-29 20:04:48 +01006407 switch (mode) {
6408 case PR_O2_AS_M_PP:
6409 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6410 end_params = (char *) begin + len;
6411 }
6412 separator = ';';
6413 break;
6414 case PR_O2_AS_M_QS:
6415 end_params = (char *) begin + len;
6416 separator = '&';
6417 break;
6418 default:
6419 /* unknown mode, shouldn't happen */
6420 return;
6421 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006422
Cyril Bontéb21570a2009-11-29 20:04:48 +01006423 cur_param = next_param = end_params;
6424 while (cur_param > first_param) {
6425 cur_param--;
6426 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6427 /* let's see if this is the appsession parameter */
6428 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6429 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6430 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6431 /* Cool... it's the right one */
6432 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6433 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6434 if (value_len > 0) {
6435 manage_client_side_appsession(t, cur_param, value_len);
6436 }
6437 break;
6438 }
6439 next_param = cur_param;
6440 }
6441 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006442#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006443 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006444 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006445#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446}
6447
Willy Tarreaub2513902006-12-17 14:52:38 +01006448/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006449 * In a GET or HEAD request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006450 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01006451 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006452 * It is assumed that the request is either a HEAD or GET and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006453 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006454 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006455 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01006456 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006457int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01006458{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006459 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006460 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006461 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006462
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006463 if (!uri_auth)
6464 return 0;
6465
6466 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)
6467 return 0;
6468
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006469 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006471 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006472 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006473 return 0;
6474
Willy Tarreau962c3f42010-01-10 00:15:35 +01006475 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006477 /* the URI is in h */
6478 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006479 return 0;
6480
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006481 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006482 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006483 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006484 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006485 break;
6486 }
6487 h++;
6488 }
6489
6490 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006491 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6492 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006493 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006494 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006495 break;
6496 }
6497 h++;
6498 }
6499 }
6500
Willy Tarreau962c3f42010-01-10 00:15:35 +01006501 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6502 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006503 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006504 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006505 break;
6506 }
6507 h++;
6508 }
6509
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006510 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6511
Willy Tarreaub2513902006-12-17 14:52:38 +01006512 return 1;
6513}
6514
Willy Tarreau4076a152009-04-02 15:18:36 +02006515/*
6516 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006517 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6518 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006519 */
6520void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6521 struct buffer *buf, struct http_msg *msg,
6522 struct proxy *other_end)
6523{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006524 es->len = buf->r - (buf->data + msg->som);
6525 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006526 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006527 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006528 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006529 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006530 es->when = date; // user-visible date
6531 es->sid = s->uniq_id;
6532 es->srv = s->srv;
6533 es->oe = other_end;
6534 es->src = s->cli_addr;
6535}
Willy Tarreaub2513902006-12-17 14:52:38 +01006536
Willy Tarreaubaaee002006-06-26 02:48:02 +02006537/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 * Print a debug line with a header
6539 */
6540void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6541{
6542 int len, max;
6543 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006544 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545 max = end - start;
6546 UBOUND(max, sizeof(trash) - len - 1);
6547 len += strlcpy2(trash + len, start, max + 1);
6548 trash[len++] = '\n';
6549 write(1, trash, len);
6550}
6551
Willy Tarreau0937bc42009-12-22 15:03:09 +01006552/*
6553 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6554 * the required fields are properly allocated and that we only need to (re)init
6555 * them. This should be used before processing any new request.
6556 */
6557void http_init_txn(struct session *s)
6558{
6559 struct http_txn *txn = &s->txn;
6560 struct proxy *fe = s->fe;
6561
6562 txn->flags = 0;
6563 txn->status = -1;
6564
6565 txn->req.sol = txn->req.eol = NULL;
6566 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6567 txn->rsp.sol = txn->rsp.eol = NULL;
6568 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6569 txn->req.hdr_content_len = 0LL;
6570 txn->rsp.hdr_content_len = 0LL;
6571 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6572 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006573
6574 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006575
6576 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6577 if (fe->options2 & PR_O2_REQBUG_OK)
6578 txn->req.err_pos = -1; /* let buggy requests pass */
6579
Willy Tarreau46023632010-01-07 22:51:47 +01006580 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006581 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6582
Willy Tarreau46023632010-01-07 22:51:47 +01006583 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006584 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6585
6586 if (txn->hdr_idx.v)
6587 hdr_idx_init(&txn->hdr_idx);
6588}
6589
6590/* to be used at the end of a transaction */
6591void http_end_txn(struct session *s)
6592{
6593 struct http_txn *txn = &s->txn;
6594
6595 /* these ones will have been dynamically allocated */
6596 pool_free2(pool2_requri, txn->uri);
6597 pool_free2(pool2_capture, txn->cli_cookie);
6598 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006599 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006600
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006601 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006602 txn->uri = NULL;
6603 txn->srv_cookie = NULL;
6604 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006605
6606 if (txn->req.cap) {
6607 struct cap_hdr *h;
6608 for (h = s->fe->req_cap; h; h = h->next)
6609 pool_free2(h->pool, txn->req.cap[h->index]);
6610 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6611 }
6612
6613 if (txn->rsp.cap) {
6614 struct cap_hdr *h;
6615 for (h = s->fe->rsp_cap; h; h = h->next)
6616 pool_free2(h->pool, txn->rsp.cap[h->index]);
6617 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6618 }
6619
Willy Tarreau0937bc42009-12-22 15:03:09 +01006620}
6621
6622/* to be used at the end of a transaction to prepare a new one */
6623void http_reset_txn(struct session *s)
6624{
6625 http_end_txn(s);
6626 http_init_txn(s);
6627
6628 s->be = s->fe;
6629 s->req->analysers = s->listener->analysers;
6630 s->logs.logwait = s->fe->to_log;
6631 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006632 /* re-init store persistence */
6633 s->store_count = 0;
6634
Willy Tarreau0937bc42009-12-22 15:03:09 +01006635 s->pend_pos = NULL;
6636 s->conn_retries = s->be->conn_retries;
6637
6638 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6639
Willy Tarreau739cfba2010-01-25 23:11:14 +01006640 /* We must trim any excess data from the response buffer, because we
6641 * may have blocked an invalid response from a server that we don't
6642 * want to accidentely forward once we disable the analysers, nor do
6643 * we want those data to come along with next response. A typical
6644 * example of such data would be from a buggy server responding to
6645 * a HEAD with some data, or sending more than the advertised
6646 * content-length.
6647 */
6648 if (unlikely(s->rep->l > s->rep->send_max)) {
6649 s->rep->l = s->rep->send_max;
6650 s->rep->r = s->rep->w + s->rep->l;
6651 if (s->rep->r >= s->rep->data + s->rep->size)
6652 s->rep->r -= s->rep->size;
6653 }
6654
Willy Tarreau0937bc42009-12-22 15:03:09 +01006655 s->req->rto = s->fe->timeout.client;
6656 s->req->wto = s->be->timeout.server;
6657 s->req->cto = s->be->timeout.connect;
6658
6659 s->rep->rto = s->be->timeout.server;
6660 s->rep->wto = s->fe->timeout.client;
6661 s->rep->cto = TICK_ETERNITY;
6662
6663 s->req->rex = TICK_ETERNITY;
6664 s->req->wex = TICK_ETERNITY;
6665 s->req->analyse_exp = TICK_ETERNITY;
6666 s->rep->rex = TICK_ETERNITY;
6667 s->rep->wex = TICK_ETERNITY;
6668 s->rep->analyse_exp = TICK_ETERNITY;
6669}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006670
Willy Tarreau8797c062007-05-07 00:55:35 +02006671/************************************************************************/
6672/* The code below is dedicated to ACL parsing and matching */
6673/************************************************************************/
6674
6675
6676
6677
6678/* 1. Check on METHOD
6679 * We use the pre-parsed method if it is known, and store its number as an
6680 * integer. If it is unknown, we use the pointer and the length.
6681 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006682static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006683{
6684 int len, meth;
6685
Willy Tarreauae8b7962007-06-09 23:10:04 +02006686 len = strlen(*text);
6687 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006688
6689 pattern->val.i = meth;
6690 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006691 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006692 if (!pattern->ptr.str)
6693 return 0;
6694 pattern->len = len;
6695 }
6696 return 1;
6697}
6698
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006699static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006700acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6701 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006702{
6703 int meth;
6704 struct http_txn *txn = l7;
6705
Willy Tarreaub6866442008-07-14 23:54:42 +02006706 if (!txn)
6707 return 0;
6708
Willy Tarreau655dce92009-11-08 13:10:58 +01006709 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006710 return 0;
6711
Willy Tarreau8797c062007-05-07 00:55:35 +02006712 meth = txn->meth;
6713 test->i = meth;
6714 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006715 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6716 /* ensure the indexes are not affected */
6717 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006718 test->len = txn->req.sl.rq.m_l;
6719 test->ptr = txn->req.sol;
6720 }
6721 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6722 return 1;
6723}
6724
6725static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6726{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006727 int icase;
6728
Willy Tarreau8797c062007-05-07 00:55:35 +02006729 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006730 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006731
6732 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006733 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006734
6735 /* Other method, we must compare the strings */
6736 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006737 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006738
6739 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6740 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6741 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006742 return ACL_PAT_FAIL;
6743 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006744}
6745
6746/* 2. Check on Request/Status Version
6747 * We simply compare strings here.
6748 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006749static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006750{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006751 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006752 if (!pattern->ptr.str)
6753 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006754 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006755 return 1;
6756}
6757
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006758static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006759acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6760 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006761{
6762 struct http_txn *txn = l7;
6763 char *ptr;
6764 int len;
6765
Willy Tarreaub6866442008-07-14 23:54:42 +02006766 if (!txn)
6767 return 0;
6768
Willy Tarreau655dce92009-11-08 13:10:58 +01006769 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006770 return 0;
6771
Willy Tarreau8797c062007-05-07 00:55:35 +02006772 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006773 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006774
6775 while ((len-- > 0) && (*ptr++ != '/'));
6776 if (len <= 0)
6777 return 0;
6778
6779 test->ptr = ptr;
6780 test->len = len;
6781
6782 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6783 return 1;
6784}
6785
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006786static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006787acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6788 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006789{
6790 struct http_txn *txn = l7;
6791 char *ptr;
6792 int len;
6793
Willy Tarreaub6866442008-07-14 23:54:42 +02006794 if (!txn)
6795 return 0;
6796
Willy Tarreau655dce92009-11-08 13:10:58 +01006797 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006798 return 0;
6799
Willy Tarreau8797c062007-05-07 00:55:35 +02006800 len = txn->rsp.sl.st.v_l;
6801 ptr = txn->rsp.sol;
6802
6803 while ((len-- > 0) && (*ptr++ != '/'));
6804 if (len <= 0)
6805 return 0;
6806
6807 test->ptr = ptr;
6808 test->len = len;
6809
6810 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6811 return 1;
6812}
6813
6814/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006815static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006816acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6817 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006818{
6819 struct http_txn *txn = l7;
6820 char *ptr;
6821 int len;
6822
Willy Tarreaub6866442008-07-14 23:54:42 +02006823 if (!txn)
6824 return 0;
6825
Willy Tarreau655dce92009-11-08 13:10:58 +01006826 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006827 return 0;
6828
Willy Tarreau8797c062007-05-07 00:55:35 +02006829 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006830 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006831
6832 test->i = __strl2ui(ptr, len);
6833 test->flags = ACL_TEST_F_VOL_1ST;
6834 return 1;
6835}
6836
6837/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006838static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006839acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6840 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006841{
6842 struct http_txn *txn = l7;
6843
Willy Tarreaub6866442008-07-14 23:54:42 +02006844 if (!txn)
6845 return 0;
6846
Willy Tarreau655dce92009-11-08 13:10:58 +01006847 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006848 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006849
Willy Tarreauc11416f2007-06-17 16:58:38 +02006850 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6851 /* ensure the indexes are not affected */
6852 return 0;
6853
Willy Tarreau8797c062007-05-07 00:55:35 +02006854 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006855 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006856
Willy Tarreauf3d25982007-05-08 22:45:09 +02006857 /* we do not need to set READ_ONLY because the data is in a buffer */
6858 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006859 return 1;
6860}
6861
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006862static int
6863acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6864 struct acl_expr *expr, struct acl_test *test)
6865{
6866 struct http_txn *txn = l7;
6867
Willy Tarreaub6866442008-07-14 23:54:42 +02006868 if (!txn)
6869 return 0;
6870
Willy Tarreau655dce92009-11-08 13:10:58 +01006871 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006872 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006873
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006874 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6875 /* ensure the indexes are not affected */
6876 return 0;
6877
6878 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006879 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 +01006880 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6881 test->i = AF_INET;
6882
6883 /*
6884 * If we are parsing url in frontend space, we prepare backend stage
6885 * to not parse again the same url ! optimization lazyness...
6886 */
6887 if (px->options & PR_O_HTTP_PROXY)
6888 l4->flags |= SN_ADDR_SET;
6889
6890 test->flags = ACL_TEST_F_READ_ONLY;
6891 return 1;
6892}
6893
6894static int
6895acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6896 struct acl_expr *expr, struct acl_test *test)
6897{
6898 struct http_txn *txn = l7;
6899
Willy Tarreaub6866442008-07-14 23:54:42 +02006900 if (!txn)
6901 return 0;
6902
Willy Tarreau655dce92009-11-08 13:10:58 +01006903 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006904 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006905
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006906 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6907 /* ensure the indexes are not affected */
6908 return 0;
6909
6910 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006911 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 +01006912 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6913
6914 if (px->options & PR_O_HTTP_PROXY)
6915 l4->flags |= SN_ADDR_SET;
6916
6917 test->flags = ACL_TEST_F_READ_ONLY;
6918 return 1;
6919}
6920
Willy Tarreauc11416f2007-06-17 16:58:38 +02006921/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6922 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6923 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006924static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006925acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006926 struct acl_expr *expr, struct acl_test *test)
6927{
6928 struct http_txn *txn = l7;
6929 struct hdr_idx *idx = &txn->hdr_idx;
6930 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006931
Willy Tarreaub6866442008-07-14 23:54:42 +02006932 if (!txn)
6933 return 0;
6934
Willy Tarreau33a7e692007-06-10 19:45:56 +02006935 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6936 /* search for header from the beginning */
6937 ctx->idx = 0;
6938
Willy Tarreau33a7e692007-06-10 19:45:56 +02006939 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6940 test->flags |= ACL_TEST_F_FETCH_MORE;
6941 test->flags |= ACL_TEST_F_VOL_HDR;
6942 test->len = ctx->vlen;
6943 test->ptr = (char *)ctx->line + ctx->val;
6944 return 1;
6945 }
6946
6947 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6948 test->flags |= ACL_TEST_F_VOL_HDR;
6949 return 0;
6950}
6951
Willy Tarreau33a7e692007-06-10 19:45:56 +02006952static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006953acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6954 struct acl_expr *expr, struct acl_test *test)
6955{
6956 struct http_txn *txn = l7;
6957
Willy Tarreaub6866442008-07-14 23:54:42 +02006958 if (!txn)
6959 return 0;
6960
Willy Tarreau655dce92009-11-08 13:10:58 +01006961 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006962 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006963
Willy Tarreauc11416f2007-06-17 16:58:38 +02006964 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6965 /* ensure the indexes are not affected */
6966 return 0;
6967
6968 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6969}
6970
6971static int
6972acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6973 struct acl_expr *expr, struct acl_test *test)
6974{
6975 struct http_txn *txn = l7;
6976
Willy Tarreaub6866442008-07-14 23:54:42 +02006977 if (!txn)
6978 return 0;
6979
Willy Tarreau655dce92009-11-08 13:10:58 +01006980 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006981 return 0;
6982
6983 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6984}
6985
6986/* 6. Check on HTTP header count. The number of occurrences is returned.
6987 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6988 */
6989static int
6990acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006991 struct acl_expr *expr, struct acl_test *test)
6992{
6993 struct http_txn *txn = l7;
6994 struct hdr_idx *idx = &txn->hdr_idx;
6995 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006996 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006997
Willy Tarreaub6866442008-07-14 23:54:42 +02006998 if (!txn)
6999 return 0;
7000
Willy Tarreau33a7e692007-06-10 19:45:56 +02007001 ctx.idx = 0;
7002 cnt = 0;
7003 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7004 cnt++;
7005
7006 test->i = cnt;
7007 test->flags = ACL_TEST_F_VOL_HDR;
7008 return 1;
7009}
7010
Willy Tarreauc11416f2007-06-17 16:58:38 +02007011static int
7012acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7013 struct acl_expr *expr, struct acl_test *test)
7014{
7015 struct http_txn *txn = l7;
7016
Willy Tarreaub6866442008-07-14 23:54:42 +02007017 if (!txn)
7018 return 0;
7019
Willy Tarreau655dce92009-11-08 13:10:58 +01007020 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007021 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007022
Willy Tarreauc11416f2007-06-17 16:58:38 +02007023 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7024 /* ensure the indexes are not affected */
7025 return 0;
7026
7027 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7028}
7029
7030static int
7031acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7032 struct acl_expr *expr, struct acl_test *test)
7033{
7034 struct http_txn *txn = l7;
7035
Willy Tarreaub6866442008-07-14 23:54:42 +02007036 if (!txn)
7037 return 0;
7038
Willy Tarreau655dce92009-11-08 13:10:58 +01007039 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007040 return 0;
7041
7042 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7043}
7044
Willy Tarreau33a7e692007-06-10 19:45:56 +02007045/* 7. Check on HTTP header's integer value. The integer value is returned.
7046 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007047 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007048 */
7049static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007050acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007051 struct acl_expr *expr, struct acl_test *test)
7052{
7053 struct http_txn *txn = l7;
7054 struct hdr_idx *idx = &txn->hdr_idx;
7055 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007056
Willy Tarreaub6866442008-07-14 23:54:42 +02007057 if (!txn)
7058 return 0;
7059
Willy Tarreau33a7e692007-06-10 19:45:56 +02007060 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7061 /* search for header from the beginning */
7062 ctx->idx = 0;
7063
Willy Tarreau33a7e692007-06-10 19:45:56 +02007064 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7065 test->flags |= ACL_TEST_F_FETCH_MORE;
7066 test->flags |= ACL_TEST_F_VOL_HDR;
7067 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7068 return 1;
7069 }
7070
7071 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7072 test->flags |= ACL_TEST_F_VOL_HDR;
7073 return 0;
7074}
7075
Willy Tarreauc11416f2007-06-17 16:58:38 +02007076static int
7077acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7078 struct acl_expr *expr, struct acl_test *test)
7079{
7080 struct http_txn *txn = l7;
7081
Willy Tarreaub6866442008-07-14 23:54:42 +02007082 if (!txn)
7083 return 0;
7084
Willy Tarreau655dce92009-11-08 13:10:58 +01007085 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007086 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007087
Willy Tarreauc11416f2007-06-17 16:58:38 +02007088 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7089 /* ensure the indexes are not affected */
7090 return 0;
7091
7092 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7093}
7094
7095static int
7096acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7097 struct acl_expr *expr, struct acl_test *test)
7098{
7099 struct http_txn *txn = l7;
7100
Willy Tarreaub6866442008-07-14 23:54:42 +02007101 if (!txn)
7102 return 0;
7103
Willy Tarreau655dce92009-11-08 13:10:58 +01007104 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007105 return 0;
7106
7107 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7108}
7109
Willy Tarreau106f9792009-09-19 07:54:16 +02007110/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7111 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7112 */
7113static int
7114acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7115 struct acl_expr *expr, struct acl_test *test)
7116{
7117 struct http_txn *txn = l7;
7118 struct hdr_idx *idx = &txn->hdr_idx;
7119 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7120
7121 if (!txn)
7122 return 0;
7123
7124 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7125 /* search for header from the beginning */
7126 ctx->idx = 0;
7127
7128 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7129 test->flags |= ACL_TEST_F_FETCH_MORE;
7130 test->flags |= ACL_TEST_F_VOL_HDR;
7131 /* Same optimization as url_ip */
7132 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7133 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7134 test->ptr = (void *)&l4->srv_addr.sin_addr;
7135 test->i = AF_INET;
7136 return 1;
7137 }
7138
7139 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7140 test->flags |= ACL_TEST_F_VOL_HDR;
7141 return 0;
7142}
7143
7144static int
7145acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7146 struct acl_expr *expr, struct acl_test *test)
7147{
7148 struct http_txn *txn = l7;
7149
7150 if (!txn)
7151 return 0;
7152
Willy Tarreau655dce92009-11-08 13:10:58 +01007153 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007154 return 0;
7155
7156 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7157 /* ensure the indexes are not affected */
7158 return 0;
7159
7160 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7161}
7162
7163static int
7164acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7165 struct acl_expr *expr, struct acl_test *test)
7166{
7167 struct http_txn *txn = l7;
7168
7169 if (!txn)
7170 return 0;
7171
Willy Tarreau655dce92009-11-08 13:10:58 +01007172 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007173 return 0;
7174
7175 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7176}
7177
Willy Tarreau737b0c12007-06-10 21:28:46 +02007178/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7179 * the first '/' after the possible hostname, and ends before the possible '?'.
7180 */
7181static int
7182acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7183 struct acl_expr *expr, struct acl_test *test)
7184{
7185 struct http_txn *txn = l7;
7186 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007187
Willy Tarreaub6866442008-07-14 23:54:42 +02007188 if (!txn)
7189 return 0;
7190
Willy Tarreau655dce92009-11-08 13:10:58 +01007191 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007192 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007193
Willy Tarreauc11416f2007-06-17 16:58:38 +02007194 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7195 /* ensure the indexes are not affected */
7196 return 0;
7197
Willy Tarreau962c3f42010-01-10 00:15:35 +01007198 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007199 ptr = http_get_path(txn);
7200 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007201 return 0;
7202
7203 /* OK, we got the '/' ! */
7204 test->ptr = ptr;
7205
7206 while (ptr < end && *ptr != '?')
7207 ptr++;
7208
7209 test->len = ptr - test->ptr;
7210
7211 /* we do not need to set READ_ONLY because the data is in a buffer */
7212 test->flags = ACL_TEST_F_VOL_1ST;
7213 return 1;
7214}
7215
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007216static int
7217acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7218 struct acl_expr *expr, struct acl_test *test)
7219{
7220 struct buffer *req = s->req;
7221 struct http_txn *txn = &s->txn;
7222 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007223
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007224 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7225 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7226 */
7227
7228 if (!s || !req)
7229 return 0;
7230
Willy Tarreau655dce92009-11-08 13:10:58 +01007231 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007232 /* Already decoded as OK */
7233 test->flags |= ACL_TEST_F_SET_RES_PASS;
7234 return 1;
7235 }
7236
7237 /* Try to decode HTTP request */
7238 if (likely(req->lr < req->r))
7239 http_msg_analyzer(req, msg, &txn->hdr_idx);
7240
Willy Tarreau655dce92009-11-08 13:10:58 +01007241 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007242 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7243 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7244 return 1;
7245 }
7246 /* wait for final state */
7247 test->flags |= ACL_TEST_F_MAY_CHANGE;
7248 return 0;
7249 }
7250
7251 /* OK we got a valid HTTP request. We have some minor preparation to
7252 * perform so that further checks can rely on HTTP tests.
7253 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007254 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007255 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7256 s->flags |= SN_REDIRECTABLE;
7257
7258 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7259 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7260 return 1;
7261 }
7262
7263 test->flags |= ACL_TEST_F_SET_RES_PASS;
7264 return 1;
7265}
7266
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007267static int
7268acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
7269 struct acl_expr *expr, struct acl_test *test)
7270{
7271
7272 if (!s)
7273 return 0;
7274
7275 if (!get_http_auth(s))
7276 return 0;
7277
7278 test->ctx.a[0] = expr->arg.ul;
7279 test->ctx.a[1] = s->txn.auth.user;
7280 test->ctx.a[2] = s->txn.auth.pass;
7281
7282 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
7283
7284 return 1;
7285}
Willy Tarreau8797c062007-05-07 00:55:35 +02007286
7287/************************************************************************/
7288/* All supported keywords must be declared here. */
7289/************************************************************************/
7290
7291/* Note: must not be declared <const> as its list will be overwritten */
7292static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007293 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7294
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007295 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7296 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7297 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7298 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007299
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007300 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7301 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7302 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7303 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7304 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7305 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7306 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7307 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7308 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007309
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007310 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7311 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7312 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7313 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7314 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7315 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7316 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7317 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7318 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7319 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007320 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007321
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007322 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7323 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7324 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7325 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7326 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7327 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7328 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7329 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7330 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007331 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007332
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007333 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7334 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7335 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7336 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7337 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7338 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7339 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007340
Willy Tarreauf3d25982007-05-08 22:45:09 +02007341#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007342 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7343 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7344 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7345 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7346 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7347 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7348 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7349
Willy Tarreau8797c062007-05-07 00:55:35 +02007350 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7351 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7352 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7353 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7354 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7355 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7356 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7357 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007358#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02007359
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007360 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth },
7361 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth },
Willy Tarreau8797c062007-05-07 00:55:35 +02007362 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02007363}};
7364
7365
7366__attribute__((constructor))
7367static void __http_protocol_init(void)
7368{
7369 acl_register_keywords(&acl_kws);
7370}
7371
7372
Willy Tarreau58f10d72006-12-04 02:26:12 +01007373/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007374 * Local variables:
7375 * c-indent-level: 8
7376 * c-basic-offset: 8
7377 * End:
7378 */