blob: ed8e018420d554bff0da1e0e57ae575a78042455 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 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 Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010045#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/backend.h>
47#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010048#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020051#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010053#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020054#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020055#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010061#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020062#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/task.h>
64
Willy Tarreau522d6c02009-12-06 18:49:18 +010065const char HTTP_100[] =
66 "HTTP/1.1 100 Continue\r\n\r\n";
67
68const struct chunk http_100_chunk = {
69 .str = (char *)&HTTP_100,
70 .len = sizeof(HTTP_100)-1
71};
72
Willy Tarreaua9679ac2010-01-03 17:32:57 +010073/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Location: "; /* not terminated since it will be concatenated with the URL */
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Location: "; /* not terminated since it will be concatenated with the URL */
85
86/* same as 302 except that the browser MUST retry with the GET method */
87const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreaubaaee002006-06-26 02:48:02 +020093/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
94const char *HTTP_401_fmt =
95 "HTTP/1.0 401 Unauthorized\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020098 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
100 "\r\n"
101 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
102
Willy Tarreau844a7e72010-01-31 21:46:18 +0100103const char *HTTP_407_fmt =
104 "HTTP/1.0 407 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
107 "Content-Type: text/html\r\n"
108 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200114 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100115 [HTTP_ERR_400] = 400,
116 [HTTP_ERR_403] = 403,
117 [HTTP_ERR_408] = 408,
118 [HTTP_ERR_500] = 500,
119 [HTTP_ERR_502] = 502,
120 [HTTP_ERR_503] = 503,
121 [HTTP_ERR_504] = 504,
122};
123
Willy Tarreau80587432006-12-24 17:47:20 +0100124static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200125 [HTTP_ERR_200] =
126 "HTTP/1.0 200 OK\r\n"
127 "Cache-Control: no-cache\r\n"
128 "Connection: close\r\n"
129 "Content-Type: text/html\r\n"
130 "\r\n"
131 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
132
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100134 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 "Cache-Control: no-cache\r\n"
136 "Connection: close\r\n"
137 "Content-Type: text/html\r\n"
138 "\r\n"
139 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
140
141 [HTTP_ERR_403] =
142 "HTTP/1.0 403 Forbidden\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
148
149 [HTTP_ERR_408] =
150 "HTTP/1.0 408 Request Time-out\r\n"
151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
156
157 [HTTP_ERR_500] =
158 "HTTP/1.0 500 Server Error\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
164
165 [HTTP_ERR_502] =
166 "HTTP/1.0 502 Bad Gateway\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
172
173 [HTTP_ERR_503] =
174 "HTTP/1.0 503 Service Unavailable\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
180
181 [HTTP_ERR_504] =
182 "HTTP/1.0 504 Gateway Time-out\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
188
189};
190
Willy Tarreau80587432006-12-24 17:47:20 +0100191/* We must put the messages here since GCC cannot initialize consts depending
192 * on strlen().
193 */
194struct chunk http_err_chunks[HTTP_ERR_SIZE];
195
Willy Tarreau42250582007-04-01 01:30:43 +0200196#define FD_SETS_ARE_BITFIELDS
197#ifdef FD_SETS_ARE_BITFIELDS
198/*
199 * This map is used with all the FD_* macros to check whether a particular bit
200 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
201 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
202 * byte should be encoded. Be careful to always pass bytes from 0 to 255
203 * exclusively to the macros.
204 */
205fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
206fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
207
208#else
209#error "Check if your OS uses bitfields for fd_sets"
210#endif
211
Willy Tarreau80587432006-12-24 17:47:20 +0100212void init_proto_http()
213{
Willy Tarreau42250582007-04-01 01:30:43 +0200214 int i;
215 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100216 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200217
Willy Tarreau80587432006-12-24 17:47:20 +0100218 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
219 if (!http_err_msgs[msg]) {
220 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
221 abort();
222 }
223
224 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
225 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
226 }
Willy Tarreau42250582007-04-01 01:30:43 +0200227
228 /* initialize the log header encoding map : '{|}"#' should be encoded with
229 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
230 * URL encoding only requires '"', '#' to be encoded as well as non-
231 * printable characters above.
232 */
233 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
234 memset(url_encode_map, 0, sizeof(url_encode_map));
235 for (i = 0; i < 32; i++) {
236 FD_SET(i, hdr_encode_map);
237 FD_SET(i, url_encode_map);
238 }
239 for (i = 127; i < 256; i++) {
240 FD_SET(i, hdr_encode_map);
241 FD_SET(i, url_encode_map);
242 }
243
244 tmp = "\"#{|}";
245 while (*tmp) {
246 FD_SET(*tmp, hdr_encode_map);
247 tmp++;
248 }
249
250 tmp = "\"#";
251 while (*tmp) {
252 FD_SET(*tmp, url_encode_map);
253 tmp++;
254 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200255
256 /* memory allocations */
257 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200258 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100259}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260
Willy Tarreau53b6c742006-12-17 13:37:46 +0100261/*
262 * We have 26 list of methods (1 per first letter), each of which can have
263 * up to 3 entries (2 valid, 1 null).
264 */
265struct http_method_desc {
266 http_meth_t meth;
267 int len;
268 const char text[8];
269};
270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100271const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100272 ['C' - 'A'] = {
273 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
274 },
275 ['D' - 'A'] = {
276 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
277 },
278 ['G' - 'A'] = {
279 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
280 },
281 ['H' - 'A'] = {
282 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
283 },
284 ['P' - 'A'] = {
285 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
286 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
287 },
288 ['T' - 'A'] = {
289 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
290 },
291 /* rest is empty like this :
292 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
293 */
294};
295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100296/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200297 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100298 * RFC2616 for those chars.
299 */
300
301const char http_is_spht[256] = {
302 [' '] = 1, ['\t'] = 1,
303};
304
305const char http_is_crlf[256] = {
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_lws[256] = {
310 [' '] = 1, ['\t'] = 1,
311 ['\r'] = 1, ['\n'] = 1,
312};
313
314const char http_is_sep[256] = {
315 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
316 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
317 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
318 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
319 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
320};
321
322const char http_is_ctl[256] = {
323 [0 ... 31] = 1,
324 [127] = 1,
325};
326
327/*
328 * A token is any ASCII char that is neither a separator nor a CTL char.
329 * Do not overwrite values in assignment since gcc-2.95 will not handle
330 * them correctly. Instead, define every non-CTL char's status.
331 */
332const char http_is_token[256] = {
333 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
334 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
335 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
336 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
337 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
338 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
339 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
340 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
341 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
342 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
343 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
344 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
345 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
346 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
347 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
348 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
349 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
350 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
351 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
352 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
353 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
354 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
355 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
356 ['|'] = 1, ['}'] = 0, ['~'] = 1,
357};
358
359
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100360/*
361 * An http ver_token is any ASCII which can be found in an HTTP version,
362 * which includes 'H', 'T', 'P', '/', '.' and any digit.
363 */
364const char http_is_ver_token[256] = {
365 ['.'] = 1, ['/'] = 1,
366 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
367 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
368 ['H'] = 1, ['P'] = 1, ['T'] = 1,
369};
370
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100373 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
374 */
375#if defined(DEBUG_FSM)
376static void http_silent_debug(int line, struct session *s)
377{
378 int size = 0;
379 size += snprintf(trash + size, sizeof(trash) - size,
380 "[%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",
381 line,
382 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
383 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);
384 write(-1, trash, size);
385 size = 0;
386 size += snprintf(trash + size, sizeof(trash) - size,
387 " %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",
388 line,
389 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
390 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);
391
392 write(-1, trash, size);
393}
394#else
395#define http_silent_debug(l,s) do { } while (0)
396#endif
397
398/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100399 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
400 * CRLF. Text length is measured first, so it cannot be NULL.
401 * The header is also automatically added to the index <hdr_idx>, and the end
402 * of headers is automatically adjusted. The number of bytes added is returned
403 * on success, otherwise <0 is returned indicating an error.
404 */
405int http_header_add_tail(struct buffer *b, struct http_msg *msg,
406 struct hdr_idx *hdr_idx, const char *text)
407{
408 int bytes, len;
409
410 len = strlen(text);
411 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
412 if (!bytes)
413 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100414 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100415 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
416}
417
418/*
419 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
420 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
421 * the buffer is only opened and the space reserved, but nothing is copied.
422 * The header is also automatically added to the index <hdr_idx>, and the end
423 * of headers is automatically adjusted. The number of bytes added is returned
424 * on success, otherwise <0 is returned indicating an error.
425 */
426int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
427 struct hdr_idx *hdr_idx, const char *text, int len)
428{
429 int bytes;
430
431 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
432 if (!bytes)
433 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100434 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100435 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
438/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100439 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
440 * If so, returns the position of the first non-space character relative to
441 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
442 * to return a pointer to the place after the first space. Returns 0 if the
443 * header name does not match. Checks are case-insensitive.
444 */
445int http_header_match2(const char *hdr, const char *end,
446 const char *name, int len)
447{
448 const char *val;
449
450 if (hdr + len >= end)
451 return 0;
452 if (hdr[len] != ':')
453 return 0;
454 if (strncasecmp(hdr, name, len) != 0)
455 return 0;
456 val = hdr + len + 1;
457 while (val < end && HTTP_IS_SPHT(*val))
458 val++;
459 if ((val >= end) && (len + 2 <= end - hdr))
460 return len + 2; /* we may replace starting from second space */
461 return val - hdr;
462}
463
Willy Tarreau68085d82010-01-18 14:54:04 +0100464/* Find the end of the header value contained between <s> and <e>. See RFC2616,
465 * par 2.2 for more information. Note that it requires a valid header to return
466 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200467 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100468char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200469{
470 int quoted, qdpair;
471
472 quoted = qdpair = 0;
473 for (; s < e; s++) {
474 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200475 else if (quoted) {
476 if (*s == '\\') qdpair = 1;
477 else if (*s == '"') quoted = 0;
478 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200479 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 Tarreau588bd4f2011-09-01 22:22:28 +0200508 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 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);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200556 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200557 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200558 eol--;
559 ctx->tws++;
560 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200561 ctx->vlen = eol - sov;
562 return 1;
563 }
564 next_hdr:
565 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100566 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200567 cur_idx = idx->v[cur_idx].next;
568 }
569 return 0;
570}
571
572int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100573 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200574 struct hdr_ctx *ctx)
575{
576 return http_find_header2(name, strlen(name), sol, idx, ctx);
577}
578
Willy Tarreau68085d82010-01-18 14:54:04 +0100579/* Remove one value of a header. This only works on a <ctx> returned by one of
580 * the http_find_header functions. The value is removed, as well as surrounding
581 * commas if any. If the removed value was alone, the whole header is removed.
582 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
583 * message <msg>. The new index is returned. If it is zero, it means there is
584 * no more header, so any processing may stop. The ctx is always left in a form
585 * that can be handled by http_find_header2() to find next occurrence.
586 */
587int http_remove_header2(struct http_msg *msg, struct buffer *buf,
588 struct hdr_idx *idx, struct hdr_ctx *ctx)
589{
590 int cur_idx = ctx->idx;
591 char *sol = ctx->line;
592 struct hdr_idx_elem *hdr;
593 int delta, skip_comma;
594
595 if (!cur_idx)
596 return 0;
597
598 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200599 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100600 /* This was the only value of the header, we must now remove it entirely. */
601 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
602 http_msg_move_end(msg, delta);
603 idx->used--;
604 hdr->len = 0; /* unused entry */
605 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100606 if (idx->tail == ctx->idx)
607 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100608 ctx->idx = ctx->prev; /* walk back to the end of previous header */
609 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
610 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200611 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 return ctx->idx;
613 }
614
615 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200616 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
617 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 */
619
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200620 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200622 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 NULL, 0);
624 hdr->len += delta;
625 http_msg_move_end(msg, delta);
626 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200627 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 return ctx->idx;
629}
630
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100631/* This function handles a server error at the stream interface level. The
632 * stream interface is assumed to be already in a closed state. An optional
633 * message is copied into the input buffer, and an HTTP status code stored.
634 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100635 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100637static void http_server_error(struct session *t, struct stream_interface *si,
638 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100640 buffer_auto_read(si->ob);
641 buffer_abort(si->ob);
642 buffer_auto_close(si->ob);
643 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200644 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100645 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100646 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100647 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100648 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 }
650 if (!(t->flags & SN_ERR_MASK))
651 t->flags |= err;
652 if (!(t->flags & SN_FINST_MASK))
653 t->flags |= finst;
654}
655
Willy Tarreau80587432006-12-24 17:47:20 +0100656/* This function returns the appropriate error location for the given session
657 * and message.
658 */
659
660struct chunk *error_message(struct session *s, int msgnum)
661{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200662 if (s->be->errmsg[msgnum].str)
663 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100664 else if (s->fe->errmsg[msgnum].str)
665 return &s->fe->errmsg[msgnum];
666 else
667 return &http_err_chunks[msgnum];
668}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
Willy Tarreau53b6c742006-12-17 13:37:46 +0100670/*
671 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
672 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
673 */
674static http_meth_t find_http_meth(const char *str, const int len)
675{
676 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100677 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100678
679 m = ((unsigned)*str - 'A');
680
681 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100682 for (h = http_methods[m]; h->len > 0; h++) {
683 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100684 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100685 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100686 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100687 };
688 return HTTP_METH_OTHER;
689 }
690 return HTTP_METH_NONE;
691
692}
693
Willy Tarreau21d2af32008-02-14 20:25:24 +0100694/* Parse the URI from the given transaction (which is assumed to be in request
695 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
696 * It is returned otherwise.
697 */
698static char *
699http_get_path(struct http_txn *txn)
700{
701 char *ptr, *end;
702
Willy Tarreau962c3f42010-01-10 00:15:35 +0100703 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100704 end = ptr + txn->req.sl.rq.u_l;
705
706 if (ptr >= end)
707 return NULL;
708
709 /* RFC2616, par. 5.1.2 :
710 * Request-URI = "*" | absuri | abspath | authority
711 */
712
713 if (*ptr == '*')
714 return NULL;
715
716 if (isalpha((unsigned char)*ptr)) {
717 /* this is a scheme as described by RFC3986, par. 3.1 */
718 ptr++;
719 while (ptr < end &&
720 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
721 ptr++;
722 /* skip '://' */
723 if (ptr == end || *ptr++ != ':')
724 return NULL;
725 if (ptr == end || *ptr++ != '/')
726 return NULL;
727 if (ptr == end || *ptr++ != '/')
728 return NULL;
729 }
730 /* skip [user[:passwd]@]host[:[port]] */
731
732 while (ptr < end && *ptr != '/')
733 ptr++;
734
735 if (ptr == end)
736 return NULL;
737
738 /* OK, we got the '/' ! */
739 return ptr;
740}
741
Willy Tarreauefb453c2008-10-26 20:49:47 +0100742/* Returns a 302 for a redirectable request. This may only be called just after
743 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
744 * left unchanged and will follow normal proxy processing.
745 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100747{
748 struct http_txn *txn;
749 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100750 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 char *path;
752 int len;
753
754 /* 1: create the response header */
755 rdr.len = strlen(HTTP_302);
756 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100757 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758 memcpy(rdr.str, HTTP_302, rdr.len);
759
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 srv = target_srv(&s->target);
761
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100763 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764 return;
765
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100766 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100767 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
768 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
769 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100770 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771
772 /* 3: add the request URI */
773 txn = &s->txn;
774 path = http_get_path(txn);
775 if (!path)
776 return;
777
Willy Tarreau962c3f42010-01-10 00:15:35 +0100778 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200779 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780 return;
781
782 memcpy(rdr.str + rdr.len, path, len);
783 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100784
785 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
786 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
787 rdr.len += 29;
788 } else {
789 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
790 rdr.len += 23;
791 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792
793 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100794 si->shutr(si);
795 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796 si->err_type = SI_ET_NONE;
797 si->err_loc = NULL;
798 si->state = SI_ST_CLO;
799
800 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100801 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802
803 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100804 if (srv)
805 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100806}
807
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100808/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809 * that the server side is closed. Note that err_type is actually a
810 * bitmask, where almost only aborts may be cumulated with other
811 * values. We consider that aborted operations are more important
812 * than timeouts or errors due to the fact that nobody else in the
813 * logs might explain incomplete retries. All others should avoid
814 * being cumulated. It should normally not be possible to have multiple
815 * aborts at once, but just in case, the first one in sequence is reported.
816 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100817void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100819 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820
821 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100822 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
823 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100825 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
826 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100827 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100828 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
829 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100831 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
832 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100833 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100834 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
835 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100837 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
838 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100840 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
841 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842}
843
Willy Tarreau42250582007-04-01 01:30:43 +0200844extern const char sess_term_cond[8];
845extern const char sess_fin_state[8];
846extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200847struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200848struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100849
Willy Tarreau117f59e2007-03-04 18:17:17 +0100850/*
851 * Capture headers from message starting at <som> according to header list
852 * <cap_hdr>, and fill the <idx> structure appropriately.
853 */
854void capture_headers(char *som, struct hdr_idx *idx,
855 char **cap, struct cap_hdr *cap_hdr)
856{
857 char *eol, *sol, *col, *sov;
858 int cur_idx;
859 struct cap_hdr *h;
860 int len;
861
862 sol = som + hdr_idx_first_pos(idx);
863 cur_idx = hdr_idx_first_idx(idx);
864
865 while (cur_idx) {
866 eol = sol + idx->v[cur_idx].len;
867
868 col = sol;
869 while (col < eol && *col != ':')
870 col++;
871
872 sov = col + 1;
873 while (sov < eol && http_is_lws[(unsigned char)*sov])
874 sov++;
875
876 for (h = cap_hdr; h; h = h->next) {
877 if ((h->namelen == col - sol) &&
878 (strncasecmp(sol, h->name, h->namelen) == 0)) {
879 if (cap[h->index] == NULL)
880 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200881 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100882
883 if (cap[h->index] == NULL) {
884 Alert("HTTP capture : out of memory.\n");
885 continue;
886 }
887
888 len = eol - sov;
889 if (len > h->len)
890 len = h->len;
891
892 memcpy(cap[h->index], sov, len);
893 cap[h->index][len]=0;
894 }
895 }
896 sol = eol + idx->v[cur_idx].cr + 1;
897 cur_idx = idx->v[cur_idx].next;
898 }
899}
900
901
Willy Tarreau42250582007-04-01 01:30:43 +0200902/* either we find an LF at <ptr> or we jump to <bad>.
903 */
904#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
905
906/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
907 * otherwise to <http_msg_ood> with <state> set to <st>.
908 */
909#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
910 ptr++; \
911 if (likely(ptr < end)) \
912 goto good; \
913 else { \
914 state = (st); \
915 goto http_msg_ood; \
916 } \
917 } while (0)
918
919
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100921 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100922 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
923 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
924 * will give undefined results.
925 * Note that it is upon the caller's responsibility to ensure that ptr < end,
926 * and that msg->sol points to the beginning of the response.
927 * If a complete line is found (which implies that at least one CR or LF is
928 * found before <end>, the updated <ptr> is returned, otherwise NULL is
929 * returned indicating an incomplete line (which does not mean that parts have
930 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
931 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
932 * upon next call.
933 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200934 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
936 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200937 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100938 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100939const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
940 unsigned int state, const char *ptr, const char *end,
941 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100942{
Willy Tarreau8973c702007-01-21 23:58:29 +0100943 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100944 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200945 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100946 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
948
949 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100950 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
952 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100953 state = HTTP_MSG_ERROR;
954 break;
955
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200957 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100959 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 goto http_msg_rpcode;
961 }
962 if (likely(HTTP_IS_SPHT(*ptr)))
963 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
964 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100965 state = HTTP_MSG_ERROR;
966 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100967
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200969 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 if (likely(!HTTP_IS_LWS(*ptr)))
971 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
972
973 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100974 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
976 }
977
978 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100979 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 http_msg_rsp_reason:
981 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100982 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 msg->sl.st.r_l = 0;
984 goto http_msg_rpline_eol;
985
Willy Tarreau8973c702007-01-21 23:58:29 +0100986 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200987 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100988 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100989 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 goto http_msg_rpreason;
991 }
992 if (likely(HTTP_IS_SPHT(*ptr)))
993 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
994 /* so it's a CR/LF, so there is no reason phrase */
995 goto http_msg_rsp_reason;
996
Willy Tarreau8973c702007-01-21 23:58:29 +0100997 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200998 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 if (likely(!HTTP_IS_CRLF(*ptr)))
1000 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001001 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 http_msg_rpline_eol:
1003 /* We have seen the end of line. Note that we do not
1004 * necessarily have the \n yet, but at least we know that we
1005 * have EITHER \r OR \n, otherwise the response would not be
1006 * complete. We can then record the response length and return
1007 * to the caller which will be able to register it.
1008 */
1009 msg->sl.st.l = ptr - msg->sol;
1010 return ptr;
1011
1012#ifdef DEBUG_FULL
1013 default:
1014 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1015 exit(1);
1016#endif
1017 }
1018
1019 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001020 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 if (ret_state)
1022 *ret_state = state;
1023 if (ret_ptr)
1024 *ret_ptr = (char *)ptr;
1025 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001026}
1027
Willy Tarreau8973c702007-01-21 23:58:29 +01001028/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029 * This function parses a request line between <ptr> and <end>, starting with
1030 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1031 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1032 * will give undefined results.
1033 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1034 * and that msg->sol points to the beginning of the request.
1035 * If a complete line is found (which implies that at least one CR or LF is
1036 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1037 * returned indicating an incomplete line (which does not mean that parts have
1038 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1039 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1040 * upon next call.
1041 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001042 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1044 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001045 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001047const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1048 unsigned int state, const char *ptr, const char *end,
1049 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001050{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001051 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001052 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001053 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001054 if (likely(HTTP_IS_TOKEN(*ptr)))
1055 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001056
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001058 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001059 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001062 if (likely(HTTP_IS_CRLF(*ptr))) {
1063 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001064 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001066 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001068 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001070 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 msg->sl.rq.v_l = 0;
1072 goto http_msg_rqline_eol;
1073 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001074 state = HTTP_MSG_ERROR;
1075 break;
1076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001078 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001080 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 goto http_msg_rquri;
1082 }
1083 if (likely(HTTP_IS_SPHT(*ptr)))
1084 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1085 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1086 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001089 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001090 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001094 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001097
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001098 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001099 /* non-ASCII chars are forbidden unless option
1100 * accept-invalid-http-request is enabled in the frontend.
1101 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001102 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001103 if (msg->err_pos < -1)
1104 goto invalid_char;
1105 if (msg->err_pos == -1)
1106 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1108 }
1109
1110 if (likely(HTTP_IS_CRLF(*ptr))) {
1111 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1112 goto http_msg_req09_uri_e;
1113 }
1114
1115 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001116 invalid_char:
1117 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001118 state = HTTP_MSG_ERROR;
1119 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001120
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001121 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001122 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001123 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001124 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 goto http_msg_rqver;
1126 }
1127 if (likely(HTTP_IS_SPHT(*ptr)))
1128 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1129 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1130 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001133 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001134 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001135 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001136
1137 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001138 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001139 http_msg_rqline_eol:
1140 /* We have seen the end of line. Note that we do not
1141 * necessarily have the \n yet, but at least we know that we
1142 * have EITHER \r OR \n, otherwise the request would not be
1143 * complete. We can then record the request length and return
1144 * to the caller which will be able to register it.
1145 */
1146 msg->sl.rq.l = ptr - msg->sol;
1147 return ptr;
1148 }
1149
1150 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001151 state = HTTP_MSG_ERROR;
1152 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001154#ifdef DEBUG_FULL
1155 default:
1156 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1157 exit(1);
1158#endif
1159 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001160
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001162 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 if (ret_state)
1164 *ret_state = state;
1165 if (ret_ptr)
1166 *ret_ptr = (char *)ptr;
1167 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001169
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001170/*
1171 * Returns the data from Authorization header. Function may be called more
1172 * than once so data is stored in txn->auth_data. When no header is found
1173 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1174 * searching again for something we are unable to find anyway.
1175 */
1176
1177char get_http_auth_buff[BUFSIZE];
1178
1179int
1180get_http_auth(struct session *s)
1181{
1182
1183 struct http_txn *txn = &s->txn;
1184 struct chunk auth_method;
1185 struct hdr_ctx ctx;
1186 char *h, *p;
1187 int len;
1188
1189#ifdef DEBUG_AUTH
1190 printf("Auth for session %p: %d\n", s, txn->auth.method);
1191#endif
1192
1193 if (txn->auth.method == HTTP_AUTH_WRONG)
1194 return 0;
1195
1196 if (txn->auth.method)
1197 return 1;
1198
1199 txn->auth.method = HTTP_AUTH_WRONG;
1200
1201 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001202
1203 if (txn->flags & TX_USE_PX_CONN) {
1204 h = "Proxy-Authorization";
1205 len = strlen(h);
1206 } else {
1207 h = "Authorization";
1208 len = strlen(h);
1209 }
1210
1211 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001212 return 0;
1213
1214 h = ctx.line + ctx.val;
1215
1216 p = memchr(h, ' ', ctx.vlen);
1217 if (!p || p == h)
1218 return 0;
1219
1220 chunk_initlen(&auth_method, h, 0, p-h);
1221 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1222
1223 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1224
1225 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1226 get_http_auth_buff, BUFSIZE - 1);
1227
1228 if (len < 0)
1229 return 0;
1230
1231
1232 get_http_auth_buff[len] = '\0';
1233
1234 p = strchr(get_http_auth_buff, ':');
1235
1236 if (!p)
1237 return 0;
1238
1239 txn->auth.user = get_http_auth_buff;
1240 *p = '\0';
1241 txn->auth.pass = p+1;
1242
1243 txn->auth.method = HTTP_AUTH_BASIC;
1244 return 1;
1245 }
1246
1247 return 0;
1248}
1249
Willy Tarreau58f10d72006-12-04 02:26:12 +01001250
Willy Tarreau8973c702007-01-21 23:58:29 +01001251/*
1252 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001253 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001254 * when data are missing and recalled at the exact same location with no
1255 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001256 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001257 * fields. Note that msg->som and msg->sol will be initialized after completing
1258 * the first state, so that none of the msg pointers has to be initialized
1259 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1262{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001263 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001265
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001266 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 ptr = buf->lr;
1268 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001270 if (unlikely(ptr >= end))
1271 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 /*
1275 * First, states that are specific to the response only.
1276 * We check them first so that request and headers are
1277 * closer to each other (accessed more often).
1278 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001280 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001282 /* we have a start of message, but we have to check
1283 * first if we need to remove some CRLF. We can only
1284 * do this when send_max=0.
1285 */
1286 char *beg = buf->w + buf->send_max;
1287 if (beg >= buf->data + buf->size)
1288 beg -= buf->size;
1289 if (unlikely(ptr != beg)) {
1290 if (buf->send_max)
1291 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001292 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001293 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001294 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001295 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001296 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 hdr_idx_init(idx);
1298 state = HTTP_MSG_RPVER;
1299 goto http_msg_rpver;
1300 }
1301
1302 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1303 goto http_msg_invalid;
1304
1305 if (unlikely(*ptr == '\n'))
1306 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1307 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1308 /* stop here */
1309
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001311 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 EXPECT_LF_HERE(ptr, http_msg_invalid);
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1314 /* stop here */
1315
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 case HTTP_MSG_RPVER_SP:
1319 case HTTP_MSG_RPCODE:
1320 case HTTP_MSG_RPCODE_SP:
1321 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001322 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001323 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 if (unlikely(!ptr))
1325 return;
1326
1327 /* we have a full response and we know that we have either a CR
1328 * or an LF at <ptr>.
1329 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001330 //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 +01001331 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1332
1333 msg->sol = ptr;
1334 if (likely(*ptr == '\r'))
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1336 goto http_msg_rpline_end;
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 /* msg->sol must point to the first of CR or LF. */
1341 EXPECT_LF_HERE(ptr, http_msg_invalid);
1342 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1343 /* stop here */
1344
1345 /*
1346 * Second, states that are specific to the request only
1347 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001349 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001351 /* we have a start of message, but we have to check
1352 * first if we need to remove some CRLF. We can only
1353 * do this when send_max=0.
1354 */
1355 char *beg = buf->w + buf->send_max;
1356 if (beg >= buf->data + buf->size)
1357 beg -= buf->size;
1358 if (likely(ptr != beg)) {
1359 if (buf->send_max)
1360 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001361 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001362 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001364 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001365 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001366 /* we will need this when keep-alive will be supported
1367 hdr_idx_init(idx);
1368 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001369 state = HTTP_MSG_RQMETH;
1370 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1374 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 if (unlikely(*ptr == '\n'))
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1378 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001379 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001382 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 EXPECT_LF_HERE(ptr, http_msg_invalid);
1384 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001388 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 case HTTP_MSG_RQMETH_SP:
1390 case HTTP_MSG_RQURI:
1391 case HTTP_MSG_RQURI_SP:
1392 case HTTP_MSG_RQVER:
1393 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001394 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 if (unlikely(!ptr))
1396 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 /* we have a full request and we know that we have either a CR
1399 * or an LF at <ptr>.
1400 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001401 //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 +01001402 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 msg->sol = ptr;
1405 if (likely(*ptr == '\r'))
1406 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001410 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 /* check for HTTP/0.9 request : no version information available.
1412 * msg->sol must point to the first of CR or LF.
1413 */
1414 if (unlikely(msg->sl.rq.v_l == 0))
1415 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 EXPECT_LF_HERE(ptr, http_msg_invalid);
1418 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 /*
1422 * Common states below
1423 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001425 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 msg->sol = ptr;
1427 if (likely(!HTTP_IS_CRLF(*ptr))) {
1428 goto http_msg_hdr_name;
1429 }
1430
1431 if (likely(*ptr == '\r'))
1432 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1433 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001436 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 /* assumes msg->sol points to the first char */
1438 if (likely(HTTP_IS_TOKEN(*ptr)))
1439 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 if (likely(*ptr == ':')) {
1442 msg->col = ptr - buf->data;
1443 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1444 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001445
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001446 if (likely(msg->err_pos < -1) || *ptr == '\n')
1447 goto http_msg_invalid;
1448
1449 if (msg->err_pos == -1) /* capture error pointer */
1450 msg->err_pos = ptr - buf->data; /* >= 0 now */
1451
1452 /* and we still accept this non-token character */
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 /* assumes msg->sol points to the first char and msg->col to the colon */
1458 if (likely(HTTP_IS_SPHT(*ptr)))
1459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 /* header value can be basically anything except CR/LF */
1462 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 if (likely(!HTTP_IS_CRLF(*ptr))) {
1465 goto http_msg_hdr_val;
1466 }
1467
1468 if (likely(*ptr == '\r'))
1469 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1470 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 EXPECT_LF_HERE(ptr, http_msg_invalid);
1475 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001478 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 if (likely(HTTP_IS_SPHT(*ptr))) {
1480 /* replace HT,CR,LF with spaces */
1481 for (; buf->data+msg->sov < ptr; msg->sov++)
1482 buf->data[msg->sov] = ' ';
1483 goto http_msg_hdr_l1_sp;
1484 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001485 /* we had a header consisting only in spaces ! */
1486 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 goto http_msg_complete_header;
1488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001490 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 /* assumes msg->sol points to the first char, msg->col to the
1492 * colon, and msg->sov points to the first character of the
1493 * value.
1494 */
1495 if (likely(!HTTP_IS_CRLF(*ptr)))
1496 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 msg->eol = ptr;
1499 /* Note: we could also copy eol into ->eoh so that we have the
1500 * real header end in case it ends with lots of LWS, but is this
1501 * really needed ?
1502 */
1503 if (likely(*ptr == '\r'))
1504 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1505 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001508 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 EXPECT_LF_HERE(ptr, http_msg_invalid);
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001513 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1515 /* LWS: replace HT,CR,LF with spaces */
1516 for (; msg->eol < ptr; msg->eol++)
1517 *msg->eol = ' ';
1518 goto http_msg_hdr_val;
1519 }
1520 http_msg_complete_header:
1521 /*
1522 * It was a new header, so the last one is finished.
1523 * Assumes msg->sol points to the first char, msg->col to the
1524 * colon, msg->sov points to the first character of the value
1525 * and msg->eol to the first CR or LF so we know how the line
1526 * ends. We insert last header into the index.
1527 */
1528 /*
1529 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1530 write(2, msg->sol, msg->eol-msg->sol);
1531 fprintf(stderr,"\n");
1532 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1535 idx, idx->tail) < 0))
1536 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 msg->sol = ptr;
1539 if (likely(!HTTP_IS_CRLF(*ptr))) {
1540 goto http_msg_hdr_name;
1541 }
1542
1543 if (likely(*ptr == '\r'))
1544 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1545 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001548 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 /* Assumes msg->sol points to the first of either CR or LF */
1550 EXPECT_LF_HERE(ptr, http_msg_invalid);
1551 ptr++;
1552 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001553 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001555 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001556 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 return;
1558#ifdef DEBUG_FULL
1559 default:
1560 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1561 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001562#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 }
1564 http_msg_ood:
1565 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001566 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 buf->lr = ptr;
1568 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 http_msg_invalid:
1571 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001572 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001573 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 return;
1575}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001576
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001577/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1578 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1579 * nothing is done and 1 is returned.
1580 */
1581static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1582{
1583 int delta;
1584 char *cur_end;
1585
1586 if (msg->sl.rq.v_l != 0)
1587 return 1;
1588
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001589 cur_end = msg->sol + msg->sl.rq.l;
1590 delta = 0;
1591
1592 if (msg->sl.rq.u_l == 0) {
1593 /* if no URI was set, add "/" */
1594 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1595 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001596 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597 }
1598 /* add HTTP version */
1599 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001600 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001601 cur_end += delta;
1602 cur_end = (char *)http_parse_reqline(msg, req->data,
1603 HTTP_MSG_RQMETH,
1604 msg->sol, cur_end + 1,
1605 NULL, NULL);
1606 if (unlikely(!cur_end))
1607 return 0;
1608
1609 /* we have a full HTTP/1.0 request now and we know that
1610 * we have either a CR or an LF at <ptr>.
1611 */
1612 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1613 return 1;
1614}
1615
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001616/* Parse the Connection: header of an HTTP request, looking for both "close"
1617 * and "keep-alive" values. If a buffer is provided and we already know that
1618 * some headers may safely be removed, we remove them now. The <to_del> flags
1619 * are used for that :
1620 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1621 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1622 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1623 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1624 * harmless combinations may be removed. Do not call that after changes have
1625 * been processed. If unused, the buffer can be NULL, and no data will be
1626 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001627 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628void 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 +01001629{
Willy Tarreau5b154472009-12-21 20:11:07 +01001630 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001631 const char *hdr_val = "Connection";
1632 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001633
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001634 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001635 return;
1636
Willy Tarreau88d349d2010-01-25 12:15:43 +01001637 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1638 hdr_val = "Proxy-Connection";
1639 hdr_len = 16;
1640 }
1641
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001643 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001644 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001645 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1646 txn->flags |= TX_HDR_CONN_KAL;
1647 if ((to_del & 2) && buf)
1648 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1649 else
1650 txn->flags |= TX_CON_KAL_SET;
1651 }
1652 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1653 txn->flags |= TX_HDR_CONN_CLO;
1654 if ((to_del & 1) && buf)
1655 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1656 else
1657 txn->flags |= TX_CON_CLO_SET;
1658 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001659 }
1660
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001661 txn->flags |= TX_HDR_CONN_PRS;
1662 return;
1663}
Willy Tarreau5b154472009-12-21 20:11:07 +01001664
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001665/* Apply desired changes on the Connection: header. Values may be removed and/or
1666 * added depending on the <wanted> flags, which are exclusively composed of
1667 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1668 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1669 */
1670void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1671{
1672 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001673 const char *hdr_val = "Connection";
1674 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675
1676 ctx.idx = 0;
1677
Willy Tarreau88d349d2010-01-25 12:15:43 +01001678
1679 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1680 hdr_val = "Proxy-Connection";
1681 hdr_len = 16;
1682 }
1683
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001684 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001685 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001686 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1687 if (wanted & TX_CON_KAL_SET)
1688 txn->flags |= TX_CON_KAL_SET;
1689 else
1690 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001691 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001692 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1693 if (wanted & TX_CON_CLO_SET)
1694 txn->flags |= TX_CON_CLO_SET;
1695 else
1696 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001697 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001698 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699
1700 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1701 return;
1702
1703 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1704 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001705 hdr_val = "Connection: close";
1706 hdr_len = 17;
1707 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1708 hdr_val = "Proxy-Connection: close";
1709 hdr_len = 23;
1710 }
1711 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001712 }
1713
1714 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1715 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001716 hdr_val = "Connection: keep-alive";
1717 hdr_len = 22;
1718 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1719 hdr_val = "Proxy-Connection: keep-alive";
1720 hdr_len = 28;
1721 }
1722 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001723 }
1724 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001725}
1726
Willy Tarreaud98cf932009-12-27 22:54:55 +01001727/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1728 * first byte of body, and increments msg->sov by the number of bytes parsed,
1729 * so that we know we can forward between ->som and ->sov. Note that due to
1730 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1731 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001732 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001733 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001734 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001735int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001736{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001737 char *ptr = buf->lr;
1738 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001739 unsigned int chunk = 0;
1740
1741 /* The chunk size is in the following form, though we are only
1742 * interested in the size and CRLF :
1743 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1744 */
1745 while (1) {
1746 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001747 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001749 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001750 if (c < 0) /* not a hex digit anymore */
1751 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001752 if (++ptr >= end)
1753 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001754 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001755 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001756 chunk = (chunk << 4) + c;
1757 }
1758
Willy Tarreaud98cf932009-12-27 22:54:55 +01001759 /* empty size not allowed */
1760 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001761 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001762
1763 while (http_is_spht[(unsigned char)*ptr]) {
1764 if (++ptr >= end)
1765 ptr = buf->data;
1766 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001767 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001768 }
1769
Willy Tarreaud98cf932009-12-27 22:54:55 +01001770 /* Up to there, we know that at least one byte is present at *ptr. Check
1771 * for the end of chunk size.
1772 */
1773 while (1) {
1774 if (likely(HTTP_IS_CRLF(*ptr))) {
1775 /* we now have a CR or an LF at ptr */
1776 if (likely(*ptr == '\r')) {
1777 if (++ptr >= end)
1778 ptr = buf->data;
1779 if (ptr == buf->r)
1780 return 0;
1781 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001782
Willy Tarreaud98cf932009-12-27 22:54:55 +01001783 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001784 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 if (++ptr >= end)
1786 ptr = buf->data;
1787 /* done */
1788 break;
1789 }
1790 else if (*ptr == ';') {
1791 /* chunk extension, ends at next CRLF */
1792 if (++ptr >= end)
1793 ptr = buf->data;
1794 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001795 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796
1797 while (!HTTP_IS_CRLF(*ptr)) {
1798 if (++ptr >= end)
1799 ptr = buf->data;
1800 if (ptr == buf->r)
1801 return 0;
1802 }
1803 /* we have a CRLF now, loop above */
1804 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001805 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001806 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001807 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001808 }
1809
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810 /* OK we found our CRLF and now <ptr> points to the next byte,
1811 * which may or may not be present. We save that into ->lr and
1812 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001813 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001814 msg->sov += ptr - buf->lr;
1815 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01001816 msg->chunk_len = chunk;
1817 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001819 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001820 error:
1821 msg->err_pos = ptr - buf->data;
1822 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001823}
1824
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825/* This function skips trailers in the buffer <buf> associated with HTTP
1826 * message <msg>. The first visited position is buf->lr. If the end of
1827 * the trailers is found, it is automatically scheduled to be forwarded,
1828 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1829 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001830 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1831 * zero. If a parse error is encountered, the function returns < 0 and does not
1832 * change anything except maybe buf->lr and msg->sov. Note that the message
1833 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1834 * which implies that all non-trailers data have already been scheduled for
1835 * forwarding, and that the difference between msg->som and msg->sov exactly
1836 * matches the length of trailers already parsed and not forwarded. It is also
1837 * important to note that this function is designed to be able to parse wrapped
1838 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001839 */
1840int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1841{
1842 /* we have buf->lr which points to next line. Look for CRLF. */
1843 while (1) {
1844 char *p1 = NULL, *p2 = NULL;
1845 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001846 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001847
1848 /* scan current line and stop at LF or CRLF */
1849 while (1) {
1850 if (ptr == buf->r)
1851 return 0;
1852
1853 if (*ptr == '\n') {
1854 if (!p1)
1855 p1 = ptr;
1856 p2 = ptr;
1857 break;
1858 }
1859
1860 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001861 if (p1) {
1862 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001864 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001865 p1 = ptr;
1866 }
1867
1868 ptr++;
1869 if (ptr >= buf->data + buf->size)
1870 ptr = buf->data;
1871 }
1872
1873 /* after LF; point to beginning of next line */
1874 p2++;
1875 if (p2 >= buf->data + buf->size)
1876 p2 = buf->data;
1877
Willy Tarreau638cd022010-01-03 07:42:04 +01001878 bytes = p2 - buf->lr;
1879 if (bytes < 0)
1880 bytes += buf->size;
1881
1882 /* schedule this line for forwarding */
1883 msg->sov += bytes;
1884 if (msg->sov >= buf->size)
1885 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886
Willy Tarreau638cd022010-01-03 07:42:04 +01001887 if (p1 == buf->lr) {
1888 /* LF/CRLF at beginning of line => end of trailers at p2.
1889 * Everything was scheduled for forwarding, there's nothing
1890 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001891 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001892 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001893 msg->msg_state = HTTP_MSG_DONE;
1894 return 1;
1895 }
1896 /* OK, next line then */
1897 buf->lr = p2;
1898 }
1899}
1900
1901/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1902 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1903 * ->som, buf->lr in order to include this part into the next forwarding phase.
1904 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1905 * not enough data are available, the function does not change anything and
1906 * returns zero. If a parse error is encountered, the function returns < 0 and
1907 * does not change anything. Note: this function is designed to parse wrapped
1908 * CRLF at the end of the buffer.
1909 */
1910int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1911{
1912 char *ptr;
1913 int bytes;
1914
1915 /* NB: we'll check data availabilty at the end. It's not a
1916 * problem because whatever we match first will be checked
1917 * against the correct length.
1918 */
1919 bytes = 1;
1920 ptr = buf->lr;
1921 if (*ptr == '\r') {
1922 bytes++;
1923 ptr++;
1924 if (ptr >= buf->data + buf->size)
1925 ptr = buf->data;
1926 }
1927
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01001928 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929 return 0;
1930
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001931 if (*ptr != '\n') {
1932 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001934 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001935
1936 ptr++;
1937 if (ptr >= buf->data + buf->size)
1938 ptr = buf->data;
1939 buf->lr = ptr;
1940 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
1941 msg->sov = ptr - buf->data;
1942 msg->som = msg->sov - bytes;
1943 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1944 return 1;
1945}
Willy Tarreau5b154472009-12-21 20:11:07 +01001946
Willy Tarreau83e3af02009-12-28 17:39:57 +01001947void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1948{
1949 char *end = buf->data + buf->size;
1950 int off = buf->data + buf->size - buf->w;
1951
1952 /* two possible cases :
1953 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001954 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001955 */
1956 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01001957 int block1 = buf->l;
1958 int block2 = 0;
1959 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001960 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01001961 block1 = buf->data + buf->size - buf->w;
1962 block2 = buf->r - buf->data;
1963 }
1964 if (block2)
1965 memcpy(swap_buffer, buf->data, block2);
1966 memmove(buf->data, buf->w, block1);
1967 if (block2)
1968 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001969 }
1970
1971 /* adjust all known pointers */
1972 buf->w = buf->data;
1973 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
1974 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
1975 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
1976 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
1977
1978 /* adjust relative pointers */
1979 msg->som = 0;
1980 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
1981 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
1982 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
1983
Willy Tarreau83e3af02009-12-28 17:39:57 +01001984 if (msg->err_pos >= 0) {
1985 msg->err_pos += off;
1986 if (msg->err_pos >= buf->size)
1987 msg->err_pos -= buf->size;
1988 }
1989
1990 buf->flags &= ~BF_FULL;
1991 if (buf->l >= buffer_max_len(buf))
1992 buf->flags |= BF_FULL;
1993}
1994
Willy Tarreaud787e662009-07-07 10:14:51 +02001995/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1996 * processing can continue on next analysers, or zero if it either needs more
1997 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1998 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1999 * when it has nothing left to do, and may remove any analyser when it wants to
2000 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002002int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003{
Willy Tarreau59234e92008-11-30 23:51:27 +01002004 /*
2005 * We will parse the partial (or complete) lines.
2006 * We will check the request syntax, and also join multi-line
2007 * headers. An index of all the lines will be elaborated while
2008 * parsing.
2009 *
2010 * For the parsing, we use a 28 states FSM.
2011 *
2012 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002013 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002014 * req->data + msg->eoh = end of processed headers / start of current one
2015 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002016 * req->lr = first non-visited byte
2017 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002018 *
2019 * At end of parsing, we may perform a capture of the error (if any), and
2020 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2021 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2022 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002023 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002024
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002026 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 struct http_txn *txn = &s->txn;
2028 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002029 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002030
Willy Tarreau6bf17362009-02-24 10:48:35 +01002031 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2032 now_ms, __FUNCTION__,
2033 s,
2034 req,
2035 req->rex, req->wex,
2036 req->flags,
2037 req->l,
2038 req->analysers);
2039
Willy Tarreau52a0c602009-08-16 22:45:38 +02002040 /* we're speaking HTTP here, so let's speak HTTP to the client */
2041 s->srv_error = http_return_srv_error;
2042
Willy Tarreau83e3af02009-12-28 17:39:57 +01002043 /* There's a protected area at the end of the buffer for rewriting
2044 * purposes. We don't want to start to parse the request if the
2045 * protected area is affected, because we may have to move processed
2046 * data later, which is much more complicated.
2047 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002048 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002049 if ((txn->flags & TX_NOT_FIRST) &&
2050 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002051 req->r < req->lr ||
2052 req->r > req->data + req->size - global.tune.maxrewrite)) {
2053 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002054 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2055 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002056 /* some data has still not left the buffer, wake us once that's done */
2057 buffer_dont_connect(req);
2058 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2059 return 0;
2060 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002061 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002062 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002063 }
2064
Willy Tarreau065e8332010-01-08 00:30:20 +01002065 /* Note that we have the same problem with the response ; we
2066 * may want to send a redirect, error or anything which requires
2067 * some spare space. So we'll ensure that we have at least
2068 * maxrewrite bytes available in the response buffer before
2069 * processing that one. This will only affect pipelined
2070 * keep-alive requests.
2071 */
2072 if ((txn->flags & TX_NOT_FIRST) &&
2073 unlikely((s->rep->flags & BF_FULL) ||
2074 s->rep->r < s->rep->lr ||
2075 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2076 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002077 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2078 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002079 /* don't let a connection request be initiated */
2080 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002081 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002082 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002083 return 0;
2084 }
2085 }
2086
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002087 if (likely(req->lr < req->r))
2088 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002089 }
2090
Willy Tarreau59234e92008-11-30 23:51:27 +01002091 /* 1: we might have to print this header in debug mode */
2092 if (unlikely((global.mode & MODE_DEBUG) &&
2093 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002094 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002095 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002096 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002097
Willy Tarreau663308b2010-06-07 14:06:08 +02002098 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002099 eol = sol + msg->sl.rq.l;
2100 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002101
Willy Tarreau59234e92008-11-30 23:51:27 +01002102 sol += hdr_idx_first_pos(&txn->hdr_idx);
2103 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002104
Willy Tarreau59234e92008-11-30 23:51:27 +01002105 while (cur_idx) {
2106 eol = sol + txn->hdr_idx.v[cur_idx].len;
2107 debug_hdr("clihdr", s, sol, eol);
2108 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2109 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002110 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002111 }
2112
Willy Tarreau58f10d72006-12-04 02:26:12 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 /*
2115 * Now we quickly check if we have found a full valid request.
2116 * If not so, we check the FD and buffer states before leaving.
2117 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002118 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002119 * requests are checked first. When waiting for a second request
2120 * on a keep-alive session, if we encounter and error, close, t/o,
2121 * we note the error in the session flags but don't set any state.
2122 * Since the error will be noted there, it will not be counted by
2123 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002124 * Last, we may increase some tracked counters' http request errors on
2125 * the cases that are deliberately the client's fault. For instance,
2126 * a timeout or connection reset is not counted as an error. However
2127 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002128 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002129
Willy Tarreau655dce92009-11-08 13:10:58 +01002130 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002131 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002132 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002133 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002134 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002135 session_inc_http_req_ctr(s);
2136 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002137 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002138 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002140
Willy Tarreau59234e92008-11-30 23:51:27 +01002141 /* 1: Since we are in header mode, if there's no space
2142 * left for headers, we won't be able to free more
2143 * later, so the session will never terminate. We
2144 * must terminate it now.
2145 */
2146 if (unlikely(req->flags & BF_FULL)) {
2147 /* FIXME: check if URI is set and return Status
2148 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002149 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002150 session_inc_http_req_ctr(s);
2151 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002152 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002153 if (msg->err_pos < 0)
2154 msg->err_pos = req->l;
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 goto return_bad_req;
2156 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002157
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 /* 2: have we encountered a read error ? */
2159 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002160 if (!(s->flags & SN_ERR_MASK))
2161 s->flags |= SN_ERR_CLICL;
2162
Willy Tarreaufcffa692010-01-10 14:21:19 +01002163 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002164 goto failed_keep_alive;
2165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002167 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002168 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002169 session_inc_http_err_ctr(s);
2170 }
2171
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 msg->msg_state = HTTP_MSG_ERROR;
2173 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002174
Willy Tarreauda7ff642010-06-23 11:44:09 +02002175 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002176 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002177 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002178 if (s->listener->counters)
2179 s->listener->counters->failed_req++;
2180
Willy Tarreau59234e92008-11-30 23:51:27 +01002181 if (!(s->flags & SN_FINST_MASK))
2182 s->flags |= SN_FINST_R;
2183 return 0;
2184 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 /* 3: has the read timeout expired ? */
2187 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002188 if (!(s->flags & SN_ERR_MASK))
2189 s->flags |= SN_ERR_CLITO;
2190
Willy Tarreaufcffa692010-01-10 14:21:19 +01002191 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002192 goto failed_keep_alive;
2193
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002195 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002196 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002197 session_inc_http_err_ctr(s);
2198 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002199 txn->status = 408;
2200 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2201 msg->msg_state = HTTP_MSG_ERROR;
2202 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002203
Willy Tarreauda7ff642010-06-23 11:44:09 +02002204 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002205 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002206 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002207 if (s->listener->counters)
2208 s->listener->counters->failed_req++;
2209
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 if (!(s->flags & SN_FINST_MASK))
2211 s->flags |= SN_FINST_R;
2212 return 0;
2213 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 /* 4: have we encountered a close ? */
2216 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002217 if (!(s->flags & SN_ERR_MASK))
2218 s->flags |= SN_ERR_CLICL;
2219
Willy Tarreaufcffa692010-01-10 14:21:19 +01002220 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002221 goto failed_keep_alive;
2222
Willy Tarreau4076a152009-04-02 15:18:36 +02002223 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002224 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 txn->status = 400;
2226 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2227 msg->msg_state = HTTP_MSG_ERROR;
2228 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002229
Willy Tarreauda7ff642010-06-23 11:44:09 +02002230 session_inc_http_err_ctr(s);
2231 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002232 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002233 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002234 if (s->listener->counters)
2235 s->listener->counters->failed_req++;
2236
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 if (!(s->flags & SN_FINST_MASK))
2238 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002239 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002240 }
2241
Willy Tarreau520d95e2009-09-19 21:04:57 +02002242 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002243 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002244 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002245#ifdef TCP_QUICKACK
2246 if (s->listener->options & LI_O_NOQUICKACK) {
2247 /* We need more data, we have to re-enable quick-ack in case we
2248 * previously disabled it, otherwise we might cause the client
2249 * to delay next data.
2250 */
2251 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2252 }
2253#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002254
Willy Tarreaufcffa692010-01-10 14:21:19 +01002255 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2256 /* If the client starts to talk, let's fall back to
2257 * request timeout processing.
2258 */
2259 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002260 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002261 }
2262
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002264 if (!tick_isset(req->analyse_exp)) {
2265 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2266 (txn->flags & TX_WAIT_NEXT_RQ) &&
2267 tick_isset(s->be->timeout.httpka))
2268 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2269 else
2270 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2271 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 /* we're not ready yet */
2274 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002275
2276 failed_keep_alive:
2277 /* Here we process low-level errors for keep-alive requests. In
2278 * short, if the request is not the first one and it experiences
2279 * a timeout, read error or shutdown, we just silently close so
2280 * that the client can try again.
2281 */
2282 txn->status = 0;
2283 msg->msg_state = HTTP_MSG_RQBEFORE;
2284 req->analysers = 0;
2285 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002286 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002287 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002288 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290
Willy Tarreaud787e662009-07-07 10:14:51 +02002291 /* OK now we have a complete HTTP request with indexed headers. Let's
2292 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002293 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2294 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2295 * points to the CRLF of the request line. req->lr points to the first
2296 * byte after the last LF. msg->col and msg->sov point to the first
2297 * byte of data. msg->eol cannot be trusted because it may have been
2298 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002299 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002300
Willy Tarreauda7ff642010-06-23 11:44:09 +02002301 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002302 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2303
Willy Tarreaub16a5742010-01-10 14:46:16 +01002304 if (txn->flags & TX_WAIT_NEXT_RQ) {
2305 /* kill the pending keep-alive timeout */
2306 txn->flags &= ~TX_WAIT_NEXT_RQ;
2307 req->analyse_exp = TICK_ETERNITY;
2308 }
2309
2310
Willy Tarreaud787e662009-07-07 10:14:51 +02002311 /* Maybe we found in invalid header name while we were configured not
2312 * to block on that, so we have to capture it now.
2313 */
2314 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002315 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002316
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 /*
2318 * 1: identify the method
2319 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002320 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002321
2322 /* we can make use of server redirect on GET and HEAD */
2323 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2324 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 /*
2327 * 2: check if the URI matches the monitor_uri.
2328 * We have to do this for every request which gets in, because
2329 * the monitor-uri is defined by the frontend.
2330 */
2331 if (unlikely((s->fe->monitor_uri_len != 0) &&
2332 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002333 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 s->fe->monitor_uri,
2335 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002336 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002338 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002342 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002345 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2346 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 ret = acl_pass(ret);
2349 if (cond->pol == ACL_COND_UNLESS)
2350 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002351
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 if (ret) {
2353 /* we fail this request, let's return 503 service unavail */
2354 txn->status = 503;
2355 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2356 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002357 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002358 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 /* nothing to fail, let's reply normaly */
2361 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002362 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 goto return_prx_cond;
2364 }
2365
2366 /*
2367 * 3: Maybe we have to copy the original REQURI for the logs ?
2368 * Note: we cannot log anymore if the request has been
2369 * classified as invalid.
2370 */
2371 if (unlikely(s->logs.logwait & LW_REQ)) {
2372 /* we have a complete HTTP request that we must log */
2373 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2374 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002375
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 if (urilen >= REQURI_LEN)
2377 urilen = REQURI_LEN - 1;
2378 memcpy(txn->uri, &req->data[msg->som], urilen);
2379 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (!(s->logs.logwait &= ~LW_REQ))
2382 s->do_log(s);
2383 } else {
2384 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002385 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 }
Willy Tarreau06619262006-12-17 08:37:22 +01002387
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002389 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2390 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002391
Willy Tarreau5b154472009-12-21 20:11:07 +01002392 /* ... and check if the request is HTTP/1.1 or above */
2393 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002394 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2395 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2396 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002397 txn->flags |= TX_REQ_VER_11;
2398
2399 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002400 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002401
Willy Tarreau88d349d2010-01-25 12:15:43 +01002402 /* if the frontend has "option http-use-proxy-header", we'll check if
2403 * we have what looks like a proxied connection instead of a connection,
2404 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2405 * Note that this is *not* RFC-compliant, however browsers and proxies
2406 * happen to do that despite being non-standard :-(
2407 * We consider that a request not beginning with either '/' or '*' is
2408 * a proxied connection, which covers both "scheme://location" and
2409 * CONNECT ip:port.
2410 */
2411 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2412 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2413 txn->flags |= TX_USE_PX_CONN;
2414
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002415 /* transfer length unknown*/
2416 txn->flags &= ~TX_REQ_XFER_LEN;
2417
Willy Tarreau59234e92008-11-30 23:51:27 +01002418 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002419 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002420 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002421 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002422
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002423 /* 6: determine the transfer-length.
2424 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2425 * the presence of a message-body in a REQUEST and its transfer length
2426 * must be determined that way (in order of precedence) :
2427 * 1. The presence of a message-body in a request is signaled by the
2428 * inclusion of a Content-Length or Transfer-Encoding header field
2429 * in the request's header fields. When a request message contains
2430 * both a message-body of non-zero length and a method that does
2431 * not define any semantics for that request message-body, then an
2432 * origin server SHOULD either ignore the message-body or respond
2433 * with an appropriate error message (e.g., 413). A proxy or
2434 * gateway, when presented the same request, SHOULD either forward
2435 * the request inbound with the message- body or ignore the
2436 * message-body when determining a response.
2437 *
2438 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2439 * and the "chunked" transfer-coding (Section 6.2) is used, the
2440 * transfer-length is defined by the use of this transfer-coding.
2441 * If a Transfer-Encoding header field is present and the "chunked"
2442 * transfer-coding is not present, the transfer-length is defined
2443 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002444 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 * 3. If a Content-Length header field is present, its decimal value in
2446 * OCTETs represents both the entity-length and the transfer-length.
2447 * If a message is received with both a Transfer-Encoding header
2448 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002449 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002450 * 4. By the server closing the connection. (Closing the connection
2451 * cannot be used to indicate the end of a request body, since that
2452 * would leave no possibility for the server to send back a response.)
2453 *
2454 * Whenever a transfer-coding is applied to a message-body, the set of
2455 * transfer-codings MUST include "chunked", unless the message indicates
2456 * it is terminated by closing the connection. When the "chunked"
2457 * transfer-coding is used, it MUST be the last transfer-coding applied
2458 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002459 */
2460
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002461 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002464 while ((txn->flags & TX_REQ_VER_11) &&
2465 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002466 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2467 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2468 else if (txn->flags & TX_REQ_TE_CHNK) {
2469 /* bad transfer-encoding (chunked followed by something else) */
2470 use_close_only = 1;
2471 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2472 break;
2473 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 }
2475
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2479 signed long long cl;
2480
Willy Tarreauad14f752011-09-02 20:33:27 +02002481 if (!ctx.vlen) {
2482 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002483 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002484 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002485
Willy Tarreauad14f752011-09-02 20:33:27 +02002486 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2487 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002488 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002489 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002490
Willy Tarreauad14f752011-09-02 20:33:27 +02002491 if (cl < 0) {
2492 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002494 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495
Willy Tarreauad14f752011-09-02 20:33:27 +02002496 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) {
2497 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002499 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002500
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002501 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002502 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503 }
2504
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002505 /* bodyless requests have a known length */
2506 if (!use_close_only)
2507 txn->flags |= TX_REQ_XFER_LEN;
2508
Willy Tarreaud787e662009-07-07 10:14:51 +02002509 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002510 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002511 req->analyse_exp = TICK_ETERNITY;
2512 return 1;
2513
2514 return_bad_req:
2515 /* We centralize bad requests processing here */
2516 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2517 /* we detected a parsing error. We want to archive this request
2518 * in the dedicated proxy area for later troubleshooting.
2519 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002520 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 }
2522
2523 txn->req.msg_state = HTTP_MSG_ERROR;
2524 txn->status = 400;
2525 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002526
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002527 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002528 if (s->listener->counters)
2529 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002530
2531 return_prx_cond:
2532 if (!(s->flags & SN_ERR_MASK))
2533 s->flags |= SN_ERR_PRXCOND;
2534 if (!(s->flags & SN_FINST_MASK))
2535 s->flags |= SN_FINST_R;
2536
2537 req->analysers = 0;
2538 req->analyse_exp = TICK_ETERNITY;
2539 return 0;
2540}
2541
Cyril Bonté70be45d2010-10-12 00:14:35 +02002542/* We reached the stats page through a POST request.
2543 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002544 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002545 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002546int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002547{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002548 struct proxy *px;
2549 struct server *sv;
2550
2551 char *backend = NULL;
2552 int action = 0;
2553
2554 char *first_param, *cur_param, *next_param, *end_params;
2555
2556 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002557 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002558
2559 cur_param = next_param = end_params;
2560
Cyril Bonté23b39d92011-02-10 22:54:44 +01002561 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002562 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002563 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002564 return 1;
2565 }
2566 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002567 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002568 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002569 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002570 }
2571
2572 *end_params = '\0';
2573
Willy Tarreau295a8372011-03-10 11:25:07 +01002574 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002575
2576 /*
2577 * Parse the parameters in reverse order to only store the last value.
2578 * From the html form, the backend and the action are at the end.
2579 */
2580 while (cur_param > first_param) {
2581 char *key, *value;
2582
2583 cur_param--;
2584 if ((*cur_param == '&') || (cur_param == first_param)) {
2585 /* Parse the key */
2586 key = cur_param;
2587 if (cur_param != first_param) {
2588 /* delimit the string for the next loop */
2589 *key++ = '\0';
2590 }
2591
2592 /* Parse the value */
2593 value = key;
2594 while (*value != '\0' && *value != '=') {
2595 value++;
2596 }
2597 if (*value == '=') {
2598 /* Ok, a value is found, we can mark the end of the key */
2599 *value++ = '\0';
2600 }
2601
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002602 if (!url_decode(key) || !url_decode(value))
2603 break;
2604
Cyril Bonté70be45d2010-10-12 00:14:35 +02002605 /* Now we can check the key to see what to do */
2606 if (!backend && strcmp(key, "b") == 0) {
2607 backend = value;
2608 }
2609 else if (!action && strcmp(key, "action") == 0) {
2610 if (strcmp(value, "disable") == 0) {
2611 action = 1;
2612 }
2613 else if (strcmp(value, "enable") == 0) {
2614 action = 2;
2615 } else {
2616 /* unknown action, no need to continue */
2617 break;
2618 }
2619 }
2620 else if (strcmp(key, "s") == 0) {
2621 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2622 switch (action) {
2623 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002624 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002625 /* Not already in maintenance, we can change the server state */
2626 sv->state |= SRV_MAINTAIN;
2627 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002628 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002629 }
2630 break;
2631 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002632 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002633 /* Already in maintenance, we can change the server state */
2634 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002635 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002636 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002637 }
2638 break;
2639 }
2640 }
2641 }
2642 next_param = cur_param;
2643 }
2644 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002645 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646}
2647
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002648/* returns a pointer to the first rule which forbids access (deny or http_auth),
2649 * or NULL if everything's OK.
2650 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002651static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002652http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2653{
Willy Tarreauff011f22011-01-06 17:51:27 +01002654 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002655
Willy Tarreauff011f22011-01-06 17:51:27 +01002656 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002657 int ret = 1;
2658
Willy Tarreauff011f22011-01-06 17:51:27 +01002659 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002660 continue;
2661
2662 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002663 if (rule->cond) {
2664 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002665 ret = acl_pass(ret);
2666
Willy Tarreauff011f22011-01-06 17:51:27 +01002667 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002668 ret = !ret;
2669 }
2670
2671 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002672 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002673 return NULL; /* no problem */
2674 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002675 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002676 }
2677 }
2678 return NULL;
2679}
2680
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002681/* This stream analyser runs all HTTP request processing which is common to
2682 * frontends and backends, which means blocking ACLs, filters, connection-close,
2683 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002684 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002685 * either needs more data or wants to immediately abort the request (eg: deny,
2686 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002687 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002688int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002689{
Willy Tarreaud787e662009-07-07 10:14:51 +02002690 struct http_txn *txn = &s->txn;
2691 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002692 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002693 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002694 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002695 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002696 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002697
Willy Tarreau655dce92009-11-08 13:10:58 +01002698 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002699 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002700 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002701 return 0;
2702 }
2703
Willy Tarreau3a816292009-07-07 10:55:49 +02002704 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002705 req->analyse_exp = TICK_ETERNITY;
2706
2707 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2708 now_ms, __FUNCTION__,
2709 s,
2710 req,
2711 req->rex, req->wex,
2712 req->flags,
2713 req->l,
2714 req->analysers);
2715
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002716 /* first check whether we have some ACLs set to block this request */
2717 list_for_each_entry(cond, &px->block_cond, list) {
2718 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002719
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002720 ret = acl_pass(ret);
2721 if (cond->pol == ACL_COND_UNLESS)
2722 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002723
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002724 if (ret) {
2725 txn->status = 403;
2726 /* let's log the request time */
2727 s->logs.tv_request = now;
2728 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002729 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002730 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002731 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002732 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002733
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002734 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002735 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002736
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002737 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002738 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002739 do_stats = stats_check_uri(s->rep->prod, txn, px);
2740 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002741 http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002742 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002743 else
2744 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002745
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002746 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002747 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002748 txn->status = 403;
2749 s->logs.tv_request = now;
2750 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002751 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002752 s->fe->fe_counters.denied_req++;
2753 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2754 s->be->be_counters.denied_req++;
2755 if (s->listener->counters)
2756 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002757 goto return_prx_cond;
2758 }
2759
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 /* try headers filters */
2761 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002762 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002764
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 /* has the request been denied ? */
2766 if (txn->flags & TX_CLDENY) {
2767 /* no need to go further */
2768 txn->status = 403;
2769 /* let's log the request time */
2770 s->logs.tv_request = now;
2771 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002772 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 goto return_prx_cond;
2774 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002775
2776 /* When a connection is tarpitted, we use the tarpit timeout,
2777 * which may be the same as the connect timeout if unspecified.
2778 * If unset, then set it to zero because we really want it to
2779 * eventually expire. We build the tarpit as an analyser.
2780 */
2781 if (txn->flags & TX_CLTARPIT) {
2782 buffer_erase(s->req);
2783 /* wipe the request out so that we can drop the connection early
2784 * if the client closes first.
2785 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002786 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002787 req->analysers = 0; /* remove switching rules etc... */
2788 req->analysers |= AN_REQ_HTTP_TARPIT;
2789 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2790 if (!req->analyse_exp)
2791 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002792 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002793 return 1;
2794 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002795 }
Willy Tarreau06619262006-12-17 08:37:22 +01002796
Willy Tarreau5b154472009-12-21 20:11:07 +01002797 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2798 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002799 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2800 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002801 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2802 * avoid to redo the same work if FE and BE have the same settings (common).
2803 * The method consists in checking if options changed between the two calls
2804 * (implying that either one is non-null, or one of them is non-null and we
2805 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002806 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002807
Willy Tarreaudc008c52010-02-01 16:20:08 +01002808 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2809 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2810 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2811 (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 +01002812 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002813
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002814 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2815 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002816 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002817 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2818 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002819 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002820 tmp = TX_CON_WANT_CLO;
2821
Willy Tarreau5b154472009-12-21 20:11:07 +01002822 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2823 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002824
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002825 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2826 /* parse the Connection header and possibly clean it */
2827 int to_del = 0;
2828 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002829 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2830 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002831 to_del |= 2; /* remove "keep-alive" */
2832 if (!(txn->flags & TX_REQ_VER_11))
2833 to_del |= 1; /* remove "close" */
2834 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002835 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002836
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002837 /* check if client or config asks for explicit close in KAL/SCL */
2838 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2839 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2840 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2841 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002842 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002843 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2844 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002845 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2846 }
Willy Tarreau78599912009-10-17 20:12:21 +02002847
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002848 /* we can be blocked here because the request needs to be authenticated,
2849 * either to pass or to access stats.
2850 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002851 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002852 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002853 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002854
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002855 if (!realm)
2856 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2857
Willy Tarreau844a7e72010-01-31 21:46:18 +01002858 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002859 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2860 txn->status = 401;
2861 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002862 /* on 401 we still count one error, because normal browsing
2863 * won't significantly increase the counter but brute force
2864 * attempts will.
2865 */
2866 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002867 goto return_prx_cond;
2868 }
2869
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002870 /* add request headers from the rule sets in the same order */
2871 list_for_each_entry(wl, &px->req_add, list) {
2872 if (wl->cond) {
2873 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2874 ret = acl_pass(ret);
2875 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2876 ret = !ret;
2877 if (!ret)
2878 continue;
2879 }
2880
2881 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
2882 goto return_bad_req;
2883 }
2884
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002885 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002886 struct stats_admin_rule *stats_admin_rule;
2887
2888 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002889 * FIXME!!! that one is rather dangerous, we want to
2890 * make it follow standard rules (eg: clear req->analysers).
2891 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002892
Cyril Bonté474be412010-10-12 00:14:36 +02002893 /* now check whether we have some admin rules for this request */
2894 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2895 int ret = 1;
2896
2897 if (stats_admin_rule->cond) {
2898 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2899 ret = acl_pass(ret);
2900 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2901 ret = !ret;
2902 }
2903
2904 if (ret) {
2905 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002906 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002907 break;
2908 }
2909 }
2910
Cyril Bonté70be45d2010-10-12 00:14:35 +02002911 /* Was the status page requested with a POST ? */
2912 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002913 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002914 if (msg->msg_state < HTTP_MSG_100_SENT) {
2915 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2916 * send an HTTP/1.1 100 Continue intermediate response.
2917 */
2918 if (txn->flags & TX_REQ_VER_11) {
2919 struct hdr_ctx ctx;
2920 ctx.idx = 0;
2921 /* Expect is allowed in 1.1, look for it */
2922 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
2923 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2924 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2925 }
2926 }
2927 msg->msg_state = HTTP_MSG_100_SENT;
2928 s->logs.tv_request = now; /* update the request timer to reflect full request */
2929 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002930 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002931 /* we need more data */
2932 req->analysers |= an_bit;
2933 buffer_dont_connect(req);
2934 return 0;
2935 }
Cyril Bonté474be412010-10-12 00:14:36 +02002936 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01002937 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02002938 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002939 }
2940
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002941 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002942 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002943 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02002944 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01002945 s->rep->prod->applet.private = s;
2946 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002947 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02002948 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
2949 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002950
2951 return 0;
2952
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002953 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002954
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002955 /* check whether we have some ACLs set to redirect this request */
2956 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002957 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002958
Willy Tarreauf285f542010-01-03 20:03:03 +01002959 if (rule->cond) {
2960 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2961 ret = acl_pass(ret);
2962 if (rule->cond->pol == ACL_COND_UNLESS)
2963 ret = !ret;
2964 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002965
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002967 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002968 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002969
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002970 /* build redirect message */
2971 switch(rule->code) {
2972 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002973 msg_fmt = HTTP_303;
2974 break;
2975 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002976 msg_fmt = HTTP_301;
2977 break;
2978 case 302:
2979 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002980 msg_fmt = HTTP_302;
2981 break;
2982 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002983
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002984 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002985 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002986
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002987 switch(rule->type) {
2988 case REDIRECT_TYPE_PREFIX: {
2989 const char *path;
2990 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002991
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002992 path = http_get_path(txn);
2993 /* build message using path */
2994 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01002995 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002996 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2997 int qs = 0;
2998 while (qs < pathlen) {
2999 if (path[qs] == '?') {
3000 pathlen = qs;
3001 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003002 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003003 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003004 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003005 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003006 } else {
3007 path = "/";
3008 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003009 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003010
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003011 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003012 goto return_bad_req;
3013
3014 /* add prefix. Note that if prefix == "/", we don't want to
3015 * add anything, otherwise it makes it hard for the user to
3016 * configure a self-redirection.
3017 */
3018 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003019 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3020 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003021 }
3022
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023 /* add path */
3024 memcpy(rdr.str + rdr.len, path, pathlen);
3025 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003026
3027 /* append a slash at the end of the location is needed and missing */
3028 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3029 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3030 if (rdr.len > rdr.size - 5)
3031 goto return_bad_req;
3032 rdr.str[rdr.len] = '/';
3033 rdr.len++;
3034 }
3035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 break;
3037 }
3038 case REDIRECT_TYPE_LOCATION:
3039 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003040 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003041 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 /* add location */
3044 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3045 rdr.len += rule->rdr_len;
3046 break;
3047 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 if (rule->cookie_len) {
3050 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3051 rdr.len += 14;
3052 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3053 rdr.len += rule->cookie_len;
3054 memcpy(rdr.str + rdr.len, "\r\n", 2);
3055 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003056 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003057
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003058 /* add end of headers and the keep-alive/close status.
3059 * We may choose to set keep-alive if the Location begins
3060 * with a slash, because the client will come back to the
3061 * same server.
3062 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 txn->status = rule->code;
3064 /* let's log the request time */
3065 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003066
3067 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3068 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003069 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003070 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3071 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3072 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003073 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003074 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3075 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3076 rdr.len += 30;
3077 } else {
3078 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3079 rdr.len += 24;
3080 }
Willy Tarreau75661452010-01-10 10:35:01 +01003081 }
3082 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3083 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003084 buffer_write(req->prod->ob, rdr.str, rdr.len);
3085 /* "eat" the request */
3086 buffer_ignore(req, msg->sov - msg->som);
3087 msg->som = msg->sov;
3088 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003089 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3090 txn->req.msg_state = HTTP_MSG_CLOSED;
3091 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003092 break;
3093 } else {
3094 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003095 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3096 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3097 rdr.len += 29;
3098 } else {
3099 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3100 rdr.len += 23;
3101 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003102 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003103 goto return_prx_cond;
3104 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003105 }
3106 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003107
Willy Tarreau2be39392010-01-03 17:24:51 +01003108 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3109 * If this happens, then the data will not come immediately, so we must
3110 * send all what we have without waiting. Note that due to the small gain
3111 * in waiting for the body of the request, it's easier to simply put the
3112 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3113 * itself once used.
3114 */
3115 req->flags |= BF_SEND_DONTWAIT;
3116
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003117 /* that's OK for us now, let's move on to next analysers */
3118 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003119
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003120 return_bad_req:
3121 /* We centralize bad requests processing here */
3122 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3123 /* we detected a parsing error. We want to archive this request
3124 * in the dedicated proxy area for later troubleshooting.
3125 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003126 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003127 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003128
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003129 txn->req.msg_state = HTTP_MSG_ERROR;
3130 txn->status = 400;
3131 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003132
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003133 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003134 if (s->listener->counters)
3135 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003136
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003137 return_prx_cond:
3138 if (!(s->flags & SN_ERR_MASK))
3139 s->flags |= SN_ERR_PRXCOND;
3140 if (!(s->flags & SN_FINST_MASK))
3141 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003142
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003143 req->analysers = 0;
3144 req->analyse_exp = TICK_ETERNITY;
3145 return 0;
3146}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003147
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003148/* This function performs all the processing enabled for the current request.
3149 * It returns 1 if the processing can continue on next analysers, or zero if it
3150 * needs more data, encounters an error, or wants to immediately abort the
3151 * request. It relies on buffers flags, and updates s->req->analysers.
3152 */
3153int http_process_request(struct session *s, struct buffer *req, int an_bit)
3154{
3155 struct http_txn *txn = &s->txn;
3156 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003157
Willy Tarreau655dce92009-11-08 13:10:58 +01003158 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003159 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003160 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003161 return 0;
3162 }
3163
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003164 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3165 now_ms, __FUNCTION__,
3166 s,
3167 req,
3168 req->rex, req->wex,
3169 req->flags,
3170 req->l,
3171 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003172
Willy Tarreau59234e92008-11-30 23:51:27 +01003173 /*
3174 * Right now, we know that we have processed the entire headers
3175 * and that unwanted requests have been filtered out. We can do
3176 * whatever we want with the remaining request. Also, now we
3177 * may have separate values for ->fe, ->be.
3178 */
Willy Tarreau06619262006-12-17 08:37:22 +01003179
Willy Tarreau59234e92008-11-30 23:51:27 +01003180 /*
3181 * If HTTP PROXY is set we simply get remote server address
3182 * parsing incoming request.
3183 */
3184 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau6471afb2011-09-23 10:54:59 +02003185 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003186 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003187
Willy Tarreau59234e92008-11-30 23:51:27 +01003188 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003189 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003190 * Note that doing so might move headers in the request, but
3191 * the fields will stay coherent and the URI will not move.
3192 * This should only be performed in the backend.
3193 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003194 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003195 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3196 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003197
Willy Tarreau59234e92008-11-30 23:51:27 +01003198 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003199 * 8: the appsession cookie was looked up very early in 1.2,
3200 * so let's do the same now.
3201 */
3202
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003203 /* It needs to look into the URI unless persistence must be ignored */
3204 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003205 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003206 }
3207
3208 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003209 * 9: add X-Forwarded-For if either the frontend or the backend
3210 * asks for it.
3211 */
3212 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003213 struct hdr_ctx ctx = { .idx = 0 };
3214
3215 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Sagi Bashari1611e2d2011-10-08 22:48:48 +02003216 http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003217 /* The header is set to be added only if none is present
3218 * and we found it, so don't do anything.
3219 */
3220 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003221 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003222 /* Add an X-Forwarded-For header unless the source IP is
3223 * in the 'except' network range.
3224 */
3225 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003226 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003227 != s->fe->except_net.s_addr) &&
3228 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003229 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003230 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003231 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003232 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003233 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003234
3235 /* Note: we rely on the backend to get the header name to be used for
3236 * x-forwarded-for, because the header is really meant for the backends.
3237 * However, if the backend did not specify any option, we have to rely
3238 * on the frontend's header name.
3239 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003240 if (s->be->fwdfor_hdr_len) {
3241 len = s->be->fwdfor_hdr_len;
3242 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003243 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003244 len = s->fe->fwdfor_hdr_len;
3245 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003246 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003248
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003249 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003250 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003251 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003252 }
3253 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003254 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003255 /* FIXME: for the sake of completeness, we should also support
3256 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003257 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003258 int len;
3259 char pn[INET6_ADDRSTRLEN];
3260 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003261 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003262 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003263
Willy Tarreau59234e92008-11-30 23:51:27 +01003264 /* Note: we rely on the backend to get the header name to be used for
3265 * x-forwarded-for, because the header is really meant for the backends.
3266 * However, if the backend did not specify any option, we have to rely
3267 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003268 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003269 if (s->be->fwdfor_hdr_len) {
3270 len = s->be->fwdfor_hdr_len;
3271 memcpy(trash, s->be->fwdfor_hdr_name, len);
3272 } else {
3273 len = s->fe->fwdfor_hdr_len;
3274 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003275 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003277
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003279 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003280 goto return_bad_req;
3281 }
3282 }
3283
3284 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003285 * 10: add X-Original-To if either the frontend or the backend
3286 * asks for it.
3287 */
3288 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3289
3290 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003291 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003292 /* Add an X-Original-To header unless the destination IP is
3293 * in the 'except' network range.
3294 */
3295 if (!(s->flags & SN_FRT_ADDR_SET))
3296 get_frt_addr(s);
3297
Willy Tarreau6471afb2011-09-23 10:54:59 +02003298 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003299 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003300 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003301 != s->fe->except_to.s_addr) &&
3302 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003303 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003304 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003305 int len;
3306 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003307 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003308
3309 /* Note: we rely on the backend to get the header name to be used for
3310 * x-original-to, 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 */
3314 if (s->be->orgto_hdr_len) {
3315 len = s->be->orgto_hdr_len;
3316 memcpy(trash, s->be->orgto_hdr_name, len);
3317 } else {
3318 len = s->fe->orgto_hdr_len;
3319 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003320 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003321 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3322
3323 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003324 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003325 goto return_bad_req;
3326 }
3327 }
3328 }
3329
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003330 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3331 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003332 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003333 unsigned int want_flags = 0;
3334
3335 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003336 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3337 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3338 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003339 want_flags |= TX_CON_CLO_SET;
3340 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003341 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3342 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3343 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003344 want_flags |= TX_CON_KAL_SET;
3345 }
3346
3347 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3348 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003349 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003350
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003351
Willy Tarreau522d6c02009-12-06 18:49:18 +01003352 /* If we have no server assigned yet and we're balancing on url_param
3353 * with a POST request, we may be interested in checking the body for
3354 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003355 */
3356 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3357 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003358 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003359 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003360 buffer_dont_connect(req);
3361 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003362 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003363
Willy Tarreau5e205522011-12-17 16:34:27 +01003364 if (txn->flags & TX_REQ_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003365 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003366#ifdef TCP_QUICKACK
3367 /* We expect some data from the client. Unless we know for sure
3368 * we already have a full request, we have to re-enable quick-ack
3369 * in case we previously disabled it, otherwise we might cause
3370 * the client to delay further data.
3371 */
3372 if ((s->listener->options & LI_O_NOQUICKACK) &&
3373 ((txn->flags & TX_REQ_TE_CHNK) ||
3374 (msg->body_len > req->l - txn->req.eoh - 2)))
3375 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3376#endif
3377 }
Willy Tarreau03945942009-12-22 16:50:27 +01003378
Willy Tarreau59234e92008-11-30 23:51:27 +01003379 /*************************************************************
3380 * OK, that's finished for the headers. We have done what we *
3381 * could. Let's switch to the DATA state. *
3382 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003383 req->analyse_exp = TICK_ETERNITY;
3384 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003385
Willy Tarreau59234e92008-11-30 23:51:27 +01003386 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003387 /* OK let's go on with the BODY now */
3388 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003389
Willy Tarreau59234e92008-11-30 23:51:27 +01003390 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003391 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003392 /* we detected a parsing error. We want to archive this request
3393 * in the dedicated proxy area for later troubleshooting.
3394 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003395 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003396 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003397
Willy Tarreau59234e92008-11-30 23:51:27 +01003398 txn->req.msg_state = HTTP_MSG_ERROR;
3399 txn->status = 400;
3400 req->analysers = 0;
3401 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003402
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003403 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003404 if (s->listener->counters)
3405 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003406
Willy Tarreau59234e92008-11-30 23:51:27 +01003407 if (!(s->flags & SN_ERR_MASK))
3408 s->flags |= SN_ERR_PRXCOND;
3409 if (!(s->flags & SN_FINST_MASK))
3410 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003411 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003412}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003413
Willy Tarreau60b85b02008-11-30 23:28:40 +01003414/* This function is an analyser which processes the HTTP tarpit. It always
3415 * returns zero, at the beginning because it prevents any other processing
3416 * from occurring, and at the end because it terminates the request.
3417 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003418int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003419{
3420 struct http_txn *txn = &s->txn;
3421
3422 /* This connection is being tarpitted. The CLIENT side has
3423 * already set the connect expiration date to the right
3424 * timeout. We just have to check that the client is still
3425 * there and that the timeout has not expired.
3426 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003427 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003428 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3429 !tick_is_expired(req->analyse_exp, now_ms))
3430 return 0;
3431
3432 /* We will set the queue timer to the time spent, just for
3433 * logging purposes. We fake a 500 server error, so that the
3434 * attacker will not suspect his connection has been tarpitted.
3435 * It will not cause trouble to the logs because we can exclude
3436 * the tarpitted connections by filtering on the 'PT' status flags.
3437 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003438 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3439
3440 txn->status = 500;
3441 if (req->flags != BF_READ_ERROR)
3442 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3443
3444 req->analysers = 0;
3445 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003446
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003447 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003448 if (s->listener->counters)
3449 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003450
Willy Tarreau60b85b02008-11-30 23:28:40 +01003451 if (!(s->flags & SN_ERR_MASK))
3452 s->flags |= SN_ERR_PRXCOND;
3453 if (!(s->flags & SN_FINST_MASK))
3454 s->flags |= SN_FINST_T;
3455 return 0;
3456}
3457
Willy Tarreaud34af782008-11-30 23:36:37 +01003458/* This function is an analyser which processes the HTTP request body. It looks
3459 * for parameters to be used for the load balancing algorithm (url_param). It
3460 * must only be called after the standard HTTP request processing has occurred,
3461 * because it expects the request to be parsed. It returns zero if it needs to
3462 * read more data, or 1 once it has completed its analysis.
3463 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003464int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003465{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003466 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003467 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003468 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003469
3470 /* We have to parse the HTTP request body to find any required data.
3471 * "balance url_param check_post" should have been the only way to get
3472 * into this. We were brought here after HTTP header analysis, so all
3473 * related structures are ready.
3474 */
3475
Willy Tarreau522d6c02009-12-06 18:49:18 +01003476 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3477 goto missing_data;
3478
3479 if (msg->msg_state < HTTP_MSG_100_SENT) {
3480 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3481 * send an HTTP/1.1 100 Continue intermediate response.
3482 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003483 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003484 struct hdr_ctx ctx;
3485 ctx.idx = 0;
3486 /* Expect is allowed in 1.1, look for it */
3487 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3488 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3489 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3490 }
3491 }
3492 msg->msg_state = HTTP_MSG_100_SENT;
3493 }
3494
3495 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003496 /* we have msg->col and msg->sov which both point to the first
3497 * byte of message body. msg->som still points to the beginning
3498 * of the message. We must save the body in req->lr because it
3499 * survives buffer re-alignments.
3500 */
3501 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003502 if (txn->flags & TX_REQ_TE_CHNK)
3503 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3504 else
3505 msg->msg_state = HTTP_MSG_DATA;
3506 }
3507
3508 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003509 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003510 * set ->sov and ->lr to point to the body and switch to DATA or
3511 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003512 */
3513 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003514
Willy Tarreau115acb92009-12-26 13:56:06 +01003515 if (!ret)
3516 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003517 else if (ret < 0) {
3518 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003519 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003520 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003521 }
3522
Willy Tarreaud98cf932009-12-27 22:54:55 +01003523 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003524 * We have the first non-header byte in msg->col, which is either the
3525 * beginning of the chunk size or of the data. The first data byte is in
3526 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3527 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003528 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003529
Willy Tarreau124d9912011-03-01 20:30:48 +01003530 if (msg->body_len < limit)
3531 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003532
Willy Tarreau7c96f672009-12-27 22:47:25 +01003533 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003534 goto http_end;
3535
3536 missing_data:
3537 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003538 if (req->flags & BF_FULL) {
3539 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003540 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003541 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003542
Willy Tarreau522d6c02009-12-06 18:49:18 +01003543 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3544 txn->status = 408;
3545 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003546
3547 if (!(s->flags & SN_ERR_MASK))
3548 s->flags |= SN_ERR_CLITO;
3549 if (!(s->flags & SN_FINST_MASK))
3550 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003551 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003552 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003553
3554 /* we get here if we need to wait for more data */
3555 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003556 /* Not enough data. We'll re-use the http-request
3557 * timeout here. Ideally, we should set the timeout
3558 * relative to the accept() date. We just set the
3559 * request timeout once at the beginning of the
3560 * request.
3561 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003562 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003563 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003564 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003565 return 0;
3566 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003567
3568 http_end:
3569 /* The situation will not evolve, so let's give up on the analysis. */
3570 s->logs.tv_request = now; /* update the request timer to reflect full request */
3571 req->analysers &= ~an_bit;
3572 req->analyse_exp = TICK_ETERNITY;
3573 return 1;
3574
3575 return_bad_req: /* let's centralize all bad requests */
3576 txn->req.msg_state = HTTP_MSG_ERROR;
3577 txn->status = 400;
3578 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3579
Willy Tarreau79ebac62010-06-07 13:47:49 +02003580 if (!(s->flags & SN_ERR_MASK))
3581 s->flags |= SN_ERR_PRXCOND;
3582 if (!(s->flags & SN_FINST_MASK))
3583 s->flags |= SN_FINST_R;
3584
Willy Tarreau522d6c02009-12-06 18:49:18 +01003585 return_err_msg:
3586 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003587 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003588 if (s->listener->counters)
3589 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003590 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003591}
3592
Mark Lamourinec2247f02012-01-04 13:02:01 -05003593int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3594
3595 struct hdr_ctx ctx;
3596
Mark Lamourinec2247f02012-01-04 13:02:01 -05003597 char *hdr_name = be->server_id_hdr_name;
3598 int hdr_name_len = be->server_id_hdr_len;
3599
3600 char *hdr_val;
3601
William Lallemandd9e90662012-01-30 17:27:17 +01003602 ctx.idx = 0;
3603
Mark Lamourinec2247f02012-01-04 13:02:01 -05003604 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3605 /* remove any existing values from the header */
3606 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3607 }
3608
3609 /* Add the new header requested with the server value */
3610 hdr_val = trash;
3611 memcpy(hdr_val, hdr_name, hdr_name_len);
3612 hdr_val += hdr_name_len;
3613 *hdr_val++ = ':';
3614 *hdr_val++ = ' ';
3615 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3616 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3617
3618 return 0;
3619}
3620
Willy Tarreau610ecce2010-01-04 21:15:02 +01003621/* Terminate current transaction and prepare a new one. This is very tricky
3622 * right now but it works.
3623 */
3624void http_end_txn_clean_session(struct session *s)
3625{
3626 /* FIXME: We need a more portable way of releasing a backend's and a
3627 * server's connections. We need a safer way to reinitialize buffer
3628 * flags. We also need a more accurate method for computing per-request
3629 * data.
3630 */
3631 http_silent_debug(__LINE__, s);
3632
3633 s->req->cons->flags |= SI_FL_NOLINGER;
3634 s->req->cons->shutr(s->req->cons);
3635 s->req->cons->shutw(s->req->cons);
3636
3637 http_silent_debug(__LINE__, s);
3638
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003639 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003640 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003641 if (unlikely(s->srv_conn))
3642 sess_change_server(s, NULL);
3643 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003644
3645 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3646 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003647 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003648
3649 if (s->txn.status) {
3650 int n;
3651
3652 n = s->txn.status / 100;
3653 if (n < 1 || n > 5)
3654 n = 0;
3655
3656 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003657 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003658
Willy Tarreau24657792010-02-26 10:30:28 +01003659 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003660 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003661 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003662 }
3663
3664 /* don't count other requests' data */
3665 s->logs.bytes_in -= s->req->l - s->req->send_max;
3666 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3667
3668 /* let's do a final log if we need it */
3669 if (s->logs.logwait &&
3670 !(s->flags & SN_MONITOR) &&
3671 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3672 s->do_log(s);
3673 }
3674
3675 s->logs.accept_date = date; /* user-visible date for logging */
3676 s->logs.tv_accept = now; /* corrected date for internal use */
3677 tv_zero(&s->logs.tv_request);
3678 s->logs.t_queue = -1;
3679 s->logs.t_connect = -1;
3680 s->logs.t_data = -1;
3681 s->logs.t_close = 0;
3682 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3683 s->logs.srv_queue_size = 0; /* we will get this number soon */
3684
3685 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3686 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3687
3688 if (s->pend_pos)
3689 pendconn_free(s->pend_pos);
3690
Willy Tarreau827aee92011-03-10 16:55:02 +01003691 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003692 if (s->flags & SN_CURR_SESS) {
3693 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003694 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003695 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003696 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3697 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003698 }
3699
Willy Tarreau9e000c62011-03-10 14:03:36 +01003700 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701
3702 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3703 s->req->cons->fd = -1; /* just to help with debugging */
3704 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003705 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003706 s->req->cons->err_loc = NULL;
3707 s->req->cons->exp = TICK_ETERNITY;
3708 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003709 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3710 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|BF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003711 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003712 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3713 s->txn.meth = 0;
3714 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003715 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003716 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717 s->req->cons->flags |= SI_FL_INDEP_STR;
3718
Willy Tarreau96e31212011-05-30 18:10:30 +02003719 if (s->fe->options2 & PR_O2_NODELAY) {
3720 s->req->flags |= BF_NEVER_WAIT;
3721 s->rep->flags |= BF_NEVER_WAIT;
3722 }
3723
Willy Tarreau610ecce2010-01-04 21:15:02 +01003724 /* if the request buffer is not empty, it means we're
3725 * about to process another request, so send pending
3726 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003727 * Just don't do this if the buffer is close to be full,
3728 * because the request will wait for it to flush a little
3729 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003730 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003731 if (s->req->l > s->req->send_max) {
3732 if (s->rep->send_max &&
3733 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003734 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3735 s->rep->flags |= BF_EXPECT_MORE;
3736 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003737
3738 /* we're removing the analysers, we MUST re-enable events detection */
3739 buffer_auto_read(s->req);
3740 buffer_auto_close(s->req);
3741 buffer_auto_read(s->rep);
3742 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743
3744 /* make ->lr point to the first non-forwarded byte */
3745 s->req->lr = s->req->w + s->req->send_max;
3746 if (s->req->lr >= s->req->data + s->req->size)
3747 s->req->lr -= s->req->size;
3748 s->rep->lr = s->rep->w + s->rep->send_max;
3749 if (s->rep->lr >= s->rep->data + s->rep->size)
3750 s->rep->lr -= s->req->size;
3751
Willy Tarreau342b11c2010-11-24 16:22:09 +01003752 s->req->analysers = s->listener->analysers;
3753 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003754 s->rep->analysers = 0;
3755
3756 http_silent_debug(__LINE__, s);
3757}
3758
3759
3760/* This function updates the request state machine according to the response
3761 * state machine and buffer flags. It returns 1 if it changes anything (flag
3762 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3763 * it is only used to find when a request/response couple is complete. Both
3764 * this function and its equivalent should loop until both return zero. It
3765 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3766 */
3767int http_sync_req_state(struct session *s)
3768{
3769 struct buffer *buf = s->req;
3770 struct http_txn *txn = &s->txn;
3771 unsigned int old_flags = buf->flags;
3772 unsigned int old_state = txn->req.msg_state;
3773
3774 http_silent_debug(__LINE__, s);
3775 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3776 return 0;
3777
3778 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003779 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003780 * We can shut the read side unless we want to abort_on_close,
3781 * or we have a POST request. The issue with POST requests is
3782 * that some browsers still send a CRLF after the request, and
3783 * this CRLF must be read so that it does not remain in the kernel
3784 * buffers, otherwise a close could cause an RST on some systems
3785 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003786 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003787 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003788 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789
3790 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3791 goto wait_other_side;
3792
3793 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3794 /* The server has not finished to respond, so we
3795 * don't want to move in order not to upset it.
3796 */
3797 goto wait_other_side;
3798 }
3799
3800 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3801 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003802 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003803 txn->req.msg_state = HTTP_MSG_TUNNEL;
3804 goto wait_other_side;
3805 }
3806
3807 /* When we get here, it means that both the request and the
3808 * response have finished receiving. Depending on the connection
3809 * mode, we'll have to wait for the last bytes to leave in either
3810 * direction, and sometimes for a close to be effective.
3811 */
3812
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003813 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3814 /* Server-close mode : queue a connection close to the server */
3815 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003816 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003817 }
3818 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3819 /* Option forceclose is set, or either side wants to close,
3820 * let's enforce it now that we're not expecting any new
3821 * data to come. The caller knows the session is complete
3822 * once both states are CLOSED.
3823 */
3824 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003825 buffer_shutr_now(buf);
3826 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003827 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003828 }
3829 else {
3830 /* The last possible modes are keep-alive and tunnel. Since tunnel
3831 * mode does not set the body analyser, we can't reach this place
3832 * in tunnel mode, so we're left with keep-alive only.
3833 * This mode is currently not implemented, we switch to tunnel mode.
3834 */
3835 buffer_auto_read(buf);
3836 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003837 }
3838
3839 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3840 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003841 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3842
Willy Tarreau610ecce2010-01-04 21:15:02 +01003843 if (!(buf->flags & BF_OUT_EMPTY)) {
3844 txn->req.msg_state = HTTP_MSG_CLOSING;
3845 goto http_msg_closing;
3846 }
3847 else {
3848 txn->req.msg_state = HTTP_MSG_CLOSED;
3849 goto http_msg_closed;
3850 }
3851 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003852 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853 }
3854
3855 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3856 http_msg_closing:
3857 /* nothing else to forward, just waiting for the output buffer
3858 * to be empty and for the shutw_now to take effect.
3859 */
3860 if (buf->flags & BF_OUT_EMPTY) {
3861 txn->req.msg_state = HTTP_MSG_CLOSED;
3862 goto http_msg_closed;
3863 }
3864 else if (buf->flags & BF_SHUTW) {
3865 txn->req.msg_state = HTTP_MSG_ERROR;
3866 goto wait_other_side;
3867 }
3868 }
3869
3870 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3871 http_msg_closed:
3872 goto wait_other_side;
3873 }
3874
3875 wait_other_side:
3876 http_silent_debug(__LINE__, s);
3877 return txn->req.msg_state != old_state || buf->flags != old_flags;
3878}
3879
3880
3881/* This function updates the response state machine according to the request
3882 * state machine and buffer flags. It returns 1 if it changes anything (flag
3883 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3884 * it is only used to find when a request/response couple is complete. Both
3885 * this function and its equivalent should loop until both return zero. It
3886 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3887 */
3888int http_sync_res_state(struct session *s)
3889{
3890 struct buffer *buf = s->rep;
3891 struct http_txn *txn = &s->txn;
3892 unsigned int old_flags = buf->flags;
3893 unsigned int old_state = txn->rsp.msg_state;
3894
3895 http_silent_debug(__LINE__, s);
3896 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3897 return 0;
3898
3899 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3900 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003901 * still monitor the server connection for a possible close
3902 * while the request is being uploaded, so we don't disable
3903 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003905 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003906
3907 if (txn->req.msg_state == HTTP_MSG_ERROR)
3908 goto wait_other_side;
3909
3910 if (txn->req.msg_state < HTTP_MSG_DONE) {
3911 /* The client seems to still be sending data, probably
3912 * because we got an error response during an upload.
3913 * We have the choice of either breaking the connection
3914 * or letting it pass through. Let's do the later.
3915 */
3916 goto wait_other_side;
3917 }
3918
3919 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3920 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003921 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003922 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3923 goto wait_other_side;
3924 }
3925
3926 /* When we get here, it means that both the request and the
3927 * response have finished receiving. Depending on the connection
3928 * mode, we'll have to wait for the last bytes to leave in either
3929 * direction, and sometimes for a close to be effective.
3930 */
3931
3932 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3933 /* Server-close mode : shut read and wait for the request
3934 * side to close its output buffer. The caller will detect
3935 * when we're in DONE and the other is in CLOSED and will
3936 * catch that for the final cleanup.
3937 */
3938 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3939 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003940 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003941 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3942 /* Option forceclose is set, or either side wants to close,
3943 * let's enforce it now that we're not expecting any new
3944 * data to come. The caller knows the session is complete
3945 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003946 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003947 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3948 buffer_shutr_now(buf);
3949 buffer_shutw_now(buf);
3950 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003951 }
3952 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003953 /* The last possible modes are keep-alive and tunnel. Since tunnel
3954 * mode does not set the body analyser, we can't reach this place
3955 * in tunnel mode, so we're left with keep-alive only.
3956 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003958 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003959 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003960 }
3961
3962 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3963 /* if we've just closed an output, let's switch */
3964 if (!(buf->flags & BF_OUT_EMPTY)) {
3965 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3966 goto http_msg_closing;
3967 }
3968 else {
3969 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3970 goto http_msg_closed;
3971 }
3972 }
3973 goto wait_other_side;
3974 }
3975
3976 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3977 http_msg_closing:
3978 /* nothing else to forward, just waiting for the output buffer
3979 * to be empty and for the shutw_now to take effect.
3980 */
3981 if (buf->flags & BF_OUT_EMPTY) {
3982 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3983 goto http_msg_closed;
3984 }
3985 else if (buf->flags & BF_SHUTW) {
3986 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003987 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01003988 if (target_srv(&s->target))
3989 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003990 goto wait_other_side;
3991 }
3992 }
3993
3994 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3995 http_msg_closed:
3996 /* drop any pending data */
3997 buffer_ignore(buf, buf->l - buf->send_max);
3998 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01003999 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004000 goto wait_other_side;
4001 }
4002
4003 wait_other_side:
4004 http_silent_debug(__LINE__, s);
4005 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4006}
4007
4008
4009/* Resync the request and response state machines. Return 1 if either state
4010 * changes.
4011 */
4012int http_resync_states(struct session *s)
4013{
4014 struct http_txn *txn = &s->txn;
4015 int old_req_state = txn->req.msg_state;
4016 int old_res_state = txn->rsp.msg_state;
4017
4018 http_silent_debug(__LINE__, s);
4019 http_sync_req_state(s);
4020 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004021 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 if (!http_sync_res_state(s))
4023 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004024 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004025 if (!http_sync_req_state(s))
4026 break;
4027 }
4028 http_silent_debug(__LINE__, s);
4029 /* OK, both state machines agree on a compatible state.
4030 * There are a few cases we're interested in :
4031 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4032 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4033 * directions, so let's simply disable both analysers.
4034 * - HTTP_MSG_CLOSED on the response only means we must abort the
4035 * request.
4036 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4037 * with server-close mode means we've completed one request and we
4038 * must re-initialize the server connection.
4039 */
4040
4041 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4042 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4043 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4044 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4045 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004046 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004047 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004049 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004050 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004052 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4053 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004054 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004055 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004056 s->rep->analysers = 0;
4057 buffer_auto_close(s->rep);
4058 buffer_auto_read(s->rep);
4059 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060 buffer_abort(s->req);
4061 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004062 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004064 }
4065 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4066 txn->rsp.msg_state == HTTP_MSG_DONE &&
4067 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4068 /* server-close: terminate this server connection and
4069 * reinitialize a fresh-new transaction.
4070 */
4071 http_end_txn_clean_session(s);
4072 }
4073
4074 http_silent_debug(__LINE__, s);
4075 return txn->req.msg_state != old_req_state ||
4076 txn->rsp.msg_state != old_res_state;
4077}
4078
Willy Tarreaud98cf932009-12-27 22:54:55 +01004079/* This function is an analyser which forwards request body (including chunk
4080 * sizes if any). It is called as soon as we must forward, even if we forward
4081 * zero byte. The only situation where it must not be called is when we're in
4082 * tunnel mode and we want to forward till the close. It's used both to forward
4083 * remaining data and to resync after end of body. It expects the msg_state to
4084 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4085 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004086 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004087 * bytes of pending data + the headers if not already done (between som and sov).
4088 * It eventually adjusts som to match sov after the data in between have been sent.
4089 */
4090int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4091{
4092 struct http_txn *txn = &s->txn;
4093 struct http_msg *msg = &s->txn.req;
4094
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004095 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4096 return 0;
4097
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004098 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4099 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004100 /* Output closed while we were sending data. We must abort and
4101 * wake the other side up.
4102 */
4103 msg->msg_state = HTTP_MSG_ERROR;
4104 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004105 return 1;
4106 }
4107
Willy Tarreau4fe41902010-06-07 22:27:41 +02004108 /* in most states, we should abort in case of early close */
4109 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004110
4111 /* Note that we don't have to send 100-continue back because we don't
4112 * need the data to complete our job, and it's up to the server to
4113 * decide whether to return 100, 417 or anything else in return of
4114 * an "Expect: 100-continue" header.
4115 */
4116
4117 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4118 /* we have msg->col and msg->sov which both point to the first
4119 * byte of message body. msg->som still points to the beginning
4120 * of the message. We must save the body in req->lr because it
4121 * survives buffer re-alignments.
4122 */
4123 req->lr = req->data + msg->sov;
4124 if (txn->flags & TX_REQ_TE_CHNK)
4125 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4126 else {
4127 msg->msg_state = HTTP_MSG_DATA;
4128 }
4129 }
4130
Willy Tarreaud98cf932009-12-27 22:54:55 +01004131 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004132 int bytes;
4133
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004135 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004136 bytes = msg->sov - msg->som;
4137 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004138 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004139 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4140 bytes += req->size;
4141 msg->chunk_len += (unsigned int)bytes;
4142 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004143 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004144
Willy Tarreaucaabe412010-01-03 23:08:28 +01004145 if (msg->msg_state == HTTP_MSG_DATA) {
4146 /* must still forward */
4147 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004148 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004149
4150 /* nothing left to forward */
4151 if (txn->flags & TX_REQ_TE_CHNK)
4152 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004153 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004154 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004155 }
4156 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004157 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004158 * set ->sov and ->lr to point to the body and switch to DATA or
4159 * TRAILERS state.
4160 */
4161 int ret = http_parse_chunk_size(req, msg);
4162
4163 if (!ret)
4164 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004165 else if (ret < 0) {
4166 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004167 if (msg->err_pos >= 0)
4168 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004169 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004170 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004171 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004172 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004173 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4174 /* we want the CRLF after the data */
4175 int ret;
4176
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004177 req->lr = req->w + req->send_max;
4178 if (req->lr >= req->data + req->size)
4179 req->lr -= req->size;
4180
Willy Tarreaud98cf932009-12-27 22:54:55 +01004181 ret = http_skip_chunk_crlf(req, msg);
4182
4183 if (ret == 0)
4184 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004185 else if (ret < 0) {
4186 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004187 if (msg->err_pos >= 0)
4188 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004189 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004190 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004191 /* we're in MSG_CHUNK_SIZE now */
4192 }
4193 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4194 int ret = http_forward_trailers(req, msg);
4195
4196 if (ret == 0)
4197 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004198 else if (ret < 0) {
4199 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004200 if (msg->err_pos >= 0)
4201 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004202 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004203 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004204 /* we're in HTTP_MSG_DONE now */
4205 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004207 int old_state = msg->msg_state;
4208
Willy Tarreau610ecce2010-01-04 21:15:02 +01004209 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004210 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004211 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4212 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4213 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004214 if (http_resync_states(s)) {
4215 /* some state changes occurred, maybe the analyser
4216 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004217 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004218 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4219 if (req->flags & BF_SHUTW) {
4220 /* request errors are most likely due to
4221 * the server aborting the transfer.
4222 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004223 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004224 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004225 if (msg->err_pos >= 0)
4226 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004227 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004228 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004229 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004230 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004231
4232 /* If "option abortonclose" is set on the backend, we
4233 * want to monitor the client's connection and forward
4234 * any shutdown notification to the server, which will
4235 * decide whether to close or to go on processing the
4236 * request.
4237 */
4238 if (s->be->options & PR_O_ABRT_CLOSE) {
4239 buffer_auto_read(req);
4240 buffer_auto_close(req);
4241 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004242 else if (s->txn.meth == HTTP_METH_POST) {
4243 /* POST requests may require to read extra CRLF
4244 * sent by broken browsers and which could cause
4245 * an RST to be sent upon close on some systems
4246 * (eg: Linux).
4247 */
4248 buffer_auto_read(req);
4249 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004250
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004252 }
4253 }
4254
Willy Tarreaud98cf932009-12-27 22:54:55 +01004255 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004256 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004257 if (req->flags & BF_SHUTR) {
4258 if (!(s->flags & SN_ERR_MASK))
4259 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004260 if (!(s->flags & SN_FINST_MASK)) {
4261 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4262 s->flags |= SN_FINST_H;
4263 else
4264 s->flags |= SN_FINST_D;
4265 }
4266
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004267 s->fe->fe_counters.cli_aborts++;
4268 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004269 if (target_srv(&s->target))
4270 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004271
4272 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004273 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004274
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004275 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004276 if (req->flags & BF_SHUTW)
4277 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004279 /* When TE: chunked is used, we need to get there again to parse remaining
4280 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4281 */
4282 if (txn->flags & TX_REQ_TE_CHNK)
4283 buffer_dont_close(req);
4284
Willy Tarreau5c620922011-05-11 19:56:11 +02004285 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004286 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4287 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004288 * modes are already handled by the stream sock layer. We must not do
4289 * this in content-length mode because it could present the MSG_MORE
4290 * flag with the last block of forwarded data, which would cause an
4291 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004292 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004293 if (txn->flags & TX_REQ_TE_CHNK)
4294 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004295
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004297 return 0;
4298
Willy Tarreaud98cf932009-12-27 22:54:55 +01004299 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004300 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004301 if (s->listener->counters)
4302 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004303 return_bad_req_stats_ok:
4304 txn->req.msg_state = HTTP_MSG_ERROR;
4305 if (txn->status) {
4306 /* Note: we don't send any error if some data were already sent */
4307 stream_int_retnclose(req->prod, NULL);
4308 } else {
4309 txn->status = 400;
4310 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4311 }
4312 req->analysers = 0;
4313 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004314
4315 if (!(s->flags & SN_ERR_MASK))
4316 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004317 if (!(s->flags & SN_FINST_MASK)) {
4318 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4319 s->flags |= SN_FINST_H;
4320 else
4321 s->flags |= SN_FINST_D;
4322 }
4323 return 0;
4324
4325 aborted_xfer:
4326 txn->req.msg_state = HTTP_MSG_ERROR;
4327 if (txn->status) {
4328 /* Note: we don't send any error if some data were already sent */
4329 stream_int_retnclose(req->prod, NULL);
4330 } else {
4331 txn->status = 502;
4332 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4333 }
4334 req->analysers = 0;
4335 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4336
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004337 s->fe->fe_counters.srv_aborts++;
4338 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004339 if (target_srv(&s->target))
4340 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004341
4342 if (!(s->flags & SN_ERR_MASK))
4343 s->flags |= SN_ERR_SRVCL;
4344 if (!(s->flags & SN_FINST_MASK)) {
4345 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4346 s->flags |= SN_FINST_H;
4347 else
4348 s->flags |= SN_FINST_D;
4349 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004350 return 0;
4351}
4352
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004353/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4354 * processing can continue on next analysers, or zero if it either needs more
4355 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4356 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4357 * when it has nothing left to do, and may remove any analyser when it wants to
4358 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004359 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004360int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004361{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004362 struct http_txn *txn = &s->txn;
4363 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004364 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004365 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004366 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004367 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004368
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004369 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 +02004370 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004371 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004372 rep,
4373 rep->rex, rep->wex,
4374 rep->flags,
4375 rep->l,
4376 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004377
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004378 /*
4379 * Now parse the partial (or complete) lines.
4380 * We will check the response syntax, and also join multi-line
4381 * headers. An index of all the lines will be elaborated while
4382 * parsing.
4383 *
4384 * For the parsing, we use a 28 states FSM.
4385 *
4386 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004387 * rep->data + msg->som = beginning of response
4388 * rep->data + msg->eoh = end of processed headers / start of current one
4389 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004390 * rep->lr = first non-visited byte
4391 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004392 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004393 */
4394
Willy Tarreau83e3af02009-12-28 17:39:57 +01004395 /* There's a protected area at the end of the buffer for rewriting
4396 * purposes. We don't want to start to parse the request if the
4397 * protected area is affected, because we may have to move processed
4398 * data later, which is much more complicated.
4399 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004400 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4401 if (unlikely((rep->flags & BF_FULL) ||
4402 rep->r < rep->lr ||
4403 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4404 if (rep->send_max) {
4405 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004406 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4407 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004408 buffer_dont_close(rep);
4409 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4410 return 0;
4411 }
4412 if (rep->l <= rep->size - global.tune.maxrewrite)
4413 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004414 }
4415
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004416 if (likely(rep->lr < rep->r))
4417 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004418 }
4419
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004420 /* 1: we might have to print this header in debug mode */
4421 if (unlikely((global.mode & MODE_DEBUG) &&
4422 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004423 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004424 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004426
Willy Tarreau663308b2010-06-07 14:06:08 +02004427 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004428 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004429 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004430
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004431 sol += hdr_idx_first_pos(&txn->hdr_idx);
4432 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004433
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004434 while (cur_idx) {
4435 eol = sol + txn->hdr_idx.v[cur_idx].len;
4436 debug_hdr("srvhdr", s, sol, eol);
4437 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4438 cur_idx = txn->hdr_idx.v[cur_idx].next;
4439 }
4440 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004441
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004442 /*
4443 * Now we quickly check if we have found a full valid response.
4444 * If not so, we check the FD and buffer states before leaving.
4445 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004446 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447 * responses are checked first.
4448 *
4449 * Depending on whether the client is still there or not, we
4450 * may send an error response back or not. Note that normally
4451 * we should only check for HTTP status there, and check I/O
4452 * errors somewhere else.
4453 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004454
Willy Tarreau655dce92009-11-08 13:10:58 +01004455 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004456 /* Invalid response */
4457 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4458 /* we detected a parsing error. We want to archive this response
4459 * in the dedicated proxy area for later troubleshooting.
4460 */
4461 hdr_response_bad:
4462 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004463 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004464
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004465 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004466 if (target_srv(&s->target)) {
4467 target_srv(&s->target)->counters.failed_resp++;
4468 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004469 }
Willy Tarreau64648412010-03-05 10:41:54 +01004470 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004471 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004472 rep->analysers = 0;
4473 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004474 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004475 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004476 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4477
4478 if (!(s->flags & SN_ERR_MASK))
4479 s->flags |= SN_ERR_PRXCOND;
4480 if (!(s->flags & SN_FINST_MASK))
4481 s->flags |= SN_FINST_H;
4482
4483 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004484 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004485
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004486 /* too large response does not fit in buffer. */
4487 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004488 if (msg->err_pos < 0)
4489 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004490 goto hdr_response_bad;
4491 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004492
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 /* read error */
4494 else if (rep->flags & BF_READ_ERROR) {
4495 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004496 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004497
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004498 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004499 if (target_srv(&s->target)) {
4500 target_srv(&s->target)->counters.failed_resp++;
4501 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004502 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004503
Willy Tarreau90deb182010-01-07 00:20:41 +01004504 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 rep->analysers = 0;
4506 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004507 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004508 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004510
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511 if (!(s->flags & SN_ERR_MASK))
4512 s->flags |= SN_ERR_SRVCL;
4513 if (!(s->flags & SN_FINST_MASK))
4514 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004515 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004516 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004517
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004518 /* read timeout : return a 504 to the client. */
4519 else if (rep->flags & BF_READ_TIMEOUT) {
4520 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004521 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004522
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004523 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004524 if (target_srv(&s->target)) {
4525 target_srv(&s->target)->counters.failed_resp++;
4526 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004527 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004528
Willy Tarreau90deb182010-01-07 00:20:41 +01004529 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 rep->analysers = 0;
4531 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004532 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004533 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004534 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004535
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004536 if (!(s->flags & SN_ERR_MASK))
4537 s->flags |= SN_ERR_SRVTO;
4538 if (!(s->flags & SN_FINST_MASK))
4539 s->flags |= SN_FINST_H;
4540 return 0;
4541 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004542
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004543 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004545 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004546 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004547
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004548 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004549 if (target_srv(&s->target)) {
4550 target_srv(&s->target)->counters.failed_resp++;
4551 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004552 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004553
Willy Tarreau90deb182010-01-07 00:20:41 +01004554 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 rep->analysers = 0;
4556 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004557 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004558 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004559 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004560
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004561 if (!(s->flags & SN_ERR_MASK))
4562 s->flags |= SN_ERR_SRVCL;
4563 if (!(s->flags & SN_FINST_MASK))
4564 s->flags |= SN_FINST_H;
4565 return 0;
4566 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004567
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 /* write error to client (we don't send any message then) */
4569 else if (rep->flags & BF_WRITE_ERROR) {
4570 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004571 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004572
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004573 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004575 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004576
4577 if (!(s->flags & SN_ERR_MASK))
4578 s->flags |= SN_ERR_CLICL;
4579 if (!(s->flags & SN_FINST_MASK))
4580 s->flags |= SN_FINST_H;
4581
4582 /* process_session() will take care of the error */
4583 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004585
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004586 buffer_dont_close(rep);
4587 return 0;
4588 }
4589
4590 /* More interesting part now : we know that we have a complete
4591 * response which at least looks like HTTP. We have an indicator
4592 * of each header's length, so we can parse them quickly.
4593 */
4594
4595 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004596 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 /*
4599 * 1: get the status code
4600 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004601 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004602 if (n < 1 || n > 5)
4603 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004604 /* when the client triggers a 4xx from the server, it's most often due
4605 * to a missing object or permission. These events should be tracked
4606 * because if they happen often, it may indicate a brute force or a
4607 * vulnerability scan.
4608 */
4609 if (n == 4)
4610 session_inc_http_err_ctr(s);
4611
Willy Tarreau827aee92011-03-10 16:55:02 +01004612 if (target_srv(&s->target))
4613 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614
Willy Tarreau5b154472009-12-21 20:11:07 +01004615 /* check if the response is HTTP/1.1 or above */
4616 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004617 ((msg->sol[5] > '1') ||
4618 ((msg->sol[5] == '1') &&
4619 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004620 txn->flags |= TX_RES_VER_11;
4621
4622 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004623 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 +01004624
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004625 /* transfer length unknown*/
4626 txn->flags &= ~TX_RES_XFER_LEN;
4627
Willy Tarreau962c3f42010-01-10 00:15:35 +01004628 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629
Willy Tarreau39650402010-03-15 19:44:39 +01004630 /* Adjust server's health based on status code. Note: status codes 501
4631 * and 505 are triggered on demand by client request, so we must not
4632 * count them as server failures.
4633 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004634 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004635 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004636 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004637 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004638 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004639 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004641 /*
4642 * 2: check for cacheability.
4643 */
4644
4645 switch (txn->status) {
4646 case 200:
4647 case 203:
4648 case 206:
4649 case 300:
4650 case 301:
4651 case 410:
4652 /* RFC2616 @13.4:
4653 * "A response received with a status code of
4654 * 200, 203, 206, 300, 301 or 410 MAY be stored
4655 * by a cache (...) unless a cache-control
4656 * directive prohibits caching."
4657 *
4658 * RFC2616 @9.5: POST method :
4659 * "Responses to this method are not cacheable,
4660 * unless the response includes appropriate
4661 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004662 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663 if (likely(txn->meth != HTTP_METH_POST) &&
4664 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4665 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4666 break;
4667 default:
4668 break;
4669 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004670
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004671 /*
4672 * 3: we may need to capture headers
4673 */
4674 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004675 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004676 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677 txn->rsp.cap, s->fe->rsp_cap);
4678
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004679 /* 4: determine the transfer-length.
4680 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4681 * the presence of a message-body in a RESPONSE and its transfer length
4682 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004683 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004684 * All responses to the HEAD request method MUST NOT include a
4685 * message-body, even though the presence of entity-header fields
4686 * might lead one to believe they do. All 1xx (informational), 204
4687 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4688 * message-body. All other responses do include a message-body,
4689 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004690 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004691 * 1. Any response which "MUST NOT" include a message-body (such as the
4692 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4693 * always terminated by the first empty line after the header fields,
4694 * regardless of the entity-header fields present in the message.
4695 *
4696 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4697 * the "chunked" transfer-coding (Section 6.2) is used, the
4698 * transfer-length is defined by the use of this transfer-coding.
4699 * If a Transfer-Encoding header field is present and the "chunked"
4700 * transfer-coding is not present, the transfer-length is defined by
4701 * the sender closing the connection.
4702 *
4703 * 3. If a Content-Length header field is present, its decimal value in
4704 * OCTETs represents both the entity-length and the transfer-length.
4705 * If a message is received with both a Transfer-Encoding header
4706 * field and a Content-Length header field, the latter MUST be ignored.
4707 *
4708 * 4. If the message uses the media type "multipart/byteranges", and
4709 * the transfer-length is not otherwise specified, then this self-
4710 * delimiting media type defines the transfer-length. This media
4711 * type MUST NOT be used unless the sender knows that the recipient
4712 * can parse it; the presence in a request of a Range header with
4713 * multiple byte-range specifiers from a 1.1 client implies that the
4714 * client can parse multipart/byteranges responses.
4715 *
4716 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004717 */
4718
4719 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004720 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004721 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004722 * FIXME: should we parse anyway and return an error on chunked encoding ?
4723 */
4724 if (txn->meth == HTTP_METH_HEAD ||
4725 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004726 txn->status == 204 || txn->status == 304) {
4727 txn->flags |= TX_RES_XFER_LEN;
4728 goto skip_content_length;
4729 }
4730
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004731 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004732 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004733 while ((txn->flags & TX_RES_VER_11) &&
4734 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004735 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4736 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4737 else if (txn->flags & TX_RES_TE_CHNK) {
4738 /* bad transfer-encoding (chunked followed by something else) */
4739 use_close_only = 1;
4740 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4741 break;
4742 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004743 }
4744
4745 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4746 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004747 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004748 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4749 signed long long cl;
4750
Willy Tarreauad14f752011-09-02 20:33:27 +02004751 if (!ctx.vlen) {
4752 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004753 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004754 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004755
Willy Tarreauad14f752011-09-02 20:33:27 +02004756 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4757 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004758 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004759 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004760
Willy Tarreauad14f752011-09-02 20:33:27 +02004761 if (cl < 0) {
4762 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004763 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004764 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004765
Willy Tarreauad14f752011-09-02 20:33:27 +02004766 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) {
4767 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004768 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004769 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004770
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004771 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004772 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004773 }
4774
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004775 /* FIXME: we should also implement the multipart/byterange method.
4776 * For now on, we resort to close mode in this case (unknown length).
4777 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004778skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004779
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780 /* end of job, return OK */
4781 rep->analysers &= ~an_bit;
4782 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004783 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 return 1;
4785}
4786
4787/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004788 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4789 * and updates t->rep->analysers. It might make sense to explode it into several
4790 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004791 */
4792int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4793{
4794 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004795 struct http_msg *msg = &txn->rsp;
4796 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004797 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798
4799 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4800 now_ms, __FUNCTION__,
4801 t,
4802 rep,
4803 rep->rex, rep->wex,
4804 rep->flags,
4805 rep->l,
4806 rep->analysers);
4807
Willy Tarreau655dce92009-11-08 13:10:58 +01004808 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 return 0;
4810
4811 rep->analysers &= ~an_bit;
4812 rep->analyse_exp = TICK_ETERNITY;
4813
Willy Tarreau5b154472009-12-21 20:11:07 +01004814 /* Now we have to check if we need to modify the Connection header.
4815 * This is more difficult on the response than it is on the request,
4816 * because we can have two different HTTP versions and we don't know
4817 * how the client will interprete a response. For instance, let's say
4818 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4819 * HTTP/1.1 response without any header. Maybe it will bound itself to
4820 * HTTP/1.0 because it only knows about it, and will consider the lack
4821 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4822 * the lack of header as a keep-alive. Thus we will use two flags
4823 * indicating how a request MAY be understood by the client. In case
4824 * of multiple possibilities, we'll fix the header to be explicit. If
4825 * ambiguous cases such as both close and keepalive are seen, then we
4826 * will fall back to explicit close. Note that we won't take risks with
4827 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004828 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004829 */
4830
Willy Tarreaudc008c52010-02-01 16:20:08 +01004831 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4832 txn->status == 101)) {
4833 /* Either we've established an explicit tunnel, or we're
4834 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004835 * to understand the next protocols. We have to switch to tunnel
4836 * mode, so that we transfer the request and responses then let
4837 * this protocol pass unmodified. When we later implement specific
4838 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004839 * header which contains information about that protocol for
4840 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004841 */
4842 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4843 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004844 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4845 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4846 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004847 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004848
Willy Tarreau60466522010-01-18 19:08:45 +01004849 /* on unknown transfer length, we must close */
4850 if (!(txn->flags & TX_RES_XFER_LEN) &&
4851 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4852 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004853
Willy Tarreau60466522010-01-18 19:08:45 +01004854 /* now adjust header transformations depending on current state */
4855 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4856 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4857 to_del |= 2; /* remove "keep-alive" on any response */
4858 if (!(txn->flags & TX_RES_VER_11))
4859 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004860 }
Willy Tarreau60466522010-01-18 19:08:45 +01004861 else { /* SCL / KAL */
4862 to_del |= 1; /* remove "close" on any response */
4863 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4864 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004865 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004866
Willy Tarreau60466522010-01-18 19:08:45 +01004867 /* Parse and remove some headers from the connection header */
4868 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004869
Willy Tarreau60466522010-01-18 19:08:45 +01004870 /* Some keep-alive responses are converted to Server-close if
4871 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004872 */
Willy Tarreau60466522010-01-18 19:08:45 +01004873 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4874 if ((txn->flags & TX_HDR_CONN_CLO) ||
4875 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4876 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004877 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004878 }
4879
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004880 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004881 /*
4882 * 3: we will have to evaluate the filters.
4883 * As opposed to version 1.2, now they will be evaluated in the
4884 * filters order and not in the header order. This means that
4885 * each filter has to be validated among all headers.
4886 *
4887 * Filters are tried with ->be first, then with ->fe if it is
4888 * different from ->be.
4889 */
4890
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004891 cur_proxy = t->be;
4892 while (1) {
4893 struct proxy *rule_set = cur_proxy;
4894
4895 /* try headers filters */
4896 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004897 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004898 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004899 if (target_srv(&t->target)) {
4900 target_srv(&t->target)->counters.failed_resp++;
4901 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004902 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004903 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004904 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004905 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004906 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004907 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004908 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004909 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004910 if (!(t->flags & SN_ERR_MASK))
4911 t->flags |= SN_ERR_PRXCOND;
4912 if (!(t->flags & SN_FINST_MASK))
4913 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004914 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004915 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004916 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004917
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004918 /* has the response been denied ? */
4919 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004920 if (target_srv(&t->target))
4921 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004922
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004923 t->be->be_counters.denied_resp++;
4924 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004925 if (t->listener->counters)
4926 t->listener->counters->denied_resp++;
4927
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004928 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004929 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004930
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004931 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004932 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004933 if (txn->status < 200)
4934 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004935 if (wl->cond) {
4936 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4937 ret = acl_pass(ret);
4938 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4939 ret = !ret;
4940 if (!ret)
4941 continue;
4942 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004943 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004944 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004945 }
4946
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004947 /* check whether we're already working on the frontend */
4948 if (cur_proxy == t->fe)
4949 break;
4950 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004951 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004952
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004953 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004954 * We may be facing a 100-continue response, in which case this
4955 * is not the right response, and we're waiting for the next one.
4956 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004957 * next one.
4958 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004959 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004960 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004961 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004962 msg->msg_state = HTTP_MSG_RPBEFORE;
4963 txn->status = 0;
4964 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4965 return 1;
4966 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004967 else if (unlikely(txn->status < 200))
4968 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004969
4970 /* we don't have any 1xx status code now */
4971
4972 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004973 * 4: check for server cookie.
4974 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004975 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4976 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004977 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004978
Willy Tarreaubaaee002006-06-26 02:48:02 +02004979
Willy Tarreaua15645d2007-03-18 16:22:39 +01004980 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004981 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004982 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004983 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004984 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004986 /*
4987 * 6: add server cookie in the response if needed
4988 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004989 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02004990 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02004991 (!(t->flags & SN_DIRECT) ||
4992 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
4993 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
4994 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
4995 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004996 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
4997 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02004999 /* the server is known, it's not the one the client requested, or the
5000 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005001 * insert a set-cookie here, except if we want to insert only on POST
5002 * requests and this one isn't. Note that servers which don't have cookies
5003 * (eg: some backup servers) will return a full cookie removal request.
5004 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005005 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005006 len = sprintf(trash,
5007 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5008 t->be->cookie_name);
5009 }
5010 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005011 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005012
5013 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5014 /* emit last_date, which is mandatory */
5015 trash[len++] = COOKIE_DELIM_DATE;
5016 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5017 if (t->be->cookie_maxlife) {
5018 /* emit first_date, which is either the original one or
5019 * the current date.
5020 */
5021 trash[len++] = COOKIE_DELIM_DATE;
5022 s30tob64(txn->cookie_first_date ?
5023 txn->cookie_first_date >> 2 :
5024 (date.tv_sec+3) >> 2, trash + len);
5025 len += 5;
5026 }
5027 }
5028 len += sprintf(trash + len, "; path=/");
5029 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005030
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005031 if (t->be->cookie_domain)
5032 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005034 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005035 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005036 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005037
Willy Tarreauf1348312010-10-07 15:54:11 +02005038 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005039 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005040 /* the server did not change, only the date was updated */
5041 txn->flags |= TX_SCK_UPDATED;
5042 else
5043 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005044
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005045 /* Here, we will tell an eventual cache on the client side that we don't
5046 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5047 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5048 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5049 */
5050 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005051
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005052 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5053
5054 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005055 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005057 }
5058 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005059
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005060 /*
5061 * 7: check if result will be cacheable with a cookie.
5062 * We'll block the response if security checks have caught
5063 * nasty things such as a cacheable cookie.
5064 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005065 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5066 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005067 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005068
5069 /* we're in presence of a cacheable response containing
5070 * a set-cookie header. We'll block it as requested by
5071 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005072 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005073 if (target_srv(&t->target))
5074 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005075
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005076 t->be->be_counters.denied_resp++;
5077 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005078 if (t->listener->counters)
5079 t->listener->counters->denied_resp++;
5080
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005082 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005083 send_log(t->be, LOG_ALERT,
5084 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005085 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005086 goto return_srv_prx_502;
5087 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005088
5089 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005090 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005091 */
Willy Tarreau60466522010-01-18 19:08:45 +01005092 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5093 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5094 unsigned int want_flags = 0;
5095
5096 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5097 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5098 /* we want a keep-alive response here. Keep-alive header
5099 * required if either side is not 1.1.
5100 */
5101 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5102 want_flags |= TX_CON_KAL_SET;
5103 }
5104 else {
5105 /* we want a close response here. Close header required if
5106 * the server is 1.1, regardless of the client.
5107 */
5108 if (txn->flags & TX_RES_VER_11)
5109 want_flags |= TX_CON_CLO_SET;
5110 }
5111
5112 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5113 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005114 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005115
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005116 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005117 if ((txn->flags & TX_RES_XFER_LEN) ||
5118 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005119 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005120
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121 /*************************************************************
5122 * OK, that's finished for the headers. We have done what we *
5123 * could. Let's switch to the DATA state. *
5124 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005125
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005126 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005127
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005128 /* if the user wants to log as soon as possible, without counting
5129 * bytes from the server, then this is the right moment. We have
5130 * to temporarily assign bytes_out to log what we currently have.
5131 */
5132 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5133 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5134 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005135 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005136 t->logs.bytes_out = 0;
5137 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005138
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 /* Note: we must not try to cheat by jumping directly to DATA,
5140 * otherwise we would not let the client side wake up.
5141 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005142
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005143 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005144 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005145 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005146}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005147
Willy Tarreaud98cf932009-12-27 22:54:55 +01005148/* This function is an analyser which forwards response body (including chunk
5149 * sizes if any). It is called as soon as we must forward, even if we forward
5150 * zero byte. The only situation where it must not be called is when we're in
5151 * tunnel mode and we want to forward till the close. It's used both to forward
5152 * remaining data and to resync after end of body. It expects the msg_state to
5153 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5154 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005155 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005156 * bytes of pending data + the headers if not already done (between som and sov).
5157 * It eventually adjusts som to match sov after the data in between have been sent.
5158 */
5159int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5160{
5161 struct http_txn *txn = &s->txn;
5162 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005163 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005164
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005165 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5166 return 0;
5167
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005168 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005169 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005170 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005171 /* Output closed while we were sending data. We must abort and
5172 * wake the other side up.
5173 */
5174 msg->msg_state = HTTP_MSG_ERROR;
5175 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005176 return 1;
5177 }
5178
Willy Tarreau4fe41902010-06-07 22:27:41 +02005179 /* in most states, we should abort in case of early close */
5180 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005181
Willy Tarreaud98cf932009-12-27 22:54:55 +01005182 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5183 /* we have msg->col and msg->sov which both point to the first
5184 * byte of message body. msg->som still points to the beginning
5185 * of the message. We must save the body in req->lr because it
5186 * survives buffer re-alignments.
5187 */
5188 res->lr = res->data + msg->sov;
5189 if (txn->flags & TX_RES_TE_CHNK)
5190 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5191 else {
5192 msg->msg_state = HTTP_MSG_DATA;
5193 }
5194 }
5195
Willy Tarreaud98cf932009-12-27 22:54:55 +01005196 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005197 int bytes;
5198
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005200 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005201 bytes = msg->sov - msg->som;
5202 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005203 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005204 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5205 bytes += res->size;
5206 msg->chunk_len += (unsigned int)bytes;
5207 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005208 }
5209
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005210
Willy Tarreaucaabe412010-01-03 23:08:28 +01005211 if (msg->msg_state == HTTP_MSG_DATA) {
5212 /* must still forward */
5213 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005214 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005215
5216 /* nothing left to forward */
5217 if (txn->flags & TX_RES_TE_CHNK)
5218 msg->msg_state = HTTP_MSG_DATA_CRLF;
5219 else
5220 msg->msg_state = HTTP_MSG_DONE;
5221 }
5222 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005223 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005224 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5225 */
5226 int ret = http_parse_chunk_size(res, msg);
5227
5228 if (!ret)
5229 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005230 else if (ret < 0) {
5231 if (msg->err_pos >= 0)
5232 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005233 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005234 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005235 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005236 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005237 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5238 /* we want the CRLF after the data */
5239 int ret;
5240
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005241 res->lr = res->w + res->send_max;
5242 if (res->lr >= res->data + res->size)
5243 res->lr -= res->size;
5244
Willy Tarreaud98cf932009-12-27 22:54:55 +01005245 ret = http_skip_chunk_crlf(res, msg);
5246
5247 if (!ret)
5248 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005249 else if (ret < 0) {
5250 if (msg->err_pos >= 0)
5251 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005252 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005253 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005254 /* we're in MSG_CHUNK_SIZE now */
5255 }
5256 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5257 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005258
Willy Tarreaud98cf932009-12-27 22:54:55 +01005259 if (ret == 0)
5260 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005261 else if (ret < 0) {
5262 if (msg->err_pos >= 0)
5263 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005264 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005265 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005266 /* we're in HTTP_MSG_DONE now */
5267 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005269 int old_state = msg->msg_state;
5270
Willy Tarreau610ecce2010-01-04 21:15:02 +01005271 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005272 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005273 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5274 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5275 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 if (http_resync_states(s)) {
5277 http_silent_debug(__LINE__, s);
5278 /* some state changes occurred, maybe the analyser
5279 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005280 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005281 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5282 if (res->flags & BF_SHUTW) {
5283 /* response errors are most likely due to
5284 * the client aborting the transfer.
5285 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005286 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005287 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005288 if (msg->err_pos >= 0)
5289 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005290 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005291 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005293 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005294 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295 }
5296 }
5297
Willy Tarreaud98cf932009-12-27 22:54:55 +01005298 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005299 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005300 if (res->flags & BF_SHUTR) {
5301 if (!(s->flags & SN_ERR_MASK))
5302 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005303 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005304 if (target_srv(&s->target))
5305 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005306 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005307 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005308
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005309 if (res->flags & BF_SHUTW)
5310 goto aborted_xfer;
5311
Willy Tarreau40dba092010-03-04 18:14:51 +01005312 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 if (!s->req->analysers)
5314 goto return_bad_res;
5315
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005316 /* forward any pending data */
5317 bytes = msg->sov - msg->som;
5318 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005319 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005320 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5321 bytes += res->size;
5322 msg->chunk_len += (unsigned int)bytes;
5323 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005324 }
5325
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005326 /* When TE: chunked is used, we need to get there again to parse remaining
5327 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5328 * Similarly, with keep-alive on the client side, we don't want to forward a
5329 * close.
5330 */
5331 if ((txn->flags & TX_RES_TE_CHNK) ||
5332 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5333 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5334 buffer_dont_close(res);
5335
Willy Tarreau5c620922011-05-11 19:56:11 +02005336 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005337 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5338 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005339 * modes are already handled by the stream sock layer. We must not do
5340 * this in content-length mode because it could present the MSG_MORE
5341 * flag with the last block of forwarded data, which would cause an
5342 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005343 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005344 if (txn->flags & TX_RES_TE_CHNK)
5345 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005346
Willy Tarreaud98cf932009-12-27 22:54:55 +01005347 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005349 return 0;
5350
Willy Tarreau40dba092010-03-04 18:14:51 +01005351 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005352 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005353 if (target_srv(&s->target))
5354 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005355
5356 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005357 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005358 /* don't send any error message as we're in the body */
5359 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005360 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005361 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005362 if (target_srv(&s->target))
5363 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005364
5365 if (!(s->flags & SN_ERR_MASK))
5366 s->flags |= SN_ERR_PRXCOND;
5367 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005368 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005369 return 0;
5370
5371 aborted_xfer:
5372 txn->rsp.msg_state = HTTP_MSG_ERROR;
5373 /* don't send any error message as we're in the body */
5374 stream_int_retnclose(res->cons, NULL);
5375 res->analysers = 0;
5376 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5377
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005378 s->fe->fe_counters.cli_aborts++;
5379 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005380 if (target_srv(&s->target))
5381 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005382
5383 if (!(s->flags & SN_ERR_MASK))
5384 s->flags |= SN_ERR_CLICL;
5385 if (!(s->flags & SN_FINST_MASK))
5386 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005387 return 0;
5388}
5389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005390/* Iterate the same filter through all request headers.
5391 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005392 * Since it can manage the switch to another backend, it updates the per-proxy
5393 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005394 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005395int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005396{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005397 char term;
5398 char *cur_ptr, *cur_end, *cur_next;
5399 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005400 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005401 struct hdr_idx_elem *cur_hdr;
5402 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005404 last_hdr = 0;
5405
Willy Tarreau962c3f42010-01-10 00:15:35 +01005406 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005407 old_idx = 0;
5408
5409 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005410 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005411 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005412 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005413 (exp->action == ACT_ALLOW ||
5414 exp->action == ACT_DENY ||
5415 exp->action == ACT_TARPIT))
5416 return 0;
5417
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005418 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005419 if (!cur_idx)
5420 break;
5421
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005422 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005423 cur_ptr = cur_next;
5424 cur_end = cur_ptr + cur_hdr->len;
5425 cur_next = cur_end + cur_hdr->cr + 1;
5426
5427 /* Now we have one header between cur_ptr and cur_end,
5428 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005429 */
5430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005431 /* The annoying part is that pattern matching needs
5432 * that we modify the contents to null-terminate all
5433 * strings before testing them.
5434 */
5435
5436 term = *cur_end;
5437 *cur_end = '\0';
5438
5439 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5440 switch (exp->action) {
5441 case ACT_SETBE:
5442 /* It is not possible to jump a second time.
5443 * FIXME: should we return an HTTP/500 here so that
5444 * the admin knows there's a problem ?
5445 */
5446 if (t->be != t->fe)
5447 break;
5448
5449 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005450 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005451 last_hdr = 1;
5452 break;
5453
5454 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005455 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005456 last_hdr = 1;
5457 break;
5458
5459 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005460 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005461 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005462
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005463 t->fe->fe_counters.denied_req++;
5464 if (t->fe != t->be)
5465 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005466 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005467 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005469 break;
5470
5471 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005472 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005473 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005474
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005475 t->fe->fe_counters.denied_req++;
5476 if (t->fe != t->be)
5477 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005478 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005479 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005481 break;
5482
5483 case ACT_REPLACE:
5484 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5485 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5486 /* FIXME: if the user adds a newline in the replacement, the
5487 * index will not be recalculated for now, and the new line
5488 * will not be counted as a new header.
5489 */
5490
5491 cur_end += delta;
5492 cur_next += delta;
5493 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005494 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005495 break;
5496
5497 case ACT_REMOVE:
5498 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5499 cur_next += delta;
5500
Willy Tarreaufa355d42009-11-29 18:12:29 +01005501 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005502 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5503 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005504 cur_hdr->len = 0;
5505 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005506 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005507 break;
5508
5509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005510 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005511 if (cur_end)
5512 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005514 /* keep the link from this header to next one in case of later
5515 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005516 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005517 old_idx = cur_idx;
5518 }
5519 return 0;
5520}
5521
5522
5523/* Apply the filter to the request line.
5524 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5525 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005526 * Since it can manage the switch to another backend, it updates the per-proxy
5527 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005528 */
5529int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5530{
5531 char term;
5532 char *cur_ptr, *cur_end;
5533 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005534 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005535 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005536
Willy Tarreau58f10d72006-12-04 02:26:12 +01005537
Willy Tarreau3d300592007-03-18 18:34:41 +01005538 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005539 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005540 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005541 (exp->action == ACT_ALLOW ||
5542 exp->action == ACT_DENY ||
5543 exp->action == ACT_TARPIT))
5544 return 0;
5545 else if (exp->action == ACT_REMOVE)
5546 return 0;
5547
5548 done = 0;
5549
Willy Tarreau962c3f42010-01-10 00:15:35 +01005550 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005551 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005552
5553 /* Now we have the request line between cur_ptr and cur_end */
5554
5555 /* The annoying part is that pattern matching needs
5556 * that we modify the contents to null-terminate all
5557 * strings before testing them.
5558 */
5559
5560 term = *cur_end;
5561 *cur_end = '\0';
5562
5563 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5564 switch (exp->action) {
5565 case ACT_SETBE:
5566 /* It is not possible to jump a second time.
5567 * FIXME: should we return an HTTP/500 here so that
5568 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005569 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005570 if (t->be != t->fe)
5571 break;
5572
5573 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005574 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005575 done = 1;
5576 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005577
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005578 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005579 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005580 done = 1;
5581 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005584 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005585
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005586 t->fe->fe_counters.denied_req++;
5587 if (t->fe != t->be)
5588 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005589 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005590 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 done = 1;
5593 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005594
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005595 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005596 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005597
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005598 t->fe->fe_counters.denied_req++;
5599 if (t->fe != t->be)
5600 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005601 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005602 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 done = 1;
5605 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005607 case ACT_REPLACE:
5608 *cur_end = term; /* restore the string terminator */
5609 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5610 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5611 /* FIXME: if the user adds a newline in the replacement, the
5612 * index will not be recalculated for now, and the new line
5613 * will not be counted as a new header.
5614 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005615
Willy Tarreaufa355d42009-11-29 18:12:29 +01005616 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005618 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005619 HTTP_MSG_RQMETH,
5620 cur_ptr, cur_end + 1,
5621 NULL, NULL);
5622 if (unlikely(!cur_end))
5623 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005625 /* we have a full request and we know that we have either a CR
5626 * or an LF at <ptr>.
5627 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005628 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5629 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005630 /* there is no point trying this regex on headers */
5631 return 1;
5632 }
5633 }
5634 *cur_end = term; /* restore the string terminator */
5635 return done;
5636}
Willy Tarreau97de6242006-12-27 17:18:38 +01005637
Willy Tarreau58f10d72006-12-04 02:26:12 +01005638
Willy Tarreau58f10d72006-12-04 02:26:12 +01005639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005641 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005643 * unparsable request. Since it can manage the switch to another backend, it
5644 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005645 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005646int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005648 struct http_txn *txn = &s->txn;
5649 struct hdr_exp *exp;
5650
5651 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005654 /*
5655 * The interleaving of transformations and verdicts
5656 * makes it difficult to decide to continue or stop
5657 * the evaluation.
5658 */
5659
Willy Tarreau6c123b12010-01-28 20:22:06 +01005660 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5661 break;
5662
Willy Tarreau3d300592007-03-18 18:34:41 +01005663 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005665 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005666 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005667
5668 /* if this filter had a condition, evaluate it now and skip to
5669 * next filter if the condition does not match.
5670 */
5671 if (exp->cond) {
5672 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5673 ret = acl_pass(ret);
5674 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5675 ret = !ret;
5676
5677 if (!ret)
5678 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005679 }
5680
5681 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005682 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 if (unlikely(ret < 0))
5684 return -1;
5685
5686 if (likely(ret == 0)) {
5687 /* The filter did not match the request, it can be
5688 * iterated through all headers.
5689 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005690 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005691 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005692 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005693 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005694}
5695
5696
Willy Tarreaua15645d2007-03-18 16:22:39 +01005697
Willy Tarreau58f10d72006-12-04 02:26:12 +01005698/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005699 * Try to retrieve the server associated to the appsession.
5700 * If the server is found, it's assigned to the session.
5701 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005702void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005703 struct http_txn *txn = &t->txn;
5704 appsess *asession = NULL;
5705 char *sessid_temp = NULL;
5706
Cyril Bontéb21570a2009-11-29 20:04:48 +01005707 if (len > t->be->appsession_len) {
5708 len = t->be->appsession_len;
5709 }
5710
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005711 if (t->be->options2 & PR_O2_AS_REQL) {
5712 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005713 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005714 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005715 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005716 }
5717
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005718 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005719 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5720 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5721 return;
5722 }
5723
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005724 memcpy(txn->sessid, buf, len);
5725 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005726 }
5727
5728 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5729 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5730 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5731 return;
5732 }
5733
Cyril Bontéb21570a2009-11-29 20:04:48 +01005734 memcpy(sessid_temp, buf, len);
5735 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005736
5737 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5738 /* free previously allocated memory */
5739 pool_free2(apools.sessid, sessid_temp);
5740
5741 if (asession != NULL) {
5742 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5743 if (!(t->be->options2 & PR_O2_AS_REQL))
5744 asession->request_count++;
5745
5746 if (asession->serverid != NULL) {
5747 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005748
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005749 while (srv) {
5750 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005751 if ((srv->state & SRV_RUNNING) ||
5752 (t->be->options & PR_O_PERSIST) ||
5753 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005754 /* we found the server and it's usable */
5755 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005756 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005757 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005758 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005759
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005760 break;
5761 } else {
5762 txn->flags &= ~TX_CK_MASK;
5763 txn->flags |= TX_CK_DOWN;
5764 }
5765 }
5766 srv = srv->next;
5767 }
5768 }
5769 }
5770}
5771
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005772/* Find the end of a cookie value contained between <s> and <e>. It works the
5773 * same way as with headers above except that the semi-colon also ends a token.
5774 * See RFC2965 for more information. Note that it requires a valid header to
5775 * return a valid result.
5776 */
5777char *find_cookie_value_end(char *s, const char *e)
5778{
5779 int quoted, qdpair;
5780
5781 quoted = qdpair = 0;
5782 for (; s < e; s++) {
5783 if (qdpair) qdpair = 0;
5784 else if (quoted) {
5785 if (*s == '\\') qdpair = 1;
5786 else if (*s == '"') quoted = 0;
5787 }
5788 else if (*s == '"') quoted = 1;
5789 else if (*s == ',' || *s == ';') return s;
5790 }
5791 return s;
5792}
5793
5794/* Delete a value in a header between delimiters <from> and <next> in buffer
5795 * <buf>. The number of characters displaced is returned, and the pointer to
5796 * the first delimiter is updated if required. The function tries as much as
5797 * possible to respect the following principles :
5798 * - replace <from> delimiter by the <next> one unless <from> points to a
5799 * colon, in which case <next> is simply removed
5800 * - set exactly one space character after the new first delimiter, unless
5801 * there are not enough characters in the block being moved to do so.
5802 * - remove unneeded spaces before the previous delimiter and after the new
5803 * one.
5804 *
5805 * It is the caller's responsibility to ensure that :
5806 * - <from> points to a valid delimiter or the colon ;
5807 * - <next> points to a valid delimiter or the final CR/LF ;
5808 * - there are non-space chars before <from> ;
5809 * - there is a CR/LF at or after <next>.
5810 */
5811int del_hdr_value(struct buffer *buf, char **from, char *next)
5812{
5813 char *prev = *from;
5814
5815 if (*prev == ':') {
5816 /* We're removing the first value, preserve the colon and add a
5817 * space if possible.
5818 */
5819 if (!http_is_crlf[(unsigned char)*next])
5820 next++;
5821 prev++;
5822 if (prev < next)
5823 *prev++ = ' ';
5824
5825 while (http_is_spht[(unsigned char)*next])
5826 next++;
5827 } else {
5828 /* Remove useless spaces before the old delimiter. */
5829 while (http_is_spht[(unsigned char)*(prev-1)])
5830 prev--;
5831 *from = prev;
5832
5833 /* copy the delimiter and if possible a space if we're
5834 * not at the end of the line.
5835 */
5836 if (!http_is_crlf[(unsigned char)*next]) {
5837 *prev++ = *next++;
5838 if (prev + 1 < next)
5839 *prev++ = ' ';
5840 while (http_is_spht[(unsigned char)*next])
5841 next++;
5842 }
5843 }
5844 return buffer_replace2(buf, prev, next, NULL, 0);
5845}
5846
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005847/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005848 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005849 * desirable to call it only when needed. This code is quite complex because
5850 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5851 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005852 */
5853void manage_client_side_cookies(struct session *t, struct buffer *req)
5854{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005855 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005856 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005857 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005858 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5859 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005860
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005861 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005862 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005863 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005864
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005865 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005866 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005867 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005868
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005869 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005870 hdr_beg = hdr_next;
5871 hdr_end = hdr_beg + cur_hdr->len;
5872 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005873
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005874 /* We have one full header between hdr_beg and hdr_end, and the
5875 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005876 * "Cookie:" headers.
5877 */
5878
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005879 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005880 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005881 old_idx = cur_idx;
5882 continue;
5883 }
5884
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005885 del_from = NULL; /* nothing to be deleted */
5886 preserve_hdr = 0; /* assume we may kill the whole header */
5887
Willy Tarreau58f10d72006-12-04 02:26:12 +01005888 /* Now look for cookies. Conforming to RFC2109, we have to support
5889 * attributes whose name begin with a '$', and associate them with
5890 * the right cookie, if we want to delete this cookie.
5891 * So there are 3 cases for each cookie read :
5892 * 1) it's a special attribute, beginning with a '$' : ignore it.
5893 * 2) it's a server id cookie that we *MAY* want to delete : save
5894 * some pointers on it (last semi-colon, beginning of cookie...)
5895 * 3) it's an application cookie : we *MAY* have to delete a previous
5896 * "special" cookie.
5897 * At the end of loop, if a "special" cookie remains, we may have to
5898 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005899 * *MUST* delete it.
5900 *
5901 * Note: RFC2965 is unclear about the processing of spaces around
5902 * the equal sign in the ATTR=VALUE form. A careful inspection of
5903 * the RFC explicitly allows spaces before it, and not within the
5904 * tokens (attrs or values). An inspection of RFC2109 allows that
5905 * too but section 10.1.3 lets one think that spaces may be allowed
5906 * after the equal sign too, resulting in some (rare) buggy
5907 * implementations trying to do that. So let's do what servers do.
5908 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5909 * allowed quoted strings in values, with any possible character
5910 * after a backslash, including control chars and delimitors, which
5911 * causes parsing to become ambiguous. Browsers also allow spaces
5912 * within values even without quotes.
5913 *
5914 * We have to keep multiple pointers in order to support cookie
5915 * removal at the beginning, middle or end of header without
5916 * corrupting the header. All of these headers are valid :
5917 *
5918 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5919 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5920 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5921 * | | | | | | | | |
5922 * | | | | | | | | hdr_end <--+
5923 * | | | | | | | +--> next
5924 * | | | | | | +----> val_end
5925 * | | | | | +-----------> val_beg
5926 * | | | | +--------------> equal
5927 * | | | +----------------> att_end
5928 * | | +---------------------> att_beg
5929 * | +--------------------------> prev
5930 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931 */
5932
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005933 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5934 /* Iterate through all cookies on this line */
5935
5936 /* find att_beg */
5937 att_beg = prev + 1;
5938 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5939 att_beg++;
5940
5941 /* find att_end : this is the first character after the last non
5942 * space before the equal. It may be equal to hdr_end.
5943 */
5944 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005946 while (equal < hdr_end) {
5947 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005949 if (http_is_spht[(unsigned char)*equal++])
5950 continue;
5951 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005952 }
5953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005954 /* here, <equal> points to '=', a delimitor or the end. <att_end>
5955 * is between <att_beg> and <equal>, both may be identical.
5956 */
5957
5958 /* look for end of cookie if there is an equal sign */
5959 if (equal < hdr_end && *equal == '=') {
5960 /* look for the beginning of the value */
5961 val_beg = equal + 1;
5962 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
5963 val_beg++;
5964
5965 /* find the end of the value, respecting quotes */
5966 next = find_cookie_value_end(val_beg, hdr_end);
5967
5968 /* make val_end point to the first white space or delimitor after the value */
5969 val_end = next;
5970 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
5971 val_end--;
5972 } else {
5973 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01005974 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005975
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005976 /* We have nothing to do with attributes beginning with '$'. However,
5977 * they will automatically be removed if a header before them is removed,
5978 * since they're supposed to be linked together.
5979 */
5980 if (*att_beg == '$')
5981 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005982
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005983 /* Ignore cookies with no equal sign */
5984 if (equal == next) {
5985 /* This is not our cookie, so we must preserve it. But if we already
5986 * scheduled another cookie for removal, we cannot remove the
5987 * complete header, but we can remove the previous block itself.
5988 */
5989 preserve_hdr = 1;
5990 if (del_from != NULL) {
5991 int delta = del_hdr_value(req, &del_from, prev);
5992 val_end += delta;
5993 next += delta;
5994 hdr_end += delta;
5995 hdr_next += delta;
5996 cur_hdr->len += delta;
5997 http_msg_move_end(&txn->req, delta);
5998 prev = del_from;
5999 del_from = NULL;
6000 }
6001 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006002 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006003
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006004 /* if there are spaces around the equal sign, we need to
6005 * strip them otherwise we'll get trouble for cookie captures,
6006 * or even for rewrites. Since this happens extremely rarely,
6007 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006008 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006009 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6010 int stripped_before = 0;
6011 int stripped_after = 0;
6012
6013 if (att_end != equal) {
6014 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6015 equal += stripped_before;
6016 val_beg += stripped_before;
6017 }
6018
6019 if (val_beg > equal + 1) {
6020 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6021 val_beg += stripped_after;
6022 stripped_before += stripped_after;
6023 }
6024
6025 val_end += stripped_before;
6026 next += stripped_before;
6027 hdr_end += stripped_before;
6028 hdr_next += stripped_before;
6029 cur_hdr->len += stripped_before;
6030 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006031 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006032 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006034 /* First, let's see if we want to capture this cookie. We check
6035 * that we don't already have a client side cookie, because we
6036 * can only capture one. Also as an optimisation, we ignore
6037 * cookies shorter than the declared name.
6038 */
6039 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6040 (val_end - att_beg >= t->fe->capture_namelen) &&
6041 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6042 int log_len = val_end - att_beg;
6043
6044 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6045 Alert("HTTP logging : out of memory.\n");
6046 } else {
6047 if (log_len > t->fe->capture_len)
6048 log_len = t->fe->capture_len;
6049 memcpy(txn->cli_cookie, att_beg, log_len);
6050 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006052 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006053
Willy Tarreaubca99692010-10-06 19:25:55 +02006054 /* Persistence cookies in passive, rewrite or insert mode have the
6055 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006056 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006057 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006058 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006059 * For cookies in prefix mode, the form is :
6060 *
6061 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006062 */
6063 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6064 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6065 struct server *srv = t->be->srv;
6066 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006067
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006068 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6069 * have the server ID between val_beg and delim, and the original cookie between
6070 * delim+1 and val_end. Otherwise, delim==val_end :
6071 *
6072 * Cookie: NAME=SRV; # in all but prefix modes
6073 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6074 * | || || | |+-> next
6075 * | || || | +--> val_end
6076 * | || || +---------> delim
6077 * | || |+------------> val_beg
6078 * | || +-------------> att_end = equal
6079 * | |+-----------------> att_beg
6080 * | +------------------> prev
6081 * +-------------------------> hdr_beg
6082 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006083
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006084 if (t->be->options & PR_O_COOK_PFX) {
6085 for (delim = val_beg; delim < val_end; delim++)
6086 if (*delim == COOKIE_DELIM)
6087 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006088 } else {
6089 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006090 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006091 /* Now check if the cookie contains a date field, which would
6092 * appear after a vertical bar ('|') just after the server name
6093 * and before the delimiter.
6094 */
6095 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6096 if (vbar1) {
6097 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006098 * right is the last seen date. It is a base64 encoded
6099 * 30-bit value representing the UNIX date since the
6100 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006101 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006102 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006103 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006104 if (val_end - vbar1 >= 5) {
6105 val = b64tos30(vbar1);
6106 if (val > 0)
6107 txn->cookie_last_date = val << 2;
6108 }
6109 /* look for a second vertical bar */
6110 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6111 if (vbar1 && (val_end - vbar1 > 5)) {
6112 val = b64tos30(vbar1 + 1);
6113 if (val > 0)
6114 txn->cookie_first_date = val << 2;
6115 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006116 }
6117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118
Willy Tarreauf64d1412010-10-07 20:06:11 +02006119 /* if the cookie has an expiration date and the proxy wants to check
6120 * it, then we do that now. We first check if the cookie is too old,
6121 * then only if it has expired. We detect strict overflow because the
6122 * time resolution here is not great (4 seconds). Cookies with dates
6123 * in the future are ignored if their offset is beyond one day. This
6124 * allows an admin to fix timezone issues without expiring everyone
6125 * and at the same time avoids keeping unwanted side effects for too
6126 * long.
6127 */
6128 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006129 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6130 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006131 txn->flags &= ~TX_CK_MASK;
6132 txn->flags |= TX_CK_OLD;
6133 delim = val_beg; // let's pretend we have not found the cookie
6134 txn->cookie_first_date = 0;
6135 txn->cookie_last_date = 0;
6136 }
6137 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006138 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6139 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006140 txn->flags &= ~TX_CK_MASK;
6141 txn->flags |= TX_CK_EXPIRED;
6142 delim = val_beg; // let's pretend we have not found the cookie
6143 txn->cookie_first_date = 0;
6144 txn->cookie_last_date = 0;
6145 }
6146
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006147 /* Here, we'll look for the first running server which supports the cookie.
6148 * This allows to share a same cookie between several servers, for example
6149 * to dedicate backup servers to specific servers only.
6150 * However, to prevent clients from sticking to cookie-less backup server
6151 * when they have incidentely learned an empty cookie, we simply ignore
6152 * empty cookies and mark them as invalid.
6153 * The same behaviour is applied when persistence must be ignored.
6154 */
6155 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6156 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006157
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006158 while (srv) {
6159 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6160 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6161 if ((srv->state & SRV_RUNNING) ||
6162 (t->be->options & PR_O_PERSIST) ||
6163 (t->flags & SN_FORCE_PRST)) {
6164 /* we found the server and we can use it */
6165 txn->flags &= ~TX_CK_MASK;
6166 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6167 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006168 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006169 break;
6170 } else {
6171 /* we found a server, but it's down,
6172 * mark it as such and go on in case
6173 * another one is available.
6174 */
6175 txn->flags &= ~TX_CK_MASK;
6176 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006177 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006179 srv = srv->next;
6180 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006181
Willy Tarreauf64d1412010-10-07 20:06:11 +02006182 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006183 /* no server matched this cookie */
6184 txn->flags &= ~TX_CK_MASK;
6185 txn->flags |= TX_CK_INVALID;
6186 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006187
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006188 /* depending on the cookie mode, we may have to either :
6189 * - delete the complete cookie if we're in insert+indirect mode, so that
6190 * the server never sees it ;
6191 * - remove the server id from the cookie value, and tag the cookie as an
6192 * application cookie so that it does not get accidentely removed later,
6193 * if we're in cookie prefix mode
6194 */
6195 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6196 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006198 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6199 val_end += delta;
6200 next += delta;
6201 hdr_end += delta;
6202 hdr_next += delta;
6203 cur_hdr->len += delta;
6204 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006205
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006206 del_from = NULL;
6207 preserve_hdr = 1; /* we want to keep this cookie */
6208 }
6209 else if (del_from == NULL &&
6210 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6211 del_from = prev;
6212 }
6213 } else {
6214 /* This is not our cookie, so we must preserve it. But if we already
6215 * scheduled another cookie for removal, we cannot remove the
6216 * complete header, but we can remove the previous block itself.
6217 */
6218 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006219
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 if (del_from != NULL) {
6221 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006222 if (att_beg >= del_from)
6223 att_beg += delta;
6224 if (att_end >= del_from)
6225 att_end += delta;
6226 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 val_end += delta;
6228 next += delta;
6229 hdr_end += delta;
6230 hdr_next += delta;
6231 cur_hdr->len += delta;
6232 http_msg_move_end(&txn->req, delta);
6233 prev = del_from;
6234 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006235 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006236 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 /* Look for the appsession cookie unless persistence must be ignored */
6239 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6240 int cmp_len, value_len;
6241 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006242
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006243 if (t->be->options2 & PR_O2_AS_PFX) {
6244 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6245 value_begin = att_beg + t->be->appsession_name_len;
6246 value_len = val_end - att_beg - t->be->appsession_name_len;
6247 } else {
6248 cmp_len = att_end - att_beg;
6249 value_begin = val_beg;
6250 value_len = val_end - val_beg;
6251 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006252
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 /* let's see if the cookie is our appcookie */
6254 if (cmp_len == t->be->appsession_name_len &&
6255 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6256 manage_client_side_appsession(t, value_begin, value_len);
6257 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258 }
6259
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006260 /* continue with next cookie on this header line */
6261 att_beg = next;
6262 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006263
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 /* There are no more cookies on this line.
6265 * We may still have one (or several) marked for deletion at the
6266 * end of the line. We must do this now in two ways :
6267 * - if some cookies must be preserved, we only delete from the
6268 * mark to the end of line ;
6269 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006272 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006273 if (preserve_hdr) {
6274 delta = del_hdr_value(req, &del_from, hdr_end);
6275 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006276 cur_hdr->len += delta;
6277 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279
6280 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006281 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6282 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006284 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006285 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006287 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006288 }
6289
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006291 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006293}
6294
6295
Willy Tarreaua15645d2007-03-18 16:22:39 +01006296/* Iterate the same filter through all response headers contained in <rtr>.
6297 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6298 */
6299int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6300{
6301 char term;
6302 char *cur_ptr, *cur_end, *cur_next;
6303 int cur_idx, old_idx, last_hdr;
6304 struct http_txn *txn = &t->txn;
6305 struct hdr_idx_elem *cur_hdr;
6306 int len, delta;
6307
6308 last_hdr = 0;
6309
Willy Tarreau962c3f42010-01-10 00:15:35 +01006310 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006311 old_idx = 0;
6312
6313 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006314 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006315 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006316 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006317 (exp->action == ACT_ALLOW ||
6318 exp->action == ACT_DENY))
6319 return 0;
6320
6321 cur_idx = txn->hdr_idx.v[old_idx].next;
6322 if (!cur_idx)
6323 break;
6324
6325 cur_hdr = &txn->hdr_idx.v[cur_idx];
6326 cur_ptr = cur_next;
6327 cur_end = cur_ptr + cur_hdr->len;
6328 cur_next = cur_end + cur_hdr->cr + 1;
6329
6330 /* Now we have one header between cur_ptr and cur_end,
6331 * and the next header starts at cur_next.
6332 */
6333
6334 /* The annoying part is that pattern matching needs
6335 * that we modify the contents to null-terminate all
6336 * strings before testing them.
6337 */
6338
6339 term = *cur_end;
6340 *cur_end = '\0';
6341
6342 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6343 switch (exp->action) {
6344 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006345 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006346 last_hdr = 1;
6347 break;
6348
6349 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006350 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006351 last_hdr = 1;
6352 break;
6353
6354 case ACT_REPLACE:
6355 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6356 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6357 /* FIXME: if the user adds a newline in the replacement, the
6358 * index will not be recalculated for now, and the new line
6359 * will not be counted as a new header.
6360 */
6361
6362 cur_end += delta;
6363 cur_next += delta;
6364 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006365 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366 break;
6367
6368 case ACT_REMOVE:
6369 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6370 cur_next += delta;
6371
Willy Tarreaufa355d42009-11-29 18:12:29 +01006372 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006373 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6374 txn->hdr_idx.used--;
6375 cur_hdr->len = 0;
6376 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006377 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006378 break;
6379
6380 }
6381 }
6382 if (cur_end)
6383 *cur_end = term; /* restore the string terminator */
6384
6385 /* keep the link from this header to next one in case of later
6386 * removal of next header.
6387 */
6388 old_idx = cur_idx;
6389 }
6390 return 0;
6391}
6392
6393
6394/* Apply the filter to the status line in the response buffer <rtr>.
6395 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6396 * or -1 if a replacement resulted in an invalid status line.
6397 */
6398int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6399{
6400 char term;
6401 char *cur_ptr, *cur_end;
6402 int done;
6403 struct http_txn *txn = &t->txn;
6404 int len, delta;
6405
6406
Willy Tarreau3d300592007-03-18 18:34:41 +01006407 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006408 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006409 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006410 (exp->action == ACT_ALLOW ||
6411 exp->action == ACT_DENY))
6412 return 0;
6413 else if (exp->action == ACT_REMOVE)
6414 return 0;
6415
6416 done = 0;
6417
Willy Tarreau962c3f42010-01-10 00:15:35 +01006418 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006419 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006420
6421 /* Now we have the status line between cur_ptr and cur_end */
6422
6423 /* The annoying part is that pattern matching needs
6424 * that we modify the contents to null-terminate all
6425 * strings before testing them.
6426 */
6427
6428 term = *cur_end;
6429 *cur_end = '\0';
6430
6431 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6432 switch (exp->action) {
6433 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006434 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006435 done = 1;
6436 break;
6437
6438 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006439 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006440 done = 1;
6441 break;
6442
6443 case ACT_REPLACE:
6444 *cur_end = term; /* restore the string terminator */
6445 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6446 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6447 /* FIXME: if the user adds a newline in the replacement, the
6448 * index will not be recalculated for now, and the new line
6449 * will not be counted as a new header.
6450 */
6451
Willy Tarreaufa355d42009-11-29 18:12:29 +01006452 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006453 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006454 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006455 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006456 cur_ptr, cur_end + 1,
6457 NULL, NULL);
6458 if (unlikely(!cur_end))
6459 return -1;
6460
6461 /* we have a full respnse and we know that we have either a CR
6462 * or an LF at <ptr>.
6463 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006464 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006465 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006466 /* there is no point trying this regex on headers */
6467 return 1;
6468 }
6469 }
6470 *cur_end = term; /* restore the string terminator */
6471 return done;
6472}
6473
6474
6475
6476/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006477 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006478 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6479 * unparsable response.
6480 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006481int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006482{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006483 struct http_txn *txn = &s->txn;
6484 struct hdr_exp *exp;
6485
6486 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006487 int ret;
6488
6489 /*
6490 * The interleaving of transformations and verdicts
6491 * makes it difficult to decide to continue or stop
6492 * the evaluation.
6493 */
6494
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006495 if (txn->flags & TX_SVDENY)
6496 break;
6497
Willy Tarreau3d300592007-03-18 18:34:41 +01006498 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6500 exp->action == ACT_PASS)) {
6501 exp = exp->next;
6502 continue;
6503 }
6504
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006505 /* if this filter had a condition, evaluate it now and skip to
6506 * next filter if the condition does not match.
6507 */
6508 if (exp->cond) {
6509 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6510 ret = acl_pass(ret);
6511 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6512 ret = !ret;
6513 if (!ret)
6514 continue;
6515 }
6516
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006518 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519 if (unlikely(ret < 0))
6520 return -1;
6521
6522 if (likely(ret == 0)) {
6523 /* The filter did not match the response, it can be
6524 * iterated through all headers.
6525 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006526 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006527 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006528 }
6529 return 0;
6530}
6531
6532
Willy Tarreaua15645d2007-03-18 16:22:39 +01006533/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006534 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006535 * desirable to call it only when needed. This function is also used when we
6536 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006538void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006539{
6540 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006541 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006542 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006543 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006544 char *hdr_beg, *hdr_end, *hdr_next;
6545 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546
Willy Tarreaua15645d2007-03-18 16:22:39 +01006547 /* Iterate through the headers.
6548 * we start with the start line.
6549 */
6550 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006551 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006552
6553 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6554 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006555 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556
6557 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006558 hdr_beg = hdr_next;
6559 hdr_end = hdr_beg + cur_hdr->len;
6560 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006561
Willy Tarreau24581ba2010-08-31 22:39:35 +02006562 /* We have one full header between hdr_beg and hdr_end, and the
6563 * next header starts at hdr_next. We're only interested in
6564 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006565 */
6566
Willy Tarreau24581ba2010-08-31 22:39:35 +02006567 is_cookie2 = 0;
6568 prev = hdr_beg + 10;
6569 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006570 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006571 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6572 if (!val) {
6573 old_idx = cur_idx;
6574 continue;
6575 }
6576 is_cookie2 = 1;
6577 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578 }
6579
Willy Tarreau24581ba2010-08-31 22:39:35 +02006580 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6581 * <prev> points to the colon.
6582 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006583 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006584
Willy Tarreau24581ba2010-08-31 22:39:35 +02006585 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6586 * check-cache is enabled) and we are not interested in checking
6587 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006588 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006589 if (t->be->cookie_name == NULL &&
6590 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006591 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006592 return;
6593
Willy Tarreau24581ba2010-08-31 22:39:35 +02006594 /* OK so now we know we have to process this response cookie.
6595 * The format of the Set-Cookie header is slightly different
6596 * from the format of the Cookie header in that it does not
6597 * support the comma as a cookie delimiter (thus the header
6598 * cannot be folded) because the Expires attribute described in
6599 * the original Netscape's spec may contain an unquoted date
6600 * with a comma inside. We have to live with this because
6601 * many browsers don't support Max-Age and some browsers don't
6602 * support quoted strings. However the Set-Cookie2 header is
6603 * clean.
6604 *
6605 * We have to keep multiple pointers in order to support cookie
6606 * removal at the beginning, middle or end of header without
6607 * corrupting the header (in case of set-cookie2). A special
6608 * pointer, <scav> points to the beginning of the set-cookie-av
6609 * fields after the first semi-colon. The <next> pointer points
6610 * either to the end of line (set-cookie) or next unquoted comma
6611 * (set-cookie2). All of these headers are valid :
6612 *
6613 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6614 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6615 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6616 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6617 * | | | | | | | | | |
6618 * | | | | | | | | +-> next hdr_end <--+
6619 * | | | | | | | +------------> scav
6620 * | | | | | | +--------------> val_end
6621 * | | | | | +--------------------> val_beg
6622 * | | | | +----------------------> equal
6623 * | | | +------------------------> att_end
6624 * | | +----------------------------> att_beg
6625 * | +------------------------------> prev
6626 * +-----------------------------------------> hdr_beg
6627 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628
Willy Tarreau24581ba2010-08-31 22:39:35 +02006629 for (; prev < hdr_end; prev = next) {
6630 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006631
Willy Tarreau24581ba2010-08-31 22:39:35 +02006632 /* find att_beg */
6633 att_beg = prev + 1;
6634 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6635 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636
Willy Tarreau24581ba2010-08-31 22:39:35 +02006637 /* find att_end : this is the first character after the last non
6638 * space before the equal. It may be equal to hdr_end.
6639 */
6640 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641
Willy Tarreau24581ba2010-08-31 22:39:35 +02006642 while (equal < hdr_end) {
6643 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6644 break;
6645 if (http_is_spht[(unsigned char)*equal++])
6646 continue;
6647 att_end = equal;
6648 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006649
Willy Tarreau24581ba2010-08-31 22:39:35 +02006650 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6651 * is between <att_beg> and <equal>, both may be identical.
6652 */
6653
6654 /* look for end of cookie if there is an equal sign */
6655 if (equal < hdr_end && *equal == '=') {
6656 /* look for the beginning of the value */
6657 val_beg = equal + 1;
6658 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6659 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660
Willy Tarreau24581ba2010-08-31 22:39:35 +02006661 /* find the end of the value, respecting quotes */
6662 next = find_cookie_value_end(val_beg, hdr_end);
6663
6664 /* make val_end point to the first white space or delimitor after the value */
6665 val_end = next;
6666 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6667 val_end--;
6668 } else {
6669 /* <equal> points to next comma, semi-colon or EOL */
6670 val_beg = val_end = next = equal;
6671 }
6672
6673 if (next < hdr_end) {
6674 /* Set-Cookie2 supports multiple cookies, and <next> points to
6675 * a colon or semi-colon before the end. So skip all attr-value
6676 * pairs and look for the next comma. For Set-Cookie, since
6677 * commas are permitted in values, skip to the end.
6678 */
6679 if (is_cookie2)
6680 next = find_hdr_value_end(next, hdr_end);
6681 else
6682 next = hdr_end;
6683 }
6684
6685 /* Now everything is as on the diagram above */
6686
6687 /* Ignore cookies with no equal sign */
6688 if (equal == val_end)
6689 continue;
6690
6691 /* If there are spaces around the equal sign, we need to
6692 * strip them otherwise we'll get trouble for cookie captures,
6693 * or even for rewrites. Since this happens extremely rarely,
6694 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006696 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6697 int stripped_before = 0;
6698 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006699
Willy Tarreau24581ba2010-08-31 22:39:35 +02006700 if (att_end != equal) {
6701 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6702 equal += stripped_before;
6703 val_beg += stripped_before;
6704 }
6705
6706 if (val_beg > equal + 1) {
6707 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6708 val_beg += stripped_after;
6709 stripped_before += stripped_after;
6710 }
6711
6712 val_end += stripped_before;
6713 next += stripped_before;
6714 hdr_end += stripped_before;
6715 hdr_next += stripped_before;
6716 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006717 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006718 }
6719
6720 /* First, let's see if we want to capture this cookie. We check
6721 * that we don't already have a server side cookie, because we
6722 * can only capture one. Also as an optimisation, we ignore
6723 * cookies shorter than the declared name.
6724 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006725 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006726 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006727 (val_end - att_beg >= t->fe->capture_namelen) &&
6728 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6729 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006730 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731 Alert("HTTP logging : out of memory.\n");
6732 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006733 else {
6734 if (log_len > t->fe->capture_len)
6735 log_len = t->fe->capture_len;
6736 memcpy(txn->srv_cookie, att_beg, log_len);
6737 txn->srv_cookie[log_len] = 0;
6738 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739 }
6740
Willy Tarreau827aee92011-03-10 16:55:02 +01006741 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006743 if (!(t->flags & SN_IGNORE_PRST) &&
6744 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6745 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006746 /* assume passive cookie by default */
6747 txn->flags &= ~TX_SCK_MASK;
6748 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006749
6750 /* If the cookie is in insert mode on a known server, we'll delete
6751 * this occurrence because we'll insert another one later.
6752 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006753 * a direct access.
6754 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006755 if (t->be->options2 & PR_O2_COOK_PSV) {
6756 /* The "preserve" flag was set, we don't want to touch the
6757 * server's cookie.
6758 */
6759 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006760 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006761 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006762 /* this cookie must be deleted */
6763 if (*prev == ':' && next == hdr_end) {
6764 /* whole header */
6765 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6766 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6767 txn->hdr_idx.used--;
6768 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006769 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006770 hdr_next += delta;
6771 http_msg_move_end(&txn->rsp, delta);
6772 /* note: while both invalid now, <next> and <hdr_end>
6773 * are still equal, so the for() will stop as expected.
6774 */
6775 } else {
6776 /* just remove the value */
6777 int delta = del_hdr_value(res, &prev, next);
6778 next = prev;
6779 hdr_end += delta;
6780 hdr_next += delta;
6781 cur_hdr->len += delta;
6782 http_msg_move_end(&txn->rsp, delta);
6783 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006784 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006785 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006786 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006788 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006789 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 * with this server since we know it.
6791 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006792 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006793 next += delta;
6794 hdr_end += delta;
6795 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006797 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798
Willy Tarreauf1348312010-10-07 15:54:11 +02006799 txn->flags &= ~TX_SCK_MASK;
6800 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006802 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006804 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006805 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006806 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006807 next += delta;
6808 hdr_end += delta;
6809 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006811 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812
Willy Tarreau827aee92011-03-10 16:55:02 +01006813 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006814 txn->flags &= ~TX_SCK_MASK;
6815 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816 }
6817 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006818 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6819 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006820 int cmp_len, value_len;
6821 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822
Cyril Bontéb21570a2009-11-29 20:04:48 +01006823 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006824 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6825 value_begin = att_beg + t->be->appsession_name_len;
6826 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006827 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006828 cmp_len = att_end - att_beg;
6829 value_begin = val_beg;
6830 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006831 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006832
Cyril Bonté17530c32010-04-06 21:11:10 +02006833 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6835 /* free a possibly previously allocated memory */
6836 pool_free2(apools.sessid, txn->sessid);
6837
Cyril Bontéb21570a2009-11-29 20:04:48 +01006838 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006839 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006840 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6841 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6842 return;
6843 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006844 memcpy(txn->sessid, value_begin, value_len);
6845 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006847 }
6848 /* that's done for this cookie, check the next one on the same
6849 * line when next != hdr_end (only if is_cookie2).
6850 */
6851 }
6852 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006854 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006855
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006856 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006857 appsess *asession = NULL;
6858 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006859 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006860 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006861 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006862 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6863 Alert("Not enough Memory process_srv():asession:calloc().\n");
6864 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6865 return;
6866 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006867 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6868
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006869 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6870 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6871 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006872 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006873 return;
6874 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006875 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006876 asession->sessid[t->be->appsession_len] = 0;
6877
Willy Tarreau827aee92011-03-10 16:55:02 +01006878 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006879 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006880 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006881 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006882 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006883 return;
6884 }
6885 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006886 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006887
6888 asession->request_count = 0;
6889 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6890 }
6891
6892 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6893 asession->request_count++;
6894 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895}
6896
6897
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898/*
6899 * Check if response is cacheable or not. Updates t->flags.
6900 */
6901void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6902{
6903 struct http_txn *txn = &t->txn;
6904 char *p1, *p2;
6905
6906 char *cur_ptr, *cur_end, *cur_next;
6907 int cur_idx;
6908
Willy Tarreau5df51872007-11-25 16:20:08 +01006909 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910 return;
6911
6912 /* Iterate through the headers.
6913 * we start with the start line.
6914 */
6915 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006916 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006917
6918 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6919 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006920 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921
6922 cur_hdr = &txn->hdr_idx.v[cur_idx];
6923 cur_ptr = cur_next;
6924 cur_end = cur_ptr + cur_hdr->len;
6925 cur_next = cur_end + cur_hdr->cr + 1;
6926
6927 /* We have one full header between cur_ptr and cur_end, and the
6928 * next header starts at cur_next. We're only interested in
6929 * "Cookie:" headers.
6930 */
6931
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006932 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6933 if (val) {
6934 if ((cur_end - (cur_ptr + val) >= 8) &&
6935 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6936 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6937 return;
6938 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006939 }
6940
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006941 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6942 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943 continue;
6944
6945 /* OK, right now we know we have a cache-control header at cur_ptr */
6946
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006947 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006948
6949 if (p1 >= cur_end) /* no more info */
6950 continue;
6951
6952 /* p1 is at the beginning of the value */
6953 p2 = p1;
6954
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006955 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956 p2++;
6957
6958 /* we have a complete value between p1 and p2 */
6959 if (p2 < cur_end && *p2 == '=') {
6960 /* we have something of the form no-cache="set-cookie" */
6961 if ((cur_end - p1 >= 21) &&
6962 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6963 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006964 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006965 continue;
6966 }
6967
6968 /* OK, so we know that either p2 points to the end of string or to a comma */
6969 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6970 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6971 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6972 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006973 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974 return;
6975 }
6976
6977 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006978 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 continue;
6980 }
6981 }
6982}
6983
6984
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985/*
6986 * Try to retrieve a known appsession in the URI, then the associated server.
6987 * If the server is found, it's assigned to the session.
6988 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006990{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006991 char *end_params, *first_param, *cur_param, *next_param;
6992 char separator;
6993 int value_len;
6994
6995 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006997 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02006998 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007000 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007001
Cyril Bontéb21570a2009-11-29 20:04:48 +01007002 first_param = NULL;
7003 switch (mode) {
7004 case PR_O2_AS_M_PP:
7005 first_param = memchr(begin, ';', len);
7006 break;
7007 case PR_O2_AS_M_QS:
7008 first_param = memchr(begin, '?', len);
7009 break;
7010 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007011
Cyril Bontéb21570a2009-11-29 20:04:48 +01007012 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007013 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007014 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Cyril Bontéb21570a2009-11-29 20:04:48 +01007016 switch (mode) {
7017 case PR_O2_AS_M_PP:
7018 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7019 end_params = (char *) begin + len;
7020 }
7021 separator = ';';
7022 break;
7023 case PR_O2_AS_M_QS:
7024 end_params = (char *) begin + len;
7025 separator = '&';
7026 break;
7027 default:
7028 /* unknown mode, shouldn't happen */
7029 return;
7030 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007031
Cyril Bontéb21570a2009-11-29 20:04:48 +01007032 cur_param = next_param = end_params;
7033 while (cur_param > first_param) {
7034 cur_param--;
7035 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7036 /* let's see if this is the appsession parameter */
7037 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7038 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7039 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7040 /* Cool... it's the right one */
7041 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7042 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7043 if (value_len > 0) {
7044 manage_client_side_appsession(t, cur_param, value_len);
7045 }
7046 break;
7047 }
7048 next_param = cur_param;
7049 }
7050 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007051#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007052 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007053 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007054#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007055}
7056
Willy Tarreaub2513902006-12-17 14:52:38 +01007057/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007058 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007059 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007060 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007061 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007062 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007063 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007064 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007065 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007066int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007067{
7068 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007070
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007071 if (!uri_auth)
7072 return 0;
7073
Cyril Bonté70be45d2010-10-12 00:14:35 +02007074 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007075 return 0;
7076
Willy Tarreau295a8372011-03-10 11:25:07 +01007077 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007080 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007081 return 0;
7082
Willy Tarreau962c3f42010-01-10 00:15:35 +01007083 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007084
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007085 /* the URI is in h */
7086 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007087 return 0;
7088
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007089 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007090 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007091 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007092 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007093 break;
7094 }
7095 h++;
7096 }
7097
7098 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007099 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7100 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007101 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007102 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007103 break;
7104 }
7105 h++;
7106 }
7107 }
7108
Willy Tarreau962c3f42010-01-10 00:15:35 +01007109 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7110 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007111 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007112 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007113 break;
7114 }
7115 h++;
7116 }
7117
Cyril Bonté70be45d2010-10-12 00:14:35 +02007118 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7119 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7120 if (memcmp(h, ";st=", 4) == 0) {
7121 h += 4;
7122
7123 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007124 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007125 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007126 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007127 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007128 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007129 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007130 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007131 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007132 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007133 break;
7134 }
7135 h++;
7136 }
7137
Willy Tarreau295a8372011-03-10 11:25:07 +01007138 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007139
Willy Tarreaub2513902006-12-17 14:52:38 +01007140 return 1;
7141}
7142
Willy Tarreau4076a152009-04-02 15:18:36 +02007143/*
7144 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007145 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7146 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007147 */
7148void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7149 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007150 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007151{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007152 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7153 int len1 = buf->size - msg->som;
7154 es->len = buf->r - (buf->data + msg->som) + buf->size;
7155 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7156 if (es->len > len1 && len1 < sizeof(es->buf))
7157 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7158 }
7159 else {
7160 es->len = buf->r - (buf->data + msg->som);
7161 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7162 }
7163
Willy Tarreau4076a152009-04-02 15:18:36 +02007164 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007165 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007166 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007167 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007168 else
7169 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7170
Willy Tarreau4076a152009-04-02 15:18:36 +02007171 es->when = date; // user-visible date
7172 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007173 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007174 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007175 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007176 es->state = state;
7177 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007178 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007179}
Willy Tarreaub2513902006-12-17 14:52:38 +01007180
Willy Tarreau294c4732011-12-16 21:35:50 +01007181/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7182 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7183 * performed over the whole headers. Otherwise it must contain a valid header
7184 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7185 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7186 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7187 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7188 * -1.
7189 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007190 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007191unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7192 struct hdr_idx *idx, int occ,
7193 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007194{
Willy Tarreau294c4732011-12-16 21:35:50 +01007195 struct hdr_ctx local_ctx;
7196 char *ptr_hist[MAX_HDR_HISTORY];
7197 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007198 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007199 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007200
Willy Tarreau294c4732011-12-16 21:35:50 +01007201 if (!ctx) {
7202 local_ctx.idx = 0;
7203 ctx = &local_ctx;
7204 }
7205
Willy Tarreaubce70882009-09-07 11:51:47 +02007206 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007207 /* search from the beginning */
7208 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007209 occ--;
7210 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007211 *vptr = ctx->line + ctx->val;
7212 *vlen = ctx->vlen;
7213 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007214 }
7215 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007216 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007217 }
7218
7219 /* negative occurrence, we scan all the list then walk back */
7220 if (-occ > MAX_HDR_HISTORY)
7221 return 0;
7222
Willy Tarreau294c4732011-12-16 21:35:50 +01007223 found = hist_ptr = 0;
7224 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7225 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7226 len_hist[hist_ptr] = ctx->vlen;
7227 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007228 hist_ptr = 0;
7229 found++;
7230 }
7231 if (-occ > found)
7232 return 0;
7233 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7234 * find occurrence -occ, so we have to check [hist_ptr+occ].
7235 */
7236 hist_ptr += occ;
7237 if (hist_ptr >= MAX_HDR_HISTORY)
7238 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007239 *vptr = ptr_hist[hist_ptr];
7240 *vlen = len_hist[hist_ptr];
7241 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007242}
7243
Willy Tarreaubaaee002006-06-26 02:48:02 +02007244/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007245 * Print a debug line with a header
7246 */
7247void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7248{
7249 int len, max;
7250 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007251 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007252 max = end - start;
7253 UBOUND(max, sizeof(trash) - len - 1);
7254 len += strlcpy2(trash + len, start, max + 1);
7255 trash[len++] = '\n';
7256 write(1, trash, len);
7257}
7258
Willy Tarreau0937bc42009-12-22 15:03:09 +01007259/*
7260 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7261 * the required fields are properly allocated and that we only need to (re)init
7262 * them. This should be used before processing any new request.
7263 */
7264void http_init_txn(struct session *s)
7265{
7266 struct http_txn *txn = &s->txn;
7267 struct proxy *fe = s->fe;
7268
7269 txn->flags = 0;
7270 txn->status = -1;
7271
Willy Tarreauf64d1412010-10-07 20:06:11 +02007272 txn->cookie_first_date = 0;
7273 txn->cookie_last_date = 0;
7274
Willy Tarreau0937bc42009-12-22 15:03:09 +01007275 txn->req.sol = txn->req.eol = NULL;
7276 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7277 txn->rsp.sol = txn->rsp.eol = NULL;
7278 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007279 txn->req.chunk_len = 0LL;
7280 txn->req.body_len = 0LL;
7281 txn->rsp.chunk_len = 0LL;
7282 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007283 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7284 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007285
7286 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007287
7288 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7289 if (fe->options2 & PR_O2_REQBUG_OK)
7290 txn->req.err_pos = -1; /* let buggy requests pass */
7291
Willy Tarreau46023632010-01-07 22:51:47 +01007292 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007293 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7294
Willy Tarreau46023632010-01-07 22:51:47 +01007295 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007296 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7297
7298 if (txn->hdr_idx.v)
7299 hdr_idx_init(&txn->hdr_idx);
7300}
7301
7302/* to be used at the end of a transaction */
7303void http_end_txn(struct session *s)
7304{
7305 struct http_txn *txn = &s->txn;
7306
7307 /* these ones will have been dynamically allocated */
7308 pool_free2(pool2_requri, txn->uri);
7309 pool_free2(pool2_capture, txn->cli_cookie);
7310 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007311 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007312
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007313 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007314 txn->uri = NULL;
7315 txn->srv_cookie = NULL;
7316 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007317
7318 if (txn->req.cap) {
7319 struct cap_hdr *h;
7320 for (h = s->fe->req_cap; h; h = h->next)
7321 pool_free2(h->pool, txn->req.cap[h->index]);
7322 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7323 }
7324
7325 if (txn->rsp.cap) {
7326 struct cap_hdr *h;
7327 for (h = s->fe->rsp_cap; h; h = h->next)
7328 pool_free2(h->pool, txn->rsp.cap[h->index]);
7329 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7330 }
7331
Willy Tarreau0937bc42009-12-22 15:03:09 +01007332}
7333
7334/* to be used at the end of a transaction to prepare a new one */
7335void http_reset_txn(struct session *s)
7336{
7337 http_end_txn(s);
7338 http_init_txn(s);
7339
7340 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007341 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007342 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007343 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007344 /* re-init store persistence */
7345 s->store_count = 0;
7346
Willy Tarreau0937bc42009-12-22 15:03:09 +01007347 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007348
7349 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7350
Willy Tarreau739cfba2010-01-25 23:11:14 +01007351 /* We must trim any excess data from the response buffer, because we
7352 * may have blocked an invalid response from a server that we don't
7353 * want to accidentely forward once we disable the analysers, nor do
7354 * we want those data to come along with next response. A typical
7355 * example of such data would be from a buggy server responding to
7356 * a HEAD with some data, or sending more than the advertised
7357 * content-length.
7358 */
7359 if (unlikely(s->rep->l > s->rep->send_max)) {
7360 s->rep->l = s->rep->send_max;
7361 s->rep->r = s->rep->w + s->rep->l;
7362 if (s->rep->r >= s->rep->data + s->rep->size)
7363 s->rep->r -= s->rep->size;
7364 }
7365
Willy Tarreau0937bc42009-12-22 15:03:09 +01007366 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007367 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007368
Willy Tarreaud04e8582010-05-31 12:31:35 +02007369 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007370 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007371
7372 s->req->rex = TICK_ETERNITY;
7373 s->req->wex = TICK_ETERNITY;
7374 s->req->analyse_exp = TICK_ETERNITY;
7375 s->rep->rex = TICK_ETERNITY;
7376 s->rep->wex = TICK_ETERNITY;
7377 s->rep->analyse_exp = TICK_ETERNITY;
7378}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007379
Willy Tarreauff011f22011-01-06 17:51:27 +01007380void free_http_req_rules(struct list *r) {
7381 struct http_req_rule *tr, *pr;
7382
7383 list_for_each_entry_safe(pr, tr, r, list) {
7384 LIST_DEL(&pr->list);
7385 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7386 free(pr->http_auth.realm);
7387
7388 free(pr);
7389 }
7390}
7391
7392struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7393{
7394 struct http_req_rule *rule;
7395 int cur_arg;
7396
7397 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7398 if (!rule) {
7399 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7400 return NULL;
7401 }
7402
7403 if (!*args[0]) {
7404 goto req_error_parsing;
7405 } else if (!strcmp(args[0], "allow")) {
7406 rule->action = HTTP_REQ_ACT_ALLOW;
7407 cur_arg = 1;
7408 } else if (!strcmp(args[0], "deny")) {
7409 rule->action = HTTP_REQ_ACT_DENY;
7410 cur_arg = 1;
7411 } else if (!strcmp(args[0], "auth")) {
7412 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7413 cur_arg = 1;
7414
7415 while(*args[cur_arg]) {
7416 if (!strcmp(args[cur_arg], "realm")) {
7417 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7418 cur_arg+=2;
7419 continue;
7420 } else
7421 break;
7422 }
7423 } else {
7424req_error_parsing:
7425 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7426 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7427 return NULL;
7428 }
7429
7430 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7431 struct acl_cond *cond;
7432
7433 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7434 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7435 file, linenum, args[0]);
7436 return NULL;
7437 }
7438 rule->cond = cond;
7439 }
7440 else if (*args[cur_arg]) {
7441 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7442 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7443 file, linenum, args[0], args[cur_arg]);
7444 return NULL;
7445 }
7446
7447 return rule;
7448}
7449
Willy Tarreau8797c062007-05-07 00:55:35 +02007450/************************************************************************/
7451/* The code below is dedicated to ACL parsing and matching */
7452/************************************************************************/
7453
7454
7455
7456
7457/* 1. Check on METHOD
7458 * We use the pre-parsed method if it is known, and store its number as an
7459 * integer. If it is unknown, we use the pointer and the length.
7460 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007461static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007462{
7463 int len, meth;
7464
Willy Tarreauae8b7962007-06-09 23:10:04 +02007465 len = strlen(*text);
7466 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007467
7468 pattern->val.i = meth;
7469 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007470 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007471 if (!pattern->ptr.str)
7472 return 0;
7473 pattern->len = len;
7474 }
7475 return 1;
7476}
7477
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007478/* This function fetches the method of current HTTP request and stores
7479 * it in the global pattern struct as a chunk. There are two possibilities :
7480 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7481 * in <len> and <ptr> is NULL ;
7482 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7483 * <len> to its length.
7484 * This is intended to be used with acl_match_meth() only.
7485 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007486static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007487acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7488 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007489{
7490 int meth;
7491 struct http_txn *txn = l7;
7492
Willy Tarreaub6866442008-07-14 23:54:42 +02007493 if (!txn)
7494 return 0;
7495
Willy Tarreau655dce92009-11-08 13:10:58 +01007496 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007497 return 0;
7498
Willy Tarreau8797c062007-05-07 00:55:35 +02007499 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007500 temp_pattern.data.str.len = meth;
7501 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007502 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007503 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7504 /* ensure the indexes are not affected */
7505 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007506 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7507 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007508 }
7509 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7510 return 1;
7511}
7512
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007513/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007514static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7515{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007516 int icase;
7517
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007518
7519 if (temp_pattern.data.str.str == NULL) {
7520 /* well-known method */
7521 if (temp_pattern.data.str.len == pattern->val.i)
7522 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007523 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007524 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007525
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007526 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7527 if (pattern->val.i != HTTP_METH_OTHER)
7528 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007529
7530 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007531 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007532 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007533
7534 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007535 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7536 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007537 return ACL_PAT_FAIL;
7538 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007539}
7540
7541/* 2. Check on Request/Status Version
7542 * We simply compare strings here.
7543 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007544static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007545{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007546 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007547 if (!pattern->ptr.str)
7548 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007549 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007550 return 1;
7551}
7552
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007553static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007554acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7555 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007556{
7557 struct http_txn *txn = l7;
7558 char *ptr;
7559 int len;
7560
Willy Tarreaub6866442008-07-14 23:54:42 +02007561 if (!txn)
7562 return 0;
7563
Willy Tarreau655dce92009-11-08 13:10:58 +01007564 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007565 return 0;
7566
Willy Tarreau8797c062007-05-07 00:55:35 +02007567 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007568 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007569
7570 while ((len-- > 0) && (*ptr++ != '/'));
7571 if (len <= 0)
7572 return 0;
7573
Willy Tarreau664092c2011-12-16 19:11:42 +01007574 temp_pattern.data.str.str = ptr;
7575 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007576
7577 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7578 return 1;
7579}
7580
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007581static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007582acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7583 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007584{
7585 struct http_txn *txn = l7;
7586 char *ptr;
7587 int len;
7588
Willy Tarreaub6866442008-07-14 23:54:42 +02007589 if (!txn)
7590 return 0;
7591
Willy Tarreau655dce92009-11-08 13:10:58 +01007592 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007593 return 0;
7594
Willy Tarreau8797c062007-05-07 00:55:35 +02007595 len = txn->rsp.sl.st.v_l;
7596 ptr = txn->rsp.sol;
7597
7598 while ((len-- > 0) && (*ptr++ != '/'));
7599 if (len <= 0)
7600 return 0;
7601
Willy Tarreau664092c2011-12-16 19:11:42 +01007602 temp_pattern.data.str.str = ptr;
7603 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007604
7605 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7606 return 1;
7607}
7608
7609/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007610static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007611acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7612 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007613{
7614 struct http_txn *txn = l7;
7615 char *ptr;
7616 int len;
7617
Willy Tarreaub6866442008-07-14 23:54:42 +02007618 if (!txn)
7619 return 0;
7620
Willy Tarreau655dce92009-11-08 13:10:58 +01007621 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007622 return 0;
7623
Willy Tarreau8797c062007-05-07 00:55:35 +02007624 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007625 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007626
Willy Tarreaua5e37562011-12-16 17:06:15 +01007627 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007628 test->flags = ACL_TEST_F_VOL_1ST;
7629 return 1;
7630}
7631
7632/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007633static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007634acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7635 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007636{
7637 struct http_txn *txn = l7;
7638
Willy Tarreaub6866442008-07-14 23:54:42 +02007639 if (!txn)
7640 return 0;
7641
Willy Tarreau655dce92009-11-08 13:10:58 +01007642 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007643 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007644
Willy Tarreauc11416f2007-06-17 16:58:38 +02007645 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7646 /* ensure the indexes are not affected */
7647 return 0;
7648
Willy Tarreau664092c2011-12-16 19:11:42 +01007649 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7650 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007651
Willy Tarreauf3d25982007-05-08 22:45:09 +02007652 /* we do not need to set READ_ONLY because the data is in a buffer */
7653 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007654 return 1;
7655}
7656
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007657static int
7658acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7659 struct acl_expr *expr, struct acl_test *test)
7660{
7661 struct http_txn *txn = l7;
7662
Willy Tarreaub6866442008-07-14 23:54:42 +02007663 if (!txn)
7664 return 0;
7665
Willy Tarreau655dce92009-11-08 13:10:58 +01007666 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007667 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007668
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007669 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7670 /* ensure the indexes are not affected */
7671 return 0;
7672
7673 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007674 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf4362b32011-12-16 17:49:52 +01007675 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7676 return 0;
7677 temp_pattern.type = PATTERN_TYPE_IP;
7678 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007679
7680 /*
7681 * If we are parsing url in frontend space, we prepare backend stage
7682 * to not parse again the same url ! optimization lazyness...
7683 */
7684 if (px->options & PR_O_HTTP_PROXY)
7685 l4->flags |= SN_ADDR_SET;
7686
Willy Tarreauf4362b32011-12-16 17:49:52 +01007687 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007688 return 1;
7689}
7690
7691static int
7692acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7693 struct acl_expr *expr, struct acl_test *test)
7694{
7695 struct http_txn *txn = l7;
7696
Willy Tarreaub6866442008-07-14 23:54:42 +02007697 if (!txn)
7698 return 0;
7699
Willy Tarreau655dce92009-11-08 13:10:58 +01007700 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007701 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007702
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007703 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7704 /* ensure the indexes are not affected */
7705 return 0;
7706
7707 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007708 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreaua5e37562011-12-16 17:06:15 +01007709 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007710
7711 if (px->options & PR_O_HTTP_PROXY)
7712 l4->flags |= SN_ADDR_SET;
7713
7714 test->flags = ACL_TEST_F_READ_ONLY;
7715 return 1;
7716}
7717
Willy Tarreauc11416f2007-06-17 16:58:38 +02007718/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7719 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7720 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007721static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007722acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007723 struct acl_expr *expr, struct acl_test *test)
7724{
7725 struct http_txn *txn = l7;
7726 struct hdr_idx *idx = &txn->hdr_idx;
7727 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007728
Willy Tarreaub6866442008-07-14 23:54:42 +02007729 if (!txn)
7730 return 0;
7731
Willy Tarreau33a7e692007-06-10 19:45:56 +02007732 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7733 /* search for header from the beginning */
7734 ctx->idx = 0;
7735
Willy Tarreau33a7e692007-06-10 19:45:56 +02007736 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7737 test->flags |= ACL_TEST_F_FETCH_MORE;
7738 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007739 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7740 temp_pattern.data.str.len = ctx->vlen;
7741
Willy Tarreau33a7e692007-06-10 19:45:56 +02007742 return 1;
7743 }
7744
7745 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7746 test->flags |= ACL_TEST_F_VOL_HDR;
7747 return 0;
7748}
7749
Willy Tarreau33a7e692007-06-10 19:45:56 +02007750static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007751acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7752 struct acl_expr *expr, struct acl_test *test)
7753{
7754 struct http_txn *txn = l7;
7755
Willy Tarreaub6866442008-07-14 23:54:42 +02007756 if (!txn)
7757 return 0;
7758
Willy Tarreau655dce92009-11-08 13:10:58 +01007759 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007760 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007761
Willy Tarreauc11416f2007-06-17 16:58:38 +02007762 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7763 /* ensure the indexes are not affected */
7764 return 0;
7765
7766 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7767}
7768
7769static int
7770acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7771 struct acl_expr *expr, struct acl_test *test)
7772{
7773 struct http_txn *txn = l7;
7774
Willy Tarreaub6866442008-07-14 23:54:42 +02007775 if (!txn)
7776 return 0;
7777
Willy Tarreau655dce92009-11-08 13:10:58 +01007778 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007779 return 0;
7780
7781 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7782}
7783
7784/* 6. Check on HTTP header count. The number of occurrences is returned.
7785 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7786 */
7787static int
7788acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007789 struct acl_expr *expr, struct acl_test *test)
7790{
7791 struct http_txn *txn = l7;
7792 struct hdr_idx *idx = &txn->hdr_idx;
7793 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007794 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007795
Willy Tarreaub6866442008-07-14 23:54:42 +02007796 if (!txn)
7797 return 0;
7798
Willy Tarreau33a7e692007-06-10 19:45:56 +02007799 ctx.idx = 0;
7800 cnt = 0;
7801 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7802 cnt++;
7803
Willy Tarreaua5e37562011-12-16 17:06:15 +01007804 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007805 test->flags = ACL_TEST_F_VOL_HDR;
7806 return 1;
7807}
7808
Willy Tarreauc11416f2007-06-17 16:58:38 +02007809static int
7810acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7811 struct acl_expr *expr, struct acl_test *test)
7812{
7813 struct http_txn *txn = l7;
7814
Willy Tarreaub6866442008-07-14 23:54:42 +02007815 if (!txn)
7816 return 0;
7817
Willy Tarreau655dce92009-11-08 13:10:58 +01007818 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007819 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007820
Willy Tarreauc11416f2007-06-17 16:58:38 +02007821 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7822 /* ensure the indexes are not affected */
7823 return 0;
7824
7825 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7826}
7827
7828static int
7829acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7830 struct acl_expr *expr, struct acl_test *test)
7831{
7832 struct http_txn *txn = l7;
7833
Willy Tarreaub6866442008-07-14 23:54:42 +02007834 if (!txn)
7835 return 0;
7836
Willy Tarreau655dce92009-11-08 13:10:58 +01007837 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007838 return 0;
7839
7840 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7841}
7842
Willy Tarreau33a7e692007-06-10 19:45:56 +02007843/* 7. Check on HTTP header's integer value. The integer value is returned.
7844 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007845 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007846 */
7847static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007848acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007849 struct acl_expr *expr, struct acl_test *test)
7850{
7851 struct http_txn *txn = l7;
7852 struct hdr_idx *idx = &txn->hdr_idx;
7853 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007854
Willy Tarreaub6866442008-07-14 23:54:42 +02007855 if (!txn)
7856 return 0;
7857
Willy Tarreau33a7e692007-06-10 19:45:56 +02007858 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7859 /* search for header from the beginning */
7860 ctx->idx = 0;
7861
Willy Tarreau33a7e692007-06-10 19:45:56 +02007862 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7863 test->flags |= ACL_TEST_F_FETCH_MORE;
7864 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007865 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007866 return 1;
7867 }
7868
7869 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7870 test->flags |= ACL_TEST_F_VOL_HDR;
7871 return 0;
7872}
7873
Willy Tarreauc11416f2007-06-17 16:58:38 +02007874static int
7875acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7876 struct acl_expr *expr, struct acl_test *test)
7877{
7878 struct http_txn *txn = l7;
7879
Willy Tarreaub6866442008-07-14 23:54:42 +02007880 if (!txn)
7881 return 0;
7882
Willy Tarreau655dce92009-11-08 13:10:58 +01007883 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007884 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007885
Willy Tarreauc11416f2007-06-17 16:58:38 +02007886 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7887 /* ensure the indexes are not affected */
7888 return 0;
7889
7890 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7891}
7892
7893static int
7894acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7895 struct acl_expr *expr, struct acl_test *test)
7896{
7897 struct http_txn *txn = l7;
7898
Willy Tarreaub6866442008-07-14 23:54:42 +02007899 if (!txn)
7900 return 0;
7901
Willy Tarreau655dce92009-11-08 13:10:58 +01007902 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007903 return 0;
7904
7905 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7906}
7907
Willy Tarreau106f9792009-09-19 07:54:16 +02007908/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7909 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7910 */
7911static int
7912acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7913 struct acl_expr *expr, struct acl_test *test)
7914{
7915 struct http_txn *txn = l7;
7916 struct hdr_idx *idx = &txn->hdr_idx;
7917 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7918
7919 if (!txn)
7920 return 0;
7921
7922 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7923 /* search for header from the beginning */
7924 ctx->idx = 0;
7925
Willy Tarreauf4362b32011-12-16 17:49:52 +01007926 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02007927 test->flags |= ACL_TEST_F_FETCH_MORE;
7928 test->flags |= ACL_TEST_F_VOL_HDR;
7929 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01007930 temp_pattern.type = PATTERN_TYPE_IP;
7931 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
7932 return 1;
7933 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02007934 }
7935
7936 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7937 test->flags |= ACL_TEST_F_VOL_HDR;
7938 return 0;
7939}
7940
7941static int
7942acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7943 struct acl_expr *expr, struct acl_test *test)
7944{
7945 struct http_txn *txn = l7;
7946
7947 if (!txn)
7948 return 0;
7949
Willy Tarreau655dce92009-11-08 13:10:58 +01007950 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007951 return 0;
7952
7953 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7954 /* ensure the indexes are not affected */
7955 return 0;
7956
7957 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7958}
7959
7960static int
7961acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7962 struct acl_expr *expr, struct acl_test *test)
7963{
7964 struct http_txn *txn = l7;
7965
7966 if (!txn)
7967 return 0;
7968
Willy Tarreau655dce92009-11-08 13:10:58 +01007969 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007970 return 0;
7971
7972 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7973}
7974
Willy Tarreau737b0c12007-06-10 21:28:46 +02007975/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7976 * the first '/' after the possible hostname, and ends before the possible '?'.
7977 */
7978static int
7979acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7980 struct acl_expr *expr, struct acl_test *test)
7981{
7982 struct http_txn *txn = l7;
7983 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007984
Willy Tarreaub6866442008-07-14 23:54:42 +02007985 if (!txn)
7986 return 0;
7987
Willy Tarreau655dce92009-11-08 13:10:58 +01007988 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007989 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007990
Willy Tarreauc11416f2007-06-17 16:58:38 +02007991 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7992 /* ensure the indexes are not affected */
7993 return 0;
7994
Willy Tarreau962c3f42010-01-10 00:15:35 +01007995 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007996 ptr = http_get_path(txn);
7997 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007998 return 0;
7999
8000 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008001 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008002
8003 while (ptr < end && *ptr != '?')
8004 ptr++;
8005
Willy Tarreau664092c2011-12-16 19:11:42 +01008006 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008007
8008 /* we do not need to set READ_ONLY because the data is in a buffer */
8009 test->flags = ACL_TEST_F_VOL_1ST;
8010 return 1;
8011}
8012
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008013static int
8014acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8015 struct acl_expr *expr, struct acl_test *test)
8016{
8017 struct buffer *req = s->req;
8018 struct http_txn *txn = &s->txn;
8019 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008020
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008021 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8022 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8023 */
8024
8025 if (!s || !req)
8026 return 0;
8027
Willy Tarreau655dce92009-11-08 13:10:58 +01008028 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008029 /* Already decoded as OK */
8030 test->flags |= ACL_TEST_F_SET_RES_PASS;
8031 return 1;
8032 }
8033
8034 /* Try to decode HTTP request */
8035 if (likely(req->lr < req->r))
8036 http_msg_analyzer(req, msg, &txn->hdr_idx);
8037
Willy Tarreau655dce92009-11-08 13:10:58 +01008038 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008039 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8040 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8041 return 1;
8042 }
8043 /* wait for final state */
8044 test->flags |= ACL_TEST_F_MAY_CHANGE;
8045 return 0;
8046 }
8047
8048 /* OK we got a valid HTTP request. We have some minor preparation to
8049 * perform so that further checks can rely on HTTP tests.
8050 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008051 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008052 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8053 s->flags |= SN_REDIRECTABLE;
8054
8055 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8056 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8057 return 1;
8058 }
8059
8060 test->flags |= ACL_TEST_F_SET_RES_PASS;
8061 return 1;
8062}
8063
Willy Tarreau7f18e522010-10-22 20:04:13 +02008064/* return a valid test if the current request is the first one on the connection */
8065static int
8066acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8067 struct acl_expr *expr, struct acl_test *test)
8068{
8069 if (!s)
8070 return 0;
8071
8072 if (s->txn.flags & TX_NOT_FIRST)
8073 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8074 else
8075 test->flags |= ACL_TEST_F_SET_RES_PASS;
8076
8077 return 1;
8078}
8079
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008080static int
8081acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8082 struct acl_expr *expr, struct acl_test *test)
8083{
8084
8085 if (!s)
8086 return 0;
8087
8088 if (!get_http_auth(s))
8089 return 0;
8090
8091 test->ctx.a[0] = expr->arg.ul;
8092 test->ctx.a[1] = s->txn.auth.user;
8093 test->ctx.a[2] = s->txn.auth.pass;
8094
8095 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8096
8097 return 1;
8098}
Willy Tarreau8797c062007-05-07 00:55:35 +02008099
8100/************************************************************************/
8101/* All supported keywords must be declared here. */
8102/************************************************************************/
8103
8104/* Note: must not be declared <const> as its list will be overwritten */
8105static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008106 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8107
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008108 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008109 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8110 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008111 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008112
Willy Tarreauc4262962010-05-10 23:42:40 +02008113 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008114 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8115 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8116 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8117 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8118 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8119 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008120 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008121 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008122 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008123
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008124 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008125 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008126 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8127 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8128 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8129 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8130 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8131 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8132 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008133 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008134 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008135 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008136
Willy Tarreauc4262962010-05-10 23:42:40 +02008137 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008138 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8139 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8140 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8141 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8142 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8143 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8144 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008145 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008146 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008147 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008148
Willy Tarreauc4262962010-05-10 23:42:40 +02008149 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008150 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8151 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8152 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8153 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8154 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8155 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008156 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008157
Willy Tarreauf3d25982007-05-08 22:45:09 +02008158#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008159 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8160 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8161 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8162 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8163 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8164 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8165 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8166
Willy Tarreau8797c062007-05-07 00:55:35 +02008167 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8168 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8169 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8170 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8171 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8172 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8173 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8174 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008175#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008176
Willy Tarreau7f18e522010-10-22 20:04:13 +02008177 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8178 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8179 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008180 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008181}};
8182
Willy Tarreau4a568972010-05-12 08:08:50 +02008183/************************************************************************/
8184/* The code below is dedicated to pattern fetching and matching */
8185/************************************************************************/
8186
Willy Tarreaue428fb72011-12-16 21:50:30 +01008187/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008188static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008189pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8190 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008191{
8192 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008193
Willy Tarreaue428fb72011-12-16 21:50:30 +01008194 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8195 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008196}
8197
David Cournapeau16023ee2010-12-23 20:55:41 +09008198/*
8199 * Given a path string and its length, find the position of beginning of the
8200 * query string. Returns NULL if no query string is found in the path.
8201 *
8202 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8203 *
8204 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8205 */
8206static inline char *find_query_string(char *path, size_t path_l)
8207{
8208 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008209
David Cournapeau16023ee2010-12-23 20:55:41 +09008210 p = memchr(path, '?', path_l);
8211 return p ? p + 1 : NULL;
8212}
8213
8214static inline int is_param_delimiter(char c)
8215{
8216 return c == '&' || c == ';';
8217}
8218
8219/*
8220 * Given a url parameter, find the starting position of the first occurence,
8221 * or NULL if the parameter is not found.
8222 *
8223 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8224 * the function will return query_string+8.
8225 */
8226static char*
8227find_url_param_pos(char* query_string, size_t query_string_l,
8228 char* url_param_name, size_t url_param_name_l)
8229{
8230 char *pos, *last;
8231
8232 pos = query_string;
8233 last = query_string + query_string_l - url_param_name_l - 1;
8234
8235 while (pos <= last) {
8236 if (pos[url_param_name_l] == '=') {
8237 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8238 return pos;
8239 pos += url_param_name_l + 1;
8240 }
8241 while (pos <= last && !is_param_delimiter(*pos))
8242 pos++;
8243 pos++;
8244 }
8245 return NULL;
8246}
8247
8248/*
8249 * Given a url parameter name, returns its value and size into *value and
8250 * *value_l respectively, and returns non-zero. If the parameter is not found,
8251 * zero is returned and value/value_l are not touched.
8252 */
8253static int
8254find_url_param_value(char* path, size_t path_l,
8255 char* url_param_name, size_t url_param_name_l,
8256 char** value, size_t* value_l)
8257{
8258 char *query_string, *qs_end;
8259 char *arg_start;
8260 char *value_start, *value_end;
8261
8262 query_string = find_query_string(path, path_l);
8263 if (!query_string)
8264 return 0;
8265
8266 qs_end = path + path_l;
8267 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8268 url_param_name, url_param_name_l);
8269 if (!arg_start)
8270 return 0;
8271
8272 value_start = arg_start + url_param_name_l + 1;
8273 value_end = value_start;
8274
8275 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8276 value_end++;
8277
8278 *value = value_start;
8279 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008280 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008281}
8282
8283static int
8284pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8285 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8286{
8287 struct http_txn *txn = l7;
8288 struct http_msg *msg = &txn->req;
8289 char *url_param_value;
8290 size_t url_param_value_l;
8291
8292 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8293 arg_p->data.str.str, arg_p->data.str.len,
8294 &url_param_value, &url_param_value_l))
8295 return 0;
8296
8297 data->str.str = url_param_value;
8298 data->str.len = url_param_value_l;
8299 return 1;
8300}
8301
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008302/* Try to find the last occurrence of a cookie name in a cookie header value.
8303 * The pointer and size of the last occurrence of the cookie value is returned
8304 * into *value and *value_l, and the function returns non-zero if the value was
8305 * found. Otherwise if the cookie was not found, zero is returned and neither
8306 * value nor value_l are touched. The input hdr string should begin at the
8307 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8308 * value may represent a list of values (cookie headers).
8309 */
8310static int
8311extract_cookie_value(char *hdr, size_t hdr_l,
8312 char *cookie_name, size_t cookie_name_l, int list,
8313 char **value, size_t *value_l)
8314{
8315 int found = 0;
8316 char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
8317 char *next;
8318
8319 /* Note that multiple cookies may be delimited with semi-colons, so we
8320 * also have to loop on this.
8321 */
8322
8323 /* we search at least a cookie name followed by an equal, and more
8324 * generally something like this :
8325 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8326 */
8327 if (hdr_l < cookie_name_l + 1)
8328 return 0;
8329
8330 hdr_end = hdr + hdr_l;
8331
8332 for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
8333 /* Iterate through all cookies on this line */
8334
8335 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8336 att_beg++;
8337
8338 /* find att_end : this is the first character after the last non
8339 * space before the equal. It may be equal to hdr_end.
8340 */
8341 equal = att_end = att_beg;
8342
8343 while (equal < hdr_end) {
8344 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8345 break;
8346 if (http_is_spht[(unsigned char)*equal++])
8347 continue;
8348 att_end = equal;
8349 }
8350
8351 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8352 * is between <att_beg> and <equal>, both may be identical.
8353 */
8354
8355 /* look for end of cookie if there is an equal sign */
8356 if (equal < hdr_end && *equal == '=') {
8357 /* look for the beginning of the value */
8358 val_beg = equal + 1;
8359 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8360 val_beg++;
8361
8362 /* find the end of the value, respecting quotes */
8363 next = find_cookie_value_end(val_beg, hdr_end);
8364
8365 /* make val_end point to the first white space or delimitor after the value */
8366 val_end = next;
8367 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8368 val_end--;
8369 } else {
8370 val_beg = val_end = next = equal;
8371 }
8372
8373 /* We have nothing to do with attributes beginning with '$'. However,
8374 * they will automatically be removed if a header before them is removed,
8375 * since they're supposed to be linked together.
8376 */
8377 if (*att_beg == '$')
8378 continue;
8379
8380 /* Ignore cookies with no equal sign */
8381 if (equal == next)
8382 continue;
8383
8384 /* Now we have the cookie name between att_beg and att_end, and
8385 * its value between val_beg and val_end.
8386 */
8387
8388 if (att_end - att_beg == cookie_name_l &&
8389 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8390 found = 1;
8391 *value = val_beg;
8392 *value_l = val_end - val_beg;
8393 /* right now we want to catch the last occurrence
8394 * of the cookie, so let's go on searching.
8395 */
8396 }
8397
8398 /* Set-Cookie headers only have the name in the first attr=value part */
8399 if (!list)
8400 break;
8401 }
8402
8403 return found;
8404}
8405
8406/* Try to find in request or response message is in <msg> and whose transaction
8407 * is in <txn> the last occurrence of a cookie name in all cookie header values
8408 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8409 * pointer and size of the last occurrence of the cookie value is returned into
8410 * <value> and <value_l>, and the function returns non-zero if the value was
8411 * found. Otherwise if the cookie was not found, zero is returned and neither
8412 * value nor value_l are touched. The input hdr string should begin at the
8413 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8414 * value may represent a list of values (cookie headers).
8415 */
8416
8417static int
8418find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8419 const char *hdr_name, int hdr_name_len,
8420 char *cookie_name, size_t cookie_name_l, int list,
8421 char **value, size_t *value_l)
8422{
8423 struct hdr_ctx ctx;
8424 int found = 0;
8425
8426 ctx.idx = 0;
8427 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
8428 if (ctx.vlen < cookie_name_l + 1)
8429 continue;
8430
8431 found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen,
8432 cookie_name, cookie_name_l, 1,
8433 value, value_l);
8434 }
8435 return found;
8436}
8437
8438static int
8439pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8440 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8441{
8442 struct http_txn *txn = l7;
8443 struct http_msg *msg = &txn->req;
8444 char *cookie_value;
8445 size_t cookie_value_l;
8446 int found = 0;
8447
8448 found = find_cookie_value(msg, txn, "Cookie", 6,
8449 arg_p->data.str.str, arg_p->data.str.len, 1,
8450 &cookie_value, &cookie_value_l);
8451 if (found) {
8452 data->str.str = cookie_value;
8453 data->str.len = cookie_value_l;
8454 }
8455
8456 return found;
8457}
8458
8459
8460static int
8461pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8462 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8463{
8464 struct http_txn *txn = l7;
8465 struct http_msg *msg = &txn->rsp;
8466 char *cookie_value;
8467 size_t cookie_value_l;
8468 int found = 0;
8469
8470 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8471 arg_p->data.str.str, arg_p->data.str.len, 1,
8472 &cookie_value, &cookie_value_l);
8473 if (found) {
8474 data->str.str = cookie_value;
8475 data->str.len = cookie_value_l;
8476 }
8477
8478 return found;
8479}
8480
Emeric Brun485479d2010-09-23 18:02:19 +02008481
Willy Tarreau4a568972010-05-12 08:08:50 +02008482/************************************************************************/
8483/* All supported keywords must be declared here. */
8484/************************************************************************/
8485/* Note: must not be declared <const> as its list will be overwritten */
8486static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008487 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008488 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008489 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8490 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008491 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008492}};
8493
Willy Tarreau8797c062007-05-07 00:55:35 +02008494
8495__attribute__((constructor))
8496static void __http_protocol_init(void)
8497{
8498 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008499 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008500}
8501
8502
Willy Tarreau58f10d72006-12-04 02:26:12 +01008503/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008504 * Local variables:
8505 * c-indent-level: 8
8506 * c-basic-offset: 8
8507 * End:
8508 */