blob: 6e8fa411f20e2646d048c5d4bae13be7a307c932 [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) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002970 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2971 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002972 to_del |= 2; /* remove "keep-alive" */
2973 if (!(txn->flags & TX_REQ_VER_11))
2974 to_del |= 1; /* remove "close" */
2975 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002976 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002977
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002978 /* check if client or config asks for explicit close in KAL/SCL */
2979 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2980 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2981 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2982 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2983 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002984 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2985 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002986 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2987 }
Willy Tarreau78599912009-10-17 20:12:21 +02002988
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002989 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002990 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01002991 if (wl->cond) {
2992 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2993 ret = acl_pass(ret);
2994 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2995 ret = !ret;
2996 if (!ret)
2997 continue;
2998 }
2999
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003000 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003001 goto return_bad_req;
3002 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003003
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003004 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3005 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003006 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003007
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003008 if (!realm)
3009 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3010
Willy Tarreau844a7e72010-01-31 21:46:18 +01003011 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003012 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3013 txn->status = 401;
3014 stream_int_retnclose(req->prod, &msg);
3015 goto return_prx_cond;
3016 }
3017
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003018 if (do_stats) {
3019 /* We need to provied stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 * FIXME!!! that one is rather dangerous, we want to
3021 * make it follow standard rules (eg: clear req->analysers).
3022 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003023
3024 s->logs.tv_request = now;
3025 s->data_source = DATA_SRC_STATS;
3026 s->data_state = DATA_ST_INIT;
3027 s->task->nice = -32; /* small boost for HTTP statistics */
3028 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3029 s->rep->prod->private = s;
3030 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3031 req->analysers = 0;
3032
3033 return 0;
3034
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003035 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003036
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 /* check whether we have some ACLs set to redirect this request */
3038 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003039 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003040
Willy Tarreauf285f542010-01-03 20:03:03 +01003041 if (rule->cond) {
3042 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3043 ret = acl_pass(ret);
3044 if (rule->cond->pol == ACL_COND_UNLESS)
3045 ret = !ret;
3046 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003049 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003050 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003051
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003052 /* build redirect message */
3053 switch(rule->code) {
3054 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 msg_fmt = HTTP_303;
3056 break;
3057 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003058 msg_fmt = HTTP_301;
3059 break;
3060 case 302:
3061 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003062 msg_fmt = HTTP_302;
3063 break;
3064 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003065
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003066 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003068
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 switch(rule->type) {
3070 case REDIRECT_TYPE_PREFIX: {
3071 const char *path;
3072 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003074 path = http_get_path(txn);
3075 /* build message using path */
3076 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003077 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3079 int qs = 0;
3080 while (qs < pathlen) {
3081 if (path[qs] == '?') {
3082 pathlen = qs;
3083 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003084 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003085 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003086 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003087 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003088 } else {
3089 path = "/";
3090 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003091 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003092
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003093 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003094 goto return_bad_req;
3095
3096 /* add prefix. Note that if prefix == "/", we don't want to
3097 * add anything, otherwise it makes it hard for the user to
3098 * configure a self-redirection.
3099 */
3100 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003101 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3102 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003103 }
3104
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003105 /* add path */
3106 memcpy(rdr.str + rdr.len, path, pathlen);
3107 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003108
3109 /* append a slash at the end of the location is needed and missing */
3110 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3111 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3112 if (rdr.len > rdr.size - 5)
3113 goto return_bad_req;
3114 rdr.str[rdr.len] = '/';
3115 rdr.len++;
3116 }
3117
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003118 break;
3119 }
3120 case REDIRECT_TYPE_LOCATION:
3121 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003122 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003123 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003124
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003125 /* add location */
3126 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3127 rdr.len += rule->rdr_len;
3128 break;
3129 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003130
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003131 if (rule->cookie_len) {
3132 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3133 rdr.len += 14;
3134 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3135 rdr.len += rule->cookie_len;
3136 memcpy(rdr.str + rdr.len, "\r\n", 2);
3137 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003138 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003139
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003140 /* add end of headers and the keep-alive/close status.
3141 * We may choose to set keep-alive if the Location begins
3142 * with a slash, because the client will come back to the
3143 * same server.
3144 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003145 txn->status = rule->code;
3146 /* let's log the request time */
3147 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003148
3149 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3150 (txn->flags & TX_REQ_XFER_LEN) &&
3151 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3152 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3153 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3154 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003155 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003156 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3157 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3158 rdr.len += 30;
3159 } else {
3160 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3161 rdr.len += 24;
3162 }
Willy Tarreau75661452010-01-10 10:35:01 +01003163 }
3164 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3165 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003166 buffer_write(req->prod->ob, rdr.str, rdr.len);
3167 /* "eat" the request */
3168 buffer_ignore(req, msg->sov - msg->som);
3169 msg->som = msg->sov;
3170 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003171 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3172 txn->req.msg_state = HTTP_MSG_CLOSED;
3173 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003174 break;
3175 } else {
3176 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003177 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3178 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3179 rdr.len += 29;
3180 } else {
3181 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3182 rdr.len += 23;
3183 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003184 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003185 goto return_prx_cond;
3186 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003187 }
3188 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003189
Willy Tarreau2be39392010-01-03 17:24:51 +01003190 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3191 * If this happens, then the data will not come immediately, so we must
3192 * send all what we have without waiting. Note that due to the small gain
3193 * in waiting for the body of the request, it's easier to simply put the
3194 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3195 * itself once used.
3196 */
3197 req->flags |= BF_SEND_DONTWAIT;
3198
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003199 /* that's OK for us now, let's move on to next analysers */
3200 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003201
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003202 return_bad_req:
3203 /* We centralize bad requests processing here */
3204 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3205 /* we detected a parsing error. We want to archive this request
3206 * in the dedicated proxy area for later troubleshooting.
3207 */
3208 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3209 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003210
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003211 txn->req.msg_state = HTTP_MSG_ERROR;
3212 txn->status = 400;
3213 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003214
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003215 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003216 if (s->listener->counters)
3217 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003218
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003219 return_prx_cond:
3220 if (!(s->flags & SN_ERR_MASK))
3221 s->flags |= SN_ERR_PRXCOND;
3222 if (!(s->flags & SN_FINST_MASK))
3223 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003224
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003225 req->analysers = 0;
3226 req->analyse_exp = TICK_ETERNITY;
3227 return 0;
3228}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003229
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003230/* This function performs all the processing enabled for the current request.
3231 * It returns 1 if the processing can continue on next analysers, or zero if it
3232 * needs more data, encounters an error, or wants to immediately abort the
3233 * request. It relies on buffers flags, and updates s->req->analysers.
3234 */
3235int http_process_request(struct session *s, struct buffer *req, int an_bit)
3236{
3237 struct http_txn *txn = &s->txn;
3238 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003239
Willy Tarreau655dce92009-11-08 13:10:58 +01003240 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003241 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003242 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003243 return 0;
3244 }
3245
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003246 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3247 now_ms, __FUNCTION__,
3248 s,
3249 req,
3250 req->rex, req->wex,
3251 req->flags,
3252 req->l,
3253 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003254
Willy Tarreau59234e92008-11-30 23:51:27 +01003255 /*
3256 * Right now, we know that we have processed the entire headers
3257 * and that unwanted requests have been filtered out. We can do
3258 * whatever we want with the remaining request. Also, now we
3259 * may have separate values for ->fe, ->be.
3260 */
Willy Tarreau06619262006-12-17 08:37:22 +01003261
Willy Tarreau59234e92008-11-30 23:51:27 +01003262 /*
3263 * If HTTP PROXY is set we simply get remote server address
3264 * parsing incoming request.
3265 */
3266 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003267 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003268 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003269
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003271 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003272 * Note that doing so might move headers in the request, but
3273 * the fields will stay coherent and the URI will not move.
3274 * This should only be performed in the backend.
3275 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003276 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003277 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3278 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003279
Willy Tarreau59234e92008-11-30 23:51:27 +01003280 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003281 * 8: the appsession cookie was looked up very early in 1.2,
3282 * so let's do the same now.
3283 */
3284
3285 /* It needs to look into the URI */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01003286 if ((txn->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003287 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003288 }
3289
3290 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003291 * 9: add X-Forwarded-For if either the frontend or the backend
3292 * asks for it.
3293 */
3294 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3295 if (s->cli_addr.ss_family == AF_INET) {
3296 /* Add an X-Forwarded-For header unless the source IP is
3297 * in the 'except' network range.
3298 */
3299 if ((!s->fe->except_mask.s_addr ||
3300 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3301 != s->fe->except_net.s_addr) &&
3302 (!s->be->except_mask.s_addr ||
3303 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3304 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003305 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 unsigned char *pn;
3307 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003308
3309 /* Note: we rely on the backend to get the header name to be used for
3310 * x-forwarded-for, because the header is really meant for the backends.
3311 * However, if the backend did not specify any option, we have to rely
3312 * on the frontend's header name.
3313 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003314 if (s->be->fwdfor_hdr_len) {
3315 len = s->be->fwdfor_hdr_len;
3316 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003317 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003318 len = s->fe->fwdfor_hdr_len;
3319 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003320 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003321 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003322
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003323 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003324 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003325 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003326 }
3327 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003328 else if (s->cli_addr.ss_family == AF_INET6) {
3329 /* FIXME: for the sake of completeness, we should also support
3330 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003331 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003332 int len;
3333 char pn[INET6_ADDRSTRLEN];
3334 inet_ntop(AF_INET6,
3335 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3336 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003337
Willy Tarreau59234e92008-11-30 23:51:27 +01003338 /* Note: we rely on the backend to get the header name to be used for
3339 * x-forwarded-for, because the header is really meant for the backends.
3340 * However, if the backend did not specify any option, we have to rely
3341 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003342 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003343 if (s->be->fwdfor_hdr_len) {
3344 len = s->be->fwdfor_hdr_len;
3345 memcpy(trash, s->be->fwdfor_hdr_name, len);
3346 } else {
3347 len = s->fe->fwdfor_hdr_len;
3348 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003349 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003350 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003351
Willy Tarreau59234e92008-11-30 23:51:27 +01003352 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003353 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003354 goto return_bad_req;
3355 }
3356 }
3357
3358 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003359 * 10: add X-Original-To if either the frontend or the backend
3360 * asks for it.
3361 */
3362 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3363
3364 /* FIXME: don't know if IPv6 can handle that case too. */
3365 if (s->cli_addr.ss_family == AF_INET) {
3366 /* Add an X-Original-To header unless the destination IP is
3367 * in the 'except' network range.
3368 */
3369 if (!(s->flags & SN_FRT_ADDR_SET))
3370 get_frt_addr(s);
3371
3372 if ((!s->fe->except_mask_to.s_addr ||
3373 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3374 != s->fe->except_to.s_addr) &&
3375 (!s->be->except_mask_to.s_addr ||
3376 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3377 != s->be->except_to.s_addr)) {
3378 int len;
3379 unsigned char *pn;
3380 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3381
3382 /* Note: we rely on the backend to get the header name to be used for
3383 * x-original-to, because the header is really meant for the backends.
3384 * However, if the backend did not specify any option, we have to rely
3385 * on the frontend's header name.
3386 */
3387 if (s->be->orgto_hdr_len) {
3388 len = s->be->orgto_hdr_len;
3389 memcpy(trash, s->be->orgto_hdr_name, len);
3390 } else {
3391 len = s->fe->orgto_hdr_len;
3392 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003393 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003394 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3395
3396 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003397 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003398 goto return_bad_req;
3399 }
3400 }
3401 }
3402
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003403 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3404 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3405 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3406 unsigned int want_flags = 0;
3407
3408 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003409 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3410 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) ||
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003411 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3412 want_flags |= TX_CON_CLO_SET;
3413 } else {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003414 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3415 (((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA) &&
3416 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003417 want_flags |= TX_CON_KAL_SET;
3418 }
3419
3420 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3421 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003422 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003423
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003424
Willy Tarreau522d6c02009-12-06 18:49:18 +01003425 /* If we have no server assigned yet and we're balancing on url_param
3426 * with a POST request, we may be interested in checking the body for
3427 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003428 */
3429 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3430 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003431 s->be->url_param_post_limit != 0 &&
3432 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003433 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003434 buffer_dont_connect(req);
3435 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003437
Willy Tarreaud98cf932009-12-27 22:54:55 +01003438 if (txn->flags & TX_REQ_XFER_LEN)
3439 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003440
Willy Tarreau59234e92008-11-30 23:51:27 +01003441 /*************************************************************
3442 * OK, that's finished for the headers. We have done what we *
3443 * could. Let's switch to the DATA state. *
3444 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003445 req->analyse_exp = TICK_ETERNITY;
3446 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003447
Willy Tarreau59234e92008-11-30 23:51:27 +01003448 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003449 /* OK let's go on with the BODY now */
3450 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003451
Willy Tarreau59234e92008-11-30 23:51:27 +01003452 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003453 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003454 /* we detected a parsing error. We want to archive this request
3455 * in the dedicated proxy area for later troubleshooting.
3456 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003457 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003458 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003459
Willy Tarreau59234e92008-11-30 23:51:27 +01003460 txn->req.msg_state = HTTP_MSG_ERROR;
3461 txn->status = 400;
3462 req->analysers = 0;
3463 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003464
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003465 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003466 if (s->listener->counters)
3467 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003468
Willy Tarreau59234e92008-11-30 23:51:27 +01003469 if (!(s->flags & SN_ERR_MASK))
3470 s->flags |= SN_ERR_PRXCOND;
3471 if (!(s->flags & SN_FINST_MASK))
3472 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003473 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003474}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003475
Willy Tarreau60b85b02008-11-30 23:28:40 +01003476/* This function is an analyser which processes the HTTP tarpit. It always
3477 * returns zero, at the beginning because it prevents any other processing
3478 * from occurring, and at the end because it terminates the request.
3479 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003480int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003481{
3482 struct http_txn *txn = &s->txn;
3483
3484 /* This connection is being tarpitted. The CLIENT side has
3485 * already set the connect expiration date to the right
3486 * timeout. We just have to check that the client is still
3487 * there and that the timeout has not expired.
3488 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003489 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003490 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3491 !tick_is_expired(req->analyse_exp, now_ms))
3492 return 0;
3493
3494 /* We will set the queue timer to the time spent, just for
3495 * logging purposes. We fake a 500 server error, so that the
3496 * attacker will not suspect his connection has been tarpitted.
3497 * It will not cause trouble to the logs because we can exclude
3498 * the tarpitted connections by filtering on the 'PT' status flags.
3499 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003500 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3501
3502 txn->status = 500;
3503 if (req->flags != BF_READ_ERROR)
3504 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3505
3506 req->analysers = 0;
3507 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003508
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003509 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003510 if (s->listener->counters)
3511 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003512
Willy Tarreau60b85b02008-11-30 23:28:40 +01003513 if (!(s->flags & SN_ERR_MASK))
3514 s->flags |= SN_ERR_PRXCOND;
3515 if (!(s->flags & SN_FINST_MASK))
3516 s->flags |= SN_FINST_T;
3517 return 0;
3518}
3519
Willy Tarreaud34af782008-11-30 23:36:37 +01003520/* This function is an analyser which processes the HTTP request body. It looks
3521 * for parameters to be used for the load balancing algorithm (url_param). It
3522 * must only be called after the standard HTTP request processing has occurred,
3523 * because it expects the request to be parsed. It returns zero if it needs to
3524 * read more data, or 1 once it has completed its analysis.
3525 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003526int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003527{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003528 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003529 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003530 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003531
3532 /* We have to parse the HTTP request body to find any required data.
3533 * "balance url_param check_post" should have been the only way to get
3534 * into this. We were brought here after HTTP header analysis, so all
3535 * related structures are ready.
3536 */
3537
Willy Tarreau522d6c02009-12-06 18:49:18 +01003538 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3539 goto missing_data;
3540
3541 if (msg->msg_state < HTTP_MSG_100_SENT) {
3542 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3543 * send an HTTP/1.1 100 Continue intermediate response.
3544 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003545 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003546 struct hdr_ctx ctx;
3547 ctx.idx = 0;
3548 /* Expect is allowed in 1.1, look for it */
3549 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3550 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3551 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3552 }
3553 }
3554 msg->msg_state = HTTP_MSG_100_SENT;
3555 }
3556
3557 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003558 /* we have msg->col and msg->sov which both point to the first
3559 * byte of message body. msg->som still points to the beginning
3560 * of the message. We must save the body in req->lr because it
3561 * survives buffer re-alignments.
3562 */
3563 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 if (txn->flags & TX_REQ_TE_CHNK)
3565 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3566 else
3567 msg->msg_state = HTTP_MSG_DATA;
3568 }
3569
3570 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003571 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003572 * set ->sov and ->lr to point to the body and switch to DATA or
3573 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003574 */
3575 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003576
Willy Tarreau115acb92009-12-26 13:56:06 +01003577 if (!ret)
3578 goto missing_data;
3579 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003580 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003581 }
3582
Willy Tarreaud98cf932009-12-27 22:54:55 +01003583 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003584 * We have the first non-header byte in msg->col, which is either the
3585 * beginning of the chunk size or of the data. The first data byte is in
3586 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3587 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003588 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003589
3590 if (msg->hdr_content_len < limit)
3591 limit = msg->hdr_content_len;
3592
Willy Tarreau7c96f672009-12-27 22:47:25 +01003593 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003594 goto http_end;
3595
3596 missing_data:
3597 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003598 if (req->flags & BF_FULL)
3599 goto return_bad_req;
3600
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3602 txn->status = 408;
3603 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3604 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003605 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003606
3607 /* we get here if we need to wait for more data */
3608 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003609 /* Not enough data. We'll re-use the http-request
3610 * timeout here. Ideally, we should set the timeout
3611 * relative to the accept() date. We just set the
3612 * request timeout once at the beginning of the
3613 * request.
3614 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003615 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003616 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003617 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003618 return 0;
3619 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003620
3621 http_end:
3622 /* The situation will not evolve, so let's give up on the analysis. */
3623 s->logs.tv_request = now; /* update the request timer to reflect full request */
3624 req->analysers &= ~an_bit;
3625 req->analyse_exp = TICK_ETERNITY;
3626 return 1;
3627
3628 return_bad_req: /* let's centralize all bad requests */
3629 txn->req.msg_state = HTTP_MSG_ERROR;
3630 txn->status = 400;
3631 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3632
3633 return_err_msg:
3634 req->analysers = 0;
3635 s->fe->counters.failed_req++;
3636 if (s->listener->counters)
3637 s->listener->counters->failed_req++;
3638
3639 if (!(s->flags & SN_ERR_MASK))
3640 s->flags |= SN_ERR_PRXCOND;
3641 if (!(s->flags & SN_FINST_MASK))
3642 s->flags |= SN_FINST_R;
3643 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003644}
3645
Willy Tarreau610ecce2010-01-04 21:15:02 +01003646/* Terminate current transaction and prepare a new one. This is very tricky
3647 * right now but it works.
3648 */
3649void http_end_txn_clean_session(struct session *s)
3650{
3651 /* FIXME: We need a more portable way of releasing a backend's and a
3652 * server's connections. We need a safer way to reinitialize buffer
3653 * flags. We also need a more accurate method for computing per-request
3654 * data.
3655 */
3656 http_silent_debug(__LINE__, s);
3657
3658 s->req->cons->flags |= SI_FL_NOLINGER;
3659 s->req->cons->shutr(s->req->cons);
3660 s->req->cons->shutw(s->req->cons);
3661
3662 http_silent_debug(__LINE__, s);
3663
3664 if (s->flags & SN_BE_ASSIGNED)
3665 s->be->beconn--;
3666
3667 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3668 session_process_counters(s);
3669
3670 if (s->txn.status) {
3671 int n;
3672
3673 n = s->txn.status / 100;
3674 if (n < 1 || n > 5)
3675 n = 0;
3676
3677 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003678 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003679
Willy Tarreau24657792010-02-26 10:30:28 +01003680 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003681 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003682 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003683 }
3684
3685 /* don't count other requests' data */
3686 s->logs.bytes_in -= s->req->l - s->req->send_max;
3687 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3688
3689 /* let's do a final log if we need it */
3690 if (s->logs.logwait &&
3691 !(s->flags & SN_MONITOR) &&
3692 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3693 s->do_log(s);
3694 }
3695
3696 s->logs.accept_date = date; /* user-visible date for logging */
3697 s->logs.tv_accept = now; /* corrected date for internal use */
3698 tv_zero(&s->logs.tv_request);
3699 s->logs.t_queue = -1;
3700 s->logs.t_connect = -1;
3701 s->logs.t_data = -1;
3702 s->logs.t_close = 0;
3703 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3704 s->logs.srv_queue_size = 0; /* we will get this number soon */
3705
3706 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3707 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3708
3709 if (s->pend_pos)
3710 pendconn_free(s->pend_pos);
3711
3712 if (s->srv) {
3713 if (s->flags & SN_CURR_SESS) {
3714 s->flags &= ~SN_CURR_SESS;
3715 s->srv->cur_sess--;
3716 }
3717 if (may_dequeue_tasks(s->srv, s->be))
3718 process_srv_queue(s->srv);
3719 }
3720
3721 if (unlikely(s->srv_conn))
3722 sess_change_server(s, NULL);
3723 s->srv = NULL;
3724
3725 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3726 s->req->cons->fd = -1; /* just to help with debugging */
3727 s->req->cons->err_type = SI_ET_NONE;
3728 s->req->cons->err_loc = NULL;
3729 s->req->cons->exp = TICK_ETERNITY;
3730 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003731 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3732 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 +01003733 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003734 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3735 s->txn.meth = 0;
3736 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003737 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003738 if (s->be->options2 & PR_O2_INDEPSTR)
3739 s->req->cons->flags |= SI_FL_INDEP_STR;
3740
3741 /* if the request buffer is not empty, it means we're
3742 * about to process another request, so send pending
3743 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003744 * Just don't do this if the buffer is close to be full,
3745 * because the request will wait for it to flush a little
3746 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003747 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003748 if (s->req->l > s->req->send_max) {
3749 if (s->rep->send_max &&
3750 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003751 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3752 s->rep->flags |= BF_EXPECT_MORE;
3753 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003754
3755 /* we're removing the analysers, we MUST re-enable events detection */
3756 buffer_auto_read(s->req);
3757 buffer_auto_close(s->req);
3758 buffer_auto_read(s->rep);
3759 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003760
3761 /* make ->lr point to the first non-forwarded byte */
3762 s->req->lr = s->req->w + s->req->send_max;
3763 if (s->req->lr >= s->req->data + s->req->size)
3764 s->req->lr -= s->req->size;
3765 s->rep->lr = s->rep->w + s->rep->send_max;
3766 if (s->rep->lr >= s->rep->data + s->rep->size)
3767 s->rep->lr -= s->req->size;
3768
3769 s->req->analysers |= s->fe->fe_req_ana;
3770 s->rep->analysers = 0;
3771
3772 http_silent_debug(__LINE__, s);
3773}
3774
3775
3776/* This function updates the request state machine according to the response
3777 * state machine and buffer flags. It returns 1 if it changes anything (flag
3778 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3779 * it is only used to find when a request/response couple is complete. Both
3780 * this function and its equivalent should loop until both return zero. It
3781 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3782 */
3783int http_sync_req_state(struct session *s)
3784{
3785 struct buffer *buf = s->req;
3786 struct http_txn *txn = &s->txn;
3787 unsigned int old_flags = buf->flags;
3788 unsigned int old_state = txn->req.msg_state;
3789
3790 http_silent_debug(__LINE__, s);
3791 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3792 return 0;
3793
3794 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003795 /* No need to read anymore, the request was completely parsed.
3796 * We can shut the read side unless we want to abort_on_close.
3797 */
3798 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3799 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003800
3801 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3802 goto wait_other_side;
3803
3804 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3805 /* The server has not finished to respond, so we
3806 * don't want to move in order not to upset it.
3807 */
3808 goto wait_other_side;
3809 }
3810
3811 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3812 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003813 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 txn->req.msg_state = HTTP_MSG_TUNNEL;
3815 goto wait_other_side;
3816 }
3817
3818 /* When we get here, it means that both the request and the
3819 * response have finished receiving. Depending on the connection
3820 * mode, we'll have to wait for the last bytes to leave in either
3821 * direction, and sometimes for a close to be effective.
3822 */
3823
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003824 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3825 /* Server-close mode : queue a connection close to the server */
3826 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003827 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003828 }
3829 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3830 /* Option forceclose is set, or either side wants to close,
3831 * let's enforce it now that we're not expecting any new
3832 * data to come. The caller knows the session is complete
3833 * once both states are CLOSED.
3834 */
3835 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003836 buffer_shutr_now(buf);
3837 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003838 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003839 }
3840 else {
3841 /* The last possible modes are keep-alive and tunnel. Since tunnel
3842 * mode does not set the body analyser, we can't reach this place
3843 * in tunnel mode, so we're left with keep-alive only.
3844 * This mode is currently not implemented, we switch to tunnel mode.
3845 */
3846 buffer_auto_read(buf);
3847 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003848 }
3849
3850 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3851 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003852 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3853
Willy Tarreau610ecce2010-01-04 21:15:02 +01003854 if (!(buf->flags & BF_OUT_EMPTY)) {
3855 txn->req.msg_state = HTTP_MSG_CLOSING;
3856 goto http_msg_closing;
3857 }
3858 else {
3859 txn->req.msg_state = HTTP_MSG_CLOSED;
3860 goto http_msg_closed;
3861 }
3862 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003863 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 }
3865
3866 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3867 http_msg_closing:
3868 /* nothing else to forward, just waiting for the output buffer
3869 * to be empty and for the shutw_now to take effect.
3870 */
3871 if (buf->flags & BF_OUT_EMPTY) {
3872 txn->req.msg_state = HTTP_MSG_CLOSED;
3873 goto http_msg_closed;
3874 }
3875 else if (buf->flags & BF_SHUTW) {
3876 txn->req.msg_state = HTTP_MSG_ERROR;
3877 goto wait_other_side;
3878 }
3879 }
3880
3881 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3882 http_msg_closed:
3883 goto wait_other_side;
3884 }
3885
3886 wait_other_side:
3887 http_silent_debug(__LINE__, s);
3888 return txn->req.msg_state != old_state || buf->flags != old_flags;
3889}
3890
3891
3892/* This function updates the response state machine according to the request
3893 * state machine and buffer flags. It returns 1 if it changes anything (flag
3894 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3895 * it is only used to find when a request/response couple is complete. Both
3896 * this function and its equivalent should loop until both return zero. It
3897 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3898 */
3899int http_sync_res_state(struct session *s)
3900{
3901 struct buffer *buf = s->rep;
3902 struct http_txn *txn = &s->txn;
3903 unsigned int old_flags = buf->flags;
3904 unsigned int old_state = txn->rsp.msg_state;
3905
3906 http_silent_debug(__LINE__, s);
3907 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3908 return 0;
3909
3910 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3911 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003912 * still monitor the server connection for a possible close
3913 * while the request is being uploaded, so we don't disable
3914 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003915 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003916 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917
3918 if (txn->req.msg_state == HTTP_MSG_ERROR)
3919 goto wait_other_side;
3920
3921 if (txn->req.msg_state < HTTP_MSG_DONE) {
3922 /* The client seems to still be sending data, probably
3923 * because we got an error response during an upload.
3924 * We have the choice of either breaking the connection
3925 * or letting it pass through. Let's do the later.
3926 */
3927 goto wait_other_side;
3928 }
3929
3930 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3931 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003932 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003933 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3934 goto wait_other_side;
3935 }
3936
3937 /* When we get here, it means that both the request and the
3938 * response have finished receiving. Depending on the connection
3939 * mode, we'll have to wait for the last bytes to leave in either
3940 * direction, and sometimes for a close to be effective.
3941 */
3942
3943 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3944 /* Server-close mode : shut read and wait for the request
3945 * side to close its output buffer. The caller will detect
3946 * when we're in DONE and the other is in CLOSED and will
3947 * catch that for the final cleanup.
3948 */
3949 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3950 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003951 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003952 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3953 /* Option forceclose is set, or either side wants to close,
3954 * let's enforce it now that we're not expecting any new
3955 * data to come. The caller knows the session is complete
3956 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003958 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3959 buffer_shutr_now(buf);
3960 buffer_shutw_now(buf);
3961 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003962 }
3963 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003964 /* The last possible modes are keep-alive and tunnel. Since tunnel
3965 * mode does not set the body analyser, we can't reach this place
3966 * in tunnel mode, so we're left with keep-alive only.
3967 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003968 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003969 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003970 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003971 }
3972
3973 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3974 /* if we've just closed an output, let's switch */
3975 if (!(buf->flags & BF_OUT_EMPTY)) {
3976 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3977 goto http_msg_closing;
3978 }
3979 else {
3980 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3981 goto http_msg_closed;
3982 }
3983 }
3984 goto wait_other_side;
3985 }
3986
3987 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3988 http_msg_closing:
3989 /* nothing else to forward, just waiting for the output buffer
3990 * to be empty and for the shutw_now to take effect.
3991 */
3992 if (buf->flags & BF_OUT_EMPTY) {
3993 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3994 goto http_msg_closed;
3995 }
3996 else if (buf->flags & BF_SHUTW) {
3997 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01003998 s->be->counters.cli_aborts++;
3999 if (s->srv)
4000 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 goto wait_other_side;
4002 }
4003 }
4004
4005 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4006 http_msg_closed:
4007 /* drop any pending data */
4008 buffer_ignore(buf, buf->l - buf->send_max);
4009 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004010 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004011 goto wait_other_side;
4012 }
4013
4014 wait_other_side:
4015 http_silent_debug(__LINE__, s);
4016 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4017}
4018
4019
4020/* Resync the request and response state machines. Return 1 if either state
4021 * changes.
4022 */
4023int http_resync_states(struct session *s)
4024{
4025 struct http_txn *txn = &s->txn;
4026 int old_req_state = txn->req.msg_state;
4027 int old_res_state = txn->rsp.msg_state;
4028
4029 http_silent_debug(__LINE__, s);
4030 http_sync_req_state(s);
4031 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004032 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 if (!http_sync_res_state(s))
4034 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004035 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004036 if (!http_sync_req_state(s))
4037 break;
4038 }
4039 http_silent_debug(__LINE__, s);
4040 /* OK, both state machines agree on a compatible state.
4041 * There are a few cases we're interested in :
4042 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4043 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4044 * directions, so let's simply disable both analysers.
4045 * - HTTP_MSG_CLOSED on the response only means we must abort the
4046 * request.
4047 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4048 * with server-close mode means we've completed one request and we
4049 * must re-initialize the server connection.
4050 */
4051
4052 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4053 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4054 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4055 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4056 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004057 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004058 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004060 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004061 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004062 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004063 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4064 txn->rsp.msg_state == HTTP_MSG_ERROR ||
4065 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004066 s->rep->analysers = 0;
4067 buffer_auto_close(s->rep);
4068 buffer_auto_read(s->rep);
4069 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004070 buffer_abort(s->req);
4071 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004072 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074 }
4075 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4076 txn->rsp.msg_state == HTTP_MSG_DONE &&
4077 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4078 /* server-close: terminate this server connection and
4079 * reinitialize a fresh-new transaction.
4080 */
4081 http_end_txn_clean_session(s);
4082 }
4083
4084 http_silent_debug(__LINE__, s);
4085 return txn->req.msg_state != old_req_state ||
4086 txn->rsp.msg_state != old_res_state;
4087}
4088
Willy Tarreaud98cf932009-12-27 22:54:55 +01004089/* This function is an analyser which forwards request body (including chunk
4090 * sizes if any). It is called as soon as we must forward, even if we forward
4091 * zero byte. The only situation where it must not be called is when we're in
4092 * tunnel mode and we want to forward till the close. It's used both to forward
4093 * remaining data and to resync after end of body. It expects the msg_state to
4094 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4095 * read more data, or 1 once we can go on with next request or end the session.
4096 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4097 * bytes of pending data + the headers if not already done (between som and sov).
4098 * It eventually adjusts som to match sov after the data in between have been sent.
4099 */
4100int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4101{
4102 struct http_txn *txn = &s->txn;
4103 struct http_msg *msg = &s->txn.req;
4104
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004105 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4106 return 0;
4107
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004108 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4109 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
4110 /* Output closed while we were sending data. We must abort. */
4111 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004112 buffer_auto_read(req);
4113 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004114 req->analysers &= ~an_bit;
4115 return 1;
4116 }
4117
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004118 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004119
4120 /* Note that we don't have to send 100-continue back because we don't
4121 * need the data to complete our job, and it's up to the server to
4122 * decide whether to return 100, 417 or anything else in return of
4123 * an "Expect: 100-continue" header.
4124 */
4125
4126 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4127 /* we have msg->col and msg->sov which both point to the first
4128 * byte of message body. msg->som still points to the beginning
4129 * of the message. We must save the body in req->lr because it
4130 * survives buffer re-alignments.
4131 */
4132 req->lr = req->data + msg->sov;
4133 if (txn->flags & TX_REQ_TE_CHNK)
4134 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4135 else {
4136 msg->msg_state = HTTP_MSG_DATA;
4137 }
4138 }
4139
Willy Tarreaud98cf932009-12-27 22:54:55 +01004140 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004141 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004142 /* we may have some data pending */
4143 if (msg->hdr_content_len || msg->som != msg->sov) {
4144 int bytes = msg->sov - msg->som;
4145 if (bytes < 0) /* sov may have wrapped at the end */
4146 bytes += req->size;
4147 buffer_forward(req, bytes + msg->hdr_content_len);
4148 msg->hdr_content_len = 0; /* don't forward that again */
4149 msg->som = msg->sov;
4150 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004151
Willy Tarreaucaabe412010-01-03 23:08:28 +01004152 if (msg->msg_state == HTTP_MSG_DATA) {
4153 /* must still forward */
4154 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004155 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004156
4157 /* nothing left to forward */
4158 if (txn->flags & TX_REQ_TE_CHNK)
4159 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004160 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004161 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004162 }
4163 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004164 /* read the chunk size and assign it to ->hdr_content_len, then
4165 * set ->sov and ->lr to point to the body and switch to DATA or
4166 * TRAILERS state.
4167 */
4168 int ret = http_parse_chunk_size(req, msg);
4169
4170 if (!ret)
4171 goto missing_data;
4172 else if (ret < 0)
4173 goto return_bad_req;
4174 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004175 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004176 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4177 /* we want the CRLF after the data */
4178 int ret;
4179
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004180 req->lr = req->w + req->send_max;
4181 if (req->lr >= req->data + req->size)
4182 req->lr -= req->size;
4183
Willy Tarreaud98cf932009-12-27 22:54:55 +01004184 ret = http_skip_chunk_crlf(req, msg);
4185
4186 if (ret == 0)
4187 goto missing_data;
4188 else if (ret < 0)
4189 goto return_bad_req;
4190 /* we're in MSG_CHUNK_SIZE now */
4191 }
4192 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4193 int ret = http_forward_trailers(req, msg);
4194
4195 if (ret == 0)
4196 goto missing_data;
4197 else if (ret < 0)
4198 goto return_bad_req;
4199 /* we're in HTTP_MSG_DONE now */
4200 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 else {
4202 /* other states, DONE...TUNNEL */
4203 if (http_resync_states(s)) {
4204 /* some state changes occurred, maybe the analyser
4205 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004206 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4208 goto return_bad_req;
4209 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004210 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004211 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004212 }
4213 }
4214
Willy Tarreaud98cf932009-12-27 22:54:55 +01004215 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004216 /* stop waiting for data if the input is closed before the end */
4217 if (req->flags & BF_SHUTR)
4218 goto return_bad_req;
4219
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004220 /* waiting for the last bits to leave the buffer */
4221 if (req->flags & BF_SHUTW)
4222 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004223
4224 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004225 return 0;
4226
Willy Tarreaud98cf932009-12-27 22:54:55 +01004227 return_bad_req: /* let's centralize all bad requests */
4228 txn->req.msg_state = HTTP_MSG_ERROR;
4229 txn->status = 400;
4230 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004231 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 +01004232 req->analysers = 0;
4233 s->fe->counters.failed_req++;
4234 if (s->listener->counters)
4235 s->listener->counters->failed_req++;
4236
4237 if (!(s->flags & SN_ERR_MASK))
4238 s->flags |= SN_ERR_PRXCOND;
4239 if (!(s->flags & SN_FINST_MASK))
4240 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004241 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004242 return 0;
4243}
4244
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004245/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4246 * processing can continue on next analysers, or zero if it either needs more
4247 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4248 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4249 * when it has nothing left to do, and may remove any analyser when it wants to
4250 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004251 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004252int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004253{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004254 struct http_txn *txn = &s->txn;
4255 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004256 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004257 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004258 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004259 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004260
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004261 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 +02004262 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004263 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004264 rep,
4265 rep->rex, rep->wex,
4266 rep->flags,
4267 rep->l,
4268 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004269
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004270 /*
4271 * Now parse the partial (or complete) lines.
4272 * We will check the response syntax, and also join multi-line
4273 * headers. An index of all the lines will be elaborated while
4274 * parsing.
4275 *
4276 * For the parsing, we use a 28 states FSM.
4277 *
4278 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004279 * rep->data + msg->som = beginning of response
4280 * rep->data + msg->eoh = end of processed headers / start of current one
4281 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004282 * rep->lr = first non-visited byte
4283 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004284 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004285 */
4286
Willy Tarreau83e3af02009-12-28 17:39:57 +01004287 /* There's a protected area at the end of the buffer for rewriting
4288 * purposes. We don't want to start to parse the request if the
4289 * protected area is affected, because we may have to move processed
4290 * data later, which is much more complicated.
4291 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004292 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4293 if (unlikely((rep->flags & BF_FULL) ||
4294 rep->r < rep->lr ||
4295 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4296 if (rep->send_max) {
4297 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004298 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4299 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004300 buffer_dont_close(rep);
4301 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4302 return 0;
4303 }
4304 if (rep->l <= rep->size - global.tune.maxrewrite)
4305 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004306 }
4307
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004308 if (likely(rep->lr < rep->r))
4309 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004310 }
4311
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004312 /* 1: we might have to print this header in debug mode */
4313 if (unlikely((global.mode & MODE_DEBUG) &&
4314 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004315 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004316 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004317
Willy Tarreau962c3f42010-01-10 00:15:35 +01004318 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004319 eol = sol + msg->sl.rq.l;
4320 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004321
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004322 sol += hdr_idx_first_pos(&txn->hdr_idx);
4323 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004324
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004325 while (cur_idx) {
4326 eol = sol + txn->hdr_idx.v[cur_idx].len;
4327 debug_hdr("srvhdr", s, sol, eol);
4328 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4329 cur_idx = txn->hdr_idx.v[cur_idx].next;
4330 }
4331 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004332
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004333 /*
4334 * Now we quickly check if we have found a full valid response.
4335 * If not so, we check the FD and buffer states before leaving.
4336 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004337 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004338 * responses are checked first.
4339 *
4340 * Depending on whether the client is still there or not, we
4341 * may send an error response back or not. Note that normally
4342 * we should only check for HTTP status there, and check I/O
4343 * errors somewhere else.
4344 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004345
Willy Tarreau655dce92009-11-08 13:10:58 +01004346 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004347 /* Invalid response */
4348 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4349 /* we detected a parsing error. We want to archive this response
4350 * in the dedicated proxy area for later troubleshooting.
4351 */
4352 hdr_response_bad:
4353 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4354 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4355
4356 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004357 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004358 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004359 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4360 }
Willy Tarreau64648412010-03-05 10:41:54 +01004361 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004362 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004363 rep->analysers = 0;
4364 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004365 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004366 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004367 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4368
4369 if (!(s->flags & SN_ERR_MASK))
4370 s->flags |= SN_ERR_PRXCOND;
4371 if (!(s->flags & SN_FINST_MASK))
4372 s->flags |= SN_FINST_H;
4373
4374 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004375 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004376
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004377 /* too large response does not fit in buffer. */
4378 else if (rep->flags & BF_FULL) {
4379 goto hdr_response_bad;
4380 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004381
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004382 /* read error */
4383 else if (rep->flags & BF_READ_ERROR) {
4384 if (msg->err_pos >= 0)
4385 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004386
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004387 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004388 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004389 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004390 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4391 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004392
Willy Tarreau90deb182010-01-07 00:20:41 +01004393 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004394 rep->analysers = 0;
4395 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004396 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004397 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004398 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004399
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004400 if (!(s->flags & SN_ERR_MASK))
4401 s->flags |= SN_ERR_SRVCL;
4402 if (!(s->flags & SN_FINST_MASK))
4403 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004404 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004405 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004406
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004407 /* read timeout : return a 504 to the client. */
4408 else if (rep->flags & BF_READ_TIMEOUT) {
4409 if (msg->err_pos >= 0)
4410 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004411
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004412 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004413 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004414 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004415 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4416 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004417
Willy Tarreau90deb182010-01-07 00:20:41 +01004418 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419 rep->analysers = 0;
4420 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004421 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004422 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004423 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004424
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 if (!(s->flags & SN_ERR_MASK))
4426 s->flags |= SN_ERR_SRVTO;
4427 if (!(s->flags & SN_FINST_MASK))
4428 s->flags |= SN_FINST_H;
4429 return 0;
4430 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004431
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004432 /* close from server */
4433 else if (rep->flags & BF_SHUTR) {
4434 if (msg->err_pos >= 0)
4435 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004436
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004437 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004438 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004439 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004440 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4441 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004442
Willy Tarreau90deb182010-01-07 00:20:41 +01004443 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004444 rep->analysers = 0;
4445 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004446 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004447 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004448 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004449
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 if (!(s->flags & SN_ERR_MASK))
4451 s->flags |= SN_ERR_SRVCL;
4452 if (!(s->flags & SN_FINST_MASK))
4453 s->flags |= SN_FINST_H;
4454 return 0;
4455 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004456
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004457 /* write error to client (we don't send any message then) */
4458 else if (rep->flags & BF_WRITE_ERROR) {
4459 if (msg->err_pos >= 0)
4460 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004461
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004462 s->be->counters.failed_resp++;
4463 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004464 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004465
4466 if (!(s->flags & SN_ERR_MASK))
4467 s->flags |= SN_ERR_CLICL;
4468 if (!(s->flags & SN_FINST_MASK))
4469 s->flags |= SN_FINST_H;
4470
4471 /* process_session() will take care of the error */
4472 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004473 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004474
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004475 buffer_dont_close(rep);
4476 return 0;
4477 }
4478
4479 /* More interesting part now : we know that we have a complete
4480 * response which at least looks like HTTP. We have an indicator
4481 * of each header's length, so we can parse them quickly.
4482 */
4483
4484 if (unlikely(msg->err_pos >= 0))
4485 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4486
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004487 /*
4488 * 1: get the status code
4489 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004490 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004491 if (n < 1 || n > 5)
4492 n = 0;
4493 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494
Willy Tarreau5b154472009-12-21 20:11:07 +01004495 /* check if the response is HTTP/1.1 or above */
4496 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004497 ((msg->sol[5] > '1') ||
4498 ((msg->sol[5] == '1') &&
4499 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004500 txn->flags |= TX_RES_VER_11;
4501
4502 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004503 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 +01004504
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004505 /* transfer length unknown*/
4506 txn->flags &= ~TX_RES_XFER_LEN;
4507
Willy Tarreau962c3f42010-01-10 00:15:35 +01004508 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509
Willy Tarreau39650402010-03-15 19:44:39 +01004510 /* Adjust server's health based on status code. Note: status codes 501
4511 * and 505 are triggered on demand by client request, so we must not
4512 * count them as server failures.
4513 */
4514 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004515 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4516 else
4517 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4518
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519 /*
4520 * 2: check for cacheability.
4521 */
4522
4523 switch (txn->status) {
4524 case 200:
4525 case 203:
4526 case 206:
4527 case 300:
4528 case 301:
4529 case 410:
4530 /* RFC2616 @13.4:
4531 * "A response received with a status code of
4532 * 200, 203, 206, 300, 301 or 410 MAY be stored
4533 * by a cache (...) unless a cache-control
4534 * directive prohibits caching."
4535 *
4536 * RFC2616 @9.5: POST method :
4537 * "Responses to this method are not cacheable,
4538 * unless the response includes appropriate
4539 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004540 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004541 if (likely(txn->meth != HTTP_METH_POST) &&
4542 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4543 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4544 break;
4545 default:
4546 break;
4547 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004548
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 /*
4550 * 3: we may need to capture headers
4551 */
4552 s->logs.logwait &= ~LW_RESP;
4553 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004554 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 txn->rsp.cap, s->fe->rsp_cap);
4556
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004557 /* 4: determine the transfer-length.
4558 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4559 * the presence of a message-body in a RESPONSE and its transfer length
4560 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004561 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004562 * All responses to the HEAD request method MUST NOT include a
4563 * message-body, even though the presence of entity-header fields
4564 * might lead one to believe they do. All 1xx (informational), 204
4565 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4566 * message-body. All other responses do include a message-body,
4567 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004568 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004569 * 1. Any response which "MUST NOT" include a message-body (such as the
4570 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4571 * always terminated by the first empty line after the header fields,
4572 * regardless of the entity-header fields present in the message.
4573 *
4574 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4575 * the "chunked" transfer-coding (Section 6.2) is used, the
4576 * transfer-length is defined by the use of this transfer-coding.
4577 * If a Transfer-Encoding header field is present and the "chunked"
4578 * transfer-coding is not present, the transfer-length is defined by
4579 * the sender closing the connection.
4580 *
4581 * 3. If a Content-Length header field is present, its decimal value in
4582 * OCTETs represents both the entity-length and the transfer-length.
4583 * If a message is received with both a Transfer-Encoding header
4584 * field and a Content-Length header field, the latter MUST be ignored.
4585 *
4586 * 4. If the message uses the media type "multipart/byteranges", and
4587 * the transfer-length is not otherwise specified, then this self-
4588 * delimiting media type defines the transfer-length. This media
4589 * type MUST NOT be used unless the sender knows that the recipient
4590 * can parse it; the presence in a request of a Range header with
4591 * multiple byte-range specifiers from a 1.1 client implies that the
4592 * client can parse multipart/byteranges responses.
4593 *
4594 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004595 */
4596
4597 /* Skip parsing if no content length is possible. The response flags
4598 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004599 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004600 * FIXME: should we parse anyway and return an error on chunked encoding ?
4601 */
4602 if (txn->meth == HTTP_METH_HEAD ||
4603 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004604 txn->status == 204 || txn->status == 304) {
4605 txn->flags |= TX_RES_XFER_LEN;
4606 goto skip_content_length;
4607 }
4608
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004609 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004610 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004611 while ((txn->flags & TX_RES_VER_11) &&
4612 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004613 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4614 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4615 else if (txn->flags & TX_RES_TE_CHNK) {
4616 /* bad transfer-encoding (chunked followed by something else) */
4617 use_close_only = 1;
4618 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4619 break;
4620 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004621 }
4622
4623 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4624 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004625 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004626 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4627 signed long long cl;
4628
4629 if (!ctx.vlen)
4630 goto hdr_response_bad;
4631
4632 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4633 goto hdr_response_bad; /* parse failure */
4634
4635 if (cl < 0)
4636 goto hdr_response_bad;
4637
4638 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4639 goto hdr_response_bad; /* already specified, was different */
4640
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004641 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004642 msg->hdr_content_len = cl;
4643 }
4644
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004645 /* FIXME: we should also implement the multipart/byterange method.
4646 * For now on, we resort to close mode in this case (unknown length).
4647 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004648skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004649
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004650 /* end of job, return OK */
4651 rep->analysers &= ~an_bit;
4652 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004653 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 return 1;
4655}
4656
4657/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004658 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4659 * and updates t->rep->analysers. It might make sense to explode it into several
4660 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 */
4662int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4663{
4664 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665 struct http_msg *msg = &txn->rsp;
4666 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004667 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668
4669 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4670 now_ms, __FUNCTION__,
4671 t,
4672 rep,
4673 rep->rex, rep->wex,
4674 rep->flags,
4675 rep->l,
4676 rep->analysers);
4677
Willy Tarreau655dce92009-11-08 13:10:58 +01004678 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004679 return 0;
4680
4681 rep->analysers &= ~an_bit;
4682 rep->analyse_exp = TICK_ETERNITY;
4683
Willy Tarreau5b154472009-12-21 20:11:07 +01004684 /* Now we have to check if we need to modify the Connection header.
4685 * This is more difficult on the response than it is on the request,
4686 * because we can have two different HTTP versions and we don't know
4687 * how the client will interprete a response. For instance, let's say
4688 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4689 * HTTP/1.1 response without any header. Maybe it will bound itself to
4690 * HTTP/1.0 because it only knows about it, and will consider the lack
4691 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4692 * the lack of header as a keep-alive. Thus we will use two flags
4693 * indicating how a request MAY be understood by the client. In case
4694 * of multiple possibilities, we'll fix the header to be explicit. If
4695 * ambiguous cases such as both close and keepalive are seen, then we
4696 * will fall back to explicit close. Note that we won't take risks with
4697 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004698 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004699 */
4700
Willy Tarreaudc008c52010-02-01 16:20:08 +01004701 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4702 txn->status == 101)) {
4703 /* Either we've established an explicit tunnel, or we're
4704 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004705 * to understand the next protocols. We have to switch to tunnel
4706 * mode, so that we transfer the request and responses then let
4707 * this protocol pass unmodified. When we later implement specific
4708 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004709 * header which contains information about that protocol for
4710 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004711 */
4712 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4713 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004714 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4715 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4716 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004717 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004718
Willy Tarreau60466522010-01-18 19:08:45 +01004719 /* on unknown transfer length, we must close */
4720 if (!(txn->flags & TX_RES_XFER_LEN) &&
4721 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4722 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004723
Willy Tarreau60466522010-01-18 19:08:45 +01004724 /* now adjust header transformations depending on current state */
4725 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4726 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4727 to_del |= 2; /* remove "keep-alive" on any response */
4728 if (!(txn->flags & TX_RES_VER_11))
4729 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004730 }
Willy Tarreau60466522010-01-18 19:08:45 +01004731 else { /* SCL / KAL */
4732 to_del |= 1; /* remove "close" on any response */
4733 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4734 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004735 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004736
Willy Tarreau60466522010-01-18 19:08:45 +01004737 /* Parse and remove some headers from the connection header */
4738 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004739
Willy Tarreau60466522010-01-18 19:08:45 +01004740 /* Some keep-alive responses are converted to Server-close if
4741 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004742 */
Willy Tarreau60466522010-01-18 19:08:45 +01004743 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4744 if ((txn->flags & TX_HDR_CONN_CLO) ||
4745 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4746 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004747 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004748 }
4749
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004750 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004751 /*
4752 * 3: we will have to evaluate the filters.
4753 * As opposed to version 1.2, now they will be evaluated in the
4754 * filters order and not in the header order. This means that
4755 * each filter has to be validated among all headers.
4756 *
4757 * Filters are tried with ->be first, then with ->fe if it is
4758 * different from ->be.
4759 */
4760
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004761 cur_proxy = t->be;
4762 while (1) {
4763 struct proxy *rule_set = cur_proxy;
4764
4765 /* try headers filters */
4766 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004767 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004768 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004769 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004770 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004771 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4772 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004773 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004774 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004775 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004776 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004777 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004778 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004779 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004780 if (!(t->flags & SN_ERR_MASK))
4781 t->flags |= SN_ERR_PRXCOND;
4782 if (!(t->flags & SN_FINST_MASK))
4783 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004784 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004785 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004786 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004787
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004788 /* has the response been denied ? */
4789 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004790 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004791 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004792
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004793 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004794 if (t->listener->counters)
4795 t->listener->counters->denied_resp++;
4796
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004797 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004798 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004799
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004800 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004801 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004802 if (txn->status < 200)
4803 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004804 if (wl->cond) {
4805 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4806 ret = acl_pass(ret);
4807 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4808 ret = !ret;
4809 if (!ret)
4810 continue;
4811 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004812 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004813 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004814 }
4815
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004816 /* check whether we're already working on the frontend */
4817 if (cur_proxy == t->fe)
4818 break;
4819 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004820 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004821
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004822 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004823 * We may be facing a 100-continue response, in which case this
4824 * is not the right response, and we're waiting for the next one.
4825 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004826 * next one.
4827 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004828 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004829 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004830 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004831 msg->msg_state = HTTP_MSG_RPBEFORE;
4832 txn->status = 0;
4833 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4834 return 1;
4835 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004836 else if (unlikely(txn->status < 200))
4837 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004838
4839 /* we don't have any 1xx status code now */
4840
4841 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004842 * 4: check for server cookie.
4843 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004844 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4845 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004846 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004847
Willy Tarreaubaaee002006-06-26 02:48:02 +02004848
Willy Tarreaua15645d2007-03-18 16:22:39 +01004849 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004850 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004851 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004852 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004853 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004854
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004855 /*
4856 * 6: add server cookie in the response if needed
4857 */
4858 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004859 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004860 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004861
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004862 /* the server is known, it's not the one the client requested, we have to
4863 * insert a set-cookie here, except if we want to insert only on POST
4864 * requests and this one isn't. Note that servers which don't have cookies
4865 * (eg: some backup servers) will return a full cookie removal request.
4866 */
4867 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4868 t->be->cookie_name,
4869 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004870
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004871 if (t->be->cookie_domain)
4872 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004873
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004874 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004875 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004876 goto return_bad_resp;
4877 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004878
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004879 /* Here, we will tell an eventual cache on the client side that we don't
4880 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4881 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4882 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4883 */
4884 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004885
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004886 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4887
4888 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004889 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004890 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004891 }
4892 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004893
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004894 /*
4895 * 7: check if result will be cacheable with a cookie.
4896 * We'll block the response if security checks have caught
4897 * nasty things such as a cacheable cookie.
4898 */
4899 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4900 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004901 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004902
4903 /* we're in presence of a cacheable response containing
4904 * a set-cookie header. We'll block it as requested by
4905 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004906 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004907 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004908 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004909
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004910 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004911 if (t->listener->counters)
4912 t->listener->counters->denied_resp++;
4913
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004914 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4915 t->be->id, t->srv?t->srv->id:"<dispatch>");
4916 send_log(t->be, LOG_ALERT,
4917 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4918 t->be->id, t->srv?t->srv->id:"<dispatch>");
4919 goto return_srv_prx_502;
4920 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004921
4922 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004923 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004924 */
Willy Tarreau60466522010-01-18 19:08:45 +01004925 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4926 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4927 unsigned int want_flags = 0;
4928
4929 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4930 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4931 /* we want a keep-alive response here. Keep-alive header
4932 * required if either side is not 1.1.
4933 */
4934 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4935 want_flags |= TX_CON_KAL_SET;
4936 }
4937 else {
4938 /* we want a close response here. Close header required if
4939 * the server is 1.1, regardless of the client.
4940 */
4941 if (txn->flags & TX_RES_VER_11)
4942 want_flags |= TX_CON_CLO_SET;
4943 }
4944
4945 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4946 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004947 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004948
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004949 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01004950 if ((txn->flags & TX_RES_XFER_LEN) ||
4951 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004952 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004953
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004954 /*************************************************************
4955 * OK, that's finished for the headers. We have done what we *
4956 * could. Let's switch to the DATA state. *
4957 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004959 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004960
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004961 /* if the user wants to log as soon as possible, without counting
4962 * bytes from the server, then this is the right moment. We have
4963 * to temporarily assign bytes_out to log what we currently have.
4964 */
4965 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4966 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4967 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004968 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004969 t->logs.bytes_out = 0;
4970 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004971
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004972 /* Note: we must not try to cheat by jumping directly to DATA,
4973 * otherwise we would not let the client side wake up.
4974 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004975
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004976 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004977 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004978 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004979}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004980
Willy Tarreaud98cf932009-12-27 22:54:55 +01004981/* This function is an analyser which forwards response body (including chunk
4982 * sizes if any). It is called as soon as we must forward, even if we forward
4983 * zero byte. The only situation where it must not be called is when we're in
4984 * tunnel mode and we want to forward till the close. It's used both to forward
4985 * remaining data and to resync after end of body. It expects the msg_state to
4986 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4987 * read more data, or 1 once we can go on with next request or end the session.
4988 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4989 * bytes of pending data + the headers if not already done (between som and sov).
4990 * It eventually adjusts som to match sov after the data in between have been sent.
4991 */
4992int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4993{
4994 struct http_txn *txn = &s->txn;
4995 struct http_msg *msg = &s->txn.rsp;
4996
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004997 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4998 return 0;
4999
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005000 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005001 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005002 !s->req->analysers) {
5003 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
5004 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01005005 buffer_auto_read(res);
5006 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005007 res->analysers &= ~an_bit;
5008 return 1;
5009 }
5010
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005011 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005012
Willy Tarreaud98cf932009-12-27 22:54:55 +01005013 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5014 /* we have msg->col and msg->sov which both point to the first
5015 * byte of message body. msg->som still points to the beginning
5016 * of the message. We must save the body in req->lr because it
5017 * survives buffer re-alignments.
5018 */
5019 res->lr = res->data + msg->sov;
5020 if (txn->flags & TX_RES_TE_CHNK)
5021 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5022 else {
5023 msg->msg_state = HTTP_MSG_DATA;
5024 }
5025 }
5026
Willy Tarreaud98cf932009-12-27 22:54:55 +01005027 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005029 /* we may have some data pending */
5030 if (msg->hdr_content_len || msg->som != msg->sov) {
5031 int bytes = msg->sov - msg->som;
5032 if (bytes < 0) /* sov may have wrapped at the end */
5033 bytes += res->size;
5034 buffer_forward(res, bytes + msg->hdr_content_len);
5035 msg->hdr_content_len = 0; /* don't forward that again */
5036 msg->som = msg->sov;
5037 }
5038
Willy Tarreaucaabe412010-01-03 23:08:28 +01005039 if (msg->msg_state == HTTP_MSG_DATA) {
5040 /* must still forward */
5041 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005042 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005043
5044 /* nothing left to forward */
5045 if (txn->flags & TX_RES_TE_CHNK)
5046 msg->msg_state = HTTP_MSG_DATA_CRLF;
5047 else
5048 msg->msg_state = HTTP_MSG_DONE;
5049 }
5050 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005051 /* read the chunk size and assign it to ->hdr_content_len, then
5052 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5053 */
5054 int ret = http_parse_chunk_size(res, msg);
5055
5056 if (!ret)
5057 goto missing_data;
5058 else if (ret < 0)
5059 goto return_bad_res;
5060 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005061 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005062 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5063 /* we want the CRLF after the data */
5064 int ret;
5065
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005066 res->lr = res->w + res->send_max;
5067 if (res->lr >= res->data + res->size)
5068 res->lr -= res->size;
5069
Willy Tarreaud98cf932009-12-27 22:54:55 +01005070 ret = http_skip_chunk_crlf(res, msg);
5071
5072 if (!ret)
5073 goto missing_data;
5074 else if (ret < 0)
5075 goto return_bad_res;
5076 /* we're in MSG_CHUNK_SIZE now */
5077 }
5078 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5079 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005080
Willy Tarreaud98cf932009-12-27 22:54:55 +01005081 if (ret == 0)
5082 goto missing_data;
5083 else if (ret < 0)
5084 goto return_bad_res;
5085 /* we're in HTTP_MSG_DONE now */
5086 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005087 else {
5088 /* other states, DONE...TUNNEL */
5089 if (http_resync_states(s)) {
5090 http_silent_debug(__LINE__, s);
5091 /* some state changes occurred, maybe the analyser
5092 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005093 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005094 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5095 goto return_bad_res;
5096 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005097 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005098 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005099 }
5100 }
5101
Willy Tarreaud98cf932009-12-27 22:54:55 +01005102 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005103 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005104 if (res->flags & BF_SHUTR) {
5105 if (!(s->flags & SN_ERR_MASK))
5106 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005107 s->be->counters.srv_aborts++;
5108 if (s->srv)
5109 s->srv->counters.srv_aborts++;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005110 goto return_bad_res;
Willy Tarreau40dba092010-03-04 18:14:51 +01005111 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005112
Willy Tarreau40dba092010-03-04 18:14:51 +01005113 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114 if (!s->req->analysers)
5115 goto return_bad_res;
5116
Willy Tarreaud98cf932009-12-27 22:54:55 +01005117 /* forward the chunk size as well as any pending data */
5118 if (msg->hdr_content_len || msg->som != msg->sov) {
5119 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5120 msg->hdr_content_len = 0; /* don't forward that again */
5121 msg->som = msg->sov;
5122 }
5123
Willy Tarreaud98cf932009-12-27 22:54:55 +01005124 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005126 return 0;
5127
Willy Tarreau40dba092010-03-04 18:14:51 +01005128 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005129 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005130 /* don't send any error message as we're in the body */
5131 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005132 res->analysers = 0;
5133 s->be->counters.failed_resp++;
5134 if (s->srv) {
5135 s->srv->counters.failed_resp++;
5136 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5137 }
5138
5139 if (!(s->flags & SN_ERR_MASK))
5140 s->flags |= SN_ERR_PRXCOND;
5141 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005142 s->flags |= SN_FINST_D;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005144 return 0;
5145}
5146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005147/* Iterate the same filter through all request headers.
5148 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005149 * Since it can manage the switch to another backend, it updates the per-proxy
5150 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005151 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005152int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005153{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005154 char term;
5155 char *cur_ptr, *cur_end, *cur_next;
5156 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005157 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005158 struct hdr_idx_elem *cur_hdr;
5159 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005160
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005161 last_hdr = 0;
5162
Willy Tarreau962c3f42010-01-10 00:15:35 +01005163 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005164 old_idx = 0;
5165
5166 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005167 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005168 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005169 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005170 (exp->action == ACT_ALLOW ||
5171 exp->action == ACT_DENY ||
5172 exp->action == ACT_TARPIT))
5173 return 0;
5174
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005175 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005176 if (!cur_idx)
5177 break;
5178
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005179 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005180 cur_ptr = cur_next;
5181 cur_end = cur_ptr + cur_hdr->len;
5182 cur_next = cur_end + cur_hdr->cr + 1;
5183
5184 /* Now we have one header between cur_ptr and cur_end,
5185 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005186 */
5187
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005188 /* The annoying part is that pattern matching needs
5189 * that we modify the contents to null-terminate all
5190 * strings before testing them.
5191 */
5192
5193 term = *cur_end;
5194 *cur_end = '\0';
5195
5196 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5197 switch (exp->action) {
5198 case ACT_SETBE:
5199 /* It is not possible to jump a second time.
5200 * FIXME: should we return an HTTP/500 here so that
5201 * the admin knows there's a problem ?
5202 */
5203 if (t->be != t->fe)
5204 break;
5205
5206 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005207 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005208 last_hdr = 1;
5209 break;
5210
5211 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005212 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005213 last_hdr = 1;
5214 break;
5215
5216 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005217 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005218 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005219
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005220 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005221 if (t->listener->counters)
5222 t->listener->counters->denied_resp++;
5223
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005224 break;
5225
5226 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005227 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005228 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005229
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005230 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005231 if (t->listener->counters)
5232 t->listener->counters->denied_resp++;
5233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005234 break;
5235
5236 case ACT_REPLACE:
5237 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5238 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5239 /* FIXME: if the user adds a newline in the replacement, the
5240 * index will not be recalculated for now, and the new line
5241 * will not be counted as a new header.
5242 */
5243
5244 cur_end += delta;
5245 cur_next += delta;
5246 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005247 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005248 break;
5249
5250 case ACT_REMOVE:
5251 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5252 cur_next += delta;
5253
Willy Tarreaufa355d42009-11-29 18:12:29 +01005254 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005255 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5256 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005257 cur_hdr->len = 0;
5258 cur_end = NULL; /* null-term has been rewritten */
5259 break;
5260
5261 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005262 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005263 if (cur_end)
5264 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005266 /* keep the link from this header to next one in case of later
5267 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005268 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005269 old_idx = cur_idx;
5270 }
5271 return 0;
5272}
5273
5274
5275/* Apply the filter to the request line.
5276 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5277 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005278 * Since it can manage the switch to another backend, it updates the per-proxy
5279 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005280 */
5281int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5282{
5283 char term;
5284 char *cur_ptr, *cur_end;
5285 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005286 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005287 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005288
Willy Tarreau58f10d72006-12-04 02:26:12 +01005289
Willy Tarreau3d300592007-03-18 18:34:41 +01005290 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005291 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005292 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005293 (exp->action == ACT_ALLOW ||
5294 exp->action == ACT_DENY ||
5295 exp->action == ACT_TARPIT))
5296 return 0;
5297 else if (exp->action == ACT_REMOVE)
5298 return 0;
5299
5300 done = 0;
5301
Willy Tarreau962c3f42010-01-10 00:15:35 +01005302 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005303 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005304
5305 /* Now we have the request line between cur_ptr and cur_end */
5306
5307 /* The annoying part is that pattern matching needs
5308 * that we modify the contents to null-terminate all
5309 * strings before testing them.
5310 */
5311
5312 term = *cur_end;
5313 *cur_end = '\0';
5314
5315 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5316 switch (exp->action) {
5317 case ACT_SETBE:
5318 /* It is not possible to jump a second time.
5319 * FIXME: should we return an HTTP/500 here so that
5320 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005321 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005322 if (t->be != t->fe)
5323 break;
5324
5325 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005326 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005327 done = 1;
5328 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005330 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005331 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005332 done = 1;
5333 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005334
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005335 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005336 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005337
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005338 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005339 if (t->listener->counters)
5340 t->listener->counters->denied_resp++;
5341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005342 done = 1;
5343 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005345 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005346 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005347
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005348 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005349 if (t->listener->counters)
5350 t->listener->counters->denied_resp++;
5351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005352 done = 1;
5353 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005355 case ACT_REPLACE:
5356 *cur_end = term; /* restore the string terminator */
5357 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5358 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5359 /* FIXME: if the user adds a newline in the replacement, the
5360 * index will not be recalculated for now, and the new line
5361 * will not be counted as a new header.
5362 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005363
Willy Tarreaufa355d42009-11-29 18:12:29 +01005364 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005365 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005366 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005367 HTTP_MSG_RQMETH,
5368 cur_ptr, cur_end + 1,
5369 NULL, NULL);
5370 if (unlikely(!cur_end))
5371 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005373 /* we have a full request and we know that we have either a CR
5374 * or an LF at <ptr>.
5375 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005376 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5377 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005378 /* there is no point trying this regex on headers */
5379 return 1;
5380 }
5381 }
5382 *cur_end = term; /* restore the string terminator */
5383 return done;
5384}
Willy Tarreau97de6242006-12-27 17:18:38 +01005385
Willy Tarreau58f10d72006-12-04 02:26:12 +01005386
Willy Tarreau58f10d72006-12-04 02:26:12 +01005387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005388/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005389 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005390 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005391 * unparsable request. Since it can manage the switch to another backend, it
5392 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005393 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005394int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005395{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005396 struct http_txn *txn = &s->txn;
5397 struct hdr_exp *exp;
5398
5399 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005400 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005402 /*
5403 * The interleaving of transformations and verdicts
5404 * makes it difficult to decide to continue or stop
5405 * the evaluation.
5406 */
5407
Willy Tarreau6c123b12010-01-28 20:22:06 +01005408 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5409 break;
5410
Willy Tarreau3d300592007-03-18 18:34:41 +01005411 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005412 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005413 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005414 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005415
5416 /* if this filter had a condition, evaluate it now and skip to
5417 * next filter if the condition does not match.
5418 */
5419 if (exp->cond) {
5420 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5421 ret = acl_pass(ret);
5422 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5423 ret = !ret;
5424
5425 if (!ret)
5426 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005427 }
5428
5429 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005430 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005431 if (unlikely(ret < 0))
5432 return -1;
5433
5434 if (likely(ret == 0)) {
5435 /* The filter did not match the request, it can be
5436 * iterated through all headers.
5437 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005438 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005439 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005440 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005441 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442}
5443
5444
Willy Tarreaua15645d2007-03-18 16:22:39 +01005445
Willy Tarreau58f10d72006-12-04 02:26:12 +01005446/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005447 * Try to retrieve the server associated to the appsession.
5448 * If the server is found, it's assigned to the session.
5449 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005450void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005451 struct http_txn *txn = &t->txn;
5452 appsess *asession = NULL;
5453 char *sessid_temp = NULL;
5454
Cyril Bontéb21570a2009-11-29 20:04:48 +01005455 if (len > t->be->appsession_len) {
5456 len = t->be->appsession_len;
5457 }
5458
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005459 if (t->be->options2 & PR_O2_AS_REQL) {
5460 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005461 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005462 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005463 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005464 }
5465
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005466 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005467 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5468 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5469 return;
5470 }
5471
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005472 memcpy(txn->sessid, buf, len);
5473 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005474 }
5475
5476 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5477 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5478 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5479 return;
5480 }
5481
Cyril Bontéb21570a2009-11-29 20:04:48 +01005482 memcpy(sessid_temp, buf, len);
5483 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005484
5485 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5486 /* free previously allocated memory */
5487 pool_free2(apools.sessid, sessid_temp);
5488
5489 if (asession != NULL) {
5490 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5491 if (!(t->be->options2 & PR_O2_AS_REQL))
5492 asession->request_count++;
5493
5494 if (asession->serverid != NULL) {
5495 struct server *srv = t->be->srv;
5496 while (srv) {
5497 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005498 if ((srv->state & SRV_RUNNING) ||
5499 (t->be->options & PR_O_PERSIST) ||
5500 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005501 /* we found the server and it's usable */
5502 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005503 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005504 t->flags |= SN_DIRECT | SN_ASSIGNED;
5505 t->srv = srv;
5506 break;
5507 } else {
5508 txn->flags &= ~TX_CK_MASK;
5509 txn->flags |= TX_CK_DOWN;
5510 }
5511 }
5512 srv = srv->next;
5513 }
5514 }
5515 }
5516}
5517
5518/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005519 * Manage client-side cookie. It can impact performance by about 2% so it is
5520 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005521 */
5522void manage_client_side_cookies(struct session *t, struct buffer *req)
5523{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005524 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005525 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005526 char *del_colon, *del_cookie, *colon;
5527 int app_cookies;
5528
Willy Tarreau58f10d72006-12-04 02:26:12 +01005529 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005530 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005531
Willy Tarreau2a324282006-12-05 00:05:46 +01005532 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005533 * we start with the start line.
5534 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005535 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005536 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005537
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005538 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005539 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005540 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005541
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005542 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005543 cur_ptr = cur_next;
5544 cur_end = cur_ptr + cur_hdr->len;
5545 cur_next = cur_end + cur_hdr->cr + 1;
5546
5547 /* We have one full header between cur_ptr and cur_end, and the
5548 * next header starts at cur_next. We're only interested in
5549 * "Cookie:" headers.
5550 */
5551
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005552 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5553 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005554 old_idx = cur_idx;
5555 continue;
5556 }
5557
5558 /* Now look for cookies. Conforming to RFC2109, we have to support
5559 * attributes whose name begin with a '$', and associate them with
5560 * the right cookie, if we want to delete this cookie.
5561 * So there are 3 cases for each cookie read :
5562 * 1) it's a special attribute, beginning with a '$' : ignore it.
5563 * 2) it's a server id cookie that we *MAY* want to delete : save
5564 * some pointers on it (last semi-colon, beginning of cookie...)
5565 * 3) it's an application cookie : we *MAY* have to delete a previous
5566 * "special" cookie.
5567 * At the end of loop, if a "special" cookie remains, we may have to
5568 * remove it. If no application cookie persists in the header, we
5569 * *MUST* delete it
5570 */
5571
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005572 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005573
Willy Tarreau58f10d72006-12-04 02:26:12 +01005574 /* del_cookie == NULL => nothing to be deleted */
5575 del_colon = del_cookie = NULL;
5576 app_cookies = 0;
5577
5578 while (p1 < cur_end) {
5579 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005580 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005581 while (p1 < cur_end) {
5582 if (*p1 == ';' || *p1 == ',')
5583 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005584 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005585 break;
5586 p1++;
5587 }
5588
5589 if (p1 == cur_end)
5590 break;
5591
5592 /* p1 is at the beginning of the cookie name */
5593 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005594 while (p2 < cur_end && *p2 != '=') {
5595 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5596 /* oops, the cookie name was truncated, resync */
5597 p1 = p2;
5598 goto resync_name;
5599 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005600 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005601 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005602
5603 if (p2 == cur_end)
5604 break;
5605
5606 p3 = p2 + 1; /* skips the '=' sign */
5607 if (p3 == cur_end)
5608 break;
5609
Willy Tarreau305ae852010-01-03 19:45:54 +01005610 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5611 p5 = p4 = p3;
5612 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5613 if (!isspace((unsigned char)*p5))
5614 p4 = p5 + 1;
5615 p5++;
5616 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005617
5618 /* here, we have the cookie name between p1 and p2,
5619 * and its value between p3 and p4.
5620 * we can process it :
5621 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005622 * Cookie: NAME=VALUE ;
5623 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005624 * | || || +--> p4
5625 * | || |+-------> p3
5626 * | || +--------> p2
5627 * | |+------------> p1
5628 * | +-------------> colon
5629 * +--------------------> cur_ptr
5630 */
5631
5632 if (*p1 == '$') {
5633 /* skip this one */
5634 }
5635 else {
5636 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005637 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005638 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005639 (p4 - p1 >= t->fe->capture_namelen) &&
5640 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005641 int log_len = p4 - p1;
5642
Willy Tarreau086b3b42007-05-13 21:45:51 +02005643 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005644 Alert("HTTP logging : out of memory.\n");
5645 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005646 if (log_len > t->fe->capture_len)
5647 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005648 memcpy(txn->cli_cookie, p1, log_len);
5649 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005650 }
5651 }
5652
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005653 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5654 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005655 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005656 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005657 char *delim;
5658
5659 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5660 * have the server ID betweek p3 and delim, and the original cookie between
5661 * delim+1 and p4. Otherwise, delim==p4 :
5662 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005663 * Cookie: NAME=SRV~VALUE ;
5664 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005665 * | || || | +--> p4
5666 * | || || +--------> delim
5667 * | || |+-----------> p3
5668 * | || +------------> p2
5669 * | |+----------------> p1
5670 * | +-----------------> colon
5671 * +------------------------> cur_ptr
5672 */
5673
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005674 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005675 for (delim = p3; delim < p4; delim++)
5676 if (*delim == COOKIE_DELIM)
5677 break;
5678 }
5679 else
5680 delim = p4;
5681
5682
5683 /* Here, we'll look for the first running server which supports the cookie.
5684 * This allows to share a same cookie between several servers, for example
5685 * to dedicate backup servers to specific servers only.
5686 * However, to prevent clients from sticking to cookie-less backup server
5687 * when they have incidentely learned an empty cookie, we simply ignore
5688 * empty cookies and mark them as invalid.
5689 */
5690 if (delim == p3)
5691 srv = NULL;
5692
5693 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005694 if (srv->cookie && (srv->cklen == delim - p3) &&
5695 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005696 if ((srv->state & SRV_RUNNING) ||
5697 (t->be->options & PR_O_PERSIST) ||
5698 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005699 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005700 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005701 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005702 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005703 t->srv = srv;
5704 break;
5705 } else {
5706 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005707 txn->flags &= ~TX_CK_MASK;
5708 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005709 }
5710 }
5711 srv = srv->next;
5712 }
5713
Willy Tarreau3d300592007-03-18 18:34:41 +01005714 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005715 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005716 txn->flags &= ~TX_CK_MASK;
5717 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005718 }
5719
5720 /* depending on the cookie mode, we may have to either :
5721 * - delete the complete cookie if we're in insert+indirect mode, so that
5722 * the server never sees it ;
5723 * - remove the server id from the cookie value, and tag the cookie as an
5724 * application cookie so that it does not get accidentely removed later,
5725 * if we're in cookie prefix mode
5726 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005727 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005728 int delta; /* negative */
5729
5730 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5731 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005732 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005733 cur_end += delta;
5734 cur_next += delta;
5735 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005736 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005737
5738 del_cookie = del_colon = NULL;
5739 app_cookies++; /* protect the header from deletion */
5740 }
5741 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005742 (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 +01005743 del_cookie = p1;
5744 del_colon = colon;
5745 }
5746 } else {
5747 /* now we know that we must keep this cookie since it's
5748 * not ours. But if we wanted to delete our cookie
5749 * earlier, we cannot remove the complete header, but we
5750 * can remove the previous block itself.
5751 */
5752 app_cookies++;
5753
5754 if (del_cookie != NULL) {
5755 int delta; /* negative */
5756
5757 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5758 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005759 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005760 cur_end += delta;
5761 cur_next += delta;
5762 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005763 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005764 del_cookie = del_colon = NULL;
5765 }
5766 }
5767
Cyril Bontéb21570a2009-11-29 20:04:48 +01005768 if (t->be->appsession_name != NULL) {
5769 int cmp_len, value_len;
5770 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005771
Cyril Bontéb21570a2009-11-29 20:04:48 +01005772 if (t->be->options2 & PR_O2_AS_PFX) {
5773 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5774 value_begin = p1 + t->be->appsession_name_len;
5775 value_len = p4 - p1 - t->be->appsession_name_len;
5776 } else {
5777 cmp_len = p2 - p1;
5778 value_begin = p3;
5779 value_len = p4 - p3;
5780 }
5781
5782 /* let's see if the cookie is our appcookie */
5783 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5784 /* Cool... it's the right one */
5785 manage_client_side_appsession(t, value_begin, value_len);
5786 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005787#if defined(DEBUG_HASH)
5788 Alert("manage_client_side_cookies\n");
5789 appsession_hash_dump(&(t->be->htbl_proxy));
5790#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005791 }/* end if ((t->proxy->appsession_name != NULL) ... */
5792 }
5793
5794 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005795 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005796 } /* while (p1 < cur_end) */
5797
5798 /* There's no more cookie on this line.
5799 * We may have marked the last one(s) for deletion.
5800 * We must do this now in two ways :
5801 * - if there is no app cookie, we simply delete the header ;
5802 * - if there are app cookies, we must delete the end of the
5803 * string properly, including the colon/semi-colon before
5804 * the cookie name.
5805 */
5806 if (del_cookie != NULL) {
5807 int delta;
5808 if (app_cookies) {
5809 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5810 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005811 cur_hdr->len += delta;
5812 } else {
5813 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005814
5815 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005816 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5817 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005818 cur_hdr->len = 0;
5819 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005820 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005821 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005822 }
5823
5824 /* keep the link from this header to next one */
5825 old_idx = cur_idx;
5826 } /* end of cookie processing on this header */
5827}
5828
5829
Willy Tarreaua15645d2007-03-18 16:22:39 +01005830/* Iterate the same filter through all response headers contained in <rtr>.
5831 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5832 */
5833int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5834{
5835 char term;
5836 char *cur_ptr, *cur_end, *cur_next;
5837 int cur_idx, old_idx, last_hdr;
5838 struct http_txn *txn = &t->txn;
5839 struct hdr_idx_elem *cur_hdr;
5840 int len, delta;
5841
5842 last_hdr = 0;
5843
Willy Tarreau962c3f42010-01-10 00:15:35 +01005844 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005845 old_idx = 0;
5846
5847 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005848 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005849 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005850 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005851 (exp->action == ACT_ALLOW ||
5852 exp->action == ACT_DENY))
5853 return 0;
5854
5855 cur_idx = txn->hdr_idx.v[old_idx].next;
5856 if (!cur_idx)
5857 break;
5858
5859 cur_hdr = &txn->hdr_idx.v[cur_idx];
5860 cur_ptr = cur_next;
5861 cur_end = cur_ptr + cur_hdr->len;
5862 cur_next = cur_end + cur_hdr->cr + 1;
5863
5864 /* Now we have one header between cur_ptr and cur_end,
5865 * and the next header starts at cur_next.
5866 */
5867
5868 /* The annoying part is that pattern matching needs
5869 * that we modify the contents to null-terminate all
5870 * strings before testing them.
5871 */
5872
5873 term = *cur_end;
5874 *cur_end = '\0';
5875
5876 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5877 switch (exp->action) {
5878 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005879 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005880 last_hdr = 1;
5881 break;
5882
5883 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005884 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005885 last_hdr = 1;
5886 break;
5887
5888 case ACT_REPLACE:
5889 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5890 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5891 /* FIXME: if the user adds a newline in the replacement, the
5892 * index will not be recalculated for now, and the new line
5893 * will not be counted as a new header.
5894 */
5895
5896 cur_end += delta;
5897 cur_next += delta;
5898 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005899 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005900 break;
5901
5902 case ACT_REMOVE:
5903 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5904 cur_next += delta;
5905
Willy Tarreaufa355d42009-11-29 18:12:29 +01005906 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005907 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5908 txn->hdr_idx.used--;
5909 cur_hdr->len = 0;
5910 cur_end = NULL; /* null-term has been rewritten */
5911 break;
5912
5913 }
5914 }
5915 if (cur_end)
5916 *cur_end = term; /* restore the string terminator */
5917
5918 /* keep the link from this header to next one in case of later
5919 * removal of next header.
5920 */
5921 old_idx = cur_idx;
5922 }
5923 return 0;
5924}
5925
5926
5927/* Apply the filter to the status line in the response buffer <rtr>.
5928 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5929 * or -1 if a replacement resulted in an invalid status line.
5930 */
5931int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5932{
5933 char term;
5934 char *cur_ptr, *cur_end;
5935 int done;
5936 struct http_txn *txn = &t->txn;
5937 int len, delta;
5938
5939
Willy Tarreau3d300592007-03-18 18:34:41 +01005940 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005941 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005942 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005943 (exp->action == ACT_ALLOW ||
5944 exp->action == ACT_DENY))
5945 return 0;
5946 else if (exp->action == ACT_REMOVE)
5947 return 0;
5948
5949 done = 0;
5950
Willy Tarreau962c3f42010-01-10 00:15:35 +01005951 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005952 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5953
5954 /* Now we have the status line between cur_ptr and cur_end */
5955
5956 /* The annoying part is that pattern matching needs
5957 * that we modify the contents to null-terminate all
5958 * strings before testing them.
5959 */
5960
5961 term = *cur_end;
5962 *cur_end = '\0';
5963
5964 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5965 switch (exp->action) {
5966 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005967 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005968 done = 1;
5969 break;
5970
5971 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005972 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005973 done = 1;
5974 break;
5975
5976 case ACT_REPLACE:
5977 *cur_end = term; /* restore the string terminator */
5978 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5979 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5980 /* FIXME: if the user adds a newline in the replacement, the
5981 * index will not be recalculated for now, and the new line
5982 * will not be counted as a new header.
5983 */
5984
Willy Tarreaufa355d42009-11-29 18:12:29 +01005985 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005986 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005987 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005988 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005989 cur_ptr, cur_end + 1,
5990 NULL, NULL);
5991 if (unlikely(!cur_end))
5992 return -1;
5993
5994 /* we have a full respnse and we know that we have either a CR
5995 * or an LF at <ptr>.
5996 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01005997 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005998 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5999 /* there is no point trying this regex on headers */
6000 return 1;
6001 }
6002 }
6003 *cur_end = term; /* restore the string terminator */
6004 return done;
6005}
6006
6007
6008
6009/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006010 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006011 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6012 * unparsable response.
6013 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006014int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006015{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006016 struct http_txn *txn = &s->txn;
6017 struct hdr_exp *exp;
6018
6019 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006020 int ret;
6021
6022 /*
6023 * The interleaving of transformations and verdicts
6024 * makes it difficult to decide to continue or stop
6025 * the evaluation.
6026 */
6027
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006028 if (txn->flags & TX_SVDENY)
6029 break;
6030
Willy Tarreau3d300592007-03-18 18:34:41 +01006031 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006032 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6033 exp->action == ACT_PASS)) {
6034 exp = exp->next;
6035 continue;
6036 }
6037
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006038 /* if this filter had a condition, evaluate it now and skip to
6039 * next filter if the condition does not match.
6040 */
6041 if (exp->cond) {
6042 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6043 ret = acl_pass(ret);
6044 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6045 ret = !ret;
6046 if (!ret)
6047 continue;
6048 }
6049
Willy Tarreaua15645d2007-03-18 16:22:39 +01006050 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006051 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006052 if (unlikely(ret < 0))
6053 return -1;
6054
6055 if (likely(ret == 0)) {
6056 /* The filter did not match the response, it can be
6057 * iterated through all headers.
6058 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006059 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006060 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006061 }
6062 return 0;
6063}
6064
6065
6066
6067/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006068 * Manage server-side cookies. It can impact performance by about 2% so it is
6069 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006070 */
6071void manage_server_side_cookies(struct session *t, struct buffer *rtr)
6072{
6073 struct http_txn *txn = &t->txn;
6074 char *p1, *p2, *p3, *p4;
6075
Willy Tarreaua15645d2007-03-18 16:22:39 +01006076 char *cur_ptr, *cur_end, *cur_next;
6077 int cur_idx, old_idx, delta;
6078
Willy Tarreaua15645d2007-03-18 16:22:39 +01006079 /* Iterate through the headers.
6080 * we start with the start line.
6081 */
6082 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006083 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006084
6085 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6086 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006087 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006088
6089 cur_hdr = &txn->hdr_idx.v[cur_idx];
6090 cur_ptr = cur_next;
6091 cur_end = cur_ptr + cur_hdr->len;
6092 cur_next = cur_end + cur_hdr->cr + 1;
6093
6094 /* We have one full header between cur_ptr and cur_end, and the
6095 * next header starts at cur_next. We're only interested in
6096 * "Cookie:" headers.
6097 */
6098
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006099 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
6100 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006101 old_idx = cur_idx;
6102 continue;
6103 }
6104
6105 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01006106 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006107
6108
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006109 /* maybe we only wanted to see if there was a set-cookie. Note that
6110 * the cookie capture is declared in the fronend.
6111 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006112 if (t->be->cookie_name == NULL &&
6113 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006114 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006115 return;
6116
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006117 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006118
6119 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006120 if (p1 == cur_end || *p1 == ';') /* end of cookie */
6121 break;
6122
6123 /* p1 is at the beginning of the cookie name */
6124 p2 = p1;
6125
6126 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
6127 p2++;
6128
6129 if (p2 == cur_end || *p2 == ';') /* next cookie */
6130 break;
6131
6132 p3 = p2 + 1; /* skip the '=' sign */
6133 if (p3 == cur_end)
6134 break;
6135
6136 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006137 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01006138 p4++;
6139
6140 /* here, we have the cookie name between p1 and p2,
6141 * and its value between p3 and p4.
6142 * we can process it.
6143 */
6144
6145 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006146 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006147 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006148 (p4 - p1 >= t->fe->capture_namelen) &&
6149 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006150 int log_len = p4 - p1;
6151
Willy Tarreau086b3b42007-05-13 21:45:51 +02006152 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006153 Alert("HTTP logging : out of memory.\n");
6154 }
6155
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006156 if (log_len > t->fe->capture_len)
6157 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006158 memcpy(txn->srv_cookie, p1, log_len);
6159 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006160 }
6161
6162 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006163 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6164 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006165 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01006166 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006167
6168 /* If the cookie is in insert mode on a known server, we'll delete
6169 * this occurrence because we'll insert another one later.
6170 * We'll delete it too if the "indirect" option is set and we're in
6171 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006172 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
6173 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006174 /* this header must be deleted */
6175 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6176 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6177 txn->hdr_idx.used--;
6178 cur_hdr->len = 0;
6179 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006180 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006181
Willy Tarreau3d300592007-03-18 18:34:41 +01006182 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006183 }
6184 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006185 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006186 /* replace bytes p3->p4 with the cookie name associated
6187 * with this server since we know it.
6188 */
6189 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
6190 cur_hdr->len += delta;
6191 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006192 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006193
Willy Tarreau3d300592007-03-18 18:34:41 +01006194 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006195 }
6196 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006197 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006198 /* insert the cookie name associated with this server
6199 * before existing cookie, and insert a delimitor between them..
6200 */
6201 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6202 cur_hdr->len += delta;
6203 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006204 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006205
6206 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006207 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006208 }
6209 }
6210 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006211 else if (t->be->appsession_name != NULL) {
6212 int cmp_len, value_len;
6213 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006214
Cyril Bontéb21570a2009-11-29 20:04:48 +01006215 if (t->be->options2 & PR_O2_AS_PFX) {
6216 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6217 value_begin = p1 + t->be->appsession_name_len;
6218 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6219 } else {
6220 cmp_len = p2 - p1;
6221 value_begin = p3;
6222 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006223 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006224
6225 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
6226 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006227 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006228 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006229 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006230 }
6231 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006232 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006233 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6234 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6235 return;
6236 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006237 memcpy(txn->sessid, value_begin, value_len);
6238 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006239 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006240 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006241 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6242 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006243 /* keep the link from this header to next one */
6244 old_idx = cur_idx;
6245 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006246
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006247 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006248 appsess *asession = NULL;
6249 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006250 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006251 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006252 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006253 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6254 Alert("Not enough Memory process_srv():asession:calloc().\n");
6255 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6256 return;
6257 }
6258 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6259 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6260 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006261 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006262 return;
6263 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006264 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006265 asession->sessid[t->be->appsession_len] = 0;
6266
Willy Tarreau1fac7532010-01-09 19:23:06 +01006267 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006268 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6269 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6270 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006271 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006272 return;
6273 }
6274 asession->serverid[0] = '\0';
6275 memcpy(asession->serverid, t->srv->id, server_id_len);
6276
6277 asession->request_count = 0;
6278 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6279 }
6280
6281 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6282 asession->request_count++;
6283 }
6284
6285#if defined(DEBUG_HASH)
6286 Alert("manage_server_side_cookies\n");
6287 appsession_hash_dump(&(t->be->htbl_proxy));
6288#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006289}
6290
6291
6292
6293/*
6294 * Check if response is cacheable or not. Updates t->flags.
6295 */
6296void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6297{
6298 struct http_txn *txn = &t->txn;
6299 char *p1, *p2;
6300
6301 char *cur_ptr, *cur_end, *cur_next;
6302 int cur_idx;
6303
Willy Tarreau5df51872007-11-25 16:20:08 +01006304 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006305 return;
6306
6307 /* Iterate through the headers.
6308 * we start with the start line.
6309 */
6310 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006311 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006312
6313 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6314 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006315 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006316
6317 cur_hdr = &txn->hdr_idx.v[cur_idx];
6318 cur_ptr = cur_next;
6319 cur_end = cur_ptr + cur_hdr->len;
6320 cur_next = cur_end + cur_hdr->cr + 1;
6321
6322 /* We have one full header between cur_ptr and cur_end, and the
6323 * next header starts at cur_next. We're only interested in
6324 * "Cookie:" headers.
6325 */
6326
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006327 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6328 if (val) {
6329 if ((cur_end - (cur_ptr + val) >= 8) &&
6330 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6331 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6332 return;
6333 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006334 }
6335
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006336 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6337 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006338 continue;
6339
6340 /* OK, right now we know we have a cache-control header at cur_ptr */
6341
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006342 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006343
6344 if (p1 >= cur_end) /* no more info */
6345 continue;
6346
6347 /* p1 is at the beginning of the value */
6348 p2 = p1;
6349
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006350 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006351 p2++;
6352
6353 /* we have a complete value between p1 and p2 */
6354 if (p2 < cur_end && *p2 == '=') {
6355 /* we have something of the form no-cache="set-cookie" */
6356 if ((cur_end - p1 >= 21) &&
6357 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6358 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006359 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006360 continue;
6361 }
6362
6363 /* OK, so we know that either p2 points to the end of string or to a comma */
6364 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6365 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6366 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6367 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006368 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006369 return;
6370 }
6371
6372 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006373 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006374 continue;
6375 }
6376 }
6377}
6378
6379
Willy Tarreau58f10d72006-12-04 02:26:12 +01006380/*
6381 * Try to retrieve a known appsession in the URI, then the associated server.
6382 * If the server is found, it's assigned to the session.
6383 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006384void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006385{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006386 char *end_params, *first_param, *cur_param, *next_param;
6387 char separator;
6388 int value_len;
6389
6390 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006392 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01006393 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006394 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006395 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396
Cyril Bontéb21570a2009-11-29 20:04:48 +01006397 first_param = NULL;
6398 switch (mode) {
6399 case PR_O2_AS_M_PP:
6400 first_param = memchr(begin, ';', len);
6401 break;
6402 case PR_O2_AS_M_QS:
6403 first_param = memchr(begin, '?', len);
6404 break;
6405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006406
Cyril Bontéb21570a2009-11-29 20:04:48 +01006407 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006410
Cyril Bontéb21570a2009-11-29 20:04:48 +01006411 switch (mode) {
6412 case PR_O2_AS_M_PP:
6413 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6414 end_params = (char *) begin + len;
6415 }
6416 separator = ';';
6417 break;
6418 case PR_O2_AS_M_QS:
6419 end_params = (char *) begin + len;
6420 separator = '&';
6421 break;
6422 default:
6423 /* unknown mode, shouldn't happen */
6424 return;
6425 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006426
Cyril Bontéb21570a2009-11-29 20:04:48 +01006427 cur_param = next_param = end_params;
6428 while (cur_param > first_param) {
6429 cur_param--;
6430 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6431 /* let's see if this is the appsession parameter */
6432 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6433 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6434 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6435 /* Cool... it's the right one */
6436 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6437 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6438 if (value_len > 0) {
6439 manage_client_side_appsession(t, cur_param, value_len);
6440 }
6441 break;
6442 }
6443 next_param = cur_param;
6444 }
6445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006447 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006448 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006449#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006450}
6451
Willy Tarreaub2513902006-12-17 14:52:38 +01006452/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006453 * In a GET or HEAD request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006454 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01006455 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006456 * It is assumed that the request is either a HEAD or GET and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006457 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006458 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006459 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01006460 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006461int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01006462{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006463 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006464 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006466
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006467 if (!uri_auth)
6468 return 0;
6469
6470 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)
6471 return 0;
6472
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006473 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006476 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006477 return 0;
6478
Willy Tarreau962c3f42010-01-10 00:15:35 +01006479 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006481 /* the URI is in h */
6482 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006483 return 0;
6484
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006485 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006486 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006487 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006488 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006489 break;
6490 }
6491 h++;
6492 }
6493
6494 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006495 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6496 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006497 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006498 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006499 break;
6500 }
6501 h++;
6502 }
6503 }
6504
Willy Tarreau962c3f42010-01-10 00:15:35 +01006505 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6506 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006507 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006508 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006509 break;
6510 }
6511 h++;
6512 }
6513
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006514 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6515
Willy Tarreaub2513902006-12-17 14:52:38 +01006516 return 1;
6517}
6518
Willy Tarreau4076a152009-04-02 15:18:36 +02006519/*
6520 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006521 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6522 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006523 */
6524void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6525 struct buffer *buf, struct http_msg *msg,
6526 struct proxy *other_end)
6527{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006528 es->len = buf->r - (buf->data + msg->som);
6529 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006530 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006531 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006532 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006533 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006534 es->when = date; // user-visible date
6535 es->sid = s->uniq_id;
6536 es->srv = s->srv;
6537 es->oe = other_end;
6538 es->src = s->cli_addr;
6539}
Willy Tarreaub2513902006-12-17 14:52:38 +01006540
Willy Tarreaubce70882009-09-07 11:51:47 +02006541/* return the IP address pointed to by occurrence <occ> of header <hname> in
6542 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
6543 * occurrence number corresponding to this value is returned. If <occ> is
6544 * strictly negative, the occurrence number before the end corresponding to
6545 * this value is returned. If <occ> is null, any value is returned, so it is
6546 * not recommended to use it that way. Negative occurrences are limited to
6547 * a small value because it is required to keep them in memory while scanning.
6548 * IP address 0.0.0.0 is returned if no match is found.
6549 */
6550unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
6551{
6552 struct hdr_ctx ctx;
6553 unsigned int hdr_hist[MAX_HDR_HISTORY];
6554 unsigned int hist_ptr;
6555 int found = 0;
6556
6557 ctx.idx = 0;
6558 if (occ >= 0) {
6559 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6560 occ--;
6561 if (occ <= 0) {
6562 found = 1;
6563 break;
6564 }
6565 }
6566 if (!found)
6567 return 0;
6568 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6569 }
6570
6571 /* negative occurrence, we scan all the list then walk back */
6572 if (-occ > MAX_HDR_HISTORY)
6573 return 0;
6574
6575 hist_ptr = 0;
6576 hdr_hist[hist_ptr] = 0;
6577 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6578 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6579 if (hist_ptr >= MAX_HDR_HISTORY)
6580 hist_ptr = 0;
6581 found++;
6582 }
6583 if (-occ > found)
6584 return 0;
6585 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
6586 * find occurrence -occ, so we have to check [hist_ptr+occ].
6587 */
6588 hist_ptr += occ;
6589 if (hist_ptr >= MAX_HDR_HISTORY)
6590 hist_ptr -= MAX_HDR_HISTORY;
6591 return hdr_hist[hist_ptr];
6592}
6593
Willy Tarreaubaaee002006-06-26 02:48:02 +02006594/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006595 * Print a debug line with a header
6596 */
6597void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6598{
6599 int len, max;
6600 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006601 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006602 max = end - start;
6603 UBOUND(max, sizeof(trash) - len - 1);
6604 len += strlcpy2(trash + len, start, max + 1);
6605 trash[len++] = '\n';
6606 write(1, trash, len);
6607}
6608
Willy Tarreau0937bc42009-12-22 15:03:09 +01006609/*
6610 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6611 * the required fields are properly allocated and that we only need to (re)init
6612 * them. This should be used before processing any new request.
6613 */
6614void http_init_txn(struct session *s)
6615{
6616 struct http_txn *txn = &s->txn;
6617 struct proxy *fe = s->fe;
6618
6619 txn->flags = 0;
6620 txn->status = -1;
6621
6622 txn->req.sol = txn->req.eol = NULL;
6623 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6624 txn->rsp.sol = txn->rsp.eol = NULL;
6625 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6626 txn->req.hdr_content_len = 0LL;
6627 txn->rsp.hdr_content_len = 0LL;
6628 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6629 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006630
6631 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006632
6633 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6634 if (fe->options2 & PR_O2_REQBUG_OK)
6635 txn->req.err_pos = -1; /* let buggy requests pass */
6636
Willy Tarreau46023632010-01-07 22:51:47 +01006637 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006638 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6639
Willy Tarreau46023632010-01-07 22:51:47 +01006640 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006641 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6642
6643 if (txn->hdr_idx.v)
6644 hdr_idx_init(&txn->hdr_idx);
6645}
6646
6647/* to be used at the end of a transaction */
6648void http_end_txn(struct session *s)
6649{
6650 struct http_txn *txn = &s->txn;
6651
6652 /* these ones will have been dynamically allocated */
6653 pool_free2(pool2_requri, txn->uri);
6654 pool_free2(pool2_capture, txn->cli_cookie);
6655 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006656 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006657
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006658 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006659 txn->uri = NULL;
6660 txn->srv_cookie = NULL;
6661 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006662
6663 if (txn->req.cap) {
6664 struct cap_hdr *h;
6665 for (h = s->fe->req_cap; h; h = h->next)
6666 pool_free2(h->pool, txn->req.cap[h->index]);
6667 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6668 }
6669
6670 if (txn->rsp.cap) {
6671 struct cap_hdr *h;
6672 for (h = s->fe->rsp_cap; h; h = h->next)
6673 pool_free2(h->pool, txn->rsp.cap[h->index]);
6674 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6675 }
6676
Willy Tarreau0937bc42009-12-22 15:03:09 +01006677}
6678
6679/* to be used at the end of a transaction to prepare a new one */
6680void http_reset_txn(struct session *s)
6681{
6682 http_end_txn(s);
6683 http_init_txn(s);
6684
6685 s->be = s->fe;
6686 s->req->analysers = s->listener->analysers;
6687 s->logs.logwait = s->fe->to_log;
6688 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006689 /* re-init store persistence */
6690 s->store_count = 0;
6691
Willy Tarreau0937bc42009-12-22 15:03:09 +01006692 s->pend_pos = NULL;
6693 s->conn_retries = s->be->conn_retries;
6694
6695 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6696
Willy Tarreau739cfba2010-01-25 23:11:14 +01006697 /* We must trim any excess data from the response buffer, because we
6698 * may have blocked an invalid response from a server that we don't
6699 * want to accidentely forward once we disable the analysers, nor do
6700 * we want those data to come along with next response. A typical
6701 * example of such data would be from a buggy server responding to
6702 * a HEAD with some data, or sending more than the advertised
6703 * content-length.
6704 */
6705 if (unlikely(s->rep->l > s->rep->send_max)) {
6706 s->rep->l = s->rep->send_max;
6707 s->rep->r = s->rep->w + s->rep->l;
6708 if (s->rep->r >= s->rep->data + s->rep->size)
6709 s->rep->r -= s->rep->size;
6710 }
6711
Willy Tarreau0937bc42009-12-22 15:03:09 +01006712 s->req->rto = s->fe->timeout.client;
6713 s->req->wto = s->be->timeout.server;
6714 s->req->cto = s->be->timeout.connect;
6715
6716 s->rep->rto = s->be->timeout.server;
6717 s->rep->wto = s->fe->timeout.client;
6718 s->rep->cto = TICK_ETERNITY;
6719
6720 s->req->rex = TICK_ETERNITY;
6721 s->req->wex = TICK_ETERNITY;
6722 s->req->analyse_exp = TICK_ETERNITY;
6723 s->rep->rex = TICK_ETERNITY;
6724 s->rep->wex = TICK_ETERNITY;
6725 s->rep->analyse_exp = TICK_ETERNITY;
6726}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006727
Willy Tarreau8797c062007-05-07 00:55:35 +02006728/************************************************************************/
6729/* The code below is dedicated to ACL parsing and matching */
6730/************************************************************************/
6731
6732
6733
6734
6735/* 1. Check on METHOD
6736 * We use the pre-parsed method if it is known, and store its number as an
6737 * integer. If it is unknown, we use the pointer and the length.
6738 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006739static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006740{
6741 int len, meth;
6742
Willy Tarreauae8b7962007-06-09 23:10:04 +02006743 len = strlen(*text);
6744 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006745
6746 pattern->val.i = meth;
6747 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006748 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006749 if (!pattern->ptr.str)
6750 return 0;
6751 pattern->len = len;
6752 }
6753 return 1;
6754}
6755
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006756static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006757acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6758 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006759{
6760 int meth;
6761 struct http_txn *txn = l7;
6762
Willy Tarreaub6866442008-07-14 23:54:42 +02006763 if (!txn)
6764 return 0;
6765
Willy Tarreau655dce92009-11-08 13:10:58 +01006766 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006767 return 0;
6768
Willy Tarreau8797c062007-05-07 00:55:35 +02006769 meth = txn->meth;
6770 test->i = meth;
6771 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006772 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6773 /* ensure the indexes are not affected */
6774 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006775 test->len = txn->req.sl.rq.m_l;
6776 test->ptr = txn->req.sol;
6777 }
6778 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6779 return 1;
6780}
6781
6782static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6783{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006784 int icase;
6785
Willy Tarreau8797c062007-05-07 00:55:35 +02006786 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006787 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006788
6789 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006790 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006791
6792 /* Other method, we must compare the strings */
6793 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006794 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006795
6796 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6797 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6798 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006799 return ACL_PAT_FAIL;
6800 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006801}
6802
6803/* 2. Check on Request/Status Version
6804 * We simply compare strings here.
6805 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006806static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006807{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006808 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006809 if (!pattern->ptr.str)
6810 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006811 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006812 return 1;
6813}
6814
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006815static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006816acl_fetch_rqver(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->req.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->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006830 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006831
6832 while ((len-- > 0) && (*ptr++ != '/'));
6833 if (len <= 0)
6834 return 0;
6835
6836 test->ptr = ptr;
6837 test->len = len;
6838
6839 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6840 return 1;
6841}
6842
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006843static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006844acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6845 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006846{
6847 struct http_txn *txn = l7;
6848 char *ptr;
6849 int len;
6850
Willy Tarreaub6866442008-07-14 23:54:42 +02006851 if (!txn)
6852 return 0;
6853
Willy Tarreau655dce92009-11-08 13:10:58 +01006854 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006855 return 0;
6856
Willy Tarreau8797c062007-05-07 00:55:35 +02006857 len = txn->rsp.sl.st.v_l;
6858 ptr = txn->rsp.sol;
6859
6860 while ((len-- > 0) && (*ptr++ != '/'));
6861 if (len <= 0)
6862 return 0;
6863
6864 test->ptr = ptr;
6865 test->len = len;
6866
6867 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6868 return 1;
6869}
6870
6871/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006872static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006873acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6874 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006875{
6876 struct http_txn *txn = l7;
6877 char *ptr;
6878 int len;
6879
Willy Tarreaub6866442008-07-14 23:54:42 +02006880 if (!txn)
6881 return 0;
6882
Willy Tarreau655dce92009-11-08 13:10:58 +01006883 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006884 return 0;
6885
Willy Tarreau8797c062007-05-07 00:55:35 +02006886 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006887 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006888
6889 test->i = __strl2ui(ptr, len);
6890 test->flags = ACL_TEST_F_VOL_1ST;
6891 return 1;
6892}
6893
6894/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006895static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006896acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6897 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006898{
6899 struct http_txn *txn = l7;
6900
Willy Tarreaub6866442008-07-14 23:54:42 +02006901 if (!txn)
6902 return 0;
6903
Willy Tarreau655dce92009-11-08 13:10:58 +01006904 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006905 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006906
Willy Tarreauc11416f2007-06-17 16:58:38 +02006907 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6908 /* ensure the indexes are not affected */
6909 return 0;
6910
Willy Tarreau8797c062007-05-07 00:55:35 +02006911 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006912 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006913
Willy Tarreauf3d25982007-05-08 22:45:09 +02006914 /* we do not need to set READ_ONLY because the data is in a buffer */
6915 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006916 return 1;
6917}
6918
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006919static int
6920acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6921 struct acl_expr *expr, struct acl_test *test)
6922{
6923 struct http_txn *txn = l7;
6924
Willy Tarreaub6866442008-07-14 23:54:42 +02006925 if (!txn)
6926 return 0;
6927
Willy Tarreau655dce92009-11-08 13:10:58 +01006928 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006929 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006930
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006931 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6932 /* ensure the indexes are not affected */
6933 return 0;
6934
6935 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006936 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 +01006937 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6938 test->i = AF_INET;
6939
6940 /*
6941 * If we are parsing url in frontend space, we prepare backend stage
6942 * to not parse again the same url ! optimization lazyness...
6943 */
6944 if (px->options & PR_O_HTTP_PROXY)
6945 l4->flags |= SN_ADDR_SET;
6946
6947 test->flags = ACL_TEST_F_READ_ONLY;
6948 return 1;
6949}
6950
6951static int
6952acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6953 struct acl_expr *expr, struct acl_test *test)
6954{
6955 struct http_txn *txn = l7;
6956
Willy Tarreaub6866442008-07-14 23:54:42 +02006957 if (!txn)
6958 return 0;
6959
Willy Tarreau655dce92009-11-08 13:10:58 +01006960 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006961 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006962
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006963 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6964 /* ensure the indexes are not affected */
6965 return 0;
6966
6967 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006968 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 +01006969 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6970
6971 if (px->options & PR_O_HTTP_PROXY)
6972 l4->flags |= SN_ADDR_SET;
6973
6974 test->flags = ACL_TEST_F_READ_ONLY;
6975 return 1;
6976}
6977
Willy Tarreauc11416f2007-06-17 16:58:38 +02006978/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6979 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6980 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006981static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006982acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006983 struct acl_expr *expr, struct acl_test *test)
6984{
6985 struct http_txn *txn = l7;
6986 struct hdr_idx *idx = &txn->hdr_idx;
6987 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006988
Willy Tarreaub6866442008-07-14 23:54:42 +02006989 if (!txn)
6990 return 0;
6991
Willy Tarreau33a7e692007-06-10 19:45:56 +02006992 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6993 /* search for header from the beginning */
6994 ctx->idx = 0;
6995
Willy Tarreau33a7e692007-06-10 19:45:56 +02006996 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6997 test->flags |= ACL_TEST_F_FETCH_MORE;
6998 test->flags |= ACL_TEST_F_VOL_HDR;
6999 test->len = ctx->vlen;
7000 test->ptr = (char *)ctx->line + ctx->val;
7001 return 1;
7002 }
7003
7004 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7005 test->flags |= ACL_TEST_F_VOL_HDR;
7006 return 0;
7007}
7008
Willy Tarreau33a7e692007-06-10 19:45:56 +02007009static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007010acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7011 struct acl_expr *expr, struct acl_test *test)
7012{
7013 struct http_txn *txn = l7;
7014
Willy Tarreaub6866442008-07-14 23:54:42 +02007015 if (!txn)
7016 return 0;
7017
Willy Tarreau655dce92009-11-08 13:10:58 +01007018 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007019 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007020
Willy Tarreauc11416f2007-06-17 16:58:38 +02007021 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7022 /* ensure the indexes are not affected */
7023 return 0;
7024
7025 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7026}
7027
7028static int
7029acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7030 struct acl_expr *expr, struct acl_test *test)
7031{
7032 struct http_txn *txn = l7;
7033
Willy Tarreaub6866442008-07-14 23:54:42 +02007034 if (!txn)
7035 return 0;
7036
Willy Tarreau655dce92009-11-08 13:10:58 +01007037 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007038 return 0;
7039
7040 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7041}
7042
7043/* 6. Check on HTTP header count. The number of occurrences is returned.
7044 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7045 */
7046static int
7047acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007048 struct acl_expr *expr, struct acl_test *test)
7049{
7050 struct http_txn *txn = l7;
7051 struct hdr_idx *idx = &txn->hdr_idx;
7052 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007053 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007054
Willy Tarreaub6866442008-07-14 23:54:42 +02007055 if (!txn)
7056 return 0;
7057
Willy Tarreau33a7e692007-06-10 19:45:56 +02007058 ctx.idx = 0;
7059 cnt = 0;
7060 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7061 cnt++;
7062
7063 test->i = cnt;
7064 test->flags = ACL_TEST_F_VOL_HDR;
7065 return 1;
7066}
7067
Willy Tarreauc11416f2007-06-17 16:58:38 +02007068static int
7069acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7070 struct acl_expr *expr, struct acl_test *test)
7071{
7072 struct http_txn *txn = l7;
7073
Willy Tarreaub6866442008-07-14 23:54:42 +02007074 if (!txn)
7075 return 0;
7076
Willy Tarreau655dce92009-11-08 13:10:58 +01007077 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007078 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007079
Willy Tarreauc11416f2007-06-17 16:58:38 +02007080 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7081 /* ensure the indexes are not affected */
7082 return 0;
7083
7084 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7085}
7086
7087static int
7088acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7089 struct acl_expr *expr, struct acl_test *test)
7090{
7091 struct http_txn *txn = l7;
7092
Willy Tarreaub6866442008-07-14 23:54:42 +02007093 if (!txn)
7094 return 0;
7095
Willy Tarreau655dce92009-11-08 13:10:58 +01007096 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007097 return 0;
7098
7099 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7100}
7101
Willy Tarreau33a7e692007-06-10 19:45:56 +02007102/* 7. Check on HTTP header's integer value. The integer value is returned.
7103 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007104 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007105 */
7106static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007107acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007108 struct acl_expr *expr, struct acl_test *test)
7109{
7110 struct http_txn *txn = l7;
7111 struct hdr_idx *idx = &txn->hdr_idx;
7112 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007113
Willy Tarreaub6866442008-07-14 23:54:42 +02007114 if (!txn)
7115 return 0;
7116
Willy Tarreau33a7e692007-06-10 19:45:56 +02007117 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7118 /* search for header from the beginning */
7119 ctx->idx = 0;
7120
Willy Tarreau33a7e692007-06-10 19:45:56 +02007121 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7122 test->flags |= ACL_TEST_F_FETCH_MORE;
7123 test->flags |= ACL_TEST_F_VOL_HDR;
7124 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7125 return 1;
7126 }
7127
7128 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7129 test->flags |= ACL_TEST_F_VOL_HDR;
7130 return 0;
7131}
7132
Willy Tarreauc11416f2007-06-17 16:58:38 +02007133static int
7134acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7135 struct acl_expr *expr, struct acl_test *test)
7136{
7137 struct http_txn *txn = l7;
7138
Willy Tarreaub6866442008-07-14 23:54:42 +02007139 if (!txn)
7140 return 0;
7141
Willy Tarreau655dce92009-11-08 13:10:58 +01007142 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007143 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007144
Willy Tarreauc11416f2007-06-17 16:58:38 +02007145 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7146 /* ensure the indexes are not affected */
7147 return 0;
7148
7149 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7150}
7151
7152static int
7153acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7154 struct acl_expr *expr, struct acl_test *test)
7155{
7156 struct http_txn *txn = l7;
7157
Willy Tarreaub6866442008-07-14 23:54:42 +02007158 if (!txn)
7159 return 0;
7160
Willy Tarreau655dce92009-11-08 13:10:58 +01007161 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007162 return 0;
7163
7164 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7165}
7166
Willy Tarreau106f9792009-09-19 07:54:16 +02007167/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7168 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7169 */
7170static int
7171acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7172 struct acl_expr *expr, struct acl_test *test)
7173{
7174 struct http_txn *txn = l7;
7175 struct hdr_idx *idx = &txn->hdr_idx;
7176 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7177
7178 if (!txn)
7179 return 0;
7180
7181 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7182 /* search for header from the beginning */
7183 ctx->idx = 0;
7184
7185 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7186 test->flags |= ACL_TEST_F_FETCH_MORE;
7187 test->flags |= ACL_TEST_F_VOL_HDR;
7188 /* Same optimization as url_ip */
7189 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7190 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7191 test->ptr = (void *)&l4->srv_addr.sin_addr;
7192 test->i = AF_INET;
7193 return 1;
7194 }
7195
7196 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7197 test->flags |= ACL_TEST_F_VOL_HDR;
7198 return 0;
7199}
7200
7201static int
7202acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7203 struct acl_expr *expr, struct acl_test *test)
7204{
7205 struct http_txn *txn = l7;
7206
7207 if (!txn)
7208 return 0;
7209
Willy Tarreau655dce92009-11-08 13:10:58 +01007210 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007211 return 0;
7212
7213 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7214 /* ensure the indexes are not affected */
7215 return 0;
7216
7217 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7218}
7219
7220static int
7221acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7222 struct acl_expr *expr, struct acl_test *test)
7223{
7224 struct http_txn *txn = l7;
7225
7226 if (!txn)
7227 return 0;
7228
Willy Tarreau655dce92009-11-08 13:10:58 +01007229 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007230 return 0;
7231
7232 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7233}
7234
Willy Tarreau737b0c12007-06-10 21:28:46 +02007235/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7236 * the first '/' after the possible hostname, and ends before the possible '?'.
7237 */
7238static int
7239acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7240 struct acl_expr *expr, struct acl_test *test)
7241{
7242 struct http_txn *txn = l7;
7243 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007244
Willy Tarreaub6866442008-07-14 23:54:42 +02007245 if (!txn)
7246 return 0;
7247
Willy Tarreau655dce92009-11-08 13:10:58 +01007248 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007249 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007250
Willy Tarreauc11416f2007-06-17 16:58:38 +02007251 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7252 /* ensure the indexes are not affected */
7253 return 0;
7254
Willy Tarreau962c3f42010-01-10 00:15:35 +01007255 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007256 ptr = http_get_path(txn);
7257 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007258 return 0;
7259
7260 /* OK, we got the '/' ! */
7261 test->ptr = ptr;
7262
7263 while (ptr < end && *ptr != '?')
7264 ptr++;
7265
7266 test->len = ptr - test->ptr;
7267
7268 /* we do not need to set READ_ONLY because the data is in a buffer */
7269 test->flags = ACL_TEST_F_VOL_1ST;
7270 return 1;
7271}
7272
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007273static int
7274acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7275 struct acl_expr *expr, struct acl_test *test)
7276{
7277 struct buffer *req = s->req;
7278 struct http_txn *txn = &s->txn;
7279 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007280
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007281 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7282 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7283 */
7284
7285 if (!s || !req)
7286 return 0;
7287
Willy Tarreau655dce92009-11-08 13:10:58 +01007288 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007289 /* Already decoded as OK */
7290 test->flags |= ACL_TEST_F_SET_RES_PASS;
7291 return 1;
7292 }
7293
7294 /* Try to decode HTTP request */
7295 if (likely(req->lr < req->r))
7296 http_msg_analyzer(req, msg, &txn->hdr_idx);
7297
Willy Tarreau655dce92009-11-08 13:10:58 +01007298 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007299 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7300 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7301 return 1;
7302 }
7303 /* wait for final state */
7304 test->flags |= ACL_TEST_F_MAY_CHANGE;
7305 return 0;
7306 }
7307
7308 /* OK we got a valid HTTP request. We have some minor preparation to
7309 * perform so that further checks can rely on HTTP tests.
7310 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007311 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007312 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7313 s->flags |= SN_REDIRECTABLE;
7314
7315 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7316 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7317 return 1;
7318 }
7319
7320 test->flags |= ACL_TEST_F_SET_RES_PASS;
7321 return 1;
7322}
7323
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007324static int
7325acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
7326 struct acl_expr *expr, struct acl_test *test)
7327{
7328
7329 if (!s)
7330 return 0;
7331
7332 if (!get_http_auth(s))
7333 return 0;
7334
7335 test->ctx.a[0] = expr->arg.ul;
7336 test->ctx.a[1] = s->txn.auth.user;
7337 test->ctx.a[2] = s->txn.auth.pass;
7338
7339 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
7340
7341 return 1;
7342}
Willy Tarreau8797c062007-05-07 00:55:35 +02007343
7344/************************************************************************/
7345/* All supported keywords must be declared here. */
7346/************************************************************************/
7347
7348/* Note: must not be declared <const> as its list will be overwritten */
7349static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007350 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7351
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007352 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7353 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7354 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7355 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007356
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007357 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7358 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7359 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7360 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7361 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7362 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7363 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7364 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7365 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007366
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007367 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7368 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7369 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7370 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7371 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7372 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7373 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7374 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7375 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7376 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007377 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007378
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007379 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7380 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7381 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7382 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7383 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7384 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7385 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7386 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7387 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007388 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007389
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007390 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7391 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7392 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7393 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7394 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7395 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7396 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007397
Willy Tarreauf3d25982007-05-08 22:45:09 +02007398#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007399 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7400 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7401 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7402 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7403 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7404 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7405 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7406
Willy Tarreau8797c062007-05-07 00:55:35 +02007407 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7408 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7409 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7410 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7411 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7412 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7413 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7414 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007415#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02007416
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007417 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth },
7418 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth },
Willy Tarreau8797c062007-05-07 00:55:35 +02007419 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02007420}};
7421
7422
7423__attribute__((constructor))
7424static void __http_protocol_init(void)
7425{
7426 acl_register_keywords(&acl_kws);
7427}
7428
7429
Willy Tarreau58f10d72006-12-04 02:26:12 +01007430/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007431 * Local variables:
7432 * c-indent-level: 8
7433 * c-basic-offset: 8
7434 * End:
7435 */